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 (xmlHttpRequest.status == 403) {
14 if (typeof errorCallback != "undefined") {
20 })(url, data, successCallback, errorCallback);
23 function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, optional, dontUseTextarea) {
24 $(inputElement).each(function() {
25 textarea = $(dontUseTextarea ? "<input type=\"text\" name=\"" + inputFieldName + "\">" : "<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
26 if ($(this).val() == "") {
28 inputField = $(this).data("inputField");
29 inputField.show().removeAttr("disabled").addClass("default");
30 inputField.val(defaultText);
32 }).hide().data("inputField", $(this)).val($(this).val());
33 $(this).data("textarea", textarea).after(textarea);
34 (function(inputField, textarea) {
35 inputField.focus(function() {
36 $(this).hide().attr("disabled", "disabled");
37 /* no, show(), “display: block” is not what I need. */
38 textarea.attr("style", "display: inline").focus();
40 if (inputField.val() == "") {
41 inputField.addClass("default");
42 inputField.val(defaultText);
44 inputField.hide().attr("disabled", "disabled");
47 $(inputField.get(0).form).submit(function() {
48 inputField.attr("disabled", "disabled");
49 if (!optional && (textarea.val() == "")) {
50 inputField.removeAttr("disabled").focus();
54 })($(this), textarea);
59 * Adds a “comment” link to all status lines contained in the given element.
64 * The element to add a “comment” link to
66 function addCommentLink(postId, author, element, insertAfterThisElement) {
67 if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
70 commentElement = (function(postId, author) {
71 separator = $("<span> · </span>").addClass("separator");
72 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
73 replyElement = $("#sone .post#post-" + postId + " .create-reply");
74 replyElement.removeClass("hidden");
75 replyElement.removeClass("light");
76 (function(replyElement) {
77 replyElement.find("input.reply-input").blur(function() {
78 if ($(this).hasClass("default")) {
79 replyElement.addClass("light");
82 replyElement.removeClass("light");
85 textArea = replyElement.find("input.reply-input").focus().data("textarea");
86 if (author != getCurrentSoneId()) {
87 textArea.val(textArea.val() + "@sone://" + author + " ");
90 return commentElement;
92 $(insertAfterThisElement).after(commentElement.clone(true));
93 $(insertAfterThisElement).after(separator);
96 var translations = {};
99 * Retrieves the translation for the given key and calls the callback function.
100 * The callback function takes a single parameter, the translated string.
103 * The key of the translation string
105 * The callback function
107 function getTranslation(key, callback) {
108 if (key in translations) {
109 callback(translations[key]);
112 ajaxGet("getTranslation.ajax", {"key": key}, function(data, textStatus) {
113 if ((data != null) && data.success) {
114 translations[key] = data.value;
115 callback(data.value);
121 * Filters the given Sone ID, replacing all “~” characters by an underscore.
124 * The Sone ID to filter
125 * @returns The filtered Sone ID
127 function filterSoneId(soneId) {
128 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
132 * Updates the status of the given Sone.
135 * The ID of the Sone to update
137 * The status of the Sone (“idle”, “unknown”, “inserting”,
140 * Whether the Sone is modified
142 * Whether the Sone is locked
144 * The date and time of the last update (formatted for display)
146 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated, lastUpdatedText) {
147 $("#sone .sone." + filterSoneId(soneId)).
148 toggleClass("unknown", status == "unknown").
149 toggleClass("idle", status == "idle").
150 toggleClass("inserting", status == "inserting").
151 toggleClass("downloading", status == "downloading").
152 toggleClass("modified", modified);
153 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
154 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
155 if (lastUpdated != null) {
156 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").attr("title", lastUpdated).text(lastUpdatedText);
158 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
159 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown);
162 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
166 * Enhances a “delete” button so that the confirmation is done on the same page.
171 * The text to show on the button
172 * @param deleteCallback
173 * The callback that actually deletes something
175 function enhanceDeleteButton(button, text, deleteCallback) {
177 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
178 $(this).fadeOut("slow");
181 }).insertAfter(button);
182 (function(button, newButton) {
183 button.click(function() {
184 button.fadeOut("slow", function() {
185 newButton.fadeIn("slow");
186 $(document).one("click", function() {
187 if (this != newButton.get(0)) {
188 newButton.fadeOut(function() {
196 })(button, newButton);
201 * Enhances a post’s “delete” button.
206 * The ID of the post to delete
208 * The text to replace the button with
210 function enhanceDeletePostButton(button, postId, text) {
211 enhanceDeleteButton(button, text, function() {
212 ajaxGet("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
217 $("#sone .post#post-" + postId).slideUp();
218 } else if (data.error == "invalid-post-id") {
219 /* pretend the post is already gone. */
220 getPost(postId).slideUp();
221 } else if (data.error == "auth-required") {
222 alert("You need to be logged in.");
223 } else if (data.error == "not-authorized") {
224 alert("You are not allowed to delete this post.");
226 }, function(xmlHttpRequest, textStatus, error) {
233 * Enhances a reply’s “delete” button.
238 * The ID of the reply to delete
240 * The text to replace the button with
242 function enhanceDeleteReplyButton(button, replyId, text) {
243 enhanceDeleteButton(button, text, function() {
244 ajaxGet("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
249 $("#sone .reply#reply-" + replyId).slideUp();
250 } else if (data.error == "invalid-reply-id") {
251 /* pretend the reply is already gone. */
252 getReply(replyId).slideUp();
253 } else if (data.error == "auth-required") {
254 alert("You need to be logged in.");
255 } else if (data.error == "not-authorized") {
256 alert("You are not allowed to delete this reply.");
258 }, function(xmlHttpRequest, textStatus, error) {
264 function getFormPassword() {
265 return $("#sone #formPassword").text();
269 * Returns the element of the Sone with the given ID.
273 * @returns All Sone elements with the given ID
275 function getSone(soneId) {
276 return $("#sone .sone").filter(function(index) {
277 return $(".id", this).text() == soneId;
281 function getSoneElement(element) {
282 return $(element).closest(".sone");
286 * Returns the ID of the sone of the context menu that contains the given
290 * The element within a context menu to get the Sone ID for
291 * @return The Sone ID
293 function getMenuSone(element) {
294 return $(element).closest(".sone-menu").find(".sone-menu-id").text();
298 * Generates a list of Sones by concatening the names of the given sones with a
299 * new line character (“\n”).
302 * The sones to format
303 * @returns {String} The created string
305 function generateSoneList(sones) {
307 $.each(sones, function() {
308 if (soneList != "") {
311 soneList += this.name;
317 * Returns the ID of the Sone that this element belongs to.
320 * The element to locate the matching Sone ID for
321 * @returns The ID of the Sone, or undefined
323 function getSoneId(element) {
324 return getSoneElement(element).find(".id").text();
328 * Returns the element of the post with the given ID.
332 * @returns The element of the post
334 function getPost(postId) {
335 return $("#sone .post#post-" + postId);
338 function getPostElement(element) {
339 return $(element).closest(".post");
342 function getPostId(element) {
343 return getPostElement(element).attr("id").substr(5);
346 function getPostTime(element) {
347 return getPostElement(element).find(".post-time").text();
351 * Returns the author of the post the given element belongs to.
354 * The element whose post to get the author for
355 * @returns The ID of the authoring Sone
357 function getPostAuthor(element) {
358 return getPostElement(element).find(".post-author").text();
362 * Returns the element of the reply with the given ID.
365 * The ID of the reply
366 * @returns The element of the reply
368 function getReply(replyId) {
369 return $("#sone .reply#reply-" + replyId);
372 function getReplyElement(element) {
373 return $(element).closest(".reply");
376 function getReplyId(element) {
377 return getReplyElement(element).attr("id").substr(6);
380 function getReplyTime(element) {
381 return getReplyElement(element).find(".reply-time").text();
385 * Returns the author of the reply the given element belongs to.
388 * The element whose reply to get the author for
389 * @returns The ID of the authoring Sone
391 function getReplyAuthor(element) {
392 return getReplyElement(element).find(".reply-author").text();
396 * Returns the notification with the given ID.
398 * @param notificationId
399 * The ID of the notification
400 * @returns The notification element
402 function getNotification(notificationId) {
403 return $("#sone #notification-area .notification#" + notificationId);
407 * Returns the notification element closest to the given element.
410 * The element to get the closest notification of
411 * @return The closest notification element
413 function getNotificationElement(element) {
414 return $(element).closest(".notification");
418 * Returns the ID of the notification element.
420 * @param notificationElement
421 * The notification element
422 * @returns The ID of the notification
424 function getNotificationId(notificationElement) {
425 return $(notificationElement).attr("id");
429 * Returns the time the notification was last updated.
431 * @param notificationElement
432 * The notification element
433 * @returns The last update time of the notification
435 function getNotificationLastUpdatedTime(notificationElement) {
436 return $(notificationElement).attr("lastUpdatedTime");
439 function likePost(postId) {
440 ajaxGet("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
441 if ((data == null) || !data.success) {
444 $("#sone .post#post-" + postId + " > .inner-part > .status-line .like").addClass("hidden");
445 $("#sone .post#post-" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
446 updatePostLikes(postId);
447 }, function(xmlHttpRequest, textStatus, error) {
452 function unlikePost(postId) {
453 ajaxGet("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
454 if ((data == null) || !data.success) {
457 $("#sone .post#post-" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
458 $("#sone .post#post-" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
459 updatePostLikes(postId);
460 }, function(xmlHttpRequest, textStatus, error) {
465 function updatePostLikes(postId) {
466 ajaxGet("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
467 if ((data != null) && data.success) {
468 $("#sone .post#post-" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0);
469 $("#sone .post#post-" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
470 $("#sone .post#post-" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
472 }, function(xmlHttpRequest, textStatus, error) {
477 function likeReply(replyId) {
478 ajaxGet("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
479 if ((data == null) || !data.success) {
482 $("#sone .reply#reply-" + replyId + " .status-line .like").addClass("hidden");
483 $("#sone .reply#reply-" + replyId + " .status-line .unlike").removeClass("hidden");
484 updateReplyLikes(replyId);
485 }, function(xmlHttpRequest, textStatus, error) {
490 function unlikeReply(replyId) {
491 ajaxGet("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
492 if ((data == null) || !data.success) {
495 $("#sone .reply#reply-" + replyId + " .status-line .unlike").addClass("hidden");
496 $("#sone .reply#reply-" + replyId + " .status-line .like").removeClass("hidden");
497 updateReplyLikes(replyId);
498 }, function(xmlHttpRequest, textStatus, error) {
504 * Trusts the Sone with the given ID.
507 * The ID of the Sone to trust
509 function trustSone(soneId) {
510 ajaxGet("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
511 if ((data != null) && data.success) {
512 updateTrustControls(soneId, data.trustValue);
518 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
521 * The ID of the Sone to distrust
523 function distrustSone(soneId) {
524 ajaxGet("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
525 if ((data != null) && data.success) {
526 updateTrustControls(soneId, data.trustValue);
532 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
535 * The ID of the Sone to untrust
537 function untrustSone(soneId) {
538 ajaxGet("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
539 if ((data != null) && data.success) {
540 updateTrustControls(soneId, data.trustValue);
546 * Updates the trust controls for all posts and replies of the given Sone,
547 * according to the given trust value.
550 * The ID of the Sone to update all trust controls for
552 * The trust value for the Sone
554 function updateTrustControls(soneId, trustValue) {
555 $("#sone .post").each(function() {
556 if (getPostAuthor(this) == soneId) {
557 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
558 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
559 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
562 $("#sone .reply").each(function() {
563 if (getReplyAuthor(this) == soneId) {
564 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
565 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
566 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
572 * Bookmarks the post with the given ID.
575 * The ID of the post to bookmark
577 function bookmarkPost(postId) {
579 ajaxGet("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
580 if ((data != null) && data.success) {
581 getPost(postId).find(".bookmark").toggleClass("hidden", true);
582 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
589 * Unbookmarks the post with the given ID.
592 * The ID of the post to unbookmark
594 function unbookmarkPost(postId) {
595 ajaxGet("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
596 if ((data != null) && data.success) {
597 getPost(postId).find(".bookmark").toggleClass("hidden", false);
598 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
603 function updateReplyLikes(replyId) {
604 ajaxGet("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
605 if ((data != null) && data.success) {
606 $("#sone .reply#reply-" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0);
607 $("#sone .reply#reply-" + replyId + " .status-line .likes span.like-count").text(data.likes);
608 $("#sone .reply#reply-" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
610 }, function(xmlHttpRequest, textStatus, error) {
616 * Posts a reply and calls the given callback when the request finishes.
619 * The ID of the sender
621 * The ID of the post the reply refers to
624 * @param callbackFunction
625 * The callback function to call when the request finishes (takes 3
626 * parameters: success, error, replyId)
628 function postReply(sender, postId, text, callbackFunction) {
629 ajaxGet("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
631 /* TODO - show error */
635 callbackFunction(true, null, data.reply, data.sone);
637 callbackFunction(false, data.error);
639 }, function(xmlHttpRequest, textStatus, error) {
645 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
648 * The Sone to ajaxify
650 function ajaxifySone(soneElement) {
652 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
655 $(".follow", soneElement).submit(function() {
656 var followElement = this;
657 ajaxGet("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
658 $(followElement).addClass("hidden");
659 $(followElement).parent().find(".unfollow").removeClass("hidden");
663 $(".unfollow", soneElement).submit(function() {
664 var unfollowElement = this;
665 ajaxGet("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
666 $(unfollowElement).addClass("hidden");
667 $(unfollowElement).parent().find(".follow").removeClass("hidden");
671 $(".lock", soneElement).submit(function() {
672 var lockElement = this;
673 ajaxGet("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
674 $(lockElement).addClass("hidden");
675 $(lockElement).parent().find(".unlock").removeClass("hidden");
679 $(".unlock", soneElement).submit(function() {
680 var unlockElement = this;
681 ajaxGet("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
682 $(unlockElement).addClass("hidden");
683 $(unlockElement).parent().find(".lock").removeClass("hidden");
688 /* mark Sone as known when clicking it. */
689 $(soneElement).click(function() {
690 markSoneAsKnown(this);
695 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
698 * The post element to ajaxify
700 function ajaxifyPost(postElement) {
701 $(postElement).find("form").submit(function() {
704 $(postElement).find(".create-reply button:submit").click(function() {
706 button.attr("disabled", "disabled");
707 sender = $(this.form).find(":input[name=sender]").val();
708 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
709 postId = getPostId(this);
710 text = $(inputField).val();
711 (function(sender, postId, text, inputField) {
712 postReply(sender, postId, text, function(success, error, replyId, soneId) {
714 $(inputField).val("");
715 loadNewReply(replyId, soneId, postId);
716 $("#sone .post#post-" + postId + " .create-reply").addClass("hidden");
717 $("#sone .post#post-" + postId + " .create-reply .sender").hide();
718 $("#sone .post#post-" + postId + " .create-reply .select-sender").show();
719 $("#sone .post#post-" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
723 button.removeAttr("disabled");
725 })(sender, postId, text, inputField);
729 /* replace all “delete” buttons with javascript. */
730 (function(postElement) {
731 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
732 postId = getPostId(postElement);
733 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
737 /* convert all “like” buttons to javascript functions. */
738 $(postElement).find(".like-post").submit(function() {
739 likePost(getPostId(this));
742 $(postElement).find(".unlike-post").submit(function() {
743 unlikePost(getPostId(this));
747 /* convert trust control buttons to javascript functions. */
748 $(postElement).find(".post-trust").submit(function() {
749 trustSone(getPostAuthor(this));
752 $(postElement).find(".post-distrust").submit(function() {
753 distrustSone(getPostAuthor(this));
756 $(postElement).find(".post-untrust").submit(function() {
757 untrustSone(getPostAuthor(this));
761 /* convert bookmark/unbookmark buttons to javascript functions. */
762 $(postElement).find(".bookmark").submit(function() {
763 bookmarkPost(getPostId(this));
766 $(postElement).find(".unbookmark").submit(function() {
767 unbookmarkPost(getPostId(this));
771 /* convert “show source” link into javascript function. */
772 $(postElement).find(".show-source").each(function() {
773 $("a", this).click(function() {
774 post = getPostElement(this);
775 rawPostText = $(".post-text.raw-text", post);
776 rawPostText.toggleClass("hidden");
777 if (rawPostText.hasClass("hidden")) {
778 $(".post-text.short-text", post).removeClass("hidden");
779 $(".post-text.text", post).addClass("hidden");
780 $(".expand-post-text", post).removeClass("hidden");
781 $(".shrink-post-text", post).addClass("hidden");
783 $(".post-text.short-text", post).addClass("hidden");
784 $(".post-text.text", post).addClass("hidden");
785 $(".expand-post-text", post).addClass("hidden");
786 $(".shrink-post-text", post).addClass("hidden");
792 /* convert “show more” link into javascript function. */
793 $(postElement).find(".expand-post-text").each(function() {
794 $(this).click(function() {
795 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
796 $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
797 $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
798 $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
802 $(postElement).find(".shrink-post-text").each(function() {
803 $(this).click(function() {
804 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
805 $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
806 $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
807 $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
812 /* ajaxify author/post links */
813 $(".post-status-line .permalink a", postElement).click(function() {
814 if (!$(".create-reply", postElement).hasClass("hidden")) {
815 textArea = $("input.reply-input", postElement).focus().data("textarea");
816 $(textArea).replaceSelection($(this).attr("href"));
821 /* add “comment” link. */
822 addCommentLink(getPostId(postElement), getPostAuthor(postElement), postElement, $(postElement).find(".post-status-line .permalink-author"));
824 /* process all replies. */
826 $(postElement).find(".reply").each(function() {
827 replyIds.push(getReplyId(this));
830 updateReplyTimes(replyIds.join(","));
832 /* process reply input fields. */
833 getTranslation("WebInterface.DefaultText.Reply", function(text) {
834 $(postElement).find("input.reply-input").each(function() {
835 registerInputTextareaSwap(this, text, "text", false, false);
839 /* process sender selection. */
840 $(".select-sender", postElement).css("display", "inline");
841 $(".sender", postElement).hide();
842 $(".select-sender button", postElement).click(function() {
843 $(".sender", postElement).show();
844 $(".select-sender", postElement).hide();
848 /* mark everything as known on click. */
849 $(postElement).click(function(event) {
850 if ($(event.target).hasClass("click-to-show")) {
853 markPostAsKnown(this);
856 /* hide reply input field. */
857 $(postElement).find(".create-reply").addClass("hidden");
859 /* show Sone menu when hovering over the avatar. */
860 $(postElement).find(".post-avatar").mouseover(function() {
861 if (typeof currentSoneMenuTimeoutHandler != undefined) {
862 clearTimeout(currentSoneMenuTimeoutHandler);
864 currentSoneMenuId = getPostId(this);
865 currentSoneMenuTimeoutHandler = setTimeout(function() {
866 $(".sone-menu:visible").fadeOut();
867 $(".sone-post-menu", postElement).mouseleave(function() {
871 }).mouseleave(function() {
872 if (currentSoneMenuId = getPostId(this)) {
873 clearTimeout(currentSoneMenuTimeoutHandler);
876 (function(postElement) {
877 var soneId = $(".sone-menu-id", postElement).text();
878 $(".sone-post-menu .follow", postElement).click(function() {
879 var followElement = this;
880 ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
881 $(followElement).addClass("hidden");
882 $(followElement).parent().find(".unfollow").removeClass("hidden");
883 $("#sone .sone-menu").each(function() {
884 if (getMenuSone(this) == soneId) {
885 $(".follow", this).toggleClass("hidden", true);
886 $(".unfollow", this).toggleClass("hidden", false);
892 $(".sone-post-menu .unfollow", postElement).click(function() {
893 var unfollowElement = this;
894 ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
895 $(unfollowElement).addClass("hidden");
896 $(unfollowElement).parent().find(".follow").removeClass("hidden");
897 $("#sone .sone-menu").each(function() {
898 if (getMenuSone(this) == soneId) {
899 $(".follow", this).toggleClass("hidden", false);
900 $(".unfollow", this).toggleClass("hidden", true);
910 * Ajaxifies the given reply element.
912 * @param replyElement
913 * The reply element to ajaxify
915 function ajaxifyReply(replyElement) {
916 $(replyElement).find(".like-reply").submit(function() {
917 likeReply(getReplyId(this));
920 $(replyElement).find(".unlike-reply").submit(function() {
921 unlikeReply(getReplyId(this));
924 (function(replyElement) {
925 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
926 $(replyElement).find(".delete-reply button").each(function() {
927 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
932 /* ajaxify author links */
933 $(".reply-status-line .permalink a", replyElement).click(function() {
934 if (!$(".create-reply", getPostElement(replyElement)).hasClass("hidden")) {
935 textArea = $("input.reply-input", getPostElement(replyElement)).focus().data("textarea");
936 $(textArea).replaceSelection($(this).attr("href"));
941 addCommentLink(getPostId(replyElement), getReplyAuthor(replyElement), replyElement, $(replyElement).find(".reply-status-line .permalink-author"));
943 /* convert “show source” link into javascript function. */
944 $(replyElement).find(".show-reply-source").each(function() {
945 $("a", this).click(function() {
946 reply = getReplyElement(this);
947 rawReplyText = $(".reply-text.raw-text", reply);
948 rawReplyText.toggleClass("hidden");
949 if (rawReplyText.hasClass("hidden")) {
950 $(".reply-text.short-text", reply).removeClass("hidden");
951 $(".reply-text.text", reply).addClass("hidden");
952 $(".expand-reply-text", reply).removeClass("hidden");
953 $(".shrink-reply-text", reply).addClass("hidden");
955 $(".reply-text.short-text", reply).addClass("hidden");
956 $(".reply-text.text", reply).addClass("hidden");
957 $(".expand-reply-text", reply).addClass("hidden");
958 $(".shrink-reply-text", reply).addClass("hidden");
964 /* convert “show more” link into javascript function. */
965 $(replyElement).find(".expand-reply-text").each(function() {
966 $(this).click(function() {
967 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
968 $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
969 $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
970 $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
974 $(replyElement).find(".shrink-reply-text").each(function() {
975 $(this).click(function() {
976 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
977 $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
978 $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
979 $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
984 /* convert trust control buttons to javascript functions. */
985 $(replyElement).find(".reply-trust").submit(function() {
986 trustSone(getReplyAuthor(this));
989 $(replyElement).find(".reply-distrust").submit(function() {
990 distrustSone(getReplyAuthor(this));
993 $(replyElement).find(".reply-untrust").submit(function() {
994 untrustSone(getReplyAuthor(this));
998 /* show Sone menu when hovering over the avatar. */
999 $(replyElement).find(".reply-avatar").mouseover(function() {
1000 if (typeof currentSoneMenuTimeoutHandler != undefined) {
1001 clearTimeout(currentSoneMenuTimeoutHandler);
1003 currentSoneMenuId = getPostId(this) + "-" + getReplyId(this);
1004 currentSoneMenuTimeoutHandler = setTimeout(function() {
1005 $(".sone-menu:visible").fadeOut();
1006 $(".sone-reply-menu", replyElement).mouseleave(function() {
1010 }).mouseleave(function() {
1011 if (currentSoneMenuId = getPostId(this) + "-" + getReplyId(this)) {
1012 clearTimeout(currentSoneMenuTimeoutHandler);
1015 (function(replyElement) {
1016 var soneId = $(".sone-menu-id", replyElement).text();
1017 $(".sone-menu .follow", replyElement).click(function() {
1018 var followElement = this;
1019 ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1020 $(followElement).addClass("hidden");
1021 $(followElement).parent().find(".unfollow").removeClass("hidden");
1022 $("#sone .sone-menu").each(function() {
1023 if (getMenuSone(this) == soneId) {
1024 $(".follow", this).toggleClass("hidden", true);
1025 $(".unfollow", this).toggleClass("hidden", false);
1031 $(".sone-menu .unfollow", replyElement).click(function() {
1032 var unfollowElement = this;
1033 ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1034 $(unfollowElement).addClass("hidden");
1035 $(unfollowElement).parent().find(".follow").removeClass("hidden");
1036 $("#sone .sone-menu").each(function() {
1037 if (getMenuSone(this) == soneId) {
1038 $(".follow", this).toggleClass("hidden", false);
1039 $(".unfollow", this).toggleClass("hidden", true);
1049 * Ajaxifies the given notification by replacing the form with AJAX.
1051 * @param notification
1052 * jQuery object representing the notification.
1054 function ajaxifyNotification(notification) {
1055 notification.find("form").submit(function() {
1058 notification.find("input[name=returnPage]").val($.url.attr("relative"));
1059 if (notification.find(".short-text").length > 0) {
1060 notification.find(".short-text").removeClass("hidden");
1061 notification.find(".text").addClass("hidden");
1063 notification.find("form.mark-as-read button").click(function() {
1064 allIds = $(":input[name=id]", this.form).val().split(" ");
1065 for (index = 0; index < allIds.length; index += 16) {
1066 ids = allIds.slice(index, index + 16).join(" ");
1067 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": ids});
1070 notification.find("a[class^='link-']").each(function() {
1071 linkElement = $(this);
1072 if (linkElement.is("[href^='viewPost']")) {
1073 id = linkElement.attr("class").substr(5);
1075 linkElement.attr("href", "#post-" + id).addClass("in-page-link");
1079 notification.find("form.dismiss button").click(function() {
1080 ajaxGet("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
1081 /* dismiss in case of error, too. */
1082 notification.slideUp();
1083 }, function(xmlHttpRequest, textStatus, error) {
1087 return notification;
1091 * Returns the notification hash. This hash is used in {@link #getStatus()} to
1092 * determine whether the notifications changed and need to be reloaded.
1094 function getNotificationHash() {
1095 return $("#sone #notification-area #notification-hash").text();
1099 * Sets the notification hash.
1101 * @param notificationHash
1102 * The new notification hash
1104 function setNotificationHash(notificationHash) {
1105 $("#sone #notification-area #notification-hash").text(notificationHash);
1109 * Retrieves element IDs from notification elements.
1111 * @param notification
1112 * The notification element
1114 * The selector of the element containing the ID as text
1115 * @returns All extracted IDs
1117 function getElementIds(notification, selector) {
1119 $(selector, notification).each(function() {
1120 elementIds.push($(this).text());
1126 * Compares the given notification elements and calls {@link #markSoneAsKnown()}
1127 * for every ID that is contained in the old notification but not in the new.
1129 * @param oldNotification
1130 * The old notification element
1131 * @param newNotification
1132 * The new notification element
1134 function checkForRemovedSones(oldNotification, newNotification) {
1135 if (getNotificationId(oldNotification) != "new-sone-notification") {
1138 oldIds = getElementIds(oldNotification, ".new-sone-id");
1139 newIds = getElementIds(newNotification, ".new-sone-id");
1140 $.each(oldIds, function(index, value) {
1141 if ($.inArray(value, newIds) == -1) {
1142 markSoneAsKnown(getSone(value), true);
1148 * Compares the given notification elements and calls {@link #markPostAsKnown()}
1149 * for every ID that is contained in the old notification but not in the new.
1151 * @param oldNotification
1152 * The old notification element
1153 * @param newNotification
1154 * The new notification element
1156 function checkForRemovedPosts(oldNotification, newNotification) {
1157 if (getNotificationId(oldNotification) != "new-post-notification") {
1160 oldIds = getElementIds(oldNotification, ".post-id");
1161 newIds = getElementIds(newNotification, ".post-id");
1162 $.each(oldIds, function(index, value) {
1163 if ($.inArray(value, newIds) == -1) {
1164 markPostAsKnown(getPost(value), true);
1170 * Compares the given notification elements and calls
1171 * {@link #markReplyAsKnown()} for every ID that is contained in the old
1172 * notification but not in the new.
1174 * @param oldNotification
1175 * The old notification element
1176 * @param newNotification
1177 * The new notification element
1179 function checkForRemovedReplies(oldNotification, newNotification) {
1180 if (getNotificationId(oldNotification) != "new-reply-notification") {
1183 oldIds = getElementIds(oldNotification, ".reply-id");
1184 newIds = getElementIds(newNotification, ".reply-id");
1185 $.each(oldIds, function(index, value) {
1186 if ($.inArray(value, newIds) == -1) {
1187 markReplyAsKnown(getReply(value), true);
1192 function getStatus() {
1193 ajaxGet("getStatus.ajax", isViewSonePage() ? {"soneIds": getShownSoneId() } : {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
1194 if ((data != null) && data.success) {
1195 /* process Sone information. */
1196 $.each(data.sones, function(index, value) {
1197 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated, value.lastUpdatedText);
1199 notLoggedIn = !data.loggedIn;
1201 showOfflineMarker(!online);
1203 if (data.notificationHash != getNotificationHash()) {
1204 console.log("Old hash: ", getNotificationHash(), ", new hash: ", data.notificationHash);
1205 setNotificationHash(data.notificationHash);
1207 /* search for removed notifications. */
1208 $("#sone #notification-area .notification").each(function() {
1209 notificationId = $(this).attr("id");
1210 foundNotification = false;
1211 $.each(data.notifications, function(index, value) {
1212 if (value.id == notificationId) {
1213 foundNotification = true;
1217 if (!foundNotification) {
1218 if (notificationId == "new-sone-notification" && (data.options["ShowNotification/NewSones"] == true)) {
1219 $(".new-sone-id", this).each(function(index, element) {
1220 soneId = $(this).text();
1221 markSoneAsKnown(getSone(soneId), true);
1223 } else if (notificationId == "new-post-notification" && (data.options["ShowNotification/NewPosts"] == true)) {
1224 $(".post-id", this).each(function(index, element) {
1225 postId = $(this).text();
1226 markPostAsKnown(getPost(postId), true);
1228 } else if (notificationId == "new-reply-notification" && (data.options["ShowNotification/NewReplies"] == true)) {
1229 $(".reply-id", this).each(function(index, element) {
1230 replyId = $(this).text();
1231 markReplyAsKnown(getReply(replyId), true);
1234 $(this).slideUp("normal", function() {
1236 /* remove activity when no notifications are visible. */
1237 if ($("#sone #notification-area .notification").length == 0) {
1243 /* process notifications. */
1244 notificationIds = [];
1245 $.each(data.notifications, function(index, value) {
1246 oldNotification = getNotification(value.id);
1247 if ((oldNotification.length == 0) || (value.lastUpdatedTime > getNotificationLastUpdatedTime(oldNotification))) {
1248 notificationIds.push(value.id);
1251 if (notificationIds.length > 0) {
1252 loadNotifications(notificationIds);
1254 /* process new posts. */
1255 $.each(data.newPosts, function(index, value) {
1256 loadNewPost(value.id, value.sone, value.recipient, value.time);
1258 /* process new replies. */
1259 $.each(data.newReplies, function(index, value) {
1260 loadNewReply(value.id, value.sone, value.post, value.postSone);
1262 /* do it again in 5 seconds. */
1263 setTimeout(getStatus, 5000);
1265 /* data.success was false, wait 30 seconds. */
1266 setTimeout(getStatus, 30000);
1269 statusRequestQueued = false;
1275 * Requests multiple notifications from Sone and displays them.
1277 * @param notificationIds
1278 * Array of IDs of the notifications to load
1280 function loadNotifications(notificationIds) {
1281 ajaxGet("getNotification.ajax", {"notifications": notificationIds.join(",")}, function(data, textStatus) {
1282 if (!data || !data.success) {
1283 // TODO - show error
1286 $.each(data.notifications, function(index, value) {
1287 oldNotification = getNotification(value.id);
1288 notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide();
1289 if (oldNotification.length != 0) {
1290 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
1291 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
1292 notification.find(".short-text").toggleClass("hidden", opened);
1293 notification.find(".text").toggleClass("hidden", !opened);
1295 checkForRemovedSones(oldNotification, notification);
1296 checkForRemovedPosts(oldNotification, notification);
1297 checkForRemovedReplies(oldNotification, notification);
1298 oldNotification.replaceWith(notification.show());
1300 $("#sone #notification-area").append(notification);
1301 if (value.id.substring(0, 5) != "local") {
1302 notification.slideDown();
1311 * Returns the ID of the currently logged in Sone.
1313 * @return The ID of the current Sone, or an empty string if no Sone is logged
1316 function getCurrentSoneId() {
1317 return $("#currentSoneId").text();
1321 * Returns the content of the page-id attribute.
1323 * @returns The page ID
1325 function getPageId() {
1326 return $("#sone .page-id").text();
1330 * Returns whether the current page is the index page.
1332 * @returns {Boolean} <code>true</code> if the current page is the index page,
1333 * <code>false</code> otherwise
1335 function isIndexPage() {
1336 return getPageId() == "index";
1340 * Returns the current page of the selected pagination. If no pagination can be
1341 * found with the given selector, {@code 1} is returned.
1343 * @param paginationSelector
1344 * The pagination selector
1345 * @returns The current page of the pagination
1347 function getPage(paginationSelector) {
1348 pagination = $(paginationSelector);
1349 if (pagination.length > 0) {
1350 return $(".current-page", paginationSelector).text();
1356 * Returns whether the current page is a “view Sone” page.
1358 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
1359 * page, <code>false</code> otherwise
1361 function isViewSonePage() {
1362 return getPageId() == "view-sone";
1366 * Returns the ID of the currently shown Sone. This will only return a sensible
1367 * value if isViewSonePage() returns <code>true</code>.
1369 * @returns The ID of the currently shown Sone
1371 function getShownSoneId() {
1372 return $("#sone .sone-id").first().text();
1376 * Returns whether the current page is a “view post” page.
1378 * @returns {Boolean} <code>true</code> if the current page is a “view post”
1379 * page, <code>false</code> otherwise
1381 function isViewPostPage() {
1382 return getPageId() == "view-post";
1386 * Returns the ID of the currently shown post. This will only return a sensible
1387 * value if isViewPostPage() returns <code>true</code>.
1389 * @returns The ID of the currently shown post
1391 function getShownPostId() {
1392 return $("#sone .post-id").text();
1396 * Returns whether the current page is the “known Sones” page.
1398 * @returns {Boolean} <code>true</code> if the current page is the “known
1399 * Sones” page, <code>false</code> otherwise
1401 function isKnownSonesPage() {
1402 return getPageId() == "known-sones";
1406 * Returns whether a post with the given ID exists on the current page.
1409 * The post ID to check for
1410 * @returns {Boolean} <code>true</code> if a post with the given ID already
1411 * exists on the page, <code>false</code> otherwise
1413 function hasPost(postId) {
1414 return $(".post#post-" + postId).length > 0;
1418 * Returns whether a reply with the given ID exists on the current page.
1421 * The reply ID to check for
1422 * @returns {Boolean} <code>true</code> if a reply with the given ID already
1423 * exists on the page, <code>false</code> otherwise
1425 function hasReply(replyId) {
1426 return $("#sone .reply#reply-" + replyId).length > 0;
1429 function loadNewPost(postId, soneId, recipientId, time) {
1430 if (hasPost(postId)) {
1433 if (!isIndexPage() || (getPage(".pagination-index") > 1)) {
1434 if (!isViewPostPage() || (getShownPostId() != postId)) {
1435 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId)) || (getPage(".post-navigation") > 1)) {
1440 if (getPostTime($("#sone .post").last()) > time) {
1443 ajaxGet("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1444 if ((data != null) && data.success) {
1445 if (hasPost(data.post.id)) {
1448 if ((!isIndexPage() || (getPage(".pagination-index") > 1)) && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient) || (getPage(".post-navigation") > 1)))) {
1451 var firstOlderPost = null;
1452 $("#sone .post").each(function() {
1453 if (getPostTime(this) < data.post.time) {
1454 firstOlderPost = $(this);
1458 newPost = $(data.post.html).addClass("hidden");
1459 if ($(".post-author-local", newPost).text() == "true") {
1460 newPost.removeClass("new");
1462 if (firstOlderPost != null) {
1463 newPost.insertBefore(firstOlderPost);
1465 ajaxifyPost(newPost);
1466 updatePostTimes(data.post.id);
1467 newPost.slideDown();
1473 function loadNewReply(replyId, soneId, postId, postSoneId) {
1474 if (hasReply(replyId)) {
1477 if (!hasPost(postId)) {
1480 ajaxGet("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1482 if ((data != null) && data.success) {
1483 if (hasReply(data.reply.id)) {
1486 $("#sone .post#post-" + data.reply.postId).each(function() {
1487 var firstNewerReply = null;
1488 $(this).find(".replies .reply").each(function() {
1489 if (getReplyTime(this) > data.reply.time) {
1490 firstNewerReply = $(this);
1494 newReply = $(data.reply.html).addClass("hidden");
1495 if ($(".reply-author-local", newReply).text() == "true") {
1496 newReply.removeClass("new");
1497 (function(newReply) {
1498 setTimeout(function() {
1499 markReplyAsKnown(newReply, false);
1503 if (firstNewerReply != null) {
1504 newReply.insertBefore(firstNewerReply);
1506 if ($(this).find(".replies .create-reply")) {
1507 $(this).find(".replies .create-reply").before(newReply);
1509 $(this).find(".replies").append(newReply);
1512 ajaxifyReply(newReply);
1513 updateReplyTimes(data.reply.id);
1514 newReply.slideDown();
1523 * Marks the given Sone as known if it is still new.
1525 * @param soneElement
1526 * The Sone to mark as known
1527 * @param skipRequest
1528 * true to skip the JSON request, false or omit to perform the JSON
1531 function markSoneAsKnown(soneElement, skipRequest) {
1532 if ($(soneElement).is(".new")) {
1533 $(soneElement).removeClass("new");
1534 if ((typeof skipRequest == "undefined") || !skipRequest) {
1535 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)});
1540 function markPostAsKnown(postElements, skipRequest) {
1541 $(postElements).each(function() {
1543 if ($(postElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1544 (function(postElement) {
1545 $(postElement).removeClass("new");
1546 if ((typeof skipRequest == "undefined") || !skipRequest) {
1547 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1551 $(".click-to-show", postElement).removeClass("new");
1553 markReplyAsKnown($(postElements).find(".reply"));
1556 function markReplyAsKnown(replyElements, skipRequest) {
1557 $(replyElements).each(function() {
1558 replyElement = this;
1559 if ($(replyElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1560 (function(replyElement) {
1561 $(replyElement).removeClass("new");
1562 if ((typeof skipRequest == "undefined") || !skipRequest) {
1563 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1571 * Updates the time of the post with the given ID.
1574 * The ID of the post to update
1576 * The text of the time to show
1577 * @param refreshTime
1578 * The refresh time after which to request a new time (in seconds)
1580 * The tooltip to show
1582 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1583 if (!getPost(postId).is(":visible")) {
1586 getPost(postId).find(".post-status-line > .time a").html(timeText).attr("title", tooltip);
1587 (function(postId, refreshTime) {
1588 setTimeout(function() {
1589 updatePostTimes(postId);
1590 }, refreshTime * 1000);
1591 })(postId, refreshTime);
1595 * Requests new rendered times for the posts with the given IDs.
1598 * Comma-separated post IDs
1600 function updatePostTimes(postIds) {
1601 ajaxGet("getTimes.ajax", { "posts" : postIds }, function(data, textStatus) {
1602 if ((data != null) && data.success) {
1603 $.each(data.postTimes, function(index, value) {
1604 updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1611 * Updates the time of the reply with the given ID.
1614 * The ID of the reply to update
1616 * The text of the time to show
1617 * @param refreshTime
1618 * The refresh time after which to request a new time (in seconds)
1620 * The tooltip to show
1622 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1623 getReply(replyId).find(".reply-status-line > .time").html(timeText).attr("title", tooltip);
1624 (function(replyId, refreshTime) {
1625 setTimeout(function() {
1626 updateReplyTimes(replyId);
1627 }, refreshTime * 1000);
1628 })(replyId, refreshTime);
1632 * Requests new rendered times for the posts with the given IDs.
1635 * Comma-separated post IDs
1637 function updateReplyTimes(replyIds) {
1638 ajaxGet("getTimes.ajax", { "replies" : replyIds }, function(data, textStatus) {
1639 if ((data != null) && data.success) {
1640 $.each(data.replyTimes, function(index, value) {
1641 updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1647 function resetActivity() {
1648 title = document.title;
1649 if (title.indexOf('(') == 0) {
1650 setTitle(title.substr(title.indexOf(' ') + 1));
1652 iconBlinking = false;
1655 function setActivity() {
1657 title = document.title;
1658 if (title.indexOf('(') != 0) {
1659 setTitle("(!) " + title);
1661 if (!iconBlinking) {
1662 setTimeout(toggleIcon, 1500);
1663 iconBlinking = true;
1669 * Sets the window title after a small delay to prevent race-condition issues.
1674 function setTitle(title) {
1675 setTimeout(function() {
1676 document.title = title;
1680 /** Whether the icon is currently showing activity. */
1681 var iconActive = false;
1683 /** Whether the icon is currently supposed to blink. */
1684 var iconBlinking = false;
1687 * Toggles the icon. If the window has gained focus and the icon is still
1688 * showing the activity state, it is returned to normal.
1690 function toggleIcon() {
1691 if (focus || !iconBlinking) {
1693 changeIcon("images/icon.png");
1696 iconBlinking = false;
1698 iconActive = !iconActive;
1699 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1700 setTimeout(toggleIcon, 1500);
1705 * Changes the icon of the page.
1708 * The new URL of the icon
1710 function changeIcon(iconUrl) {
1711 $("link[rel=icon]").remove();
1712 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1713 $("iframe[id=icon-update]")[0].src += "";
1717 * Creates a new notification.
1720 * The ID of the notificaiton
1722 * The text of the notification
1723 * @param dismissable
1724 * <code>true</code> if the notification can be dismissed by the
1727 function createNotification(id, lastUpdatedTime, text, dismissable) {
1728 notification = $("<div></div>").addClass("notification").attr("id", id).attr("lastUpdatedTime", lastUpdatedTime);
1730 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id");
1731 dismissForm.find("input[name=notification]").val(id);
1732 notification.append(dismissForm);
1734 notification.append(text);
1735 return notification;
1739 * Shows the details of the notification with the given ID.
1741 * @param notificationId
1742 * The ID of the notification
1744 function showNotificationDetails(notificationId) {
1745 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1746 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1750 * Deletes the field with the given ID from the profile.
1753 * The ID of the field to delete
1755 function deleteProfileField(fieldId) {
1756 ajaxGet("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1757 if (data && data.success) {
1758 $("#sone .profile-field#" + data.field.id).slideUp();
1764 * Renames a profile field.
1767 * The ID of the field to rename
1769 * The new name of the field
1770 * @param successFunction
1771 * Called when the renaming was successful
1773 function editProfileField(fieldId, newName, successFunction) {
1774 ajaxGet("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1775 if (data && data.success) {
1782 * Moves the profile field with the given ID one slot in the given direction.
1785 * The ID of the field to move
1787 * The direction to move in (“up” or “down”)
1788 * @param successFunction
1789 * Function to call on success
1791 function moveProfileField(fieldId, direction, successFunction) {
1792 ajaxGet("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1793 if (data && data.success) {
1800 * Moves the profile field with the given ID up one slot.
1803 * The ID of the field to move
1804 * @param successFunction
1805 * Function to call on success
1807 function moveProfileFieldUp(fieldId, successFunction) {
1808 moveProfileField(fieldId, "up", successFunction);
1812 * Moves the profile field with the given ID down one slot.
1815 * The ID of the field to move
1816 * @param successFunction
1817 * Function to call on success
1819 function moveProfileFieldDown(fieldId, successFunction) {
1820 moveProfileField(fieldId, "down", successFunction);
1823 var statusRequestQueued = true;
1826 * Sets the status of the web interface as offline.
1828 function ajaxError() {
1830 showOfflineMarker(true);
1831 if (!statusRequestQueued) {
1832 setTimeout(getStatus, 5000);
1833 statusRequestQueued = true;
1838 * Sets the status of the web interface as online.
1840 function ajaxSuccess() {
1842 showOfflineMarker(!online || (initiallyLoggedIn && notLoggedIn));
1846 * Shows or hides the offline marker.
1849 * {@code true} to display the offline marker, {@code false} to hide
1852 function showOfflineMarker(visible) {
1853 /* jQuery documentation says toggle() works the other way around?! */
1854 $("#sone #offline-marker").toggle(visible);
1856 $("#sone #main").addClass("offline");
1858 $("#sone #main").removeClass("offline");
1863 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1868 var initiallyLoggedIn = $("#sone #loggedIn").text() == "true";
1869 var notLoggedIn = !initiallyLoggedIn;
1871 /** ID of the next-to-show Sone context menu. */
1872 var currentSoneMenuId;
1874 /** Timeout handler for the next-to-show Sone context menu. */
1875 var currentSoneMenuTimeoutHandler;
1877 $(document).ready(function() {
1879 /* this initializes the status update input field. */
1880 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1881 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1882 $("#sone #update-status .select-sender").css("display", "inline");
1883 $("#sone #update-status .sender").hide();
1884 $("#sone #update-status .select-sender button").click(function() {
1885 $("#sone #update-status .sender").show();
1886 $("#sone #update-status .select-sender").hide();
1889 $("#sone #update-status").submit(function() {
1890 button = $("button:submit", this);
1891 button.attr("disabled", "disabled");
1892 if ($(this).find(":input.default:enabled").length > 0) {
1895 sender = $(this).find(":input[name=sender]").val();
1896 text = $(this).find(":input[name=text]:enabled").val();
1897 ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1898 button.removeAttr("disabled");
1900 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1901 $(this).find(":input[name=text]:enabled").val("").blur();
1902 $(this).find(".sender").hide();
1903 $(this).find(".select-sender").show();
1908 /* ajaxify the search input field. */
1909 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1910 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1913 /* ajaxify input field on “view Sone” page. */
1914 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1915 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1916 $("#sone #post-message .select-sender").css("display", "inline");
1917 $("#sone #post-message .sender").hide();
1918 $("#sone #post-message .select-sender button").click(function() {
1919 $("#sone #post-message .sender").show();
1920 $("#sone #post-message .select-sender").hide();
1923 $("#sone #post-message").submit(function() {
1924 sender = $(this).find(":input[name=sender]").val();
1925 text = $(this).find(":input[name=text]:enabled").val();
1926 ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text });
1927 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1928 $(this).find(":input[name=text]:enabled").val("").blur();
1929 $(this).find(".sender").hide();
1930 $(this).find(".select-sender").show();
1935 /* Ajaxifies all posts. */
1936 /* calling getTranslation here will cache the necessary values. */
1937 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1938 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1939 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1940 $("#sone .post").each(function() {
1947 /* update post times. */
1949 $("#sone .post").each(function() {
1950 postIds.push(getPostId(this));
1952 updatePostTimes(postIds.join(","));
1954 /* hides all replies but the latest two. */
1955 if (!isViewPostPage()) {
1956 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1957 $("#sone .post .replies").each(function() {
1958 allReplies = $(this).find(".reply");
1959 if (allReplies.length > 2) {
1961 for (replyIndex = 0; !newHidden && (replyIndex < (allReplies.length - 2)); ++replyIndex) {
1962 $(allReplies[replyIndex]).addClass("hidden");
1963 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1965 clickToShowElement = $("<div></div>").addClass("click-to-show");
1967 clickToShowElement.addClass("new");
1969 (function(clickToShowElement, allReplies, text) {
1970 clickToShowElement.text(text);
1971 clickToShowElement.click(function() {
1972 allReplies.removeClass("hidden");
1973 clickToShowElement.addClass("hidden");
1975 })(clickToShowElement, allReplies, text);
1976 $(allReplies[0]).before(clickToShowElement);
1982 $("#sone .sone").each(function() {
1983 ajaxifySone($(this));
1986 /* process all existing notifications, ajaxify dismiss buttons. */
1987 $("#sone #notification-area .notification").each(function() {
1988 ajaxifyNotification($(this));
1991 /* activate status polling. */
1992 setTimeout(getStatus, 5000);
1994 /* reset activity counter when the page has focus. */
1995 $(window).focus(function() {
1998 }).blur(function() {