1 /* Sone JavaScript functions. */
3 /* jQuery overrides. */
4 oldGetJson = jQuery.prototype.getJSON;
5 jQuery.prototype.getJSON = function(url, data, successCallback, errorCallback) {
6 if (typeof errorCallback == "undefined") {
7 return oldGetJson(url, data, successCallback);
9 if (jQuery.isFunction(data)) {
10 errorCallback = successCallback;
11 successCallback = data;
17 success: successCallback,
23 return $("#sone").hasClass("online");
26 function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, optional, dontUseTextarea) {
27 $(inputElement).each(function() {
28 textarea = $(dontUseTextarea ? "<input type=\"text\" name=\"" + inputFieldName + "\">" : "<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
29 if ($(this).val() == "") {
31 inputField = $(this).data("inputField");
32 inputField.show().removeAttr("disabled").addClass("default");
33 inputField.val(defaultText);
35 }).hide().data("inputField", $(this)).val($(this).val());
36 $(this).data("textarea", textarea).after(textarea);
37 (function(inputField, textarea) {
38 inputField.focus(function() {
39 $(this).hide().attr("disabled", "disabled");
40 /* no, show(), “display: block” is not what I need. */
41 textarea.attr("style", "display: inline").focus();
43 if (inputField.val() == "") {
44 inputField.addClass("default");
45 inputField.val(defaultText);
47 inputField.hide().attr("disabled", "disabled");
50 $(inputField.get(0).form).submit(function() {
51 inputField.attr("disabled", "disabled");
52 if (!optional && (textarea.val() == "")) {
53 inputField.removeAttr("disabled").focus();
57 })($(this), textarea);
62 * Adds a “comment” link to all status lines contained in the given element.
67 * The element to add a “comment” link to
69 function addCommentLink(postId, author, element, insertAfterThisElement) {
70 if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
73 commentElement = (function(postId, author) {
74 separator = $("<span> · </span>").addClass("separator");
75 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
76 replyElement = $("#sone .post#" + postId + " .create-reply");
77 replyElement.removeClass("hidden");
78 replyElement.removeClass("light");
79 (function(replyElement) {
80 replyElement.find("input.reply-input").blur(function() {
81 if ($(this).hasClass("default")) {
82 replyElement.addClass("light");
85 replyElement.removeClass("light");
88 textArea = replyElement.find("input.reply-input").focus().data("textarea");
89 textArea.val(textArea.val() + "@sone://" + author + " ");
91 return commentElement;
93 $(insertAfterThisElement).after(commentElement.clone(true));
94 $(insertAfterThisElement).after(separator);
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 $.getJSON("getTranslation.ajax", {"key": key}, function(data, textStatus) {
114 if ((data != null) && data.success) {
115 translations[key] = data.value;
116 callback(data.value);
118 }, function(xmlHttpRequest, textStatus, error) {
124 * Filters the given Sone ID, replacing all “~” characters by an underscore.
127 * The Sone ID to filter
128 * @returns The filtered Sone ID
130 function filterSoneId(soneId) {
131 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
135 * Updates the status of the given Sone.
138 * The ID of the Sone to update
140 * The status of the Sone (“idle”, “unknown”, “inserting”,
143 * Whether the Sone is modified
145 * Whether the Sone is locked
147 * The date and time of the last update (formatted for display)
149 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
150 $("#sone .sone." + filterSoneId(soneId)).
151 toggleClass("unknown", status == "unknown").
152 toggleClass("idle", status == "idle").
153 toggleClass("inserting", status == "inserting").
154 toggleClass("downloading", status == "downloading").
155 toggleClass("modified", modified);
156 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
157 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
158 if (lastUpdated != null) {
159 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
161 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
162 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown);
165 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
169 * Enhances a “delete” button so that the confirmation is done on the same page.
174 * The text to show on the button
175 * @param deleteCallback
176 * The callback that actually deletes something
178 function enhanceDeleteButton(button, text, deleteCallback) {
180 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
181 $(this).fadeOut("slow");
184 }).insertAfter(button);
185 (function(button, newButton) {
186 button.click(function() {
187 button.fadeOut("slow", function() {
188 newButton.fadeIn("slow");
189 $(document).one("click", function() {
190 if (this != newButton.get(0)) {
191 newButton.fadeOut(function() {
199 })(button, newButton);
204 * Enhances a post’s “delete” button.
209 * The ID of the post to delete
211 * The text to replace the button with
213 function enhanceDeletePostButton(button, postId, text) {
214 enhanceDeleteButton(button, text, function() {
215 $.getJSON("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
220 $("#sone .post#" + postId).slideUp();
221 } else if (data.error == "invalid-post-id") {
222 /* pretend the post is already gone. */
223 getPost(postId).slideUp();
224 } else if (data.error == "auth-required") {
225 alert("You need to be logged in.");
226 } else if (data.error == "not-authorized") {
227 alert("You are not allowed to delete this post.");
229 }, function(xmlHttpRequest, textStatus, error) {
236 * Enhances a reply’s “delete” button.
241 * The ID of the reply to delete
243 * The text to replace the button with
245 function enhanceDeleteReplyButton(button, replyId, text) {
246 enhanceDeleteButton(button, text, function() {
247 $.getJSON("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
252 $("#sone .reply#" + replyId).slideUp();
253 } else if (data.error == "invalid-reply-id") {
254 /* pretend the reply is already gone. */
255 getReply(replyId).slideUp();
256 } else if (data.error == "auth-required") {
257 alert("You need to be logged in.");
258 } else if (data.error == "not-authorized") {
259 alert("You are not allowed to delete this reply.");
261 }, function(xmlHttpRequest, textStatus, error) {
267 function getFormPassword() {
268 return $("#sone #formPassword").text();
272 * Returns the element of the Sone with the given ID.
276 * @returns All Sone elements with the given ID
278 function getSone(soneId) {
279 return $("#sone .sone").filter(function(index) {
280 return $(".id").text() == soneId;
284 function getSoneElement(element) {
285 return $(element).closest(".sone");
289 * Generates a list of Sones by concatening the names of the given sones with a
290 * new line character (“\n”).
293 * The sones to format
294 * @returns {String} The created string
296 function generateSoneList(sones) {
298 $.each(sones, function() {
299 if (soneList != "") {
302 soneList += this.name;
308 * Returns the ID of the Sone that this element belongs to.
311 * The element to locate the matching Sone ID for
312 * @returns The ID of the Sone, or undefined
314 function getSoneId(element) {
315 return getSoneElement(element).find(".id").text();
319 * Returns the element of the post with the given ID.
323 * @returns The element of the post
325 function getPost(postId) {
326 return $("#sone .post#" + postId);
329 function getPostElement(element) {
330 return $(element).closest(".post");
333 function getPostId(element) {
334 return getPostElement(element).attr("id");
337 function getPostTime(element) {
338 return getPostElement(element).find(".post-time").text();
342 * Returns the author of the post the given element belongs to.
345 * The element whose post to get the author for
346 * @returns The ID of the authoring Sone
348 function getPostAuthor(element) {
349 return getPostElement(element).find(".post-author").text();
353 * Returns the element of the reply with the given ID.
356 * The ID of the reply
357 * @returns The element of the reply
359 function getReply(replyId) {
360 return $("#sone .reply#" + replyId);
363 function getReplyElement(element) {
364 return $(element).closest(".reply");
367 function getReplyId(element) {
368 return getReplyElement(element).attr("id");
371 function getReplyTime(element) {
372 return getReplyElement(element).find(".reply-time").text();
376 * Returns the author of the reply the given element belongs to.
379 * The element whose reply to get the author for
380 * @returns The ID of the authoring Sone
382 function getReplyAuthor(element) {
383 return getReplyElement(element).find(".reply-author").text();
387 * Returns the notification with the given ID.
389 * @param notificationId
390 * The ID of the notification
391 * @returns The notification element
393 function getNotification(notificationId) {
394 return $("#sone #notification-area .notification#" + notificationId);
398 * Returns the notification element closest to the given element.
401 * The element to get the closest notification of
402 * @return The closest notification element
404 function getNotificationElement(element) {
405 return $(element).closest(".notification");
409 * Returns the ID of the notification element.
411 * @param notificationElement
412 * The notification element
413 * @returns The ID of the notification
415 function getNotificationId(notificationElement) {
416 return $(notificationElement).attr("id");
419 function likePost(postId) {
420 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
421 if ((data == null) || !data.success) {
424 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
425 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
426 updatePostLikes(postId);
427 }, function(xmlHttpRequest, textStatus, error) {
432 function unlikePost(postId) {
433 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
434 if ((data == null) || !data.success) {
437 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
438 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
439 updatePostLikes(postId);
440 }, function(xmlHttpRequest, textStatus, error) {
445 function updatePostLikes(postId) {
446 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
447 if ((data != null) && data.success) {
448 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
449 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
450 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
452 }, function(xmlHttpRequest, textStatus, error) {
457 function likeReply(replyId) {
458 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
459 if ((data == null) || !data.success) {
462 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
463 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
464 updateReplyLikes(replyId);
465 }, function(xmlHttpRequest, textStatus, error) {
470 function unlikeReply(replyId) {
471 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
472 if ((data == null) || !data.success) {
475 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
476 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
477 updateReplyLikes(replyId);
478 }, function(xmlHttpRequest, textStatus, error) {
484 * Trusts the Sone with the given ID.
487 * The ID of the Sone to trust
489 function trustSone(soneId) {
490 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
491 if ((data != null) && data.success) {
492 updateTrustControls(soneId, data.trustValue);
498 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
501 * The ID of the Sone to distrust
503 function distrustSone(soneId) {
504 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
505 if ((data != null) && data.success) {
506 updateTrustControls(soneId, data.trustValue);
512 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
515 * The ID of the Sone to untrust
517 function untrustSone(soneId) {
518 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
519 if ((data != null) && data.success) {
520 updateTrustControls(soneId, data.trustValue);
526 * Updates the trust controls for all posts and replies of the given Sone,
527 * according to the given trust value.
530 * The ID of the Sone to update all trust controls for
532 * The trust value for the Sone
534 function updateTrustControls(soneId, trustValue) {
535 $("#sone .post").each(function() {
536 if (getPostAuthor(this) == soneId) {
537 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
538 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
539 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
542 $("#sone .reply").each(function() {
543 if (getReplyAuthor(this) == soneId) {
544 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
545 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
546 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
552 * Bookmarks the post with the given ID.
555 * The ID of the post to bookmark
557 function bookmarkPost(postId) {
559 $.getJSON("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
560 if ((data != null) && data.success) {
561 getPost(postId).find(".bookmark").toggleClass("hidden", true);
562 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
569 * Unbookmarks the post with the given ID.
572 * The ID of the post to unbookmark
574 function unbookmarkPost(postId) {
575 $.getJSON("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
576 if ((data != null) && data.success) {
577 getPost(postId).find(".bookmark").toggleClass("hidden", false);
578 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
583 function updateReplyLikes(replyId) {
584 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
585 if ((data != null) && data.success) {
586 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
587 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
588 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
590 }, function(xmlHttpRequest, textStatus, error) {
596 * Posts a reply and calls the given callback when the request finishes.
599 * The ID of the sender
601 * The ID of the post the reply refers to
604 * @param callbackFunction
605 * The callback function to call when the request finishes (takes 3
606 * parameters: success, error, replyId)
608 function postReply(sender, postId, text, callbackFunction) {
609 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
611 /* TODO - show error */
615 callbackFunction(true, null, data.reply, data.sone);
617 callbackFunction(false, data.error);
619 }, function(xmlHttpRequest, textStatus, error) {
625 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
628 * The Sone to ajaxify
630 function ajaxifySone(soneElement) {
632 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
635 $(".follow", soneElement).submit(function() {
636 var followElement = this;
637 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
638 $(followElement).addClass("hidden");
639 $(followElement).parent().find(".unfollow").removeClass("hidden");
643 $(".unfollow", soneElement).submit(function() {
644 var unfollowElement = this;
645 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
646 $(unfollowElement).addClass("hidden");
647 $(unfollowElement).parent().find(".follow").removeClass("hidden");
651 $(".lock", soneElement).submit(function() {
652 var lockElement = this;
653 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
654 $(lockElement).addClass("hidden");
655 $(lockElement).parent().find(".unlock").removeClass("hidden");
659 $(".unlock", soneElement).submit(function() {
660 var unlockElement = this;
661 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
662 $(unlockElement).addClass("hidden");
663 $(unlockElement).parent().find(".lock").removeClass("hidden");
668 /* mark Sone as known when clicking it. */
669 $(soneElement).click(function() {
670 markSoneAsKnown(this);
675 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
678 * The post element to ajaxify
680 function ajaxifyPost(postElement) {
681 $(postElement).find("form").submit(function() {
684 $(postElement).find(".create-reply button:submit").click(function() {
686 button.attr("disabled", "disabled");
687 sender = $(this.form).find(":input[name=sender]").val();
688 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
689 postId = getPostId(this);
690 text = $(inputField).val();
691 (function(sender, postId, text, inputField) {
692 postReply(sender, postId, text, function(success, error, replyId, soneId) {
694 $(inputField).val("");
695 loadNewReply(replyId, soneId, postId);
696 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
697 $("#sone .post#" + postId + " .create-reply .sender").hide();
698 $("#sone .post#" + postId + " .create-reply .select-sender").show();
699 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
703 button.removeAttr("disabled");
705 })(sender, postId, text, inputField);
709 /* replace all “delete” buttons with javascript. */
710 (function(postElement) {
711 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
712 postId = getPostId(postElement);
713 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
717 /* convert all “like” buttons to javascript functions. */
718 $(postElement).find(".like-post").submit(function() {
719 likePost(getPostId(this));
722 $(postElement).find(".unlike-post").submit(function() {
723 unlikePost(getPostId(this));
727 /* convert trust control buttons to javascript functions. */
728 $(postElement).find(".post-trust").submit(function() {
729 trustSone(getPostAuthor(this));
732 $(postElement).find(".post-distrust").submit(function() {
733 distrustSone(getPostAuthor(this));
736 $(postElement).find(".post-untrust").submit(function() {
737 untrustSone(getPostAuthor(this));
741 /* convert bookmark/unbookmark buttons to javascript functions. */
742 $(postElement).find(".bookmark").submit(function() {
743 bookmarkPost(getPostId(this));
746 $(postElement).find(".unbookmark").submit(function() {
747 unbookmarkPost(getPostId(this));
751 /* convert “show source” link into javascript function. */
752 $(postElement).find(".show-source").each(function() {
753 $("a", this).click(function() {
754 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
755 $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
760 /* add “comment” link. */
761 addCommentLink(getPostId(postElement), getPostAuthor(postElement), postElement, $(postElement).find(".post-status-line .time"));
763 /* process all replies. */
765 $(postElement).find(".reply").each(function() {
766 replyIds.push(getReplyId(this));
769 updateReplyTimes(replyIds.join(","));
771 /* process reply input fields. */
772 getTranslation("WebInterface.DefaultText.Reply", function(text) {
773 $(postElement).find("input.reply-input").each(function() {
774 registerInputTextareaSwap(this, text, "text", false, false);
778 /* process sender selection. */
779 $(".select-sender", postElement).css("display", "inline");
780 $(".sender", postElement).hide();
781 $(".select-sender button", postElement).click(function() {
782 $(".sender", postElement).show();
783 $(".select-sender", postElement).hide();
787 /* mark everything as known on click. */
788 $(postElement).click(function(event) {
789 if ($(event.target).hasClass("click-to-show")) {
792 markPostAsKnown(this);
795 /* hide reply input field. */
796 $(postElement).find(".create-reply").addClass("hidden");
800 * Ajaxifies the given reply element.
802 * @param replyElement
803 * The reply element to ajaxify
805 function ajaxifyReply(replyElement) {
806 $(replyElement).find(".like-reply").submit(function() {
807 likeReply(getReplyId(this));
810 $(replyElement).find(".unlike-reply").submit(function() {
811 unlikeReply(getReplyId(this));
814 (function(replyElement) {
815 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
816 $(replyElement).find(".delete-reply button").each(function() {
817 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
821 addCommentLink(getPostId(replyElement), getReplyAuthor(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
823 /* convert “show source” link into javascript function. */
824 $(replyElement).find(".show-reply-source").each(function() {
825 $("a", this).click(function() {
826 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
827 $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
832 /* convert trust control buttons to javascript functions. */
833 $(replyElement).find(".reply-trust").submit(function() {
834 trustSone(getReplyAuthor(this));
837 $(replyElement).find(".reply-distrust").submit(function() {
838 distrustSone(getReplyAuthor(this));
841 $(replyElement).find(".reply-untrust").submit(function() {
842 untrustSone(getReplyAuthor(this));
848 * Ajaxifies the given notification by replacing the form with AJAX.
850 * @param notification
851 * jQuery object representing the notification.
853 function ajaxifyNotification(notification) {
854 notification.find("form").submit(function() {
857 notification.find("input[name=returnPage]").val($.url.attr("relative"));
858 if (notification.find(".short-text").length > 0) {
859 notification.find(".short-text").removeClass("hidden");
860 notification.find(".text").addClass("hidden");
862 notification.find("form.mark-as-read button").click(function() {
863 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
865 notification.find("a[class^='link-']").each(function() {
866 linkElement = $(this);
867 if (linkElement.is("[href^='viewPost']")) {
868 id = linkElement.attr("class").substr(5);
870 linkElement.attr("href", "#post-" + id);
874 notification.find("form.dismiss button").click(function() {
875 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
876 /* dismiss in case of error, too. */
877 notification.slideUp();
878 }, function(xmlHttpRequest, textStatus, error) {
886 * Retrieves element IDs from notification elements.
888 * @param notification
889 * The notification element
891 * The selector of the element containing the ID as text
892 * @returns All extracted IDs
894 function getElementIds(notification, selector) {
896 $(selector, notification).each(function() {
897 elementIds.push($(this).text());
903 * Compares the given notification elements and calls {@link #markSoneAsKnown()}
904 * for every ID that is contained in the old notification but not in the new.
906 * @param oldNotification
907 * The old notification element
908 * @param newNotification
909 * The new notification element
911 function checkForRemovedSones(oldNotification, newNotification) {
912 if (getNotificationId(oldNotification) != "new-sone-notification") {
915 oldIds = getElementIds(oldNotification, ".sone-id");
916 newIds = getElementIds(newNotification, ".sone-id");
917 $.each(oldIds, function(index, value) {
918 if ($.inArray(value, newIds) == -1) {
919 markSoneAsKnown(getSone(value), true);
925 * Compares the given notification elements and calls {@link #markPostAsKnown()}
926 * for every ID that is contained in the old notification but not in the new.
928 * @param oldNotification
929 * The old notification element
930 * @param newNotification
931 * The new notification element
933 function checkForRemovedPosts(oldNotification, newNotification) {
934 if (getNotificationId(oldNotification) != "new-post-notification") {
937 oldIds = getElementIds(oldNotification, ".post-id");
938 newIds = getElementIds(newNotification, ".post-id");
939 $.each(oldIds, function(index, value) {
940 if ($.inArray(value, newIds) == -1) {
941 markPostAsKnown(getPost(value), true);
947 * Compares the given notification elements and calls
948 * {@link #markReplyAsKnown()} for every ID that is contained in the old
949 * notification but not in the new.
951 * @param oldNotification
952 * The old notification element
953 * @param newNotification
954 * The new notification element
956 function checkForRemovedReplies(oldNotification, newNotification) {
957 if (getNotificationId(oldNotification) != "new-replies-notification") {
960 oldIds = getElementIds(oldNotification, ".reply-id");
961 newIds = getElementIds(newNotification, ".reply-id");
962 $.each(oldIds, function(index, value) {
963 if ($.inArray(value, newIds) == -1) {
964 markReplyAsKnown(getReply(value), true);
969 function getStatus() {
970 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
971 if ((data != null) && data.success) {
972 /* process Sone information. */
973 $.each(data.sones, function(index, value) {
974 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
976 /* search for removed notifications. */
977 $("#sone #notification-area .notification").each(function() {
978 notificationId = $(this).attr("id");
979 foundNotification = false;
980 $.each(data.notifications, function(index, value) {
981 if (value.id == notificationId) {
982 foundNotification = true;
986 if (!foundNotification) {
987 if (notificationId == "new-sone-notification") {
988 $(".sone-id", this).each(function(index, element) {
989 soneId = $(this).text();
990 markSoneAsKnown(getSone(soneId), true);
992 } else if (notificationId == "new-post-notification") {
993 $(".post-id", this).each(function(index, element) {
994 postId = $(this).text();
995 markPostAsKnown(getPost(postId), true);
997 } else if (notificationId == "new-replies-notification") {
998 $(".reply-id", this).each(function(index, element) {
999 replyId = $(this).text();
1000 markReplyAsKnown(getReply(replyId), true);
1003 $(this).slideUp("normal", function() {
1005 /* remove activity when no notifications are visible. */
1006 if ($("#sone #notification-area .notification").length == 0) {
1012 /* process notifications. */
1013 $.each(data.notifications, function(index, value) {
1014 oldNotification = getNotification(value.id);
1015 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
1016 if (oldNotification.length != 0) {
1017 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
1018 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
1019 notification.find(".short-text").toggleClass("hidden", opened);
1020 notification.find(".text").toggleClass("hidden", !opened);
1022 checkForRemovedSones(oldNotification, notification);
1023 checkForRemovedPosts(oldNotification, notification);
1024 checkForRemovedReplies(oldNotification, notification);
1025 oldNotification.replaceWith(notification.show());
1027 $("#sone #notification-area").append(notification);
1028 notification.slideDown();
1032 /* process new posts. */
1033 $.each(data.newPosts, function(index, value) {
1034 loadNewPost(value.id, value.sone, value.recipient, value.time);
1036 /* process new replies. */
1037 $.each(data.newReplies, function(index, value) {
1038 loadNewReply(value.id, value.sone, value.post, value.postSone);
1040 /* do it again in 5 seconds. */
1041 setTimeout(getStatus, 5000);
1043 /* data.success was false, wait 30 seconds. */
1044 setTimeout(getStatus, 30000);
1046 }, function(xmlHttpRequest, textStatus, error) {
1047 /* something really bad happend, wait a minute. */
1048 setTimeout(getStatus, 60000);
1053 * Returns the ID of the currently logged in Sone.
1055 * @return The ID of the current Sone, or an empty string if no Sone is logged
1058 function getCurrentSoneId() {
1059 return $("#currentSoneId").text();
1063 * Returns the content of the page-id attribute.
1065 * @returns The page ID
1067 function getPageId() {
1068 return $("#sone .page-id").text();
1072 * Returns whether the current page is the index page.
1074 * @returns {Boolean} <code>true</code> if the current page is the index page,
1075 * <code>false</code> otherwise
1077 function isIndexPage() {
1078 return getPageId() == "index";
1082 * Returns whether the current page is a “view Sone” page.
1084 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
1085 * page, <code>false</code> otherwise
1087 function isViewSonePage() {
1088 return getPageId() == "view-sone";
1092 * Returns the ID of the currently shown Sone. This will only return a sensible
1093 * value if isViewSonePage() returns <code>true</code>.
1095 * @returns The ID of the currently shown Sone
1097 function getShownSoneId() {
1098 return $("#sone .sone-id").text();
1102 * Returns whether the current page is a “view post” page.
1104 * @returns {Boolean} <code>true</code> if the current page is a “view post”
1105 * page, <code>false</code> otherwise
1107 function isViewPostPage() {
1108 return getPageId() == "view-post";
1112 * Returns the ID of the currently shown post. This will only return a sensible
1113 * value if isViewPostPage() returns <code>true</code>.
1115 * @returns The ID of the currently shown post
1117 function getShownPostId() {
1118 return $("#sone .post-id").text();
1122 * Returns whether the current page is the “known Sones” page.
1124 * @returns {Boolean} <code>true</code> if the current page is the “known
1125 * Sones” page, <code>false</code> otherwise
1127 function isKnownSonesPage() {
1128 return getPageId() == "known-sones";
1132 * Returns whether a post with the given ID exists on the current page.
1135 * The post ID to check for
1136 * @returns {Boolean} <code>true</code> if a post with the given ID already
1137 * exists on the page, <code>false</code> otherwise
1139 function hasPost(postId) {
1140 return $(".post#" + postId).length > 0;
1144 * Returns whether a reply with the given ID exists on the current page.
1147 * The reply ID to check for
1148 * @returns {Boolean} <code>true</code> if a reply with the given ID already
1149 * exists on the page, <code>false</code> otherwise
1151 function hasReply(replyId) {
1152 return $("#sone .reply#" + replyId).length > 0;
1155 function loadNewPost(postId, soneId, recipientId, time) {
1156 if (hasPost(postId)) {
1159 if (!isIndexPage()) {
1160 if (!isViewPostPage() || (getShownPostId() != postId)) {
1161 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1166 if (getPostTime($("#sone .post").last()) > time) {
1169 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1170 if ((data != null) && data.success) {
1171 if (hasPost(data.post.id)) {
1174 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1177 var firstOlderPost = null;
1178 $("#sone .post").each(function() {
1179 if (getPostTime(this) < data.post.time) {
1180 firstOlderPost = $(this);
1184 newPost = $(data.post.html).addClass("hidden");
1185 if (firstOlderPost != null) {
1186 newPost.insertBefore(firstOlderPost);
1188 ajaxifyPost(newPost);
1189 updatePostTimes(data.post.id);
1190 newPost.slideDown();
1196 function loadNewReply(replyId, soneId, postId, postSoneId) {
1197 if (hasReply(replyId)) {
1200 if (!hasPost(postId)) {
1203 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1205 if ((data != null) && data.success) {
1206 if (hasReply(data.reply.id)) {
1209 $("#sone .post#" + data.reply.postId).each(function() {
1210 var firstNewerReply = null;
1211 $(this).find(".replies .reply").each(function() {
1212 if (getReplyTime(this) > data.reply.time) {
1213 firstNewerReply = $(this);
1217 newReply = $(data.reply.html).addClass("hidden");
1218 if (firstNewerReply != null) {
1219 newReply.insertBefore(firstNewerReply);
1221 if ($(this).find(".replies .create-reply")) {
1222 $(this).find(".replies .create-reply").before(newReply);
1224 $(this).find(".replies").append(newReply);
1227 ajaxifyReply(newReply);
1228 updateReplyTimes(data.reply.id);
1229 newReply.slideDown();
1238 * Marks the given Sone as known if it is still new.
1240 * @param soneElement
1241 * The Sone to mark as known
1242 * @param skipRequest
1243 * true to skip the JSON request, false or omit to perform the JSON
1246 function markSoneAsKnown(soneElement, skipRequest) {
1247 if ($(".new", soneElement).length > 0) {
1248 if ((typeof skipRequest != "undefined") && !skipRequest) {
1249 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1250 $(soneElement).removeClass("new");
1256 function markPostAsKnown(postElements, skipRequest) {
1257 $(postElements).each(function() {
1259 if ($(postElement).hasClass("new")) {
1260 (function(postElement) {
1261 $(postElement).removeClass("new");
1262 $(".click-to-show", postElement).removeClass("new");
1263 if ((typeof skipRequest == "undefined") || !skipRequest) {
1264 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1269 markReplyAsKnown($(postElements).find(".reply"));
1272 function markReplyAsKnown(replyElements, skipRequest) {
1273 $(replyElements).each(function() {
1274 replyElement = this;
1275 if ($(replyElement).hasClass("new")) {
1276 (function(replyElement) {
1277 $(replyElement).removeClass("new");
1278 if ((typeof skipRequest == "undefined") || !skipRequest) {
1279 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1287 * Updates the time of the post with the given ID.
1290 * The ID of the post to update
1292 * The text of the time to show
1293 * @param refreshTime
1294 * The refresh time after which to request a new time (in seconds)
1296 * The tooltip to show
1298 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1299 if (!getPost(postId).is(":visible")) {
1302 getPost(postId).find(".post-status-line > .time a").html(timeText).attr("title", tooltip);
1303 (function(postId, refreshTime) {
1304 setTimeout(function() {
1305 updatePostTimes(postId);
1306 }, refreshTime * 1000);
1307 })(postId, refreshTime);
1311 * Requests new rendered times for the posts with the given IDs.
1314 * Comma-separated post IDs
1316 function updatePostTimes(postIds) {
1317 $.getJSON("getTimes.ajax", { "posts" : postIds }, function(data, textStatus) {
1318 if ((data != null) && data.success) {
1319 $.each(data.postTimes, function(index, value) {
1320 updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1327 * Updates the time of the reply with the given ID.
1330 * The ID of the reply to update
1332 * The text of the time to show
1333 * @param refreshTime
1334 * The refresh time after which to request a new time (in seconds)
1336 * The tooltip to show
1338 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1339 getReply(replyId).find(".reply-status-line > .time").html(timeText).attr("title", tooltip);
1340 (function(replyId, refreshTime) {
1341 setTimeout(function() {
1342 updateReplyTimes(replyId);
1343 }, refreshTime * 1000);
1344 })(replyId, refreshTime);
1348 * Requests new rendered times for the posts with the given IDs.
1351 * Comma-separated post IDs
1353 function updateReplyTimes(replyIds) {
1354 $.getJSON("getTimes.ajax", { "replies" : replyIds }, function(data, textStatus) {
1355 if ((data != null) && data.success) {
1356 $.each(data.replyTimes, function(index, value) {
1357 updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1363 function resetActivity() {
1364 title = document.title;
1365 if (title.indexOf('(') == 0) {
1366 setTitle(title.substr(title.indexOf(' ') + 1));
1368 iconBlinking = false;
1371 function setActivity() {
1373 title = document.title;
1374 if (title.indexOf('(') != 0) {
1375 setTitle("(!) " + title);
1377 if (!iconBlinking) {
1378 setTimeout(toggleIcon, 1500);
1379 iconBlinking = true;
1385 * Sets the window title after a small delay to prevent race-condition issues.
1390 function setTitle(title) {
1391 setTimeout(function() {
1392 document.title = title;
1396 /** Whether the icon is currently showing activity. */
1397 var iconActive = false;
1399 /** Whether the icon is currently supposed to blink. */
1400 var iconBlinking = false;
1403 * Toggles the icon. If the window has gained focus and the icon is still
1404 * showing the activity state, it is returned to normal.
1406 function toggleIcon() {
1407 if (focus || !iconBlinking) {
1409 changeIcon("images/icon.png");
1412 iconBlinking = false;
1414 iconActive = !iconActive;
1415 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1416 setTimeout(toggleIcon, 1500);
1421 * Changes the icon of the page.
1424 * The new URL of the icon
1426 function changeIcon(iconUrl) {
1427 $("link[rel=icon]").remove();
1428 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1429 $("iframe[id=icon-update]")[0].src += "";
1433 * Creates a new notification.
1436 * The ID of the notificaiton
1438 * The text of the notification
1439 * @param dismissable
1440 * <code>true</code> if the notification can be dismissed by the
1443 function createNotification(id, text, dismissable) {
1444 notification = $("<div></div>").addClass("notification").attr("id", id);
1446 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1447 dismissForm.find("input[name=notification]").val(id);
1448 notification.append(dismissForm);
1450 notification.append(text);
1451 return notification;
1455 * Shows the details of the notification with the given ID.
1457 * @param notificationId
1458 * The ID of the notification
1460 function showNotificationDetails(notificationId) {
1461 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1462 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1466 * Deletes the field with the given ID from the profile.
1469 * The ID of the field to delete
1471 function deleteProfileField(fieldId) {
1472 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1473 if (data && data.success) {
1474 $("#sone .profile-field#" + data.field.id).slideUp();
1480 * Renames a profile field.
1483 * The ID of the field to rename
1485 * The new name of the field
1486 * @param successFunction
1487 * Called when the renaming was successful
1489 function editProfileField(fieldId, newName, successFunction) {
1490 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1491 if (data && data.success) {
1498 * Moves the profile field with the given ID one slot in the given direction.
1501 * The ID of the field to move
1503 * The direction to move in (“up” or “down”)
1504 * @param successFunction
1505 * Function to call on success
1507 function moveProfileField(fieldId, direction, successFunction) {
1508 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1509 if (data && data.success) {
1516 * Moves the profile field with the given ID up one slot.
1519 * The ID of the field to move
1520 * @param successFunction
1521 * Function to call on success
1523 function moveProfileFieldUp(fieldId, successFunction) {
1524 moveProfileField(fieldId, "up", successFunction);
1528 * Moves the profile field with the given ID down one slot.
1531 * The ID of the field to move
1532 * @param successFunction
1533 * Function to call on success
1535 function moveProfileFieldDown(fieldId, successFunction) {
1536 moveProfileField(fieldId, "down", successFunction);
1540 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1545 $(document).ready(function() {
1547 /* this initializes the status update input field. */
1548 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1549 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1550 $("#sone #update-status .select-sender").css("display", "inline");
1551 $("#sone #update-status .sender").hide();
1552 $("#sone #update-status .select-sender button").click(function() {
1553 $("#sone #update-status .sender").show();
1554 $("#sone #update-status .select-sender").hide();
1557 $("#sone #update-status").submit(function() {
1558 button = $("button:submit", this);
1559 button.attr("disabled", "disabled");
1560 if ($(this).find(":input.default:enabled").length > 0) {
1563 sender = $(this).find(":input[name=sender]").val();
1564 text = $(this).find(":input[name=text]:enabled").val();
1565 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1566 button.removeAttr("disabled");
1568 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1569 $(this).find(":input[name=text]:enabled").val("").blur();
1570 $(this).find(".sender").hide();
1571 $(this).find(".select-sender").show();
1576 /* ajaxify the search input field. */
1577 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1578 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1581 /* ajaxify input field on “view Sone” page. */
1582 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1583 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1584 $("#sone #post-message .select-sender").css("display", "inline");
1585 $("#sone #post-message .sender").hide();
1586 $("#sone #post-message .select-sender button").click(function() {
1587 $("#sone #post-message .sender").show();
1588 $("#sone #post-message .select-sender").hide();
1591 $("#sone #post-message").submit(function() {
1592 sender = $(this).find(":input[name=sender]").val();
1593 text = $(this).find(":input[name=text]:enabled").val();
1594 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text });
1595 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1596 $(this).find(":input[name=text]:enabled").val("").blur();
1597 $(this).find(".sender").hide();
1598 $(this).find(".select-sender").show();
1603 /* Ajaxifies all posts. */
1604 /* calling getTranslation here will cache the necessary values. */
1605 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1606 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1607 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1608 $("#sone .post").each(function() {
1615 /* update post times. */
1617 $("#sone .post").each(function() {
1618 postIds.push(getPostId(this));
1620 updatePostTimes(postIds.join(","));
1622 /* hides all replies but the latest two. */
1623 if (!isViewPostPage()) {
1624 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1625 $("#sone .post .replies").each(function() {
1626 allReplies = $(this).find(".reply");
1627 if (allReplies.length > 2) {
1629 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1630 $(allReplies[replyIndex]).addClass("hidden");
1631 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1633 clickToShowElement = $("<div></div>").addClass("click-to-show");
1635 clickToShowElement.addClass("new");
1637 (function(clickToShowElement, allReplies, text) {
1638 clickToShowElement.text(text);
1639 clickToShowElement.click(function() {
1640 allReplies.removeClass("hidden");
1641 clickToShowElement.addClass("hidden");
1643 })(clickToShowElement, allReplies, text);
1644 $(allReplies[0]).before(clickToShowElement);
1650 $("#sone .sone").each(function() {
1651 ajaxifySone($(this));
1654 /* process all existing notifications, ajaxify dismiss buttons. */
1655 $("#sone #notification-area .notification").each(function() {
1656 ajaxifyNotification($(this));
1659 /* activate status polling. */
1660 setTimeout(getStatus, 5000);
1662 /* reset activity counter when the page has focus. */
1663 $(window).focus(function() {
1666 }).blur(function() {