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");
420 * Returns the time the notification was last updated.
422 * @param notificationElement
423 * The notification element
424 * @returns The last update time of the notification
426 function getNotificationLastUpdatedTime(notificationElement) {
427 return $(notificationElement).attr("lastUpdatedTime");
430 function likePost(postId) {
431 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
432 if ((data == null) || !data.success) {
435 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
436 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
437 updatePostLikes(postId);
438 }, function(xmlHttpRequest, textStatus, error) {
443 function unlikePost(postId) {
444 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
445 if ((data == null) || !data.success) {
448 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
449 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
450 updatePostLikes(postId);
451 }, function(xmlHttpRequest, textStatus, error) {
456 function updatePostLikes(postId) {
457 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
458 if ((data != null) && data.success) {
459 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
460 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
461 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
463 }, function(xmlHttpRequest, textStatus, error) {
468 function likeReply(replyId) {
469 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
470 if ((data == null) || !data.success) {
473 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
474 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
475 updateReplyLikes(replyId);
476 }, function(xmlHttpRequest, textStatus, error) {
481 function unlikeReply(replyId) {
482 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
483 if ((data == null) || !data.success) {
486 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
487 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
488 updateReplyLikes(replyId);
489 }, function(xmlHttpRequest, textStatus, error) {
495 * Trusts the Sone with the given ID.
498 * The ID of the Sone to trust
500 function trustSone(soneId) {
501 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
502 if ((data != null) && data.success) {
503 updateTrustControls(soneId, data.trustValue);
509 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
512 * The ID of the Sone to distrust
514 function distrustSone(soneId) {
515 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
516 if ((data != null) && data.success) {
517 updateTrustControls(soneId, data.trustValue);
523 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
526 * The ID of the Sone to untrust
528 function untrustSone(soneId) {
529 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
530 if ((data != null) && data.success) {
531 updateTrustControls(soneId, data.trustValue);
537 * Updates the trust controls for all posts and replies of the given Sone,
538 * according to the given trust value.
541 * The ID of the Sone to update all trust controls for
543 * The trust value for the Sone
545 function updateTrustControls(soneId, trustValue) {
546 $("#sone .post").each(function() {
547 if (getPostAuthor(this) == soneId) {
548 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
549 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
550 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
553 $("#sone .reply").each(function() {
554 if (getReplyAuthor(this) == soneId) {
555 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
556 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
557 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
563 * Bookmarks the post with the given ID.
566 * The ID of the post to bookmark
568 function bookmarkPost(postId) {
570 $.getJSON("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
571 if ((data != null) && data.success) {
572 getPost(postId).find(".bookmark").toggleClass("hidden", true);
573 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
580 * Unbookmarks the post with the given ID.
583 * The ID of the post to unbookmark
585 function unbookmarkPost(postId) {
586 $.getJSON("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
587 if ((data != null) && data.success) {
588 getPost(postId).find(".bookmark").toggleClass("hidden", false);
589 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
594 function updateReplyLikes(replyId) {
595 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
596 if ((data != null) && data.success) {
597 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
598 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
599 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
601 }, function(xmlHttpRequest, textStatus, error) {
607 * Posts a reply and calls the given callback when the request finishes.
610 * The ID of the sender
612 * The ID of the post the reply refers to
615 * @param callbackFunction
616 * The callback function to call when the request finishes (takes 3
617 * parameters: success, error, replyId)
619 function postReply(sender, postId, text, callbackFunction) {
620 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
622 /* TODO - show error */
626 callbackFunction(true, null, data.reply, data.sone);
628 callbackFunction(false, data.error);
630 }, function(xmlHttpRequest, textStatus, error) {
636 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
639 * The Sone to ajaxify
641 function ajaxifySone(soneElement) {
643 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
646 $(".follow", soneElement).submit(function() {
647 var followElement = this;
648 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
649 $(followElement).addClass("hidden");
650 $(followElement).parent().find(".unfollow").removeClass("hidden");
654 $(".unfollow", soneElement).submit(function() {
655 var unfollowElement = this;
656 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
657 $(unfollowElement).addClass("hidden");
658 $(unfollowElement).parent().find(".follow").removeClass("hidden");
662 $(".lock", soneElement).submit(function() {
663 var lockElement = this;
664 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
665 $(lockElement).addClass("hidden");
666 $(lockElement).parent().find(".unlock").removeClass("hidden");
670 $(".unlock", soneElement).submit(function() {
671 var unlockElement = this;
672 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
673 $(unlockElement).addClass("hidden");
674 $(unlockElement).parent().find(".lock").removeClass("hidden");
679 /* mark Sone as known when clicking it. */
680 $(soneElement).click(function() {
681 markSoneAsKnown(this);
686 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
689 * The post element to ajaxify
691 function ajaxifyPost(postElement) {
692 $(postElement).find("form").submit(function() {
695 $(postElement).find(".create-reply button:submit").click(function() {
697 button.attr("disabled", "disabled");
698 sender = $(this.form).find(":input[name=sender]").val();
699 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
700 postId = getPostId(this);
701 text = $(inputField).val();
702 (function(sender, postId, text, inputField) {
703 postReply(sender, postId, text, function(success, error, replyId, soneId) {
705 $(inputField).val("");
706 loadNewReply(replyId, soneId, postId);
707 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
708 $("#sone .post#" + postId + " .create-reply .sender").hide();
709 $("#sone .post#" + postId + " .create-reply .select-sender").show();
710 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
714 button.removeAttr("disabled");
716 })(sender, postId, text, inputField);
720 /* replace all “delete” buttons with javascript. */
721 (function(postElement) {
722 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
723 postId = getPostId(postElement);
724 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
728 /* convert all “like” buttons to javascript functions. */
729 $(postElement).find(".like-post").submit(function() {
730 likePost(getPostId(this));
733 $(postElement).find(".unlike-post").submit(function() {
734 unlikePost(getPostId(this));
738 /* convert trust control buttons to javascript functions. */
739 $(postElement).find(".post-trust").submit(function() {
740 trustSone(getPostAuthor(this));
743 $(postElement).find(".post-distrust").submit(function() {
744 distrustSone(getPostAuthor(this));
747 $(postElement).find(".post-untrust").submit(function() {
748 untrustSone(getPostAuthor(this));
752 /* convert bookmark/unbookmark buttons to javascript functions. */
753 $(postElement).find(".bookmark").submit(function() {
754 bookmarkPost(getPostId(this));
757 $(postElement).find(".unbookmark").submit(function() {
758 unbookmarkPost(getPostId(this));
762 /* convert “show source” link into javascript function. */
763 $(postElement).find(".show-source").each(function() {
764 $("a", this).click(function() {
765 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
766 $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
771 /* add “comment” link. */
772 addCommentLink(getPostId(postElement), getPostAuthor(postElement), postElement, $(postElement).find(".post-status-line .time"));
774 /* process all replies. */
776 $(postElement).find(".reply").each(function() {
777 replyIds.push(getReplyId(this));
780 updateReplyTimes(replyIds.join(","));
782 /* process reply input fields. */
783 getTranslation("WebInterface.DefaultText.Reply", function(text) {
784 $(postElement).find("input.reply-input").each(function() {
785 registerInputTextareaSwap(this, text, "text", false, false);
789 /* process sender selection. */
790 $(".select-sender", postElement).css("display", "inline");
791 $(".sender", postElement).hide();
792 $(".select-sender button", postElement).click(function() {
793 $(".sender", postElement).show();
794 $(".select-sender", postElement).hide();
798 /* mark everything as known on click. */
799 $(postElement).click(function(event) {
800 if ($(event.target).hasClass("click-to-show")) {
803 markPostAsKnown(this);
806 /* hide reply input field. */
807 $(postElement).find(".create-reply").addClass("hidden");
811 * Ajaxifies the given reply element.
813 * @param replyElement
814 * The reply element to ajaxify
816 function ajaxifyReply(replyElement) {
817 $(replyElement).find(".like-reply").submit(function() {
818 likeReply(getReplyId(this));
821 $(replyElement).find(".unlike-reply").submit(function() {
822 unlikeReply(getReplyId(this));
825 (function(replyElement) {
826 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
827 $(replyElement).find(".delete-reply button").each(function() {
828 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
832 addCommentLink(getPostId(replyElement), getReplyAuthor(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
834 /* convert “show source” link into javascript function. */
835 $(replyElement).find(".show-reply-source").each(function() {
836 $("a", this).click(function() {
837 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
838 $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
843 /* convert trust control buttons to javascript functions. */
844 $(replyElement).find(".reply-trust").submit(function() {
845 trustSone(getReplyAuthor(this));
848 $(replyElement).find(".reply-distrust").submit(function() {
849 distrustSone(getReplyAuthor(this));
852 $(replyElement).find(".reply-untrust").submit(function() {
853 untrustSone(getReplyAuthor(this));
859 * Ajaxifies the given notification by replacing the form with AJAX.
861 * @param notification
862 * jQuery object representing the notification.
864 function ajaxifyNotification(notification) {
865 notification.find("form").submit(function() {
868 notification.find("input[name=returnPage]").val($.url.attr("relative"));
869 if (notification.find(".short-text").length > 0) {
870 notification.find(".short-text").removeClass("hidden");
871 notification.find(".text").addClass("hidden");
873 notification.find("form.mark-as-read button").click(function() {
874 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
876 notification.find("a[class^='link-']").each(function() {
877 linkElement = $(this);
878 if (linkElement.is("[href^='viewPost']")) {
879 id = linkElement.attr("class").substr(5);
881 linkElement.attr("href", "#post-" + id);
885 notification.find("form.dismiss button").click(function() {
886 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
887 /* dismiss in case of error, too. */
888 notification.slideUp();
889 }, function(xmlHttpRequest, textStatus, error) {
897 * Retrieves element IDs from notification elements.
899 * @param notification
900 * The notification element
902 * The selector of the element containing the ID as text
903 * @returns All extracted IDs
905 function getElementIds(notification, selector) {
907 $(selector, notification).each(function() {
908 elementIds.push($(this).text());
914 * Compares the given notification elements and calls {@link #markSoneAsKnown()}
915 * for every ID that is contained in the old notification but not in the new.
917 * @param oldNotification
918 * The old notification element
919 * @param newNotification
920 * The new notification element
922 function checkForRemovedSones(oldNotification, newNotification) {
923 if (getNotificationId(oldNotification) != "new-sone-notification") {
926 oldIds = getElementIds(oldNotification, ".sone-id");
927 newIds = getElementIds(newNotification, ".sone-id");
928 $.each(oldIds, function(index, value) {
929 if ($.inArray(value, newIds) == -1) {
930 markSoneAsKnown(getSone(value), true);
936 * Compares the given notification elements and calls {@link #markPostAsKnown()}
937 * for every ID that is contained in the old notification but not in the new.
939 * @param oldNotification
940 * The old notification element
941 * @param newNotification
942 * The new notification element
944 function checkForRemovedPosts(oldNotification, newNotification) {
945 if (getNotificationId(oldNotification) != "new-post-notification") {
948 oldIds = getElementIds(oldNotification, ".post-id");
949 newIds = getElementIds(newNotification, ".post-id");
950 $.each(oldIds, function(index, value) {
951 if ($.inArray(value, newIds) == -1) {
952 markPostAsKnown(getPost(value), true);
958 * Compares the given notification elements and calls
959 * {@link #markReplyAsKnown()} for every ID that is contained in the old
960 * notification but not in the new.
962 * @param oldNotification
963 * The old notification element
964 * @param newNotification
965 * The new notification element
967 function checkForRemovedReplies(oldNotification, newNotification) {
968 if (getNotificationId(oldNotification) != "new-replies-notification") {
971 oldIds = getElementIds(oldNotification, ".reply-id");
972 newIds = getElementIds(newNotification, ".reply-id");
973 $.each(oldIds, function(index, value) {
974 if ($.inArray(value, newIds) == -1) {
975 markReplyAsKnown(getReply(value), true);
980 function getStatus() {
981 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
982 if ((data != null) && data.success) {
983 /* process Sone information. */
984 $.each(data.sones, function(index, value) {
985 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
987 /* search for removed notifications. */
988 $("#sone #notification-area .notification").each(function() {
989 notificationId = $(this).attr("id");
990 foundNotification = false;
991 $.each(data.notifications, function(index, value) {
992 if (value.id == notificationId) {
993 foundNotification = true;
997 if (!foundNotification) {
998 if (notificationId == "new-sone-notification") {
999 $(".sone-id", this).each(function(index, element) {
1000 soneId = $(this).text();
1001 markSoneAsKnown(getSone(soneId), true);
1003 } else if (notificationId == "new-post-notification") {
1004 $(".post-id", this).each(function(index, element) {
1005 postId = $(this).text();
1006 markPostAsKnown(getPost(postId), true);
1008 } else if (notificationId == "new-replies-notification") {
1009 $(".reply-id", this).each(function(index, element) {
1010 replyId = $(this).text();
1011 markReplyAsKnown(getReply(replyId), true);
1014 $(this).slideUp("normal", function() {
1016 /* remove activity when no notifications are visible. */
1017 if ($("#sone #notification-area .notification").length == 0) {
1023 /* process notifications. */
1024 $.each(data.notifications, function(index, value) {
1025 oldNotification = getNotification(value.id);
1026 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
1027 if (oldNotification.length != 0) {
1028 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
1029 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
1030 notification.find(".short-text").toggleClass("hidden", opened);
1031 notification.find(".text").toggleClass("hidden", !opened);
1033 checkForRemovedSones(oldNotification, notification);
1034 checkForRemovedPosts(oldNotification, notification);
1035 checkForRemovedReplies(oldNotification, notification);
1036 oldNotification.replaceWith(notification.show());
1038 $("#sone #notification-area").append(notification);
1039 notification.slideDown();
1043 /* process new posts. */
1044 $.each(data.newPosts, function(index, value) {
1045 loadNewPost(value.id, value.sone, value.recipient, value.time);
1047 /* process new replies. */
1048 $.each(data.newReplies, function(index, value) {
1049 loadNewReply(value.id, value.sone, value.post, value.postSone);
1051 /* do it again in 5 seconds. */
1052 setTimeout(getStatus, 5000);
1054 /* data.success was false, wait 30 seconds. */
1055 setTimeout(getStatus, 30000);
1057 }, function(xmlHttpRequest, textStatus, error) {
1058 /* something really bad happend, wait a minute. */
1059 setTimeout(getStatus, 60000);
1064 * Returns the ID of the currently logged in Sone.
1066 * @return The ID of the current Sone, or an empty string if no Sone is logged
1069 function getCurrentSoneId() {
1070 return $("#currentSoneId").text();
1074 * Returns the content of the page-id attribute.
1076 * @returns The page ID
1078 function getPageId() {
1079 return $("#sone .page-id").text();
1083 * Returns whether the current page is the index page.
1085 * @returns {Boolean} <code>true</code> if the current page is the index page,
1086 * <code>false</code> otherwise
1088 function isIndexPage() {
1089 return getPageId() == "index";
1093 * Returns whether the current page is a “view Sone” page.
1095 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
1096 * page, <code>false</code> otherwise
1098 function isViewSonePage() {
1099 return getPageId() == "view-sone";
1103 * Returns the ID of the currently shown Sone. This will only return a sensible
1104 * value if isViewSonePage() returns <code>true</code>.
1106 * @returns The ID of the currently shown Sone
1108 function getShownSoneId() {
1109 return $("#sone .sone-id").text();
1113 * Returns whether the current page is a “view post” page.
1115 * @returns {Boolean} <code>true</code> if the current page is a “view post”
1116 * page, <code>false</code> otherwise
1118 function isViewPostPage() {
1119 return getPageId() == "view-post";
1123 * Returns the ID of the currently shown post. This will only return a sensible
1124 * value if isViewPostPage() returns <code>true</code>.
1126 * @returns The ID of the currently shown post
1128 function getShownPostId() {
1129 return $("#sone .post-id").text();
1133 * Returns whether the current page is the “known Sones” page.
1135 * @returns {Boolean} <code>true</code> if the current page is the “known
1136 * Sones” page, <code>false</code> otherwise
1138 function isKnownSonesPage() {
1139 return getPageId() == "known-sones";
1143 * Returns whether a post with the given ID exists on the current page.
1146 * The post ID to check for
1147 * @returns {Boolean} <code>true</code> if a post with the given ID already
1148 * exists on the page, <code>false</code> otherwise
1150 function hasPost(postId) {
1151 return $(".post#" + postId).length > 0;
1155 * Returns whether a reply with the given ID exists on the current page.
1158 * The reply ID to check for
1159 * @returns {Boolean} <code>true</code> if a reply with the given ID already
1160 * exists on the page, <code>false</code> otherwise
1162 function hasReply(replyId) {
1163 return $("#sone .reply#" + replyId).length > 0;
1166 function loadNewPost(postId, soneId, recipientId, time) {
1167 if (hasPost(postId)) {
1170 if (!isIndexPage()) {
1171 if (!isViewPostPage() || (getShownPostId() != postId)) {
1172 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1177 if (getPostTime($("#sone .post").last()) > time) {
1180 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1181 if ((data != null) && data.success) {
1182 if (hasPost(data.post.id)) {
1185 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1188 var firstOlderPost = null;
1189 $("#sone .post").each(function() {
1190 if (getPostTime(this) < data.post.time) {
1191 firstOlderPost = $(this);
1195 newPost = $(data.post.html).addClass("hidden");
1196 if (firstOlderPost != null) {
1197 newPost.insertBefore(firstOlderPost);
1199 ajaxifyPost(newPost);
1200 updatePostTimes(data.post.id);
1201 newPost.slideDown();
1207 function loadNewReply(replyId, soneId, postId, postSoneId) {
1208 if (hasReply(replyId)) {
1211 if (!hasPost(postId)) {
1214 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1216 if ((data != null) && data.success) {
1217 if (hasReply(data.reply.id)) {
1220 $("#sone .post#" + data.reply.postId).each(function() {
1221 var firstNewerReply = null;
1222 $(this).find(".replies .reply").each(function() {
1223 if (getReplyTime(this) > data.reply.time) {
1224 firstNewerReply = $(this);
1228 newReply = $(data.reply.html).addClass("hidden");
1229 if (firstNewerReply != null) {
1230 newReply.insertBefore(firstNewerReply);
1232 if ($(this).find(".replies .create-reply")) {
1233 $(this).find(".replies .create-reply").before(newReply);
1235 $(this).find(".replies").append(newReply);
1238 ajaxifyReply(newReply);
1239 updateReplyTimes(data.reply.id);
1240 newReply.slideDown();
1249 * Marks the given Sone as known if it is still new.
1251 * @param soneElement
1252 * The Sone to mark as known
1253 * @param skipRequest
1254 * true to skip the JSON request, false or omit to perform the JSON
1257 function markSoneAsKnown(soneElement, skipRequest) {
1258 if ($(".new", soneElement).length > 0) {
1259 if ((typeof skipRequest != "undefined") && !skipRequest) {
1260 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1261 $(soneElement).removeClass("new");
1267 function markPostAsKnown(postElements, skipRequest) {
1268 $(postElements).each(function() {
1270 if ($(postElement).hasClass("new")) {
1271 (function(postElement) {
1272 $(postElement).removeClass("new");
1273 $(".click-to-show", postElement).removeClass("new");
1274 if ((typeof skipRequest == "undefined") || !skipRequest) {
1275 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1280 markReplyAsKnown($(postElements).find(".reply"));
1283 function markReplyAsKnown(replyElements, skipRequest) {
1284 $(replyElements).each(function() {
1285 replyElement = this;
1286 if ($(replyElement).hasClass("new")) {
1287 (function(replyElement) {
1288 $(replyElement).removeClass("new");
1289 if ((typeof skipRequest == "undefined") || !skipRequest) {
1290 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1298 * Updates the time of the post with the given ID.
1301 * The ID of the post to update
1303 * The text of the time to show
1304 * @param refreshTime
1305 * The refresh time after which to request a new time (in seconds)
1307 * The tooltip to show
1309 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1310 if (!getPost(postId).is(":visible")) {
1313 getPost(postId).find(".post-status-line > .time a").html(timeText).attr("title", tooltip);
1314 (function(postId, refreshTime) {
1315 setTimeout(function() {
1316 updatePostTimes(postId);
1317 }, refreshTime * 1000);
1318 })(postId, refreshTime);
1322 * Requests new rendered times for the posts with the given IDs.
1325 * Comma-separated post IDs
1327 function updatePostTimes(postIds) {
1328 $.getJSON("getTimes.ajax", { "posts" : postIds }, function(data, textStatus) {
1329 if ((data != null) && data.success) {
1330 $.each(data.postTimes, function(index, value) {
1331 updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1338 * Updates the time of the reply with the given ID.
1341 * The ID of the reply to update
1343 * The text of the time to show
1344 * @param refreshTime
1345 * The refresh time after which to request a new time (in seconds)
1347 * The tooltip to show
1349 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1350 getReply(replyId).find(".reply-status-line > .time").html(timeText).attr("title", tooltip);
1351 (function(replyId, refreshTime) {
1352 setTimeout(function() {
1353 updateReplyTimes(replyId);
1354 }, refreshTime * 1000);
1355 })(replyId, refreshTime);
1359 * Requests new rendered times for the posts with the given IDs.
1362 * Comma-separated post IDs
1364 function updateReplyTimes(replyIds) {
1365 $.getJSON("getTimes.ajax", { "replies" : replyIds }, function(data, textStatus) {
1366 if ((data != null) && data.success) {
1367 $.each(data.replyTimes, function(index, value) {
1368 updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1374 function resetActivity() {
1375 title = document.title;
1376 if (title.indexOf('(') == 0) {
1377 setTitle(title.substr(title.indexOf(' ') + 1));
1379 iconBlinking = false;
1382 function setActivity() {
1384 title = document.title;
1385 if (title.indexOf('(') != 0) {
1386 setTitle("(!) " + title);
1388 if (!iconBlinking) {
1389 setTimeout(toggleIcon, 1500);
1390 iconBlinking = true;
1396 * Sets the window title after a small delay to prevent race-condition issues.
1401 function setTitle(title) {
1402 setTimeout(function() {
1403 document.title = title;
1407 /** Whether the icon is currently showing activity. */
1408 var iconActive = false;
1410 /** Whether the icon is currently supposed to blink. */
1411 var iconBlinking = false;
1414 * Toggles the icon. If the window has gained focus and the icon is still
1415 * showing the activity state, it is returned to normal.
1417 function toggleIcon() {
1418 if (focus || !iconBlinking) {
1420 changeIcon("images/icon.png");
1423 iconBlinking = false;
1425 iconActive = !iconActive;
1426 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1427 setTimeout(toggleIcon, 1500);
1432 * Changes the icon of the page.
1435 * The new URL of the icon
1437 function changeIcon(iconUrl) {
1438 $("link[rel=icon]").remove();
1439 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1440 $("iframe[id=icon-update]")[0].src += "";
1444 * Creates a new notification.
1447 * The ID of the notificaiton
1449 * The text of the notification
1450 * @param dismissable
1451 * <code>true</code> if the notification can be dismissed by the
1454 function createNotification(id, text, dismissable) {
1455 notification = $("<div></div>").addClass("notification").attr("id", id);
1457 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1458 dismissForm.find("input[name=notification]").val(id);
1459 notification.append(dismissForm);
1461 notification.append(text);
1462 return notification;
1466 * Shows the details of the notification with the given ID.
1468 * @param notificationId
1469 * The ID of the notification
1471 function showNotificationDetails(notificationId) {
1472 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1473 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1477 * Deletes the field with the given ID from the profile.
1480 * The ID of the field to delete
1482 function deleteProfileField(fieldId) {
1483 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1484 if (data && data.success) {
1485 $("#sone .profile-field#" + data.field.id).slideUp();
1491 * Renames a profile field.
1494 * The ID of the field to rename
1496 * The new name of the field
1497 * @param successFunction
1498 * Called when the renaming was successful
1500 function editProfileField(fieldId, newName, successFunction) {
1501 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1502 if (data && data.success) {
1509 * Moves the profile field with the given ID one slot in the given direction.
1512 * The ID of the field to move
1514 * The direction to move in (“up” or “down”)
1515 * @param successFunction
1516 * Function to call on success
1518 function moveProfileField(fieldId, direction, successFunction) {
1519 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1520 if (data && data.success) {
1527 * Moves the profile field with the given ID up one slot.
1530 * The ID of the field to move
1531 * @param successFunction
1532 * Function to call on success
1534 function moveProfileFieldUp(fieldId, successFunction) {
1535 moveProfileField(fieldId, "up", successFunction);
1539 * Moves the profile field with the given ID down one slot.
1542 * The ID of the field to move
1543 * @param successFunction
1544 * Function to call on success
1546 function moveProfileFieldDown(fieldId, successFunction) {
1547 moveProfileField(fieldId, "down", successFunction);
1551 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1556 $(document).ready(function() {
1558 /* this initializes the status update input field. */
1559 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1560 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1561 $("#sone #update-status .select-sender").css("display", "inline");
1562 $("#sone #update-status .sender").hide();
1563 $("#sone #update-status .select-sender button").click(function() {
1564 $("#sone #update-status .sender").show();
1565 $("#sone #update-status .select-sender").hide();
1568 $("#sone #update-status").submit(function() {
1569 button = $("button:submit", this);
1570 button.attr("disabled", "disabled");
1571 if ($(this).find(":input.default:enabled").length > 0) {
1574 sender = $(this).find(":input[name=sender]").val();
1575 text = $(this).find(":input[name=text]:enabled").val();
1576 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1577 button.removeAttr("disabled");
1579 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1580 $(this).find(":input[name=text]:enabled").val("").blur();
1581 $(this).find(".sender").hide();
1582 $(this).find(".select-sender").show();
1587 /* ajaxify the search input field. */
1588 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1589 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1592 /* ajaxify input field on “view Sone” page. */
1593 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1594 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1595 $("#sone #post-message .select-sender").css("display", "inline");
1596 $("#sone #post-message .sender").hide();
1597 $("#sone #post-message .select-sender button").click(function() {
1598 $("#sone #post-message .sender").show();
1599 $("#sone #post-message .select-sender").hide();
1602 $("#sone #post-message").submit(function() {
1603 sender = $(this).find(":input[name=sender]").val();
1604 text = $(this).find(":input[name=text]:enabled").val();
1605 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text });
1606 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1607 $(this).find(":input[name=text]:enabled").val("").blur();
1608 $(this).find(".sender").hide();
1609 $(this).find(".select-sender").show();
1614 /* Ajaxifies all posts. */
1615 /* calling getTranslation here will cache the necessary values. */
1616 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1617 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1618 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1619 $("#sone .post").each(function() {
1626 /* update post times. */
1628 $("#sone .post").each(function() {
1629 postIds.push(getPostId(this));
1631 updatePostTimes(postIds.join(","));
1633 /* hides all replies but the latest two. */
1634 if (!isViewPostPage()) {
1635 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1636 $("#sone .post .replies").each(function() {
1637 allReplies = $(this).find(".reply");
1638 if (allReplies.length > 2) {
1640 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1641 $(allReplies[replyIndex]).addClass("hidden");
1642 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1644 clickToShowElement = $("<div></div>").addClass("click-to-show");
1646 clickToShowElement.addClass("new");
1648 (function(clickToShowElement, allReplies, text) {
1649 clickToShowElement.text(text);
1650 clickToShowElement.click(function() {
1651 allReplies.removeClass("hidden");
1652 clickToShowElement.addClass("hidden");
1654 })(clickToShowElement, allReplies, text);
1655 $(allReplies[0]).before(clickToShowElement);
1661 $("#sone .sone").each(function() {
1662 ajaxifySone($(this));
1665 /* process all existing notifications, ajaxify dismiss buttons. */
1666 $("#sone #notification-area .notification").each(function() {
1667 ajaxifyNotification($(this));
1670 /* activate status polling. */
1671 setTimeout(getStatus, 5000);
1673 /* reset activity counter when the page has focus. */
1674 $(window).focus(function() {
1677 }).blur(function() {