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) {
7 if (typeof successCallback != "undefined") {
8 successCallback(data, textStatus);
10 }, "error": function(xmlHttpRequest) {
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().prop("disabled", "disabled");
37 /* no, show(), “display: block” is not what I need. */
38 textarea.prop("style", "display: inline").focus();
40 if (inputField.val() === "") {
41 inputField.addClass("default");
42 inputField.val(defaultText);
44 inputField.hide().prop("disabled", "disabled");
47 $(inputField.get(0).form).submit(function() {
48 inputField.prop("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) {
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").prop("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) {
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.");
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) {
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.");
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() {
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
303 * The sones to format
304 * @returns {String} The created string
306 function generateSoneList(sones) {
307 return sones.reduce((soneList, sone) => soneList + ", " + sone.name, "").substring(2)
311 * Returns the ID of the Sone that this element belongs to.
314 * The element to locate the matching Sone ID for
315 * @returns The ID of the Sone, or undefined
317 function getSoneId(element) {
318 return getSoneElement(element).find(".id").text();
322 * Returns the element of the post with the given ID.
326 * @returns The element of the post
328 function getPost(postId) {
329 return sone.find(".post#post-" + postId);
332 function getPostElement(element) {
333 return $(element).closest(".post");
336 function getPostId(element) {
337 return getPostElement(element).prop("id").substr(5);
340 function getPostTime(element) {
341 return getPostElement(element).find(".post-time").text();
345 * Returns the author of the post the given element belongs to.
348 * The element whose post to get the author for
349 * @returns The ID of the authoring Sone
351 function getPostAuthor(element) {
352 return getPostElement(element).find(".post-author").text();
356 * Returns the element of the reply with the given ID.
359 * The ID of the reply
360 * @returns The element of the reply
362 function getReply(replyId) {
363 return sone.find(".reply#reply-" + replyId);
366 function getReplyElement(element) {
367 return $(element).closest(".reply");
370 function getReplyId(element) {
371 return getReplyElement(element).prop("id").substr(6);
374 function getReplyTime(element) {
375 return getReplyElement(element).find(".reply-time").text();
379 * Returns the author of the reply the given element belongs to.
382 * The element whose reply to get the author for
383 * @returns The ID of the authoring Sone
385 function getReplyAuthor(element) {
386 return getReplyElement(element).find(".reply-author").text();
390 * Returns the notification with the given ID.
392 * @param notificationId
393 * The ID of the notification
394 * @returns The notification element
396 function getNotification(notificationId) {
397 return sone.find("#notification-area .notification#" + notificationId);
401 * Returns the notification element closest to the given element.
404 * The element to get the closest notification of
405 * @return The closest notification element
407 function getNotificationElement(element) {
408 return $(element).closest(".notification");
412 * Returns the ID of the notification element.
414 * @param notificationElement
415 * The notification element
416 * @returns The ID of the notification
418 function getNotificationId(notificationElement) {
419 return $(notificationElement).prop("id");
423 * Returns the time the notification was last updated.
425 * @param notificationElement
426 * The notification element
427 * @returns The last update time of the notification
429 function getNotificationLastUpdatedTime(notificationElement) {
430 return $(notificationElement).prop("lastUpdatedTime");
433 function likePost(postId) {
434 ajaxGet("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data) {
435 if ((data == null) || !data.success) {
438 sone.find(".post#post-" + postId + " > .inner-part > .status-line .like").addClass("hidden");
439 sone.find(".post#post-" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
440 updatePostLikes(postId);
446 function unlikePost(postId) {
447 ajaxGet("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data) {
448 if ((data == null) || !data.success) {
451 sone.find(".post#post-" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
452 sone.find(".post#post-" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
453 updatePostLikes(postId);
459 function updatePostLikes(postId) {
460 ajaxGet("getLikes.ajax", { "type": "post", "post": postId }, function(data) {
461 if ((data != null) && data.success) {
462 sone.find(".post#post-" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes === 0);
463 sone.find(".post#post-" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
464 sone.find(".post#post-" + postId + " > .inner-part > .status-line .likes > span").prop("title", generateSoneList(data.sones));
471 function likeReply(replyId) {
472 ajaxGet("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data) {
473 if ((data == null) || !data.success) {
476 sone.find(".reply#reply-" + replyId + " .status-line .like").addClass("hidden");
477 sone.find(".reply#reply-" + replyId + " .status-line .unlike").removeClass("hidden");
478 updateReplyLikes(replyId);
484 function unlikeReply(replyId) {
485 ajaxGet("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data) {
486 if ((data == null) || !data.success) {
489 sone.find(".reply#reply-" + replyId + " .status-line .unlike").addClass("hidden");
490 sone.find(".reply#reply-" + replyId + " .status-line .like").removeClass("hidden");
491 updateReplyLikes(replyId);
498 * Trusts the Sone with the given ID.
501 * The ID of the Sone to trust
503 function trustSone(soneId) {
504 ajaxGet("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data) {
505 if ((data != null) && data.success) {
506 updateTrustControls(soneId, data.trustValue);
512 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
515 * The ID of the Sone to distrust
517 function distrustSone(soneId) {
518 ajaxGet("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data) {
519 if ((data != null) && data.success) {
520 updateTrustControls(soneId, data.trustValue);
526 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
529 * The ID of the Sone to untrust
531 function untrustSone(soneId) {
532 ajaxGet("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data) {
533 if ((data != null) && data.success) {
534 updateTrustControls(soneId, data.trustValue);
540 * Updates the trust controls for all posts and replies of the given Sone,
541 * according to the given trust value.
544 * The ID of the Sone to update all trust controls for
546 * The trust value for the Sone
548 function updateTrustControls(soneId, trustValue) {
549 sone.find(".post").each(function() {
550 if (getPostAuthor(this) === soneId) {
551 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
552 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
553 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
556 sone.find(".reply").each(function() {
557 if (getReplyAuthor(this) === soneId) {
558 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
559 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
560 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
566 * Bookmarks the post with the given ID.
569 * The ID of the post to bookmark
571 function bookmarkPost(postId) {
573 ajaxGet("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data) {
574 if ((data != null) && data.success) {
575 getPost(postId).find(".bookmark").toggleClass("hidden", true);
576 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
583 * Unbookmarks the post with the given ID.
586 * The ID of the post to unbookmark
588 function unbookmarkPost(postId) {
589 ajaxGet("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data) {
590 if ((data != null) && data.success) {
591 getPost(postId).find(".bookmark").toggleClass("hidden", false);
592 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
597 function updateReplyLikes(replyId) {
598 ajaxGet("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data) {
599 if ((data != null) && data.success) {
600 sone.find(".reply#reply-" + replyId + " .status-line .likes").toggleClass("hidden", data.likes === 0);
601 sone.find(".reply#reply-" + replyId + " .status-line .likes span.like-count").text(data.likes);
602 sone.find(".reply#reply-" + replyId + " .status-line .likes > span").prop("title", generateSoneList(data.sones));
610 * Posts a reply and calls the given callback when the request finishes.
613 * The ID of the sender
615 * The ID of the post the reply refers to
618 * @param callbackFunction
619 * The callback function to call when the request finishes (takes 3
620 * parameters: success, error, replyId)
622 function postReply(sender, postId, text, callbackFunction) {
623 ajaxGet("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data) {
625 /* TODO - show error */
629 callbackFunction(true, null, data.reply, data.sone);
631 callbackFunction(false, data.error);
639 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
642 * The Sone to ajaxify
644 function ajaxifySone(soneElement) {
646 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
649 $(".follow", soneElement).submit(function() {
650 var followElement = this;
651 ajaxGet("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
652 $(followElement).addClass("hidden");
653 $(followElement).parent().find(".unfollow").removeClass("hidden");
657 $(".unfollow", soneElement).submit(function() {
658 var unfollowElement = this;
659 ajaxGet("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
660 $(unfollowElement).addClass("hidden");
661 $(unfollowElement).parent().find(".follow").removeClass("hidden");
665 $(".lock", soneElement).submit(function() {
666 var lockElement = this;
667 ajaxGet("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
668 $(lockElement).addClass("hidden");
669 $(lockElement).parent().find(".unlock").removeClass("hidden");
673 $(".unlock", soneElement).submit(function() {
674 var unlockElement = this;
675 ajaxGet("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
676 $(unlockElement).addClass("hidden");
677 $(unlockElement).parent().find(".lock").removeClass("hidden");
682 /* mark Sone as known when clicking it. */
683 $(soneElement).click(function() {
684 markSoneAsKnown(this);
689 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
692 * The post element to ajaxify
694 function ajaxifyPost(postElement) {
695 $(postElement).find("form").submit(function() {
698 $(postElement).find(".create-reply button:submit").click(function() {
699 var button = $(this);
700 button.prop("disabled", "disabled");
701 var sender = $(this.form).find(":input[name=sender]").val();
702 var inputField = $(this.form).find(":input[name=text]:enabled").get(0);
703 var postId = getPostId(this);
704 var text = $(inputField).val();
705 (function(sender, postId, text, inputField) {
706 postReply(sender, postId, text, function(success, error, replyId, soneId) {
708 $(inputField).val("");
709 loadNewReply(replyId, soneId, postId);
710 sone.find(".post#post-" + postId + " .create-reply").addClass("hidden");
711 sone.find(".post#post-" + postId + " .create-reply .sender").hide();
712 sone.find(".post#post-" + postId + " .create-reply .select-sender").show();
713 sone.find(".post#post-" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
714 updateReplyTimes(replyId);
718 button.removeAttr("disabled");
720 })(sender, postId, text, inputField);
724 /* replace all “delete” buttons with javascript. */
725 (function(postElement) {
726 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
727 var postId = getPostId(postElement);
728 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
732 /* convert all “like” buttons to javascript functions. */
733 $(postElement).find(".like-post").submit(function() {
734 likePost(getPostId(this));
737 $(postElement).find(".unlike-post").submit(function() {
738 unlikePost(getPostId(this));
742 /* convert trust control buttons to javascript functions. */
743 $(postElement).find(".post-trust").submit(function() {
744 trustSone(getPostAuthor(this));
747 $(postElement).find(".post-distrust").submit(function() {
748 distrustSone(getPostAuthor(this));
751 $(postElement).find(".post-untrust").submit(function() {
752 untrustSone(getPostAuthor(this));
756 /* convert bookmark/unbookmark buttons to javascript functions. */
757 $(postElement).find(".bookmark").submit(function() {
758 bookmarkPost(getPostId(this));
761 $(postElement).find(".unbookmark").submit(function() {
762 unbookmarkPost(getPostId(this));
766 /* convert “show source” link into javascript function. */
767 $(postElement).find(".show-source").each(function() {
768 $("a", this).click(function() {
769 var post = getPostElement(this);
770 var rawPostText = $(".post-text.raw-text", post);
771 rawPostText.toggleClass("hidden");
772 if (rawPostText.hasClass("hidden")) {
773 $(".post-text.short-text", post).removeClass("hidden");
774 $(".post-text.text", post).addClass("hidden");
775 $(".expand-post-text", post).removeClass("hidden");
776 $(".shrink-post-text", post).addClass("hidden");
778 $(".post-text.short-text", post).addClass("hidden");
779 $(".post-text.text", post).addClass("hidden");
780 $(".expand-post-text", post).addClass("hidden");
781 $(".shrink-post-text", post).addClass("hidden");
787 /* convert “show more” link into javascript function. */
788 $(postElement).find(".expand-post-text").each(function() {
789 $(this).click(function() {
790 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
791 $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
792 $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
793 $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
797 $(postElement).find(".shrink-post-text").each(function() {
798 $(this).click(function() {
799 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
800 $(".post-text.short-text", getPostElement(this)).toggleClass("hidden");
801 $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
802 $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
807 /* ajaxify author/post links */
808 $(".post-status-line .permalink a", postElement).click(function() {
809 if (!$(".create-reply", postElement).hasClass("hidden")) {
810 var textArea = $(":input.reply-input", postElement).focus().data("textarea");
811 $(textArea).replaceSelection($(this).prop("href"));
816 /* add “comment” link. */
817 addCommentLink(getPostId(postElement), getPostAuthor(postElement), postElement, $(postElement).find(".post-status-line .permalink-author"));
819 /* process all replies. */
821 $(postElement).find(".reply").each(function() {
822 replyIds.push(getReplyId(this));
825 updateReplyTimes(replyIds.join(","));
827 /* process reply input fields. */
828 getTranslation("WebInterface.DefaultText.Reply", function(text) {
829 $(postElement).find(":input.reply-input").each(function() {
830 registerInputTextareaSwap(this, text, "text", false, false);
834 /* process sender selection. */
835 $(".select-sender", postElement).css("display", "inline");
836 $(".sender", postElement).hide();
837 $(".select-sender button", postElement).click(function() {
838 $(".sender", postElement).show();
839 $(".select-sender", postElement).hide();
843 /* mark everything as known on click. */
844 (function(postElement) {
845 $(postElement).click(function(event) {
846 if ($(event.target).hasClass("click-to-show")) {
849 markPostAsKnown(postElement, false);
853 /* hide reply input field. */
854 $(postElement).find(".create-reply").addClass("hidden");
856 /* show Sone menu when hovering over the avatar. */
857 $(postElement).find(".post-avatar").mouseover(function() {
858 if (typeof currentSoneMenuTimeoutHandler !== undefined) {
859 clearTimeout(currentSoneMenuTimeoutHandler);
861 currentSoneMenuId = getPostId(this);
862 currentSoneMenuTimeoutHandler = setTimeout(function() {
863 $(".sone-menu:visible").fadeOut();
864 $(".sone-post-menu", postElement).mouseleave(function() {
868 }).mouseleave(function() {
869 if (currentSoneMenuId === getPostId(this)) {
870 clearTimeout(currentSoneMenuTimeoutHandler);
873 (function(postElement) {
874 var soneId = $(".sone-menu-id:first", postElement).text();
875 $(".sone-post-menu .follow", postElement).click(function() {
876 var followElement = this;
877 ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
878 $(followElement).addClass("hidden");
879 $(followElement).parent().find(".unfollow").removeClass("hidden");
880 sone.find(".sone-menu").each(function() {
881 if (getMenuSone(this) === soneId) {
882 $(".follow", this).toggleClass("hidden", true);
883 $(".unfollow", this).toggleClass("hidden", false);
889 $(".sone-post-menu .unfollow", postElement).click(function() {
890 var unfollowElement = this;
891 ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
892 $(unfollowElement).addClass("hidden");
893 $(unfollowElement).parent().find(".follow").removeClass("hidden");
894 sone.find(".sone-menu").each(function() {
895 if (getMenuSone(this) === soneId) {
896 $(".follow", this).toggleClass("hidden", false);
897 $(".unfollow", this).toggleClass("hidden", true);
907 * Ajaxifies the given reply element.
909 * @param replyElement
910 * The reply element to ajaxify
912 function ajaxifyReply(replyElement) {
913 $(replyElement).find(".like-reply").submit(function() {
914 likeReply(getReplyId(this));
917 $(replyElement).find(".unlike-reply").submit(function() {
918 unlikeReply(getReplyId(this));
921 (function(replyElement) {
922 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
923 $(replyElement).find(".delete-reply button").each(function() {
924 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
929 /* ajaxify author links */
930 $(".reply-status-line .permalink a", replyElement).click(function() {
931 if (!$(".create-reply", getPostElement(replyElement)).hasClass("hidden")) {
932 var textArea = $(":input.reply-input", getPostElement(replyElement)).focus().data("textarea");
933 $(textArea).replaceSelection($(this).prop("href"));
938 addCommentLink(getPostId(replyElement), getReplyAuthor(replyElement), replyElement, $(replyElement).find(".reply-status-line .permalink-author"));
940 /* convert “show source” link into javascript function. */
941 $(replyElement).find(".show-reply-source").each(function() {
942 $("a", this).click(function() {
943 var reply = getReplyElement(this);
944 var rawReplyText = $(".reply-text.raw-text", reply);
945 rawReplyText.toggleClass("hidden");
946 if (rawReplyText.hasClass("hidden")) {
947 $(".reply-text.short-text", reply).removeClass("hidden");
948 $(".reply-text.text", reply).addClass("hidden");
949 $(".expand-reply-text", reply).removeClass("hidden");
950 $(".shrink-reply-text", reply).addClass("hidden");
952 $(".reply-text.short-text", reply).addClass("hidden");
953 $(".reply-text.text", reply).addClass("hidden");
954 $(".expand-reply-text", reply).addClass("hidden");
955 $(".shrink-reply-text", reply).addClass("hidden");
961 /* convert “show more” link into javascript function. */
962 $(replyElement).find(".expand-reply-text").each(function() {
963 $(this).click(function() {
964 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
965 $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
966 $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
967 $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
971 $(replyElement).find(".shrink-reply-text").each(function() {
972 $(this).click(function() {
973 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
974 $(".reply-text.short-text", getReplyElement(this)).toggleClass("hidden");
975 $(".expand-reply-text", getReplyElement(this)).toggleClass("hidden");
976 $(".shrink-reply-text", getReplyElement(this)).toggleClass("hidden");
981 /* convert trust control buttons to javascript functions. */
982 $(replyElement).find(".reply-trust").submit(function() {
983 trustSone(getReplyAuthor(this));
986 $(replyElement).find(".reply-distrust").submit(function() {
987 distrustSone(getReplyAuthor(this));
990 $(replyElement).find(".reply-untrust").submit(function() {
991 untrustSone(getReplyAuthor(this));
995 /* show Sone menu when hovering over the avatar. */
996 $(replyElement).find(".reply-avatar").mouseover(function() {
997 if (typeof currentSoneMenuTimeoutHandler !== undefined) {
998 clearTimeout(currentSoneMenuTimeoutHandler);
1000 currentSoneMenuId = getPostId(this) + "-" + getReplyId(this);
1001 currentSoneMenuTimeoutHandler = setTimeout(function() {
1002 $(".sone-menu:visible").fadeOut();
1003 $(".sone-reply-menu", replyElement).mouseleave(function() {
1007 }).mouseleave(function() {
1008 if (currentSoneMenuId === getPostId(this) + "-" + getReplyId(this)) {
1009 clearTimeout(currentSoneMenuTimeoutHandler);
1012 (function(replyElement) {
1013 var soneId = $(".sone-menu-id", replyElement).text();
1014 $(".sone-menu .follow", replyElement).click(function() {
1015 var followElement = this;
1016 ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1017 $(followElement).addClass("hidden");
1018 $(followElement).parent().find(".unfollow").removeClass("hidden");
1019 sone.find(".sone-menu").each(function() {
1020 if (getMenuSone(this) === soneId) {
1021 $(".follow", this).toggleClass("hidden", true);
1022 $(".unfollow", this).toggleClass("hidden", false);
1028 $(".sone-menu .unfollow", replyElement).click(function() {
1029 var unfollowElement = this;
1030 ajaxGet("unfollowSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
1031 $(unfollowElement).addClass("hidden");
1032 $(unfollowElement).parent().find(".follow").removeClass("hidden");
1033 sone.find(".sone-menu").each(function() {
1034 if (getMenuSone(this) === soneId) {
1035 $(".follow", this).toggleClass("hidden", false);
1036 $(".unfollow", this).toggleClass("hidden", true);
1046 * Ajaxifies the given notification by replacing the form with AJAX.
1048 * @param notification
1049 * jQuery object representing the notification.
1051 function ajaxifyNotification(notification) {
1052 notification.find("form").submit(function() {
1055 notification.find("input[name=returnPage]").val($.url.attr("relative"));
1056 if (notification.find(".short-text").length > 0) {
1057 notification.find(".short-text").removeClass("hidden");
1058 notification.find(".text").addClass("hidden");
1060 notification.find("form.mark-as-read button").click(function() {
1061 var allIds = $(":input[name=id]", this.form).val().split(" ");
1062 for (var index = 0; index < allIds.length; index += 16) {
1063 var ids = allIds.slice(index, index + 16).join(" ");
1064 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": ids});
1067 notification.find("a[class^='link-']").each(function() {
1068 var linkElement = $(this);
1069 if (linkElement.is("[href^='viewPost']")) {
1070 var id = linkElement.prop("class").substr(5);
1072 linkElement.prop("href", "#post-" + id).addClass("in-page-link");
1076 notification.find("form.dismiss button").click(function() {
1077 ajaxGet("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.prop("id") }, function() {
1078 /* dismiss in case of error, too. */
1079 notification.slideUp();
1084 return notification;
1088 * Returns the notification hash. This hash is used in {@link #getStatus()} to
1089 * determine whether the notifications changed and need to be reloaded.
1091 function getNotificationHash() {
1092 return sone.find("#notification-area #notification-hash").text();
1096 * Sets the notification hash.
1098 * @param notificationHash
1099 * The new notification hash
1101 function setNotificationHash(notificationHash) {
1102 sone.find("#notification-area #notification-hash").text(notificationHash);
1106 * Retrieves element IDs from notification elements.
1108 * @param notification
1109 * The notification element
1111 * The selector of the element containing the ID as text
1112 * @returns All extracted IDs
1114 function getElementIds(notification, selector) {
1115 var elementIds = [];
1116 $(selector, notification).each(function() {
1117 elementIds.push($(this).text());
1123 * Compares the given notification elements and calls {@link #markSoneAsKnown()}
1124 * for every ID that is contained in the old notification but not in the new.
1126 * @param oldNotification
1127 * The old notification element
1128 * @param newNotification
1129 * The new notification element
1131 function checkForRemovedSones(oldNotification, newNotification) {
1132 if (getNotificationId(oldNotification) !== "new-sone-notification") {
1135 var oldIds = getElementIds(oldNotification, ".new-sone-id");
1136 var newIds = getElementIds(newNotification, ".new-sone-id");
1137 $.each(oldIds, function(index, value) {
1138 if ($.inArray(value, newIds) === -1) {
1139 markSoneAsKnown(getSone(value), true);
1145 * Compares the given notification elements and calls {@link #markPostAsKnown()}
1146 * for every ID that is contained in the old notification but not in the new.
1148 * @param oldNotification
1149 * The old notification element
1150 * @param newNotification
1151 * The new notification element
1153 function checkForRemovedPosts(oldNotification, newNotification) {
1154 if (getNotificationId(oldNotification) !== "new-post-notification") {
1157 var oldIds = getElementIds(oldNotification, ".post-id");
1158 var newIds = getElementIds(newNotification, ".post-id");
1159 $.each(oldIds, function(index, value) {
1160 if ($.inArray(value, newIds) === -1) {
1161 markPostAsKnown(getPost(value), true);
1167 * Compares the given notification elements and calls
1168 * {@link #markReplyAsKnown()} for every ID that is contained in the old
1169 * notification but not in the new.
1171 * @param oldNotification
1172 * The old notification element
1173 * @param newNotification
1174 * The new notification element
1176 function checkForRemovedReplies(oldNotification, newNotification) {
1177 if (getNotificationId(oldNotification) !== "new-reply-notification") {
1180 var oldIds = getElementIds(oldNotification, ".reply-id");
1181 var newIds = getElementIds(newNotification, ".reply-id");
1182 $.each(oldIds, function(index, value) {
1183 if ($.inArray(value, newIds) === -1) {
1184 markReplyAsKnown(getReply(value), true);
1189 function getStatus() {
1190 var parameters = isViewSonePage() ? {"soneIds": getShownSoneId() } : isKnownSonesPage() ? {"soneIds": getShownSoneIds() } : {};
1191 $.extend(parameters, {
1192 "elements": JSON.stringify($(".linked-element.not-loaded").map(function () {
1193 return $(this).prop("title");
1196 ajaxGet("getStatus.ajax", parameters, function(data) {
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);
1218 if (data.linkedElements) {
1219 loadLinkedElements(data.linkedElements)
1221 /* do it again in 5 seconds. */
1222 setTimeout(getStatus, 5000);
1224 /* data.success was false, wait 30 seconds. */
1225 setTimeout(getStatus, 30000);
1228 statusRequestQueued = false;
1233 function requestNotifications() {
1234 ajaxGet("getNotifications.ajax", {}, function(data) {
1235 if (data && data.success) {
1236 /* search for removed notifications. */
1237 sone.find("#notification-area .notification").each(function() {
1238 var notificationId = $(this).prop("id");
1239 var foundNotification = false;
1240 $.each(data.notifications, function(index, value) {
1241 if (value.id === notificationId) {
1242 foundNotification = true;
1246 if (!foundNotification) {
1247 if (notificationId === "new-sone-notification" && (data.options["ShowNotification/NewSones"] === true)) {
1248 $(".new-sone-id", this).each(function() {
1249 var soneId = $(this).text();
1250 markSoneAsKnown(getSone(soneId), true);
1252 } else if (notificationId === "new-post-notification" && (data.options["ShowNotification/NewPosts"] === true)) {
1253 $(".post-id", this).each(function() {
1254 var postId = $(this).text();
1255 markPostAsKnown(getPost(postId), true);
1257 } else if (notificationId === "new-reply-notification" && (data.options["ShowNotification/NewReplies"] === true)) {
1258 $(".reply-id", this).each(function() {
1259 var replyId = $(this).text();
1260 markReplyAsKnown(getReply(replyId), true);
1263 $(this).slideUp("normal", function() {
1265 /* remove activity when no notifications are visible. */
1266 if (sone.find("#notification-area .notification").length === 0) {
1272 /* process notifications. */
1273 $.each(data.notifications, function(index, value) {
1274 var oldNotification = getNotification(value.id);
1275 var notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide();
1276 if (oldNotification.length !== 0) {
1277 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
1278 var opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
1279 notification.find(".short-text").toggleClass("hidden", opened);
1280 notification.find(".text").toggleClass("hidden", !opened);
1282 checkForRemovedSones(oldNotification, notification);
1283 checkForRemovedPosts(oldNotification, notification);
1284 checkForRemovedReplies(oldNotification, notification);
1285 oldNotification.replaceWith(notification.show());
1287 sone.find("#notification-area").append(notification);
1288 if (value.id.substring(0, 5) !== "local") {
1289 notification.slideDown();
1294 setNotificationHash(data.notificationHash);
1300 * Returns the ID of the currently logged in Sone.
1302 * @return The ID of the current Sone, or an empty string if no Sone is logged
1305 function getCurrentSoneId() {
1306 return $("#currentSoneId").text();
1310 * Returns the content of the page-id attribute.
1312 * @returns The page ID
1314 function getPageId() {
1315 return sone.find(".page-id").text();
1319 * Returns whether the current page is the index page.
1321 * @returns {Boolean} <code>true</code> if the current page is the index page,
1322 * <code>false</code> otherwise
1324 function isIndexPage() {
1325 return getPageId() === "index";
1329 * Returns the current page of the selected pagination. If no pagination can be
1330 * found with the given selector, {@code 1} is returned.
1332 * @param paginationSelector
1333 * The pagination selector
1334 * @returns The current page of the pagination
1336 function getPage(paginationSelector) {
1337 var pagination = $(paginationSelector);
1338 if (pagination.length > 0) {
1339 return $(".current-page", paginationSelector).text();
1345 * Returns whether the current page is a “view Sone” page.
1347 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
1348 * page, <code>false</code> otherwise
1350 function isViewSonePage() {
1351 return getPageId() === "view-sone";
1355 * Returns the ID of the currently shown Sone. This will only return a sensible
1356 * value if isViewSonePage() returns <code>true</code>.
1358 * @returns The ID of the currently shown Sone
1360 function getShownSoneId() {
1361 return sone.find(".sone-id").first().text();
1365 * Returns the ID of all currently visible Sones. This is mainly used on the
1366 * “Known Sones” page.
1368 * @returns The ID of the currently shown Sones
1370 function getShownSoneIds() {
1372 sone.find("#known-sones .sone .id").each(function() {
1373 soneIds.push($(this).text());
1375 return soneIds.join(",");
1379 * Returns whether the current page is a “view post” page.
1381 * @returns {Boolean} <code>true</code> if the current page is a “view post”
1382 * page, <code>false</code> otherwise
1384 function isViewPostPage() {
1385 return getPageId() === "view-post";
1389 * Returns the ID of the currently shown post. This will only return a sensible
1390 * value if isViewPostPage() returns <code>true</code>.
1392 * @returns The ID of the currently shown post
1394 function getShownPostId() {
1395 return sone.find(".post-id").text();
1399 * Returns whether the current page is the “known Sones” page.
1401 * @returns {Boolean} <code>true</code> if the current page is the “known
1402 * Sones” page, <code>false</code> otherwise
1404 function isKnownSonesPage() {
1405 return getPageId() === "known-sones";
1409 * Returns whether a post with the given ID exists on the current page.
1412 * The post ID to check for
1413 * @returns {Boolean} <code>true</code> if a post with the given ID already
1414 * exists on the page, <code>false</code> otherwise
1416 function hasPost(postId) {
1417 return $(".post#post-" + postId).length > 0;
1421 * Returns whether a reply with the given ID exists on the current page.
1424 * The reply ID to check for
1425 * @returns {Boolean} <code>true</code> if a reply with the given ID already
1426 * exists on the page, <code>false</code> otherwise
1428 function hasReply(replyId) {
1429 return sone.find(".reply#reply-" + replyId).length > 0;
1432 function loadNewPost(postId, soneId, recipientId, time) {
1433 if (hasPost(postId)) {
1436 if (!isIndexPage() || (getPage(".pagination-index") > 1)) {
1437 if (!isViewPostPage() || (getShownPostId() !== postId)) {
1438 if (!isViewSonePage() || ((getShownSoneId() !== soneId) && (getShownSoneId() !== recipientId)) || (getPage(".post-navigation") > 1)) {
1443 if (getPostTime(sone.find(".post").last()) > time) {
1446 ajaxGet("getPost.ajax", { "post" : postId }, function(data) {
1447 if ((data != null) && data.success) {
1448 if (hasPost(data.post.id)) {
1451 if ((!isIndexPage() || (getPage(".pagination-index") > 1)) && !(isViewSonePage() && ((getShownSoneId() === data.post.sone) || (getShownSoneId() === data.post.recipient) || (getPage(".post-navigation") > 1)))) {
1454 var firstOlderPost = null;
1455 sone.find(".post").each(function() {
1456 if (getPostTime(this) < data.post.time) {
1457 firstOlderPost = $(this);
1461 var newPost = $(data.post.html).addClass("hidden");
1462 if ($(".post-author-local", newPost).text() === "true") {
1463 newPost.removeClass("new");
1465 if (firstOlderPost != null) {
1466 newPost.insertBefore(firstOlderPost);
1468 ajaxifyPost(newPost);
1469 updatePostTimes(data.post.id);
1470 newPost.slideDown();
1476 function loadNewReply(replyId, soneId, postId) {
1477 if (hasReply(replyId)) {
1480 if (!hasPost(postId)) {
1483 ajaxGet("getReply.ajax", { "reply": replyId }, function(data) {
1485 if ((data != null) && data.success) {
1486 if (hasReply(data.reply.id)) {
1489 sone.find(".post#post-" + data.reply.postId).each(function() {
1490 var firstNewerReply = null;
1491 $(this).find(".replies .reply").each(function() {
1492 if (getReplyTime(this) > data.reply.time) {
1493 firstNewerReply = $(this);
1497 var newReply = $(data.reply.html).addClass("hidden");
1498 if ($(".reply-author-local", newReply).text() === "true") {
1499 newReply.removeClass("new");
1500 (function(newReply) {
1501 setTimeout(function() {
1502 markReplyAsKnown(newReply, false);
1506 if (firstNewerReply != null) {
1507 newReply.insertBefore(firstNewerReply);
1509 if ($(this).find(".replies .create-reply")) {
1510 $(this).find(".replies .create-reply").before(newReply);
1512 $(this).find(".replies").append(newReply);
1515 ajaxifyReply(newReply);
1516 updateReplyTimes(data.reply.id);
1517 newReply.slideDown();
1525 function loadLinkedElements(links) {
1526 var failedElements = links.filter(function(element) {
1527 return element.failed;
1529 if (failedElements.length > 0) {
1530 failedElements.forEach(function(element) {
1531 getLinkedElements(element.link).each(function() {
1536 var loadedElements = links.filter(function(element) {
1537 return !element.loading && !element.failed;
1539 if (loadedElements.length > 0) {
1540 ajaxGet("getLinkedElement.ajax", {
1541 "elements": JSON.stringify(loadedElements.map(function(element) {
1542 return element.link;
1544 }, function (data) {
1545 if ((data != null) && (data.success)) {
1546 data.linkedElements.forEach(function (linkedElement) {
1547 getLinkedElements(linkedElement.link).each(function() {
1548 $(this).replaceWith(linkedElement.html);
1556 function getLinkedElements(link) {
1557 return $(".linked-element[title='" + link + "']")
1561 * Marks the given Sone as known if it is still new.
1563 * @param soneElement
1564 * The Sone to mark as known
1565 * @param skipRequest
1566 * true to skip the JSON request, false or omit to perform the JSON
1569 function markSoneAsKnown(soneElement, skipRequest) {
1570 if ($(soneElement).hasClass("new")) {
1571 $(soneElement).removeClass("new");
1572 if ((typeof skipRequest == "undefined") || !skipRequest) {
1573 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)});
1574 requestNotifications();
1579 function markPostAsKnown(postElements, skipRequest) {
1580 $(postElements).each(function() {
1581 var postElement = this;
1582 if ($(postElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1583 (function(postElement) {
1584 $(postElement).removeClass("new");
1585 if ((typeof skipRequest == "undefined") || !skipRequest) {
1586 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1587 requestNotifications();
1591 $(".click-to-show", postElement).removeClass("new");
1593 markReplyAsKnown($(postElements).find(".reply"), true);
1596 function markReplyAsKnown(replyElements, skipRequest) {
1597 $(replyElements).each(function() {
1598 var replyElement = this;
1599 if ($(replyElement).hasClass("new") || ((typeof skipRequest != "undefined"))) {
1600 (function(replyElement) {
1601 $(replyElement).removeClass("new");
1602 if ((typeof skipRequest == "undefined") || !skipRequest) {
1603 ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1604 requestNotifications();
1612 * Updates the time of the post with the given ID.
1615 * The ID of the post to update
1617 * The text of the time to show
1618 * @param refreshTime
1619 * The refresh time after which to request a new time (in seconds)
1621 * The tooltip to show
1623 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1624 if (!getPost(postId).is(":visible")) {
1627 getPost(postId).find(".post-status-line > .time a").html(timeText).prop("title", tooltip);
1628 (function(postId, refreshTime) {
1629 setTimeout(function() {
1630 updatePostTimes(postId);
1631 }, refreshTime * 1000);
1632 })(postId, refreshTime);
1636 * Requests new rendered times for the posts with the given IDs.
1639 * Comma-separated post IDs
1641 function updatePostTimes(postIds) {
1642 if (postIds !== "") {
1643 ajaxGet("getTimes.ajax", {"posts": postIds}, function (data) {
1644 if ((data != null) && data.success) {
1645 $.each(data.postTimes, function (index, value) {
1646 updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1654 * Updates the time of the reply with the given ID.
1657 * The ID of the reply to update
1659 * The text of the time to show
1660 * @param refreshTime
1661 * The refresh time after which to request a new time (in seconds)
1663 * The tooltip to show
1665 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1666 getReply(replyId).find(".reply-status-line > .time").html(timeText).prop("title", tooltip);
1667 (function(replyId, refreshTime) {
1668 setTimeout(function() {
1669 updateReplyTimes(replyId);
1670 }, refreshTime * 1000);
1671 })(replyId, refreshTime);
1675 * Requests new rendered times for the posts with the given IDs.
1678 * Comma-separated post IDs
1680 function updateReplyTimes(replyIds) {
1681 if (replyIds !== "") {
1682 ajaxGet("getTimes.ajax", {"replies": replyIds}, function (data) {
1683 if ((data != null) && data.success) {
1684 $.each(data.replyTimes, function (index, value) {
1685 updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1692 function resetActivity() {
1693 var title = document.title;
1694 if (title.indexOf('(') === 0) {
1695 setTitle(title.substr(title.indexOf(' ') + 1));
1697 iconBlinking = false;
1700 function setActivity() {
1702 var title = document.title;
1703 if (title.indexOf('(') !== 0) {
1704 setTitle("(!) " + title);
1706 if (!iconBlinking) {
1707 setTimeout(toggleIcon, 1500);
1708 iconBlinking = true;
1714 * Sets the window title after a small delay to prevent race-condition issues.
1719 function setTitle(title) {
1720 setTimeout(function() {
1721 document.title = title;
1725 /** Whether the icon is currently showing activity. */
1726 var iconActive = false;
1728 /** Whether the icon is currently supposed to blink. */
1729 var iconBlinking = false;
1732 * Toggles the icon. If the window has gained focus and the icon is still
1733 * showing the activity state, it is returned to normal.
1735 function toggleIcon() {
1736 if (focus || !iconBlinking) {
1738 changeIcon("images/icon.png");
1741 iconBlinking = false;
1743 iconActive = !iconActive;
1744 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1745 setTimeout(toggleIcon, 1500);
1750 * Changes the icon of the page.
1753 * The new URL of the icon
1755 function changeIcon(iconUrl) {
1756 $("link[rel=icon]").remove();
1757 $("head").append($("<link>").prop("rel", "icon").prop("type", "image/png").prop("href", iconUrl));
1758 $("iframe[id=icon-update]")[0].src += "";
1762 * Creates a new notification.
1765 * The ID of the notificaiton
1767 * The text of the notification
1768 * @param dismissable
1769 * <code>true</code> if the notification can be dismissed by the
1772 function createNotification(id, lastUpdatedTime, text, dismissable) {
1773 var notification = $("<div></div>").addClass("notification").prop("id", id).prop("lastUpdatedTime", lastUpdatedTime);
1775 var dismissForm = sone.find("#notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id");
1776 dismissForm.find("input[name=notification]").val(id);
1777 notification.append(dismissForm);
1779 notification.append(text);
1780 return notification;
1784 * Shows the details of the notification with the given ID.
1786 * @param notificationId
1787 * The ID of the notification
1789 function showNotificationDetails(notificationId) {
1790 sone.find(".notification#" + notificationId + " .text").removeClass("hidden");
1791 sone.find(".notification#" + notificationId + " .short-text").addClass("hidden");
1795 * Deletes the field with the given ID from the profile.
1798 * The ID of the field to delete
1800 function deleteProfileField(fieldId) {
1801 ajaxGet("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data) {
1802 if (data && data.success) {
1803 sone.find(".profile-field#" + data.field.id).slideUp();
1809 * Renames a profile field.
1812 * The ID of the field to rename
1814 * The new name of the field
1815 * @param successFunction
1816 * Called when the renaming was successful
1818 function editProfileField(fieldId, newName, successFunction) {
1819 ajaxGet("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data) {
1820 if (data && data.success) {
1827 * Moves the profile field with the given ID one slot in the given direction.
1830 * The ID of the field to move
1832 * The direction to move in (“up” or “down”)
1833 * @param successFunction
1834 * Function to call on success
1836 function moveProfileField(fieldId, direction, successFunction) {
1837 ajaxGet("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data) {
1838 if (data && data.success) {
1845 * Moves the profile field with the given ID up one slot.
1848 * The ID of the field to move
1849 * @param successFunction
1850 * Function to call on success
1852 function moveProfileFieldUp(fieldId, successFunction) {
1853 moveProfileField(fieldId, "up", successFunction);
1857 * Moves the profile field with the given ID down one slot.
1860 * The ID of the field to move
1861 * @param successFunction
1862 * Function to call on success
1864 function moveProfileFieldDown(fieldId, successFunction) {
1865 moveProfileField(fieldId, "down", successFunction);
1868 var statusRequestQueued = true;
1871 * Sets the status of the web interface as offline.
1873 function ajaxError() {
1875 showOfflineMarker(true);
1876 if (!statusRequestQueued) {
1877 setTimeout(getStatus, 5000);
1878 statusRequestQueued = true;
1883 * Sets the status of the web interface as online.
1885 function ajaxSuccess() {
1887 showOfflineMarker(!online || (initiallyLoggedIn && notLoggedIn));
1891 * Shows or hides the offline marker.
1894 * {@code true} to display the offline marker, {@code false} to hide
1897 function showOfflineMarker(visible) {
1898 /* jQuery documentation says toggle() works the other way around?! */
1899 sone.find("#offline-marker").toggle(visible);
1901 sone.find("#main").addClass("offline");
1903 sone.find("#main").removeClass("offline");
1908 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1911 var sone = $("#sone");
1914 var initiallyLoggedIn = sone.find("#loggedIn").text() === "true";
1915 var notLoggedIn = !initiallyLoggedIn;
1917 /** ID of the next-to-show Sone context menu. */
1918 var currentSoneMenuId;
1920 /** Timeout handler for the next-to-show Sone context menu. */
1921 var currentSoneMenuTimeoutHandler;
1923 $(document).ready(function() {
1925 /* rip out the status update textarea. */
1926 sone.find(".rip-out").each(function() {
1927 var oldElement = $(this);
1928 var newElement = $("<input type='text'/>");
1929 newElement.prop("class", oldElement.prop("class")).prop("name", oldElement.prop("name"));
1930 oldElement.before(newElement).remove();
1933 /* this initializes the status update input field. */
1934 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1935 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1936 sone.find("#update-status .select-sender").css("display", "inline");
1937 sone.find("#update-status .sender").hide();
1938 sone.find("#update-status .select-sender button").click(function() {
1939 sone.find("#update-status .sender").show();
1940 sone.find("#update-status .select-sender").hide();
1943 sone.find("#update-status").submit(function() {
1944 var button = $("button:submit", this);
1945 button.prop("disabled", "disabled");
1946 if ($(this).find(":input.default:enabled").length > 0) {
1949 var sender = $(this).find(":input[name=sender]").val();
1950 var text = $(this).find(":input[name=text]:enabled").val();
1951 ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function() {
1952 button.removeAttr("disabled");
1954 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1955 $(this).find(":input[name=text]:enabled").val("").blur();
1956 $(this).find(".sender").hide();
1957 $(this).find(".select-sender").show();
1962 /* ajaxify the search input field. */
1963 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1964 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1967 /* ajaxify input field on “view Sone” page. */
1968 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1969 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1970 sone.find("#post-message .select-sender").css("display", "inline");
1971 sone.find("#post-message .sender").hide();
1972 sone.find("#post-message .select-sender button").click(function() {
1973 sone.find("#post-message .sender").show();
1974 sone.find("#post-message .select-sender").hide();
1977 sone.find("#post-message").submit(function() {
1978 var sender = $(this).find(":input[name=sender]").val();
1979 var text = $(this).find(":input[name=text]:enabled").val();
1980 ajaxGet("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text });
1981 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1982 $(this).find(":input[name=text]:enabled").val("").blur();
1983 $(this).find(".sender").hide();
1984 $(this).find(".select-sender").show();
1989 /* Ajaxifies all posts. */
1990 /* calling getTranslation here will cache the necessary values. */
1991 getTranslation("WebInterface.Confirmation.DeletePostButton", function() {
1992 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function() {
1993 getTranslation("WebInterface.DefaultText.Reply", function() {
1994 getTranslation("WebInterface.Button.Comment", function () {
1995 sone.find(".post").each(function() {
2003 /* update post times. */
2005 sone.find(".post").each(function() {
2006 postIds.push(getPostId(this));
2008 updatePostTimes(postIds.join(","));
2010 /* hides all replies but the latest two. */
2011 if (!isViewPostPage()) {
2012 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
2013 sone.find(".post .replies").each(function() {
2014 var allReplies = $(this).find(".reply");
2015 if (allReplies.length > 2) {
2016 var newHidden = false;
2017 for (var replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
2018 $(allReplies[replyIndex]).addClass("hidden");
2019 newHidden |= $(allReplies[replyIndex]).hasClass("new");
2021 var clickToShowElement = $("<div></div>").addClass("click-to-show");
2023 clickToShowElement.addClass("new");
2025 (function(clickToShowElement, allReplies, text) {
2026 clickToShowElement.text(text);
2027 clickToShowElement.click(function() {
2028 allReplies.removeClass("hidden");
2029 clickToShowElement.addClass("hidden");
2031 })(clickToShowElement, allReplies, text);
2032 $(allReplies[0]).before(clickToShowElement);
2038 sone.find(".sone").each(function() {
2039 ajaxifySone($(this));
2042 /* process all existing notifications, ajaxify dismiss buttons. */
2043 sone.find("#notification-area .notification").each(function() {
2044 ajaxifyNotification($(this));
2047 /* activate status polling. */
2048 setTimeout(getStatus, 5000);
2050 /* reset activity counter when the page has focus. */
2051 $(window).focus(function() {
2054 }).blur(function() {