1 /* Sone JavaScript functions. */
3 function ajaxGet(url, data, successCallback, errorCallback) {
4 (function(url, data, successCallback, errorCallback) {
5 $.ajax({"type": "GET", "url": url, "data": data, "dataType": "json", "success": function(data, textStatus, xmlHttpRequest) {
7 if (typeof successCallback != "undefined") {
8 successCallback(data, textStatus);
10 }, "error": function(xmlHttpRequest, textStatus, errorThrown) {
11 if (typeof errorCallback != "undefined") {
17 })(url, data, successCallback, errorCallback);
20 function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, optional, dontUseTextarea) {
21 $(inputElement).each(function() {
22 textarea = $(dontUseTextarea ? "<input type=\"text\" name=\"" + inputFieldName + "\">" : "<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
23 if ($(this).val() == "") {
25 inputField = $(this).data("inputField");
26 inputField.show().removeAttr("disabled").addClass("default");
27 inputField.val(defaultText);
29 }).hide().data("inputField", $(this)).val($(this).val());
30 $(this).data("textarea", textarea).after(textarea);
31 (function(inputField, textarea) {
32 inputField.focus(function() {
33 $(this).hide().attr("disabled", "disabled");
34 /* no, show(), “display: block” is not what I need. */
35 textarea.attr("style", "display: inline").focus();
37 if (inputField.val() == "") {
38 inputField.addClass("default");
39 inputField.val(defaultText);
41 inputField.hide().attr("disabled", "disabled");
44 $(inputField.get(0).form).submit(function() {
45 inputField.attr("disabled", "disabled");
46 if (!optional && (textarea.val() == "")) {
47 inputField.removeAttr("disabled").focus();
51 })($(this), textarea);
56 * Adds a “comment” link to all status lines contained in the given element.
61 * The element to add a “comment” link to
63 function addCommentLink(postId, author, element, insertAfterThisElement) {
64 if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
67 commentElement = (function(postId, author) {
68 separator = $("<span> · </span>").addClass("separator");
69 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
70 replyElement = $("#sone .post#" + postId + " .create-reply");
71 replyElement.removeClass("hidden");
72 replyElement.removeClass("light");
73 (function(replyElement) {
74 replyElement.find("input.reply-input").blur(function() {
75 if ($(this).hasClass("default")) {
76 replyElement.addClass("light");
79 replyElement.removeClass("light");
82 textArea = replyElement.find("input.reply-input").focus().data("textarea");
83 textArea.val(textArea.val() + "@sone://" + author + " ");
85 return commentElement;
87 $(insertAfterThisElement).after(commentElement.clone(true));
88 $(insertAfterThisElement).after(separator);
91 var translations = {};
94 * Retrieves the translation for the given key and calls the callback function.
95 * The callback function takes a single parameter, the translated string.
98 * The key of the translation string
100 * The callback function
102 function getTranslation(key, callback) {
103 if (key in translations) {
104 callback(translations[key]);
107 ajaxGet("getTranslation.ajax", {"key": key}, function(data, textStatus) {
108 if ((data != null) && data.success) {
109 translations[key] = data.value;
110 callback(data.value);
116 * Filters the given Sone ID, replacing all “~” characters by an underscore.
119 * The Sone ID to filter
120 * @returns The filtered Sone ID
122 function filterSoneId(soneId) {
123 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
127 * Updates the status of the given Sone.
130 * The ID of the Sone to update
132 * The status of the Sone (“idle”, “unknown”, “inserting”,
135 * Whether the Sone is modified
137 * Whether the Sone is locked
139 * The date and time of the last update (formatted for display)
141 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated, lastUpdatedText) {
142 $("#sone .sone." + filterSoneId(soneId)).
143 toggleClass("unknown", status == "unknown").
144 toggleClass("idle", status == "idle").
145 toggleClass("inserting", status == "inserting").
146 toggleClass("downloading", status == "downloading").
147 toggleClass("modified", modified);
148 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
149 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
150 if (lastUpdated != null) {
151 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").attr("title", lastUpdated).text(lastUpdatedText);
153 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
154 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown);
157 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
161 * Enhances a “delete” button so that the confirmation is done on the same page.
166 * The text to show on the button
167 * @param deleteCallback
168 * The callback that actually deletes something
170 function enhanceDeleteButton(button, text, deleteCallback) {
172 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
173 $(this).fadeOut("slow");
176 }).insertAfter(button);
177 (function(button, newButton) {
178 button.click(function() {
179 button.fadeOut("slow", function() {
180 newButton.fadeIn("slow");
181 $(document).one("click", function() {
182 if (this != newButton.get(0)) {
183 newButton.fadeOut(function() {
191 })(button, newButton);
196 * Enhances a post’s “delete” button.
201 * The ID of the post to delete
203 * The text to replace the button with
205 function enhanceDeletePostButton(button, postId, text) {
206 enhanceDeleteButton(button, text, function() {
207 ajaxGet("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
212 $("#sone .post#" + postId).slideUp();
213 } else if (data.error == "invalid-post-id") {
214 /* pretend the post is already gone. */
215 getPost(postId).slideUp();
216 } else if (data.error == "auth-required") {
217 alert("You need to be logged in.");
218 } else if (data.error == "not-authorized") {
219 alert("You are not allowed to delete this post.");
221 }, function(xmlHttpRequest, textStatus, error) {
228 * Enhances a reply’s “delete” button.
233 * The ID of the reply to delete
235 * The text to replace the button with
237 function enhanceDeleteReplyButton(button, replyId, text) {
238 enhanceDeleteButton(button, text, function() {
239 ajaxGet("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
244 $("#sone .reply#" + replyId).slideUp();
245 } else if (data.error == "invalid-reply-id") {
246 /* pretend the reply is already gone. */
247 getReply(replyId).slideUp();
248 } else if (data.error == "auth-required") {
249 alert("You need to be logged in.");
250 } else if (data.error == "not-authorized") {
251 alert("You are not allowed to delete this reply.");
253 }, function(xmlHttpRequest, textStatus, error) {
259 function getFormPassword() {
260 return $("#sone #formPassword").text();
264 * Returns the element of the Sone with the given ID.
268 * @returns All Sone elements with the given ID
270 function getSone(soneId) {
271 return $("#sone .sone").filter(function(index) {
272 return $(".id", this).text() == soneId;
276 function getSoneElement(element) {
277 return $(element).closest(".sone");
281 * Generates a list of Sones by concatening the names of the given sones with a
282 * new line character (“\n”).
285 * The sones to format
286 * @returns {String} The created string
288 function generateSoneList(sones) {
290 $.each(sones, function() {
291 if (soneList != "") {
294 soneList += this.name;
300 * Returns the ID of the Sone that this element belongs to.
303 * The element to locate the matching Sone ID for
304 * @returns The ID of the Sone, or undefined
306 function getSoneId(element) {
307 return getSoneElement(element).find(".id").text();
311 * Returns the element of the post with the given ID.
315 * @returns The element of the post
317 function getPost(postId) {
318 return $("#sone .post#" + postId);
321 function getPostElement(element) {
322 return $(element).closest(".post");
325 function getPostId(element) {
326 return getPostElement(element).attr("id");
329 function getPostTime(element) {
330 return getPostElement(element).find(".post-time").text();
334 * Returns the author of the post the given element belongs to.
337 * The element whose post to get the author for
338 * @returns The ID of the authoring Sone
340 function getPostAuthor(element) {
341 return getPostElement(element).find(".post-author").text();
345 * Returns the element of the reply with the given ID.
348 * The ID of the reply
349 * @returns The element of the reply
351 function getReply(replyId) {
352 return $("#sone .reply#" + replyId);
355 function getReplyElement(element) {
356 return $(element).closest(".reply");
359 function getReplyId(element) {
360 return getReplyElement(element).attr("id");
363 function getReplyTime(element) {
364 return getReplyElement(element).find(".reply-time").text();
368 * Returns the author of the reply the given element belongs to.
371 * The element whose reply to get the author for
372 * @returns The ID of the authoring Sone
374 function getReplyAuthor(element) {
375 return getReplyElement(element).find(".reply-author").text();
379 * Returns the notification with the given ID.
381 * @param notificationId
382 * The ID of the notification
383 * @returns The notification element
385 function getNotification(notificationId) {
386 return $("#sone #notification-area .notification#" + notificationId);
390 * Returns the notification element closest to the given element.
393 * The element to get the closest notification of
394 * @return The closest notification element
396 function getNotificationElement(element) {
397 return $(element).closest(".notification");
401 * Returns the ID of the notification element.
403 * @param notificationElement
404 * The notification element
405 * @returns The ID of the notification
407 function getNotificationId(notificationElement) {
408 return $(notificationElement).attr("id");
412 * Returns the time the notification was last updated.
414 * @param notificationElement
415 * The notification element
416 * @returns The last update time of the notification
418 function getNotificationLastUpdatedTime(notificationElement) {
419 return $(notificationElement).attr("lastUpdatedTime");
422 function likePost(postId) {
423 ajaxGet("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
424 if ((data == null) || !data.success) {
427 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
428 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
429 updatePostLikes(postId);
430 }, function(xmlHttpRequest, textStatus, error) {
435 function unlikePost(postId) {
436 ajaxGet("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
437 if ((data == null) || !data.success) {
440 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
441 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
442 updatePostLikes(postId);
443 }, function(xmlHttpRequest, textStatus, error) {
448 function updatePostLikes(postId) {
449 ajaxGet("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
450 if ((data != null) && data.success) {
451 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0);
452 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
453 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
455 }, function(xmlHttpRequest, textStatus, error) {
460 function likeReply(replyId) {
461 ajaxGet("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
462 if ((data == null) || !data.success) {
465 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
466 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
467 updateReplyLikes(replyId);
468 }, function(xmlHttpRequest, textStatus, error) {
473 function unlikeReply(replyId) {
474 ajaxGet("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
475 if ((data == null) || !data.success) {
478 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
479 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
480 updateReplyLikes(replyId);
481 }, function(xmlHttpRequest, textStatus, error) {
487 * Trusts the Sone with the given ID.
490 * The ID of the Sone to trust
492 function trustSone(soneId) {
493 ajaxGet("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
494 if ((data != null) && data.success) {
495 updateTrustControls(soneId, data.trustValue);
501 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
504 * The ID of the Sone to distrust
506 function distrustSone(soneId) {
507 ajaxGet("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
508 if ((data != null) && data.success) {
509 updateTrustControls(soneId, data.trustValue);
515 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
518 * The ID of the Sone to untrust
520 function untrustSone(soneId) {
521 ajaxGet("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
522 if ((data != null) && data.success) {
523 updateTrustControls(soneId, data.trustValue);
529 * Updates the trust controls for all posts and replies of the given Sone,
530 * according to the given trust value.
533 * The ID of the Sone to update all trust controls for
535 * The trust value for the Sone
537 function updateTrustControls(soneId, trustValue) {
538 $("#sone .post").each(function() {
539 if (getPostAuthor(this) == soneId) {
540 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
541 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
542 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
545 $("#sone .reply").each(function() {
546 if (getReplyAuthor(this) == soneId) {
547 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
548 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
549 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
555 * Bookmarks the post with the given ID.
558 * The ID of the post to bookmark
560 function bookmarkPost(postId) {
562 ajaxGet("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
563 if ((data != null) && data.success) {
564 getPost(postId).find(".bookmark").toggleClass("hidden", true);
565 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
572 * Unbookmarks the post with the given ID.
575 * The ID of the post to unbookmark
577 function unbookmarkPost(postId) {
578 ajaxGet("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
579 if ((data != null) && data.success) {
580 getPost(postId).find(".bookmark").toggleClass("hidden", false);
581 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
586 function updateReplyLikes(replyId) {
587 ajaxGet("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
588 if ((data != null) && data.success) {
589 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0);
590 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
591 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
593 }, function(xmlHttpRequest, textStatus, error) {
599 * Posts a reply and calls the given callback when the request finishes.
602 * The ID of the sender
604 * The ID of the post the reply refers to
607 * @param callbackFunction
608 * The callback function to call when the request finishes (takes 3
609 * parameters: success, error, replyId)
611 function postReply(sender, postId, text, callbackFunction) {
612 ajaxGet("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
614 /* TODO - show error */
618 callbackFunction(true, null, data.reply, data.sone);
620 callbackFunction(false, data.error);
622 }, function(xmlHttpRequest, textStatus, error) {
628 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
631 * The Sone to ajaxify
633 function ajaxifySone(soneElement) {
635 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
638 $(".follow", soneElement).submit(function() {
639 var followElement = this;
640 ajaxGet("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
641 $(followElement).addClass("hidden");
642 $(followElement).parent().find(".unfollow").removeClass("hidden");
646 $(".unfollow", soneElement).submit(function() {
647 var unfollowElement = this;
648 ajaxGet("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
649 $(unfollowElement).addClass("hidden");
650 $(unfollowElement).parent().find(".follow").removeClass("hidden");
654 $(".lock", soneElement).submit(function() {
655 var lockElement = this;
656 ajaxGet("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
657 $(lockElement).addClass("hidden");
658 $(lockElement).parent().find(".unlock").removeClass("hidden");
662 $(".unlock", soneElement).submit(function() {
663 var unlockElement = this;
664 ajaxGet("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
665 $(unlockElement).addClass("hidden");
666 $(unlockElement).parent().find(".lock").removeClass("hidden");
671 /* mark Sone as known when clicking it. */
672 $(soneElement).click(function() {
673 markSoneAsKnown(this);
678 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
681 * The post element to ajaxify
683 function ajaxifyPost(postElement) {
684 $(postElement).find("form").submit(function() {
687 $(postElement).find(".create-reply button:submit").click(function() {
689 button.attr("disabled", "disabled");
690 sender = $(this.form).find(":input[name=sender]").val();
691 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
692 postId = getPostId(this);
693 text = $(inputField).val();
694 (function(sender, postId, text, inputField) {
695 postReply(sender, postId, text, function(success, error, replyId, soneId) {
697 $(inputField).val("");
698 loadNewReply(replyId, soneId, postId);
699 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
700 $("#sone .post#" + postId + " .create-reply .sender").hide();
701 $("#sone .post#" + postId + " .create-reply .select-sender").show();
702 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
706 button.removeAttr("disabled");
708 })(sender, postId, text, inputField);
712 /* replace all “delete” buttons with javascript. */
713 (function(postElement) {
714 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
715 postId = getPostId(postElement);
716 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
720 /* convert all “like” buttons to javascript functions. */
721 $(postElement).find(".like-post").submit(function() {
722 likePost(getPostId(this));
725 $(postElement).find(".unlike-post").submit(function() {
726 unlikePost(getPostId(this));
730 /* convert trust control buttons to javascript functions. */
731 $(postElement).find(".post-trust").submit(function() {
732 trustSone(getPostAuthor(this));
735 $(postElement).find(".post-distrust").submit(function() {
736 distrustSone(getPostAuthor(this));
739 $(postElement).find(".post-untrust").submit(function() {
740 untrustSone(getPostAuthor(this));
744 /* convert bookmark/unbookmark buttons to javascript functions. */
745 $(postElement).find(".bookmark").submit(function() {
746 bookmarkPost(getPostId(this));
749 $(postElement).find(".unbookmark").submit(function() {
750 unbookmarkPost(getPostId(this));
754 /* convert “show source” link into javascript function. */
755 $(postElement).find(".show-source").each(function() {
756 $("a", this).click(function() {
757 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
758 $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
763 /* add “comment” link. */
764 addCommentLink(getPostId(postElement), getPostAuthor(postElement), postElement, $(postElement).find(".post-status-line .permalink-author"));
766 /* process all replies. */
768 $(postElement).find(".reply").each(function() {
769 replyIds.push(getReplyId(this));
772 updateReplyTimes(replyIds.join(","));
774 /* process reply input fields. */
775 getTranslation("WebInterface.DefaultText.Reply", function(text) {
776 $(postElement).find("input.reply-input").each(function() {
777 registerInputTextareaSwap(this, text, "text", false, false);
781 /* process sender selection. */
782 $(".select-sender", postElement).css("display", "inline");
783 $(".sender", postElement).hide();
784 $(".select-sender button", postElement).click(function() {
785 $(".sender", postElement).show();
786 $(".select-sender", postElement).hide();
790 /* mark everything as known on click. */
791 $(postElement).click(function(event) {
792 if ($(event.target).hasClass("click-to-show")) {
795 markPostAsKnown(this);
798 /* hide reply input field. */
799 $(postElement).find(".create-reply").addClass("hidden");
803 * Ajaxifies the given reply element.
805 * @param replyElement
806 * The reply element to ajaxify
808 function ajaxifyReply(replyElement) {
809 $(replyElement).find(".like-reply").submit(function() {
810 likeReply(getReplyId(this));
813 $(replyElement).find(".unlike-reply").submit(function() {
814 unlikeReply(getReplyId(this));
817 (function(replyElement) {
818 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
819 $(replyElement).find(".delete-reply button").each(function() {
820 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
824 addCommentLink(getPostId(replyElement), getReplyAuthor(replyElement), replyElement, $(replyElement).find(".reply-status-line .permalink-author"));
826 /* convert “show source” link into javascript function. */
827 $(replyElement).find(".show-reply-source").each(function() {
828 $("a", this).click(function() {
829 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
830 $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
835 /* convert trust control buttons to javascript functions. */
836 $(replyElement).find(".reply-trust").submit(function() {
837 trustSone(getReplyAuthor(this));
840 $(replyElement).find(".reply-distrust").submit(function() {
841 distrustSone(getReplyAuthor(this));
844 $(replyElement).find(".reply-untrust").submit(function() {
845 untrustSone(getReplyAuthor(this));
851 * Ajaxifies the given notification by replacing the form with AJAX.
853 * @param notification
854 * jQuery object representing the notification.
856 function ajaxifyNotification(notification) {
857 notification.find("form").submit(function() {
860 notification.find("input[name=returnPage]").val($.url.attr("relative"));
861 if (notification.find(".short-text").length > 0) {
862 notification.find(".short-text").removeClass("hidden");
863 notification.find(".text").addClass("hidden");
865 notification.find("form.mark-as-read button").click(function() {
866 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
868 notification.find("a[class^='link-']").each(function() {
869 linkElement = $(this);
870 if (linkElement.is("[href^='viewPost']")) {
871 id = linkElement.attr("class").substr(5);
873 linkElement.attr("href", "#post-" + id).addClass("in-page-link");
877 notification.find("form.dismiss button").click(function() {
878 ajaxGet("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
879 /* dismiss in case of error, too. */
880 notification.slideUp();
881 }, function(xmlHttpRequest, textStatus, error) {
889 * Retrieves element IDs from notification elements.
891 * @param notification
892 * The notification element
894 * The selector of the element containing the ID as text
895 * @returns All extracted IDs
897 function getElementIds(notification, selector) {
899 $(selector, notification).each(function() {
900 elementIds.push($(this).text());
906 * Compares the given notification elements and calls {@link #markSoneAsKnown()}
907 * for every ID that is contained in the old notification but not in the new.
909 * @param oldNotification
910 * The old notification element
911 * @param newNotification
912 * The new notification element
914 function checkForRemovedSones(oldNotification, newNotification) {
915 if (getNotificationId(oldNotification) != "new-sone-notification") {
918 oldIds = getElementIds(oldNotification, ".new-sone-id");
919 newIds = getElementIds(newNotification, ".new-sone-id");
920 $.each(oldIds, function(index, value) {
921 if ($.inArray(value, newIds) == -1) {
922 markSoneAsKnown(getSone(value), true);
928 * Compares the given notification elements and calls {@link #markPostAsKnown()}
929 * for every ID that is contained in the old notification but not in the new.
931 * @param oldNotification
932 * The old notification element
933 * @param newNotification
934 * The new notification element
936 function checkForRemovedPosts(oldNotification, newNotification) {
937 if (getNotificationId(oldNotification) != "new-post-notification") {
940 oldIds = getElementIds(oldNotification, ".post-id");
941 newIds = getElementIds(newNotification, ".post-id");
942 $.each(oldIds, function(index, value) {
943 if ($.inArray(value, newIds) == -1) {
944 markPostAsKnown(getPost(value), true);
950 * Compares the given notification elements and calls
951 * {@link #markReplyAsKnown()} for every ID that is contained in the old
952 * notification but not in the new.
954 * @param oldNotification
955 * The old notification element
956 * @param newNotification
957 * The new notification element
959 function checkForRemovedReplies(oldNotification, newNotification) {
960 if (getNotificationId(oldNotification) != "new-replies-notification") {
963 oldIds = getElementIds(oldNotification, ".reply-id");
964 newIds = getElementIds(newNotification, ".reply-id");
965 $.each(oldIds, function(index, value) {
966 if ($.inArray(value, newIds) == -1) {
967 markReplyAsKnown(getReply(value), true);
972 function getStatus() {
973 ajaxGet("getStatus.ajax", isViewSonePage() ? {"soneIds": getShownSoneId() } : {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
974 if ((data != null) && data.success) {
975 /* process Sone information. */
976 $.each(data.sones, function(index, value) {
977 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated, value.lastUpdatedText);
979 /* search for removed notifications. */
980 $("#sone #notification-area .notification").each(function() {
981 notificationId = $(this).attr("id");
982 foundNotification = false;
983 $.each(data.notifications, function(index, value) {
984 if (value.id == notificationId) {
985 foundNotification = true;
989 if (!foundNotification) {
990 if (notificationId == "new-sone-notification") {
991 $(".new-sone-id", this).each(function(index, element) {
992 soneId = $(this).text();
993 markSoneAsKnown(getSone(soneId), true);
995 } else if (notificationId == "new-post-notification") {
996 $(".post-id", this).each(function(index, element) {
997 postId = $(this).text();
998 markPostAsKnown(getPost(postId), true);
1000 } else if (notificationId == "new-replies-notification") {
1001 $(".reply-id", this).each(function(index, element) {
1002 replyId = $(this).text();
1003 markReplyAsKnown(getReply(replyId), true);
1006 $(this).slideUp("normal", function() {
1008 /* remove activity when no notifications are visible. */
1009 if ($("#sone #notification-area .notification").length == 0) {
1015 /* process notifications. */
1016 notificationIds = [];
1017 $.each(data.notifications, function(index, value) {
1018 oldNotification = getNotification(value.id);
1019 if ((oldNotification.length == 0) || (value.lastUpdatedTime > getNotificationLastUpdatedTime(oldNotification))) {
1020 notificationIds.push(value.id);
1023 if (notificationIds.length > 0) {
1024 loadNotifications(notificationIds);
1026 /* process new posts. */
1027 $.each(data.newPosts, function(index, value) {
1028 loadNewPost(value.id, value.sone, value.recipient, value.time);
1030 /* process new replies. */
1031 $.each(data.newReplies, function(index, value) {
1032 loadNewReply(value.id, value.sone, value.post, value.postSone);
1034 /* do it again in 5 seconds. */
1035 setTimeout(getStatus, 5000);
1037 /* data.success was false, wait 30 seconds. */
1038 setTimeout(getStatus, 30000);
1041 statusRequestQueued = false;
1047 * Requests multiple notifications from Sone and displays them.
1049 * @param notificationIds
1050 * Array of IDs of the notifications to load
1052 function loadNotifications(notificationIds) {
1053 ajaxGet("getNotification.ajax", {"notifications": notificationIds.join(",")}, function(data, textStatus) {
1054 if (!data || !data.success) {
1055 // TODO - show error
1058 $.each(data.notifications, function(index, value) {
1059 oldNotification = getNotification(value.id);
1060 notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide();
1061 if (oldNotification.length != 0) {
1062 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
1063 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
1064 notification.find(".short-text").toggleClass("hidden", opened);
1065 notification.find(".text").toggleClass("hidden", !opened);
1067 checkForRemovedSones(oldNotification, notification);
1068 checkForRemovedPosts(oldNotification, notification);
1069 checkForRemovedReplies(oldNotification, notification);
1070 oldNotification.replaceWith(notification.show());
1072 $("#sone #notification-area").append(notification);
1073 notification.slideDown();
1081 * Returns the ID of the currently logged in Sone.
1083 * @return The ID of the current Sone, or an empty string if no Sone is logged
1086 function getCurrentSoneId() {
1087 return $("#currentSoneId").text();
1091 * Returns the content of the page-id attribute.
1093 * @returns The page ID
1095 function getPageId() {
1096 return $("#sone .page-id").text();
1100 * Returns whether the current page is the index page.
1102 * @returns {Boolean} <code>true</code> if the current page is the index page,
1103 * <code>false</code> otherwise
1105 function isIndexPage() {
1106 return getPageId() == "index";
1110 * Returns the current page of the selected pagination. If no pagination can be
1111 * found with the given selector, {@code 1} is returned.
1113 * @param paginationSelector
1114 * The pagination selector
1115 * @returns The current page of the pagination
1117 function getPage(paginationSelector) {
1118 pagination = $(paginationSelector);
1119 if (pagination.length > 0) {
1120 return $(".current-page", paginationSelector).text();
1126 * Returns whether the current page is a “view Sone” page.
1128 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
1129 * page, <code>false</code> otherwise
1131 function isViewSonePage() {
1132 return getPageId() == "view-sone";
1136 * Returns the ID of the currently shown Sone. This will only return a sensible
1137 * value if isViewSonePage() returns <code>true</code>.
1139 * @returns The ID of the currently shown Sone
1141 function getShownSoneId() {
1142 return $("#sone .sone-id").text();
1146 * Returns whether the current page is a “view post” page.
1148 * @returns {Boolean} <code>true</code> if the current page is a “view post”
1149 * page, <code>false</code> otherwise
1151 function isViewPostPage() {
1152 return getPageId() == "view-post";
1156 * Returns the ID of the currently shown post. This will only return a sensible
1157 * value if isViewPostPage() returns <code>true</code>.
1159 * @returns The ID of the currently shown post
1161 function getShownPostId() {
1162 return $("#sone .post-id").text();
1166 * Returns whether the current page is the “known Sones” page.
1168 * @returns {Boolean} <code>true</code> if the current page is the “known
1169 * Sones” page, <code>false</code> otherwise
1171 function isKnownSonesPage() {
1172 return getPageId() == "known-sones";
1176 * Returns whether a post with the given ID exists on the current page.
1179 * The post ID to check for
1180 * @returns {Boolean} <code>true</code> if a post with the given ID already
1181 * exists on the page, <code>false</code> otherwise
1183 function hasPost(postId) {
1184 return $(".post#" + postId).length > 0;
1188 * Returns whether a reply with the given ID exists on the current page.
1191 * The reply ID to check for
1192 * @returns {Boolean} <code>true</code> if a reply with the given ID already
1193 * exists on the page, <code>false</code> otherwise
1195 function hasReply(replyId) {
1196 return $("#sone .reply#" + replyId).length > 0;
1199 function loadNewPost(postId, soneId, recipientId, time) {
1200 if (hasPost(postId)) {
1203 if (!isIndexPage() || (getPage(".pagination-index") > 1)) {
1204 if (!isViewPostPage() || (getShownPostId() != postId)) {
1205 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1210 if (getPostTime($("#sone .post").last()) > time) {
1213 ajaxGet("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1214 if ((data != null) && data.success) {
1215 if (hasPost(data.post.id)) {
1218 if ((!isIndexPage() || (getPage(".pagination-index") > 1)) && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1221 var firstOlderPost = null;
1222 $("#sone .post").each(function() {
1223 if (getPostTime(this) < data.post.time) {
1224 firstOlderPost = $(this);
1228 newPost = $(data.post.html).addClass("hidden");
1229 if (firstOlderPost != null) {
1230 newPost.insertBefore(firstOlderPost);
1232 ajaxifyPost(newPost);
1233 updatePostTimes(data.post.id);
1234 newPost.slideDown();
1240 function loadNewReply(replyId, soneId, postId, postSoneId) {
1241 if (hasReply(replyId)) {
1244 if (!hasPost(postId)) {
1247 ajaxGet("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1249 if ((data != null) && data.success) {
1250 if (hasReply(data.reply.id)) {
1253 $("#sone .post#" + data.reply.postId).each(function() {
1254 var firstNewerReply = null;
1255 $(this).find(".replies .reply").each(function() {
1256 if (getReplyTime(this) > data.reply.time) {
1257 firstNewerReply = $(this);
1261 newReply = $(data.reply.html).addClass("hidden");
1262 if (firstNewerReply != null) {
1263 newReply.insertBefore(firstNewerReply);
1265 if ($(this).find(".replies .create-reply")) {
1266 $(this).find(".replies .create-reply").before(newReply);
1268 $(this).find(".replies").append(newReply);
1271 ajaxifyReply(newReply);
1272 updateReplyTimes(data.reply.id);
1273 newReply.slideDown();
1282 * Marks the given Sone as known if it is still new.
1284 * @param soneElement
1285 * The Sone to mark as known
1286 * @param skipRequest
1287 * true to skip the JSON request, false or omit to perform the JSON
1290 function markSoneAsKnown(soneElement, skipRequest) {
1291 if ($(soneElement).is(".new")) {
1292 $(soneElement).removeClass("new");
1293 if ((typeof skipRequest == "undefined") || !skipRequest) {
1294 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)});
1299 function markPostAsKnown(postElements, skipRequest) {
1300 $(postElements).each(function() {
1302 if ($(postElement).hasClass("new")) {
1303 (function(postElement) {
1304 $(postElement).removeClass("new");
1305 $(".click-to-show", postElement).removeClass("new");
1306 if ((typeof skipRequest == "undefined") || !skipRequest) {
1307 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1312 markReplyAsKnown($(postElements).find(".reply"));
1315 function markReplyAsKnown(replyElements, skipRequest) {
1316 $(replyElements).each(function() {
1317 replyElement = this;
1318 if ($(replyElement).hasClass("new")) {
1319 (function(replyElement) {
1320 $(replyElement).removeClass("new");
1321 if ((typeof skipRequest == "undefined") || !skipRequest) {
1322 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1330 * Updates the time of the post with the given ID.
1333 * The ID of the post to update
1335 * The text of the time to show
1336 * @param refreshTime
1337 * The refresh time after which to request a new time (in seconds)
1339 * The tooltip to show
1341 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1342 if (!getPost(postId).is(":visible")) {
1345 getPost(postId).find(".post-status-line > .time a").html(timeText).attr("title", tooltip);
1346 (function(postId, refreshTime) {
1347 setTimeout(function() {
1348 updatePostTimes(postId);
1349 }, refreshTime * 1000);
1350 })(postId, refreshTime);
1354 * Requests new rendered times for the posts with the given IDs.
1357 * Comma-separated post IDs
1359 function updatePostTimes(postIds) {
1360 ajaxGet("getTimes.ajax", { "posts" : postIds }, function(data, textStatus) {
1361 if ((data != null) && data.success) {
1362 $.each(data.postTimes, function(index, value) {
1363 updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1370 * Updates the time of the reply with the given ID.
1373 * The ID of the reply to update
1375 * The text of the time to show
1376 * @param refreshTime
1377 * The refresh time after which to request a new time (in seconds)
1379 * The tooltip to show
1381 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1382 getReply(replyId).find(".reply-status-line > .time").html(timeText).attr("title", tooltip);
1383 (function(replyId, refreshTime) {
1384 setTimeout(function() {
1385 updateReplyTimes(replyId);
1386 }, refreshTime * 1000);
1387 })(replyId, refreshTime);
1391 * Requests new rendered times for the posts with the given IDs.
1394 * Comma-separated post IDs
1396 function updateReplyTimes(replyIds) {
1397 ajaxGet("getTimes.ajax", { "replies" : replyIds }, function(data, textStatus) {
1398 if ((data != null) && data.success) {
1399 $.each(data.replyTimes, function(index, value) {
1400 updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1406 function resetActivity() {
1407 title = document.title;
1408 if (title.indexOf('(') == 0) {
1409 setTitle(title.substr(title.indexOf(' ') + 1));
1411 iconBlinking = false;
1414 function setActivity() {
1416 title = document.title;
1417 if (title.indexOf('(') != 0) {
1418 setTitle("(!) " + title);
1420 if (!iconBlinking) {
1421 setTimeout(toggleIcon, 1500);
1422 iconBlinking = true;
1428 * Sets the window title after a small delay to prevent race-condition issues.
1433 function setTitle(title) {
1434 setTimeout(function() {
1435 document.title = title;
1439 /** Whether the icon is currently showing activity. */
1440 var iconActive = false;
1442 /** Whether the icon is currently supposed to blink. */
1443 var iconBlinking = false;
1446 * Toggles the icon. If the window has gained focus and the icon is still
1447 * showing the activity state, it is returned to normal.
1449 function toggleIcon() {
1450 if (focus || !iconBlinking) {
1452 changeIcon("images/icon.png");
1455 iconBlinking = false;
1457 iconActive = !iconActive;
1458 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1459 setTimeout(toggleIcon, 1500);
1464 * Changes the icon of the page.
1467 * The new URL of the icon
1469 function changeIcon(iconUrl) {
1470 $("link[rel=icon]").remove();
1471 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1472 $("iframe[id=icon-update]")[0].src += "";
1476 * Creates a new notification.
1479 * The ID of the notificaiton
1481 * The text of the notification
1482 * @param dismissable
1483 * <code>true</code> if the notification can be dismissed by the
1486 function createNotification(id, lastUpdatedTime, text, dismissable) {
1487 notification = $("<div></div>").addClass("notification").attr("id", id).attr("lastUpdatedTime", lastUpdatedTime);
1489 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id");
1490 dismissForm.find("input[name=notification]").val(id);
1491 notification.append(dismissForm);
1493 notification.append(text);
1494 return notification;
1498 * Shows the details of the notification with the given ID.
1500 * @param notificationId
1501 * The ID of the notification
1503 function showNotificationDetails(notificationId) {
1504 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1505 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1509 * Deletes the field with the given ID from the profile.
1512 * The ID of the field to delete
1514 function deleteProfileField(fieldId) {
1515 ajaxGet("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1516 if (data && data.success) {
1517 $("#sone .profile-field#" + data.field.id).slideUp();
1523 * Renames a profile field.
1526 * The ID of the field to rename
1528 * The new name of the field
1529 * @param successFunction
1530 * Called when the renaming was successful
1532 function editProfileField(fieldId, newName, successFunction) {
1533 ajaxGet("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1534 if (data && data.success) {
1541 * Moves the profile field with the given ID one slot in the given direction.
1544 * The ID of the field to move
1546 * The direction to move in (“up” or “down”)
1547 * @param successFunction
1548 * Function to call on success
1550 function moveProfileField(fieldId, direction, successFunction) {
1551 ajaxGet("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1552 if (data && data.success) {
1559 * Moves the profile field with the given ID up one slot.
1562 * The ID of the field to move
1563 * @param successFunction
1564 * Function to call on success
1566 function moveProfileFieldUp(fieldId, successFunction) {
1567 moveProfileField(fieldId, "up", successFunction);
1571 * Moves the profile field with the given ID down one slot.
1574 * The ID of the field to move
1575 * @param successFunction
1576 * Function to call on success
1578 function moveProfileFieldDown(fieldId, successFunction) {
1579 moveProfileField(fieldId, "down", successFunction);
1582 var statusRequestQueued = true;
1585 * Sets the status of the web interface as offline.
1587 function ajaxError() {
1589 toggleOfflineMarker(true);
1590 if (!statusRequestQueued) {
1591 setTimeout(getStatus, 5000);
1592 statusRequestQueued = true;
1597 * Sets the status of the web interface as online.
1599 function ajaxSuccess() {
1601 toggleOfflineMarker(false);
1605 * Shows or hides the offline marker.
1608 * {@code true} to display the offline marker, {@code false} to hide
1611 function toggleOfflineMarker(visible) {
1612 /* jQuery documentation says toggle() works the other way around?! */
1613 $("#sone #offline-marker").toggle(visible);
1615 $("#sone #main").addClass("offline");
1617 $("#sone #main").removeClass("offline");
1622 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1628 $(document).ready(function() {
1630 /* this initializes the status update input field. */
1631 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1632 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1633 $("#sone #update-status .select-sender").css("display", "inline");
1634 $("#sone #update-status .sender").hide();
1635 $("#sone #update-status .select-sender button").click(function() {
1636 $("#sone #update-status .sender").show();
1637 $("#sone #update-status .select-sender").hide();
1640 $("#sone #update-status").submit(function() {
1641 button = $("button:submit", this);
1642 button.attr("disabled", "disabled");
1643 if ($(this).find(":input.default:enabled").length > 0) {
1646 sender = $(this).find(":input[name=sender]").val();
1647 text = $(this).find(":input[name=text]:enabled").val();
1648 ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1649 button.removeAttr("disabled");
1651 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1652 $(this).find(":input[name=text]:enabled").val("").blur();
1653 $(this).find(".sender").hide();
1654 $(this).find(".select-sender").show();
1659 /* ajaxify the search input field. */
1660 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1661 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1664 /* ajaxify input field on “view Sone” page. */
1665 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1666 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1667 $("#sone #post-message .select-sender").css("display", "inline");
1668 $("#sone #post-message .sender").hide();
1669 $("#sone #post-message .select-sender button").click(function() {
1670 $("#sone #post-message .sender").show();
1671 $("#sone #post-message .select-sender").hide();
1674 $("#sone #post-message").submit(function() {
1675 sender = $(this).find(":input[name=sender]").val();
1676 text = $(this).find(":input[name=text]:enabled").val();
1677 ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text });
1678 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1679 $(this).find(":input[name=text]:enabled").val("").blur();
1680 $(this).find(".sender").hide();
1681 $(this).find(".select-sender").show();
1686 /* Ajaxifies all posts. */
1687 /* calling getTranslation here will cache the necessary values. */
1688 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1689 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1690 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1691 $("#sone .post").each(function() {
1698 /* update post times. */
1700 $("#sone .post").each(function() {
1701 postIds.push(getPostId(this));
1703 updatePostTimes(postIds.join(","));
1705 /* hides all replies but the latest two. */
1706 if (!isViewPostPage()) {
1707 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1708 $("#sone .post .replies").each(function() {
1709 allReplies = $(this).find(".reply");
1710 if (allReplies.length > 2) {
1712 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1713 $(allReplies[replyIndex]).addClass("hidden");
1714 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1716 clickToShowElement = $("<div></div>").addClass("click-to-show");
1718 clickToShowElement.addClass("new");
1720 (function(clickToShowElement, allReplies, text) {
1721 clickToShowElement.text(text);
1722 clickToShowElement.click(function() {
1723 allReplies.removeClass("hidden");
1724 clickToShowElement.addClass("hidden");
1726 })(clickToShowElement, allReplies, text);
1727 $(allReplies[0]).before(clickToShowElement);
1733 $("#sone .sone").each(function() {
1734 ajaxifySone($(this));
1737 /* process all existing notifications, ajaxify dismiss buttons. */
1738 $("#sone #notification-area .notification").each(function() {
1739 ajaxifyNotification($(this));
1742 /* disable all permalinks. */
1743 $(".permalink").click(function() {
1747 /* activate status polling. */
1748 setTimeout(getStatus, 5000);
1750 /* reset activity counter when the page has focus. */
1751 $(window).focus(function() {
1754 }).blur(function() {