1 /* Sone JavaScript functions. */
3 function ajaxGet(url, data, successCallback, errorCallback) {
4 (function(url, data, successCallback, errorCallback) {
5 $.ajax({"cache": false, "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 var textarea = $(dontUseTextarea ? "<input type=\"text\" name=\"" + inputFieldName + "\">" : "<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
26 if ($(this).val() == "") {
28 var 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 (function(postId, author, insertAfterThisElement) {
71 var separator = $("<span> · </span>").addClass("separator");
72 getTranslation("WebInterface.Button.Comment", function(text) {
73 var commentElement = $("<div><span>" + text + "</span></div>").addClass("show-reply-form").click(function() {
74 var replyElement = sone.find(".post#post-" + postId + " .create-reply");
75 replyElement.removeClass("hidden");
76 replyElement.removeClass("light");
77 (function(replyElement) {
78 replyElement.find(":input.reply-input").blur(function() {
79 if ($(this).hasClass("default")) {
80 replyElement.addClass("light");
83 replyElement.removeClass("light");
86 var textArea = replyElement.find(":input.reply-input").focus().data("textarea");
87 if (author != getCurrentSoneId()) {
88 textArea.val(textArea.val() + "@sone://" + author + " ");
91 $(insertAfterThisElement).after(commentElement.clone(true));
92 $(insertAfterThisElement).after(separator);
94 })(postId, author, insertAfterThisElement);
97 var translations = {};
100 * Retrieves the translation for the given key and calls the callback function.
101 * The callback function takes a single parameter, the translated string.
104 * The key of the translation string
106 * The callback function
108 function getTranslation(key, callback) {
109 if (key in translations) {
110 callback(translations[key]);
113 ajaxGet("getTranslation.ajax", {"key": key}, function(data, textStatus) {
114 if ((data != null) && data.success) {
115 translations[key] = data.value;
116 callback(data.value);
122 * Filters the given Sone ID, replacing all “~” characters by an underscore.
125 * The Sone ID to filter
126 * @returns The filtered Sone ID
128 function filterSoneId(soneId) {
129 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
133 * Updates the status of the given Sone.
136 * The ID of the Sone to update
138 * The status of the Sone (“idle”, “unknown”, “inserting”,
141 * Whether the Sone is modified
143 * Whether the Sone is locked
145 * The date and time of the last update (formatted for display)
147 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated, lastUpdatedText) {
148 var updateSone = sone.find(".sone." + filterSoneId(soneId));
149 updateSone.toggleClass("unknown", status == "unknown").
150 toggleClass("idle", status == "idle").
151 toggleClass("inserting", status == "inserting").
152 toggleClass("downloading", status == "downloading").
153 toggleClass("modified", modified);
154 updateSone.find(".lock").toggleClass("hidden", locked);
155 updateSone.find(".unlock").toggleClass("hidden", !locked);
156 if (lastUpdated != null) {
157 updateSone.find(".last-update span.time").attr("title", lastUpdated).text(lastUpdatedText);
159 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
160 updateSone.find(".last-update span.time").text(unknown);
163 updateSone.find(".profile-link a").text(name);
167 * Enhances a “delete” button so that the confirmation is done on the same page.
172 * The text to show on the button
173 * @param deleteCallback
174 * The callback that actually deletes something
176 function enhanceDeleteButton(button, text, deleteCallback) {
178 var newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
179 $(this).fadeOut("slow");
182 }).insertAfter(button);
183 (function(button, newButton) {
184 button.click(function() {
185 button.fadeOut("slow", function() {
186 newButton.fadeIn("slow");
187 $(document).one("click", function() {
188 if (this != newButton.get(0)) {
189 newButton.fadeOut(function() {
197 })(button, newButton);
202 * Enhances a post’s “delete” button.
207 * The ID of the post to delete
209 * The text to replace the button with
211 function enhanceDeletePostButton(button, postId, text) {
212 enhanceDeleteButton(button, text, function() {
213 ajaxGet("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
218 sone.find(".post#post-" + postId).slideUp();
219 } else if (data.error == "invalid-post-id") {
220 /* pretend the post is already gone. */
221 getPost(postId).slideUp();
222 } else if (data.error == "auth-required") {
223 alert("You need to be logged in.");
224 } else if (data.error == "not-authorized") {
225 alert("You are not allowed to delete this post.");
227 }, function(xmlHttpRequest, textStatus, error) {
234 * Enhances a reply’s “delete” button.
239 * The ID of the reply to delete
241 * The text to replace the button with
243 function enhanceDeleteReplyButton(button, replyId, text) {
244 enhanceDeleteButton(button, text, function() {
245 ajaxGet("deleteReply.ajax", { "reply": replyId, "formPassword": sone.find("#formPassword").text() }, function(data, textStatus) {
250 sone.find(".reply#reply-" + replyId).slideUp();
251 } else if (data.error == "invalid-reply-id") {
252 /* pretend the reply is already gone. */
253 getReply(replyId).slideUp();
254 } else if (data.error == "auth-required") {
255 alert("You need to be logged in.");
256 } else if (data.error == "not-authorized") {
257 alert("You are not allowed to delete this reply.");
259 }, function(xmlHttpRequest, textStatus, error) {
265 function getFormPassword() {
266 return sone.find("#formPassword").text();
270 * Returns the element of the Sone with the given ID.
274 * @returns All Sone elements with the given ID
276 function getSone(soneId) {
277 return sone.find(".sone").filter(function(index) {
278 return $(".id", this).text() == soneId;
282 function getSoneElement(element) {
283 return $(element).closest(".sone");
287 * Returns the ID of the sone of the context menu that contains the given
291 * The element within a context menu to get the Sone ID for
292 * @return The Sone ID
294 function getMenuSone(element) {
295 return $(element).closest(".sone-menu").find(".sone-menu-id").text();
299 * Generates a list of Sones by concatening the names of the given sones with a
300 * new line character (“\n”).
303 * The sones to format
304 * @returns {String} The created string
306 function generateSoneList(sones) {
308 $.each(sones, function() {
309 if (soneList != "") {
312 soneList += this.name;
318 * Returns the ID of the Sone that this element belongs to.
321 * The element to locate the matching Sone ID for
322 * @returns The ID of the Sone, or undefined
324 function getSoneId(element) {
325 return getSoneElement(element).find(".id").text();
329 * Returns the element of the post with the given ID.
333 * @returns The element of the post
335 function getPost(postId) {
336 return sone.find(".post#post-" + postId);
339 function getPostElement(element) {
340 return $(element).closest(".post");
343 function getPostId(element) {
344 return getPostElement(element).attr("id").substr(5);
347 function getPostTime(element) {
348 return getPostElement(element).find(".post-time").text();
352 * Returns the author of the post the given element belongs to.
355 * The element whose post to get the author for
356 * @returns The ID of the authoring Sone
358 function getPostAuthor(element) {
359 return getPostElement(element).find(".post-author").text();
363 * Returns the element of the reply with the given ID.
366 * The ID of the reply
367 * @returns The element of the reply
369 function getReply(replyId) {
370 return sone.find(".reply#reply-" + replyId);
373 function getReplyElement(element) {
374 return $(element).closest(".reply");
377 function getReplyId(element) {
378 return getReplyElement(element).attr("id").substr(6);
381 function getReplyTime(element) {
382 return getReplyElement(element).find(".reply-time").text();
386 * Returns the author of the reply the given element belongs to.
389 * The element whose reply to get the author for
390 * @returns The ID of the authoring Sone
392 function getReplyAuthor(element) {
393 return getReplyElement(element).find(".reply-author").text();
397 * Returns the notification with the given ID.
399 * @param notificationId
400 * The ID of the notification
401 * @returns The notification element
403 function getNotification(notificationId) {
404 return sone.find("#notification-area .notification#" + notificationId);
408 * Returns the notification element closest to the given element.
411 * The element to get the closest notification of
412 * @return The closest notification element
414 function getNotificationElement(element) {
415 return $(element).closest(".notification");
419 * Returns the ID of the notification element.
421 * @param notificationElement
422 * The notification element
423 * @returns The ID of the notification
425 function getNotificationId(notificationElement) {
426 return $(notificationElement).attr("id");
430 * Returns the time the notification was last updated.
432 * @param notificationElement
433 * The notification element
434 * @returns The last update time of the notification
436 function getNotificationLastUpdatedTime(notificationElement) {
437 return $(notificationElement).attr("lastUpdatedTime");
440 function likePost(postId) {
441 ajaxGet("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
442 if ((data == null) || !data.success) {
445 sone.find(".post#post-" + postId + " > .inner-part > .status-line .like").addClass("hidden");
446 sone.find(".post#post-" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
447 updatePostLikes(postId);
448 }, function(xmlHttpRequest, textStatus, error) {
453 function unlikePost(postId) {
454 ajaxGet("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
455 if ((data == null) || !data.success) {
458 sone.find(".post#post-" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
459 sone.find(".post#post-" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
460 updatePostLikes(postId);
461 }, function(xmlHttpRequest, textStatus, error) {
466 function updatePostLikes(postId) {
467 ajaxGet("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
468 if ((data != null) && data.success) {
469 sone.find(".post#post-" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0);
470 sone.find(".post#post-" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
471 sone.find(".post#post-" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
473 }, function(xmlHttpRequest, textStatus, error) {
478 function likeReply(replyId) {
479 ajaxGet("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
480 if ((data == null) || !data.success) {
483 sone.find(".reply#reply-" + replyId + " .status-line .like").addClass("hidden");
484 sone.find(".reply#reply-" + replyId + " .status-line .unlike").removeClass("hidden");
485 updateReplyLikes(replyId);
486 }, function(xmlHttpRequest, textStatus, error) {
491 function unlikeReply(replyId) {
492 ajaxGet("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
493 if ((data == null) || !data.success) {
496 sone.find(".reply#reply-" + replyId + " .status-line .unlike").addClass("hidden");
497 sone.find(".reply#reply-" + replyId + " .status-line .like").removeClass("hidden");
498 updateReplyLikes(replyId);
499 }, function(xmlHttpRequest, textStatus, error) {
505 * Trusts the Sone with the given ID.
508 * The ID of the Sone to trust
510 function trustSone(soneId) {
511 ajaxGet("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
512 if ((data != null) && data.success) {
513 updateTrustControls(soneId, data.trustValue);
519 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
522 * The ID of the Sone to distrust
524 function distrustSone(soneId) {
525 ajaxGet("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
526 if ((data != null) && data.success) {
527 updateTrustControls(soneId, data.trustValue);
533 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
536 * The ID of the Sone to untrust
538 function untrustSone(soneId) {
539 ajaxGet("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
540 if ((data != null) && data.success) {
541 updateTrustControls(soneId, data.trustValue);
547 * Updates the trust controls for all posts and replies of the given Sone,
548 * according to the given trust value.
551 * The ID of the Sone to update all trust controls for
553 * The trust value for the Sone
555 function updateTrustControls(soneId, trustValue) {
556 sone.find(".post").each(function() {
557 if (getPostAuthor(this) == soneId) {
558 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
559 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
560 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
563 sone.find(".reply").each(function() {
564 if (getReplyAuthor(this) == soneId) {
565 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
566 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
567 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
573 * Bookmarks the post with the given ID.
576 * The ID of the post to bookmark
578 function bookmarkPost(postId) {
580 ajaxGet("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
581 if ((data != null) && data.success) {
582 getPost(postId).find(".bookmark").toggleClass("hidden", true);
583 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
590 * Unbookmarks the post with the given ID.
593 * The ID of the post to unbookmark
595 function unbookmarkPost(postId) {
596 ajaxGet("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
597 if ((data != null) && data.success) {
598 getPost(postId).find(".bookmark").toggleClass("hidden", false);
599 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
604 function updateReplyLikes(replyId) {
605 ajaxGet("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
606 if ((data != null) && data.success) {
607 sone.find(".reply#reply-" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0);
608 sone.find(".reply#reply-" + replyId + " .status-line .likes span.like-count").text(data.likes);
609 sone.find(".reply#reply-" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
611 }, function(xmlHttpRequest, textStatus, error) {
617 * Posts a reply and calls the given callback when the request finishes.
620 * The ID of the sender
622 * The ID of the post the reply refers to
625 * @param callbackFunction
626 * The callback function to call when the request finishes (takes 3
627 * parameters: success, error, replyId)
629 function postReply(sender, postId, text, callbackFunction) {
630 ajaxGet("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
632 /* TODO - show error */
636 callbackFunction(true, null, data.reply, data.sone);
638 callbackFunction(false, data.error);
640 }, function(xmlHttpRequest, textStatus, error) {
646 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
649 * The Sone to ajaxify
651 function ajaxifySone(soneElement) {
653 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
656 $(".follow", soneElement).submit(function() {
657 var followElement = this;
658 ajaxGet("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
659 $(followElement).addClass("hidden");
660 $(followElement).parent().find(".unfollow").removeClass("hidden");
664 $(".unfollow", soneElement).submit(function() {
665 var unfollowElement = this;
666 ajaxGet("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
667 $(unfollowElement).addClass("hidden");
668 $(unfollowElement).parent().find(".follow").removeClass("hidden");
672 $(".lock", soneElement).submit(function() {
673 var lockElement = this;
674 ajaxGet("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
675 $(lockElement).addClass("hidden");
676 $(lockElement).parent().find(".unlock").removeClass("hidden");
680 $(".unlock", soneElement).submit(function() {
681 var unlockElement = this;
682 ajaxGet("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
683 $(unlockElement).addClass("hidden");
684 $(unlockElement).parent().find(".lock").removeClass("hidden");
689 /* mark Sone as known when clicking it. */
690 $(soneElement).click(function() {
691 markSoneAsKnown(this);
696 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
699 * The post element to ajaxify
701 function ajaxifyPost(postElement) {
702 $(postElement).find("form").submit(function() {
705 $(postElement).find(".create-reply button:submit").click(function() {
706 var button = $(this);
707 button.attr("disabled", "disabled");
708 var sender = $(this.form).find(":input[name=sender]").val();
709 var inputField = $(this.form).find(":input[name=text]:enabled").get(0);
710 var postId = getPostId(this);
711 var text = $(inputField).val();
712 (function(sender, postId, text, inputField) {
713 postReply(sender, postId, text, function(success, error, replyId, soneId) {
715 $(inputField).val("");
716 loadNewReply(replyId, soneId, postId);
717 sone.find(".post#post-" + postId + " .create-reply").addClass("hidden");
718 sone.find(".post#post-" + postId + " .create-reply .sender").hide();
719 sone.find(".post#post-" + postId + " .create-reply .select-sender").show();
720 sone.find(".post#post-" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
724 button.removeAttr("disabled");
726 })(sender, postId, text, inputField);
730 /* replace all “delete” buttons with javascript. */
731 (function(postElement) {
732 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
733 var postId = getPostId(postElement);
734 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
738 /* convert all “like” buttons to javascript functions. */
739 $(postElement).find(".like-post").submit(function() {
740 likePost(getPostId(this));
743 $(postElement).find(".unlike-post").submit(function() {
744 unlikePost(getPostId(this));
748 /* convert trust control buttons to javascript functions. */
749 $(postElement).find(".post-trust").submit(function() {
750 trustSone(getPostAuthor(this));
753 $(postElement).find(".post-distrust").submit(function() {
754 distrustSone(getPostAuthor(this));
757 $(postElement).find(".post-untrust").submit(function() {
758 untrustSone(getPostAuthor(this));
762 /* convert bookmark/unbookmark buttons to javascript functions. */
763 $(postElement).find(".bookmark").submit(function() {
764 bookmarkPost(getPostId(this));
767 $(postElement).find(".unbookmark").submit(function() {
768 unbookmarkPost(getPostId(this));
772 /* convert “show source” link into javascript function. */
773 $(postElement).find(".show-source").each(function() {
774 $("a", this).click(function() {
775 var post = getPostElement(this);
776 var rawPostText = $(".post-text.raw-text", post);
777 rawPostText.toggleClass("hidden");
778 if (rawPostText.hasClass("hidden")) {
779 $(".post-text.short-text", post).removeClass("hidden");
780 $(".post-text.text", post).addClass("hidden");
781 $(".expand-post-text", post).removeClass("hidden");
782 $(".shrink-post-text", post).addClass("hidden");
784 $(".post-text.short-text", post).addClass("hidden");
785 $(".post-text.text", post).addClass("hidden");
786 $(".expand-post-text", post).addClass("hidden");
787 $(".shrink-post-text", post).addClass("hidden");
793 /* convert “show more” link into javascript function. */
794 $(postElement).find(".expand-post-text").each(function() {
795 $(this).click(function() {
796 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
797 $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
798 $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
799 $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
803 $(postElement).find(".shrink-post-text").each(function() {
804 $(this).click(function() {
805 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
806 $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
807 $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
808 $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
813 /* ajaxify author/post links */
814 $(".post-status-line .permalink a", postElement).click(function() {
815 if (!$(".create-reply", postElement).hasClass("hidden")) {
816 var textArea = $(":input.reply-input", postElement).focus().data("textarea");
817 $(textArea).replaceSelection($(this).attr("href"));
822 /* add “comment” link. */
823 addCommentLink(getPostId(postElement), getPostAuthor(postElement), postElement, $(postElement).find(".post-status-line .permalink-author"));
825 /* process all replies. */
827 $(postElement).find(".reply").each(function() {
828 replyIds.push(getReplyId(this));
831 updateReplyTimes(replyIds.join(","));
833 /* process reply input fields. */
834 getTranslation("WebInterface.DefaultText.Reply", function(text) {
835 $(postElement).find(":input.reply-input").each(function() {
836 registerInputTextareaSwap(this, text, "text", false, false);
840 /* process sender selection. */
841 $(".select-sender", postElement).css("display", "inline");
842 $(".sender", postElement).hide();
843 $(".select-sender button", postElement).click(function() {
844 $(".sender", postElement).show();
845 $(".select-sender", postElement).hide();
849 /* mark everything as known on click. */
850 (function(postElement) {
851 $(postElement).click(function(event) {
852 if ($(event.target).hasClass("click-to-show")) {
855 markPostAsKnown(postElement, false);
859 /* hide reply input field. */
860 $(postElement).find(".create-reply").addClass("hidden");
862 /* show Sone menu when hovering over the avatar. */
863 $(postElement).find(".post-avatar").mouseover(function() {
864 if (typeof currentSoneMenuTimeoutHandler != undefined) {
865 clearTimeout(currentSoneMenuTimeoutHandler);
867 currentSoneMenuId = getPostId(this);
868 currentSoneMenuTimeoutHandler = setTimeout(function() {
869 $(".sone-menu:visible").fadeOut();
870 $(".sone-post-menu", postElement).mouseleave(function() {
874 }).mouseleave(function() {
875 if (currentSoneMenuId == getPostId(this)) {
876 clearTimeout(currentSoneMenuTimeoutHandler);
879 (function(postElement) {
880 var soneId = $(".sone-menu-id:first", postElement).text();
881 $(".sone-post-menu .follow", postElement).click(function() {
882 var followElement = this;
883 ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
884 $(followElement).addClass("hidden");
885 $(followElement).parent().find(".unfollow").removeClass("hidden");
886 sone.find(".sone-menu").each(function() {
887 if (getMenuSone(this) == soneId) {
888 $(".follow", this).toggleClass("hidden", true);
889 $(".unfollow", this).toggleClass("hidden", false);
895 $(".sone-post-menu .unfollow", postElement).click(function() {
896 var unfollowElement = this;
897 ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
898 $(unfollowElement).addClass("hidden");
899 $(unfollowElement).parent().find(".follow").removeClass("hidden");
900 sone.find(".sone-menu").each(function() {
901 if (getMenuSone(this) == soneId) {
902 $(".follow", this).toggleClass("hidden", false);
903 $(".unfollow", this).toggleClass("hidden", true);
913 * Ajaxifies the given reply element.
915 * @param replyElement
916 * The reply element to ajaxify
918 function ajaxifyReply(replyElement) {
919 $(replyElement).find(".like-reply").submit(function() {
920 likeReply(getReplyId(this));
923 $(replyElement).find(".unlike-reply").submit(function() {
924 unlikeReply(getReplyId(this));
927 (function(replyElement) {
928 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
929 $(replyElement).find(".delete-reply button").each(function() {
930 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
935 /* ajaxify author links */
936 $(".reply-status-line .permalink a", replyElement).click(function() {
937 if (!$(".create-reply", getPostElement(replyElement)).hasClass("hidden")) {
938 var textArea = $(":input.reply-input", getPostElement(replyElement)).focus().data("textarea");
939 $(textArea).replaceSelection($(this).attr("href"));
944 addCommentLink(getPostId(replyElement), getReplyAuthor(replyElement), replyElement, $(replyElement).find(".reply-status-line .permalink-author"));
946 /* convert “show source” link into javascript function. */
947 $(replyElement).find(".show-reply-source").each(function() {
948 $("a", this).click(function() {
949 var reply = getReplyElement(this);
950 var rawReplyText = $(".reply-text.raw-text", reply);
951 rawReplyText.toggleClass("hidden");
952 if (rawReplyText.hasClass("hidden")) {
953 $(".reply-text.short-text", reply).removeClass("hidden");
954 $(".reply-text.text", reply).addClass("hidden");
955 $(".expand-reply-text", reply).removeClass("hidden");
956 $(".shrink-reply-text", reply).addClass("hidden");
958 $(".reply-text.short-text", reply).addClass("hidden");
959 $(".reply-text.text", reply).addClass("hidden");
960 $(".expand-reply-text", reply).addClass("hidden");
961 $(".shrink-reply-text", reply).addClass("hidden");
967 /* convert “show more” link into javascript function. */
968 $(replyElement).find(".expand-reply-text").each(function() {
969 $(this).click(function() {
970 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
971 $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
972 $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
973 $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
977 $(replyElement).find(".shrink-reply-text").each(function() {
978 $(this).click(function() {
979 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
980 $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
981 $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
982 $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
987 /* convert trust control buttons to javascript functions. */
988 $(replyElement).find(".reply-trust").submit(function() {
989 trustSone(getReplyAuthor(this));
992 $(replyElement).find(".reply-distrust").submit(function() {
993 distrustSone(getReplyAuthor(this));
996 $(replyElement).find(".reply-untrust").submit(function() {
997 untrustSone(getReplyAuthor(this));
1001 /* show Sone menu when hovering over the avatar. */
1002 $(replyElement).find(".reply-avatar").mouseover(function() {
1003 if (typeof currentSoneMenuTimeoutHandler != undefined) {
1004 clearTimeout(currentSoneMenuTimeoutHandler);
1006 currentSoneMenuId = getPostId(this) + "-" + getReplyId(this);
1007 currentSoneMenuTimeoutHandler = setTimeout(function() {
1008 $(".sone-menu:visible").fadeOut();
1009 $(".sone-reply-menu", replyElement).mouseleave(function() {
1013 }).mouseleave(function() {
1014 if (currentSoneMenuId == getPostId(this) + "-" + getReplyId(this)) {
1015 clearTimeout(currentSoneMenuTimeoutHandler);
1018 (function(replyElement) {
1019 var soneId = $(".sone-menu-id", replyElement).text();
1020 $(".sone-menu .follow", replyElement).click(function() {
1021 var followElement = this;
1022 ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1023 $(followElement).addClass("hidden");
1024 $(followElement).parent().find(".unfollow").removeClass("hidden");
1025 sone.find(".sone-menu").each(function() {
1026 if (getMenuSone(this) == soneId) {
1027 $(".follow", this).toggleClass("hidden", true);
1028 $(".unfollow", this).toggleClass("hidden", false);
1034 $(".sone-menu .unfollow", replyElement).click(function() {
1035 var unfollowElement = this;
1036 ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1037 $(unfollowElement).addClass("hidden");
1038 $(unfollowElement).parent().find(".follow").removeClass("hidden");
1039 sone.find(".sone-menu").each(function() {
1040 if (getMenuSone(this) == soneId) {
1041 $(".follow", this).toggleClass("hidden", false);
1042 $(".unfollow", this).toggleClass("hidden", true);
1052 * Ajaxifies the given notification by replacing the form with AJAX.
1054 * @param notification
1055 * jQuery object representing the notification.
1057 function ajaxifyNotification(notification) {
1058 notification.find("form").submit(function() {
1061 notification.find("input[name=returnPage]").val($.url.attr("relative"));
1062 if (notification.find(".short-text").length > 0) {
1063 notification.find(".short-text").removeClass("hidden");
1064 notification.find(".text").addClass("hidden");
1066 notification.find("form.mark-as-read button").click(function() {
1067 var allIds = $(":input[name=id]", this.form).val().split(" ");
1068 for (var index = 0; index < allIds.length; index += 16) {
1069 var ids = allIds.slice(index, index + 16).join(" ");
1070 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": ids});
1073 notification.find("a[class^='link-']").each(function() {
1074 var linkElement = $(this);
1075 if (linkElement.is("[href^='viewPost']")) {
1076 var id = linkElement.attr("class").substr(5);
1078 linkElement.attr("href", "#post-" + id).addClass("in-page-link");
1082 notification.find("form.dismiss button").click(function() {
1083 ajaxGet("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
1084 /* dismiss in case of error, too. */
1085 notification.slideUp();
1086 }, function(xmlHttpRequest, textStatus, error) {
1090 return notification;
1094 * Returns the notification hash. This hash is used in {@link #getStatus()} to
1095 * determine whether the notifications changed and need to be reloaded.
1097 function getNotificationHash() {
1098 return sone.find("#notification-area #notification-hash").text();
1102 * Sets the notification hash.
1104 * @param notificationHash
1105 * The new notification hash
1107 function setNotificationHash(notificationHash) {
1108 sone.find("#notification-area #notification-hash").text(notificationHash);
1112 * Retrieves element IDs from notification elements.
1114 * @param notification
1115 * The notification element
1117 * The selector of the element containing the ID as text
1118 * @returns All extracted IDs
1120 function getElementIds(notification, selector) {
1121 var elementIds = [];
1122 $(selector, notification).each(function() {
1123 elementIds.push($(this).text());
1129 * Compares the given notification elements and calls {@link #markSoneAsKnown()}
1130 * for every ID that is contained in the old notification but not in the new.
1132 * @param oldNotification
1133 * The old notification element
1134 * @param newNotification
1135 * The new notification element
1137 function checkForRemovedSones(oldNotification, newNotification) {
1138 if (getNotificationId(oldNotification) != "new-sone-notification") {
1141 var oldIds = getElementIds(oldNotification, ".new-sone-id");
1142 var newIds = getElementIds(newNotification, ".new-sone-id");
1143 $.each(oldIds, function(index, value) {
1144 if ($.inArray(value, newIds) == -1) {
1145 markSoneAsKnown(getSone(value), true);
1151 * Compares the given notification elements and calls {@link #markPostAsKnown()}
1152 * for every ID that is contained in the old notification but not in the new.
1154 * @param oldNotification
1155 * The old notification element
1156 * @param newNotification
1157 * The new notification element
1159 function checkForRemovedPosts(oldNotification, newNotification) {
1160 if (getNotificationId(oldNotification) != "new-post-notification") {
1163 var oldIds = getElementIds(oldNotification, ".post-id");
1164 var newIds = getElementIds(newNotification, ".post-id");
1165 $.each(oldIds, function(index, value) {
1166 if ($.inArray(value, newIds) == -1) {
1167 markPostAsKnown(getPost(value), true);
1173 * Compares the given notification elements and calls
1174 * {@link #markReplyAsKnown()} for every ID that is contained in the old
1175 * notification but not in the new.
1177 * @param oldNotification
1178 * The old notification element
1179 * @param newNotification
1180 * The new notification element
1182 function checkForRemovedReplies(oldNotification, newNotification) {
1183 if (getNotificationId(oldNotification) != "new-reply-notification") {
1186 var oldIds = getElementIds(oldNotification, ".reply-id");
1187 var newIds = getElementIds(newNotification, ".reply-id");
1188 $.each(oldIds, function(index, value) {
1189 if ($.inArray(value, newIds) == -1) {
1190 markReplyAsKnown(getReply(value), true);
1195 function getStatus() {
1196 ajaxGet("getStatus.ajax", isViewSonePage() ? {"soneIds": getShownSoneId() } : isKnownSonesPage() ? {"soneIds": getShownSoneIds() } : {}, function(data, textStatus) {
1197 if ((data != null) && data.success) {
1198 /* process Sone information. */
1199 $.each(data.sones, function(index, value) {
1200 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated, value.lastUpdatedText);
1202 notLoggedIn = !data.loggedIn;
1204 showOfflineMarker(!online);
1206 if (data.notificationHash != getNotificationHash()) {
1207 console.log("Old hash: ", getNotificationHash(), ", new hash: ", data.notificationHash);
1208 requestNotifications();
1209 /* process new posts. */
1210 $.each(data.newPosts, function(index, value) {
1211 loadNewPost(value.id, value.sone, value.recipient, value.time);
1213 /* process new replies. */
1214 $.each(data.newReplies, function(index, value) {
1215 loadNewReply(value.id, value.sone, value.post, value.postSone);
1218 /* do it again in 5 seconds. */
1219 setTimeout(getStatus, 5000);
1221 /* data.success was false, wait 30 seconds. */
1222 setTimeout(getStatus, 30000);
1225 statusRequestQueued = false;
1230 function requestNotifications() {
1231 ajaxGet("getNotifications.ajax", {}, function(data, textStatus) {
1232 if (data && data.success) {
1233 /* search for removed notifications. */
1234 sone.find("#notification-area .notification").each(function() {
1235 var notificationId = $(this).attr("id");
1236 var foundNotification = false;
1237 $.each(data.notifications, function(index, value) {
1238 if (value.id == notificationId) {
1239 foundNotification = true;
1243 if (!foundNotification) {
1244 if (notificationId == "new-sone-notification" && (data.options["ShowNotification/NewSones"] == true)) {
1245 $(".new-sone-id", this).each(function(index, element) {
1246 var soneId = $(this).text();
1247 markSoneAsKnown(getSone(soneId), true);
1249 } else if (notificationId == "new-post-notification" && (data.options["ShowNotification/NewPosts"] == true)) {
1250 $(".post-id", this).each(function(index, element) {
1251 var postId = $(this).text();
1252 markPostAsKnown(getPost(postId), true);
1254 } else if (notificationId == "new-reply-notification" && (data.options["ShowNotification/NewReplies"] == true)) {
1255 $(".reply-id", this).each(function(index, element) {
1256 var replyId = $(this).text();
1257 markReplyAsKnown(getReply(replyId), true);
1260 $(this).slideUp("normal", function() {
1262 /* remove activity when no notifications are visible. */
1263 if (sone.find("#notification-area .notification").length == 0) {
1269 /* process notifications. */
1270 $.each(data.notifications, function(index, value) {
1271 var oldNotification = getNotification(value.id);
1272 var notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide();
1273 if (oldNotification.length != 0) {
1274 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
1275 var opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
1276 notification.find(".short-text").toggleClass("hidden", opened);
1277 notification.find(".text").toggleClass("hidden", !opened);
1279 checkForRemovedSones(oldNotification, notification);
1280 checkForRemovedPosts(oldNotification, notification);
1281 checkForRemovedReplies(oldNotification, notification);
1282 oldNotification.replaceWith(notification.show());
1284 sone.find("#notification-area").append(notification);
1285 if (value.id.substring(0, 5) != "local") {
1286 notification.slideDown();
1291 setNotificationHash(data.notificationHash);
1297 * Returns the ID of the currently logged in Sone.
1299 * @return The ID of the current Sone, or an empty string if no Sone is logged
1302 function getCurrentSoneId() {
1303 return $("#currentSoneId").text();
1307 * Returns the content of the page-id attribute.
1309 * @returns The page ID
1311 function getPageId() {
1312 return sone.find(".page-id").text();
1316 * Returns whether the current page is the index page.
1318 * @returns {Boolean} <code>true</code> if the current page is the index page,
1319 * <code>false</code> otherwise
1321 function isIndexPage() {
1322 return getPageId() == "index";
1326 * Returns the current page of the selected pagination. If no pagination can be
1327 * found with the given selector, {@code 1} is returned.
1329 * @param paginationSelector
1330 * The pagination selector
1331 * @returns The current page of the pagination
1333 function getPage(paginationSelector) {
1334 var pagination = $(paginationSelector);
1335 if (pagination.length > 0) {
1336 return $(".current-page", paginationSelector).text();
1342 * Returns whether the current page is a “view Sone” page.
1344 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
1345 * page, <code>false</code> otherwise
1347 function isViewSonePage() {
1348 return getPageId() == "view-sone";
1352 * Returns the ID of the currently shown Sone. This will only return a sensible
1353 * value if isViewSonePage() returns <code>true</code>.
1355 * @returns The ID of the currently shown Sone
1357 function getShownSoneId() {
1358 return sone.find(".sone-id").first().text();
1362 * Returns the ID of all currently visible Sones. This is mainly used on the
1363 * “Known Sones” page.
1365 * @returns The ID of the currently shown Sones
1367 function getShownSoneIds() {
1369 sone.find("#known-sones .sone .id").each(function() {
1370 soneIds.push($(this).text());
1372 return soneIds.join(",");
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.find(".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.find(".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.find(".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.find(".post").each(function() {
1453 if (getPostTime(this) < data.post.time) {
1454 firstOlderPost = $(this);
1458 var 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.find(".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 var 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).hasClass("new")) {
1533 $(soneElement).removeClass("new");
1534 if ((typeof skipRequest == "undefined") || !skipRequest) {
1535 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)});
1536 requestNotifications();
1541 function markPostAsKnown(postElements, skipRequest) {
1542 $(postElements).each(function() {
1543 var postElement = this;
1544 if ($(postElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1545 (function(postElement) {
1546 $(postElement).removeClass("new");
1547 if ((typeof skipRequest == "undefined") || !skipRequest) {
1548 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1549 requestNotifications();
1553 $(".click-to-show", postElement).removeClass("new");
1555 markReplyAsKnown($(postElements).find(".reply"), true);
1558 function markReplyAsKnown(replyElements, skipRequest) {
1559 $(replyElements).each(function() {
1560 var replyElement = this;
1561 if ($(replyElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1562 (function(replyElement) {
1563 $(replyElement).removeClass("new");
1564 if ((typeof skipRequest == "undefined") || !skipRequest) {
1565 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1566 requestNotifications();
1574 * Updates the time of the post with the given ID.
1577 * The ID of the post to update
1579 * The text of the time to show
1580 * @param refreshTime
1581 * The refresh time after which to request a new time (in seconds)
1583 * The tooltip to show
1585 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1586 if (!getPost(postId).is(":visible")) {
1589 getPost(postId).find(".post-status-line > .time a").html(timeText).attr("title", tooltip);
1590 (function(postId, refreshTime) {
1591 setTimeout(function() {
1592 updatePostTimes(postId);
1593 }, refreshTime * 1000);
1594 })(postId, refreshTime);
1598 * Requests new rendered times for the posts with the given IDs.
1601 * Comma-separated post IDs
1603 function updatePostTimes(postIds) {
1604 ajaxGet("getTimes.ajax", { "posts" : postIds }, function(data, textStatus) {
1605 if ((data != null) && data.success) {
1606 $.each(data.postTimes, function(index, value) {
1607 updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1614 * Updates the time of the reply with the given ID.
1617 * The ID of the reply to update
1619 * The text of the time to show
1620 * @param refreshTime
1621 * The refresh time after which to request a new time (in seconds)
1623 * The tooltip to show
1625 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1626 getReply(replyId).find(".reply-status-line > .time").html(timeText).attr("title", tooltip);
1627 (function(replyId, refreshTime) {
1628 setTimeout(function() {
1629 updateReplyTimes(replyId);
1630 }, refreshTime * 1000);
1631 })(replyId, refreshTime);
1635 * Requests new rendered times for the posts with the given IDs.
1638 * Comma-separated post IDs
1640 function updateReplyTimes(replyIds) {
1641 ajaxGet("getTimes.ajax", { "replies" : replyIds }, function(data, textStatus) {
1642 if ((data != null) && data.success) {
1643 $.each(data.replyTimes, function(index, value) {
1644 updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1650 function resetActivity() {
1651 var title = document.title;
1652 if (title.indexOf('(') == 0) {
1653 setTitle(title.substr(title.indexOf(' ') + 1));
1655 iconBlinking = false;
1658 function setActivity() {
1660 var title = document.title;
1661 if (title.indexOf('(') != 0) {
1662 setTitle("(!) " + title);
1664 if (!iconBlinking) {
1665 setTimeout(toggleIcon, 1500);
1666 iconBlinking = true;
1672 * Sets the window title after a small delay to prevent race-condition issues.
1677 function setTitle(title) {
1678 setTimeout(function() {
1679 document.title = title;
1683 /** Whether the icon is currently showing activity. */
1684 var iconActive = false;
1686 /** Whether the icon is currently supposed to blink. */
1687 var iconBlinking = false;
1690 * Toggles the icon. If the window has gained focus and the icon is still
1691 * showing the activity state, it is returned to normal.
1693 function toggleIcon() {
1694 if (focus || !iconBlinking) {
1696 changeIcon("images/icon.png");
1699 iconBlinking = false;
1701 iconActive = !iconActive;
1702 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1703 setTimeout(toggleIcon, 1500);
1708 * Changes the icon of the page.
1711 * The new URL of the icon
1713 function changeIcon(iconUrl) {
1714 $("link[rel=icon]").remove();
1715 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1716 $("iframe[id=icon-update]")[0].src += "";
1720 * Creates a new notification.
1723 * The ID of the notificaiton
1725 * The text of the notification
1726 * @param dismissable
1727 * <code>true</code> if the notification can be dismissed by the
1730 function createNotification(id, lastUpdatedTime, text, dismissable) {
1731 var notification = $("<div></div>").addClass("notification").attr("id", id).attr("lastUpdatedTime", lastUpdatedTime);
1733 var dismissForm = sone.find("#notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id");
1734 dismissForm.find("input[name=notification]").val(id);
1735 notification.append(dismissForm);
1737 notification.append(text);
1738 return notification;
1742 * Shows the details of the notification with the given ID.
1744 * @param notificationId
1745 * The ID of the notification
1747 function showNotificationDetails(notificationId) {
1748 sone.find(".notification#" + notificationId + " .text").removeClass("hidden");
1749 sone.find(".notification#" + notificationId + " .short-text").addClass("hidden");
1753 * Deletes the field with the given ID from the profile.
1756 * The ID of the field to delete
1758 function deleteProfileField(fieldId) {
1759 ajaxGet("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1760 if (data && data.success) {
1761 sone.find(".profile-field#" + data.field.id).slideUp();
1767 * Renames a profile field.
1770 * The ID of the field to rename
1772 * The new name of the field
1773 * @param successFunction
1774 * Called when the renaming was successful
1776 function editProfileField(fieldId, newName, successFunction) {
1777 ajaxGet("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1778 if (data && data.success) {
1785 * Moves the profile field with the given ID one slot in the given direction.
1788 * The ID of the field to move
1790 * The direction to move in (“up” or “down”)
1791 * @param successFunction
1792 * Function to call on success
1794 function moveProfileField(fieldId, direction, successFunction) {
1795 ajaxGet("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1796 if (data && data.success) {
1803 * Moves the profile field with the given ID up one slot.
1806 * The ID of the field to move
1807 * @param successFunction
1808 * Function to call on success
1810 function moveProfileFieldUp(fieldId, successFunction) {
1811 moveProfileField(fieldId, "up", successFunction);
1815 * Moves the profile field with the given ID down one slot.
1818 * The ID of the field to move
1819 * @param successFunction
1820 * Function to call on success
1822 function moveProfileFieldDown(fieldId, successFunction) {
1823 moveProfileField(fieldId, "down", successFunction);
1826 var statusRequestQueued = true;
1829 * Sets the status of the web interface as offline.
1831 function ajaxError() {
1833 showOfflineMarker(true);
1834 if (!statusRequestQueued) {
1835 setTimeout(getStatus, 5000);
1836 statusRequestQueued = true;
1841 * Sets the status of the web interface as online.
1843 function ajaxSuccess() {
1845 showOfflineMarker(!online || (initiallyLoggedIn && notLoggedIn));
1849 * Shows or hides the offline marker.
1852 * {@code true} to display the offline marker, {@code false} to hide
1855 function showOfflineMarker(visible) {
1856 /* jQuery documentation says toggle() works the other way around?! */
1857 sone.find("#offline-marker").toggle(visible);
1859 sone.find("#main").addClass("offline");
1861 sone.find("#main").removeClass("offline");
1866 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1869 var sone = $("#sone");
1872 var initiallyLoggedIn = sone.find("#loggedIn").text() == "true";
1873 var notLoggedIn = !initiallyLoggedIn;
1875 /** ID of the next-to-show Sone context menu. */
1876 var currentSoneMenuId;
1878 /** Timeout handler for the next-to-show Sone context menu. */
1879 var currentSoneMenuTimeoutHandler;
1881 $(document).ready(function() {
1883 /* rip out the status update textarea. */
1884 sone.find(".rip-out").each(function() {
1885 var oldElement = $(this);
1886 var newElement = $("<input type='text'/>");
1887 newElement.attr("class", oldElement.attr("class")).attr("name", oldElement.attr("name"));
1888 oldElement.before(newElement).remove();
1891 /* this initializes the status update input field. */
1892 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1893 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1894 sone.find("#update-status .select-sender").css("display", "inline");
1895 sone.find("#update-status .sender").hide();
1896 sone.find("#update-status .select-sender button").click(function() {
1897 sone.find("#update-status .sender").show();
1898 sone.find("#update-status .select-sender").hide();
1901 sone.find("#update-status").submit(function() {
1902 var button = $("button:submit", this);
1903 button.attr("disabled", "disabled");
1904 if ($(this).find(":input.default:enabled").length > 0) {
1907 var sender = $(this).find(":input[name=sender]").val();
1908 var text = $(this).find(":input[name=text]:enabled").val();
1909 ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1910 button.removeAttr("disabled");
1912 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1913 $(this).find(":input[name=text]:enabled").val("").blur();
1914 $(this).find(".sender").hide();
1915 $(this).find(".select-sender").show();
1920 /* ajaxify the search input field. */
1921 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1922 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1925 /* ajaxify input field on “view Sone” page. */
1926 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1927 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1928 sone.find("#post-message .select-sender").css("display", "inline");
1929 sone.find("#post-message .sender").hide();
1930 sone.find("#post-message .select-sender button").click(function() {
1931 sone.find("#post-message .sender").show();
1932 sone.find("#post-message .select-sender").hide();
1935 sone.find("#post-message").submit(function() {
1936 var sender = $(this).find(":input[name=sender]").val();
1937 var text = $(this).find(":input[name=text]:enabled").val();
1938 ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text });
1939 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1940 $(this).find(":input[name=text]:enabled").val("").blur();
1941 $(this).find(".sender").hide();
1942 $(this).find(".select-sender").show();
1947 /* Ajaxifies all posts. */
1948 /* calling getTranslation here will cache the necessary values. */
1949 getTranslation("WebInterface.Confirmation.DeletePostButton", function() {
1950 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function() {
1951 getTranslation("WebInterface.DefaultText.Reply", function() {
1952 getTranslation("WebInterface.Button.Comment", function () {
1953 sone.find(".post").each(function() {
1961 /* update post times. */
1963 sone.find(".post").each(function() {
1964 postIds.push(getPostId(this));
1966 updatePostTimes(postIds.join(","));
1968 /* hides all replies but the latest two. */
1969 if (!isViewPostPage()) {
1970 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1971 sone.find(".post .replies").each(function() {
1972 var allReplies = $(this).find(".reply");
1973 if (allReplies.length > 2) {
1974 var newHidden = false;
1975 for (var replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1976 $(allReplies[replyIndex]).addClass("hidden");
1977 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1979 var clickToShowElement = $("<div></div>").addClass("click-to-show");
1981 clickToShowElement.addClass("new");
1983 (function(clickToShowElement, allReplies, text) {
1984 clickToShowElement.text(text);
1985 clickToShowElement.click(function() {
1986 allReplies.removeClass("hidden");
1987 clickToShowElement.addClass("hidden");
1989 })(clickToShowElement, allReplies, text);
1990 $(allReplies[0]).before(clickToShowElement);
1996 sone.find(".sone").each(function() {
1997 ajaxifySone($(this));
2000 /* process all existing notifications, ajaxify dismiss buttons. */
2001 sone.find("#notification-area .notification").each(function() {
2002 ajaxifyNotification($(this));
2005 /* activate status polling. */
2006 setTimeout(getStatus, 5000);
2008 /* reset activity counter when the page has focus. */
2009 $(window).focus(function() {
2012 }).blur(function() {