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).after(textarea);
37 (function(inputField, textarea) {
38 inputField.focus(function() {
39 $(this).hide().attr("disabled", "disabled");
40 textarea.show().focus();
42 if (inputField.val() == "") {
43 inputField.addClass("default");
44 inputField.val(defaultText);
46 inputField.hide().attr("disabled", "disabled");
49 $(inputField.get(0).form).submit(function() {
50 inputField.attr("disabled", "disabled");
51 if (!optional && (textarea.val() == "")) {
52 inputField.removeAttr("disabled").focus();
56 })($(this), textarea);
61 * Adds a “comment” link to all status lines contained in the given element.
66 * The element to add a “comment” link to
68 function addCommentLink(postId, element, insertAfterThisElement) {
69 if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
72 commentElement = (function(postId) {
73 separator = $("<span> · </span>").addClass("separator");
74 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
75 replyElement = $("#sone .post#" + postId + " .create-reply");
76 replyElement.removeClass("hidden");
77 replyElement.removeClass("light");
78 (function(replyElement) {
79 replyElement.find("input.reply-input").blur(function() {
80 if ($(this).hasClass("default")) {
81 replyElement.addClass("light");
84 replyElement.removeClass("light");
87 replyElement.find("input.reply-input").focus();
89 return commentElement;
91 $(insertAfterThisElement).after(commentElement.clone(true));
92 $(insertAfterThisElement).after(separator);
95 var translations = {};
98 * Retrieves the translation for the given key and calls the callback function.
99 * The callback function takes a single parameter, the translated string.
102 * The key of the translation string
104 * The callback function
106 function getTranslation(key, callback) {
107 if (key in translations) {
108 callback(translations[key]);
111 $.getJSON("getTranslation.ajax", {"key": key}, function(data, textStatus) {
112 if ((data != null) && data.success) {
113 translations[key] = data.value;
114 callback(data.value);
116 }, function(xmlHttpRequest, textStatus, error) {
122 * Filters the given Sone ID, replacing all “~” characters by an underscore.
125 * The Sone ID to filter
126 * @returns The filtered Sone ID
128 function filterSoneId(soneId) {
129 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
133 * Updates the status of the given Sone.
136 * The ID of the Sone to update
138 * The status of the Sone (“idle”, “unknown”, “inserting”,
141 * Whether the Sone is modified
143 * Whether the Sone is locked
145 * The date and time of the last update (formatted for display)
147 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
148 $("#sone .sone." + filterSoneId(soneId)).
149 toggleClass("unknown", status == "unknown").
150 toggleClass("idle", status == "idle").
151 toggleClass("inserting", status == "inserting").
152 toggleClass("downloading", status == "downloading").
153 toggleClass("modified", modified);
154 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
155 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
156 if (lastUpdated != null) {
157 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
159 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
160 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown);
163 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
167 * Enhances a “delete” button so that the confirmation is done on the same page.
172 * The text to show on the button
173 * @param deleteCallback
174 * The callback that actually deletes something
176 function enhanceDeleteButton(button, text, deleteCallback) {
178 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
179 $(this).fadeOut("slow");
182 }).insertAfter(button);
183 (function(button, newButton) {
184 button.click(function() {
185 button.fadeOut("slow", function() {
186 newButton.fadeIn("slow");
187 $(document).one("click", function() {
188 if (this != newButton.get(0)) {
189 newButton.fadeOut(function() {
197 })(button, newButton);
202 * Enhances a post’s “delete” button.
207 * The ID of the post to delete
209 * The text to replace the button with
211 function enhanceDeletePostButton(button, postId, text) {
212 enhanceDeleteButton(button, text, function() {
213 $.getJSON("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
218 $("#sone .post#" + postId).slideUp();
219 } else if (data.error == "invalid-post-id") {
220 alert("Invalid post ID given!");
221 } else if (data.error == "auth-required") {
222 alert("You need to be logged in.");
223 } else if (data.error == "not-authorized") {
224 alert("You are not allowed to delete this post.");
226 }, function(xmlHttpRequest, textStatus, error) {
233 * Enhances a reply’s “delete” button.
238 * The ID of the reply to delete
240 * The text to replace the button with
242 function enhanceDeleteReplyButton(button, replyId, text) {
243 enhanceDeleteButton(button, text, function() {
244 $.getJSON("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
249 $("#sone .reply#" + replyId).slideUp();
250 } else if (data.error == "invalid-reply-id") {
251 alert("Invalid reply ID given!");
252 } else if (data.error == "auth-required") {
253 alert("You need to be logged in.");
254 } else if (data.error == "not-authorized") {
255 alert("You are not allowed to delete this reply.");
257 }, function(xmlHttpRequest, textStatus, error) {
263 function getFormPassword() {
264 return $("#sone #formPassword").text();
267 function getSoneElement(element) {
268 return $(element).closest(".sone");
272 * Generates a list of Sones by concatening the names of the given sones with a
273 * new line character (“\n”).
276 * The sones to format
277 * @returns {String} The created string
279 function generateSoneList(sones) {
281 $.each(sones, function() {
282 if (soneList != "") {
285 soneList += this.name;
291 * Returns the ID of the Sone that this element belongs to.
294 * The element to locate the matching Sone ID for
295 * @returns The ID of the Sone, or undefined
297 function getSoneId(element) {
298 return getSoneElement(element).find(".id").text();
302 * Returns the element of the post with the given ID.
306 * @returns The element of the post
308 function getPost(postId) {
309 return $("#sone .post#" + postId);
312 function getPostElement(element) {
313 return $(element).closest(".post");
316 function getPostId(element) {
317 return getPostElement(element).attr("id");
320 function getPostTime(element) {
321 return getPostElement(element).find(".post-time").text();
325 * Returns the author of the post the given element belongs to.
328 * The element whose post to get the author for
329 * @returns The ID of the authoring Sone
331 function getPostAuthor(element) {
332 return getPostElement(element).find(".post-author").text();
335 function getReplyElement(element) {
336 return $(element).closest(".reply");
339 function getReplyId(element) {
340 return getReplyElement(element).attr("id");
343 function getReplyTime(element) {
344 return getReplyElement(element).find(".reply-time").text();
348 * Returns the author of the reply the given element belongs to.
351 * The element whose reply to get the author for
352 * @returns The ID of the authoring Sone
354 function getReplyAuthor(element) {
355 return getReplyElement(element).find(".reply-author").text();
358 function likePost(postId) {
359 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
360 if ((data == null) || !data.success) {
363 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
364 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
365 updatePostLikes(postId);
366 }, function(xmlHttpRequest, textStatus, error) {
371 function unlikePost(postId) {
372 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
373 if ((data == null) || !data.success) {
376 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
377 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
378 updatePostLikes(postId);
379 }, function(xmlHttpRequest, textStatus, error) {
384 function updatePostLikes(postId) {
385 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
386 if ((data != null) && data.success) {
387 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
388 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
389 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
391 }, function(xmlHttpRequest, textStatus, error) {
396 function likeReply(replyId) {
397 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
398 if ((data == null) || !data.success) {
401 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
402 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
403 updateReplyLikes(replyId);
404 }, function(xmlHttpRequest, textStatus, error) {
409 function unlikeReply(replyId) {
410 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
411 if ((data == null) || !data.success) {
414 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
415 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
416 updateReplyLikes(replyId);
417 }, function(xmlHttpRequest, textStatus, error) {
423 * Trusts the Sone with the given ID.
426 * The ID of the Sone to trust
428 function trustSone(soneId) {
429 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
430 if ((data != null) && data.success) {
431 updateTrustControls(soneId, data.trustValue);
437 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
440 * The ID of the Sone to distrust
442 function distrustSone(soneId) {
443 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
444 if ((data != null) && data.success) {
445 updateTrustControls(soneId, data.trustValue);
451 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
454 * The ID of the Sone to untrust
456 function untrustSone(soneId) {
457 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
458 if ((data != null) && data.success) {
459 updateTrustControls(soneId, data.trustValue);
465 * Updates the trust controls for all posts and replies of the given Sone,
466 * according to the given trust value.
469 * The ID of the Sone to update all trust controls for
471 * The trust value for the Sone
473 function updateTrustControls(soneId, trustValue) {
474 $("#sone .post").each(function() {
475 if (getPostAuthor(this) == soneId) {
476 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
477 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
478 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
481 $("#sone .reply").each(function() {
482 if (getReplyAuthor(this) == soneId) {
483 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
484 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
485 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
491 * Bookmarks the post with the given ID.
494 * The ID of the post to bookmark
496 function bookmarkPost(postId) {
498 $.getJSON("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
499 if ((data != null) && data.success) {
500 getPost(postId).find(".bookmark").toggleClass("hidden", true);
501 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
508 * Unbookmarks the post with the given ID.
511 * The ID of the post to unbookmark
513 function unbookmarkPost(postId) {
514 $.getJSON("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
515 if ((data != null) && data.success) {
516 getPost(postId).find(".bookmark").toggleClass("hidden", false);
517 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
522 function updateReplyLikes(replyId) {
523 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
524 if ((data != null) && data.success) {
525 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
526 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
527 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
529 }, function(xmlHttpRequest, textStatus, error) {
535 * Posts a reply and calls the given callback when the request finishes.
538 * The ID of the sender
540 * The ID of the post the reply refers to
543 * @param callbackFunction
544 * The callback function to call when the request finishes (takes 3
545 * parameters: success, error, replyId)
547 function postReply(sender, postId, text, callbackFunction) {
548 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
550 /* TODO - show error */
554 callbackFunction(true, null, data.reply, data.sone);
556 callbackFunction(false, data.error);
558 }, function(xmlHttpRequest, textStatus, error) {
564 * Requests information about the reply with the given ID.
567 * The ID of the reply
568 * @param callbackFunction
569 * A callback function (parameters soneId, soneName, replyTime,
570 * replyDisplayTime, text, html)
572 function getReply(replyId, callbackFunction) {
573 $.getJSON("getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
574 if ((data != null) && data.success) {
575 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
577 }, function(xmlHttpRequest, textStatus, error) {
583 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
586 * The Sone to ajaxify
588 function ajaxifySone(soneElement) {
590 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
593 $(".follow", soneElement).submit(function() {
594 var followElement = this;
595 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
596 $(followElement).addClass("hidden");
597 $(followElement).parent().find(".unfollow").removeClass("hidden");
601 $(".unfollow", soneElement).submit(function() {
602 var unfollowElement = this;
603 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
604 $(unfollowElement).addClass("hidden");
605 $(unfollowElement).parent().find(".follow").removeClass("hidden");
609 $(".lock", soneElement).submit(function() {
610 var lockElement = this;
611 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
612 $(lockElement).addClass("hidden");
613 $(lockElement).parent().find(".unlock").removeClass("hidden");
617 $(".unlock", soneElement).submit(function() {
618 var unlockElement = this;
619 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
620 $(unlockElement).addClass("hidden");
621 $(unlockElement).parent().find(".lock").removeClass("hidden");
626 /* mark Sone as known when clicking it. */
627 $(soneElement).click(function() {
628 markSoneAsKnown(soneElement);
633 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
636 * The post element to ajaxify
638 function ajaxifyPost(postElement) {
639 $(postElement).find("form").submit(function() {
642 $(postElement).find(".create-reply button:submit").click(function() {
644 button.attr("disabled", "disabled");
645 sender = $(this.form).find(":input[name=sender]").val();
646 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
647 postId = getPostId(this);
648 text = $(inputField).val();
649 (function(sender, postId, text, inputField) {
650 postReply(sender, postId, text, function(success, error, replyId, soneId) {
652 $(inputField).val("");
653 loadNewReply(replyId, soneId, postId);
654 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
655 $("#sone .post#" + postId + " .create-reply .sender").hide();
656 $("#sone .post#" + postId + " .create-reply .select-sender").show();
657 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
661 button.removeAttr("disabled");
663 })(sender, postId, text, inputField);
667 /* replace all “delete” buttons with javascript. */
668 (function(postElement) {
669 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
670 postId = getPostId(postElement);
671 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
675 /* convert all “like” buttons to javascript functions. */
676 $(postElement).find(".like-post").submit(function() {
677 likePost(getPostId(this));
680 $(postElement).find(".unlike-post").submit(function() {
681 unlikePost(getPostId(this));
685 /* convert trust control buttons to javascript functions. */
686 $(postElement).find(".post-trust").submit(function() {
687 trustSone(getPostAuthor(this));
690 $(postElement).find(".post-distrust").submit(function() {
691 distrustSone(getPostAuthor(this));
694 $(postElement).find(".post-untrust").submit(function() {
695 untrustSone(getPostAuthor(this));
699 /* convert bookmark/unbookmark buttons to javascript functions. */
700 $(postElement).find(".bookmark").submit(function() {
701 bookmarkPost(getPostId(this));
704 $(postElement).find(".unbookmark").submit(function() {
705 unbookmarkPost(getPostId(this));
709 /* convert “show source” link into javascript function. */
710 $(postElement).find(".show-source").each(function() {
711 $("a", this).click(function() {
712 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
713 $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
718 /* add “comment” link. */
719 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
721 /* process all replies. */
722 $(postElement).find(".reply").each(function() {
726 /* process reply input fields. */
727 getTranslation("WebInterface.DefaultText.Reply", function(text) {
728 $(postElement).find("input.reply-input").each(function() {
729 registerInputTextareaSwap(this, text, "text", false, false);
733 /* process sender selection. */
734 $(".select-sender", postElement).css("display", "inline");
735 $(".sender", postElement).hide();
736 $(".select-sender button", postElement).click(function() {
737 $(".sender", postElement).show();
738 $(".select-sender", postElement).hide();
742 /* mark everything as known on click. */
743 $(postElement).click(function(event) {
744 if ($(event.target).hasClass("click-to-show")) {
747 markPostAsKnown(this);
750 /* hide reply input field. */
751 $(postElement).find(".create-reply").addClass("hidden");
755 * Ajaxifies the given reply element.
757 * @param replyElement
758 * The reply element to ajaxify
760 function ajaxifyReply(replyElement) {
761 $(replyElement).find(".like-reply").submit(function() {
762 likeReply(getReplyId(this));
765 $(replyElement).find(".unlike-reply").submit(function() {
766 unlikeReply(getReplyId(this));
769 (function(replyElement) {
770 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
771 $(replyElement).find(".delete-reply button").each(function() {
772 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
776 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
778 /* convert “show source” link into javascript function. */
779 $(replyElement).find(".show-reply-source").each(function() {
780 $("a", this).click(function() {
781 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
782 $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
787 /* convert trust control buttons to javascript functions. */
788 $(replyElement).find(".reply-trust").submit(function() {
789 trustSone(getReplyAuthor(this));
792 $(replyElement).find(".reply-distrust").submit(function() {
793 distrustSone(getReplyAuthor(this));
796 $(replyElement).find(".reply-untrust").submit(function() {
797 untrustSone(getReplyAuthor(this));
803 * Ajaxifies the given notification by replacing the form with AJAX.
805 * @param notification
806 * jQuery object representing the notification.
808 function ajaxifyNotification(notification) {
809 notification.find("form").submit(function() {
812 notification.find("input[name=returnPage]").val($.url.attr("relative"));
813 if (notification.find(".short-text").length > 0) {
814 notification.find(".short-text").removeClass("hidden");
815 notification.find(".text").addClass("hidden");
817 notification.find("form.mark-as-read button").click(function() {
818 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
820 notification.find("a[class^='link-']").each(function() {
821 linkElement = $(this);
822 if (linkElement.is("[href^='viewPost']")) {
823 id = linkElement.attr("class").substr(5);
825 linkElement.attr("href", "#post-" + id);
829 notification.find("form.dismiss button").click(function() {
830 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
831 /* dismiss in case of error, too. */
832 notification.slideUp();
833 }, function(xmlHttpRequest, textStatus, error) {
840 function getStatus() {
841 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
842 if ((data != null) && data.success) {
843 /* process Sone information. */
844 $.each(data.sones, function(index, value) {
845 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
847 /* process notifications. */
848 $.each(data.notifications, function(index, value) {
849 oldNotification = $("#sone #notification-area .notification#" + value.id);
850 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
851 if (oldNotification.length != 0) {
852 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
853 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
854 notification.find(".short-text").toggleClass("hidden", opened);
855 notification.find(".text").toggleClass("hidden", !opened);
857 oldNotification.replaceWith(notification.show());
859 $("#sone #notification-area").append(notification);
860 notification.slideDown();
864 $.each(data.removedNotifications, function(index, value) {
865 $("#sone #notification-area .notification#" + value.id).slideUp();
867 /* process new posts. */
868 $.each(data.newPosts, function(index, value) {
869 loadNewPost(value.id, value.sone, value.recipient, value.time);
871 /* process new replies. */
872 $.each(data.newReplies, function(index, value) {
873 loadNewReply(value.id, value.sone, value.post, value.postSone);
875 /* do it again in 5 seconds. */
876 setTimeout(getStatus, 5000);
878 /* data.success was false, wait 30 seconds. */
879 setTimeout(getStatus, 30000);
881 }, function(xmlHttpRequest, textStatus, error) {
882 /* something really bad happend, wait a minute. */
883 setTimeout(getStatus, 60000);
888 * Returns the ID of the currently logged in Sone.
890 * @return The ID of the current Sone, or an empty string if no Sone is logged
893 function getCurrentSoneId() {
894 return $("#currentSoneId").text();
898 * Returns the content of the page-id attribute.
900 * @returns The page ID
902 function getPageId() {
903 return $("#sone .page-id").text();
907 * Returns whether the current page is the index page.
909 * @returns {Boolean} <code>true</code> if the current page is the index page,
910 * <code>false</code> otherwise
912 function isIndexPage() {
913 return getPageId() == "index";
917 * Returns whether the current page is a “view Sone” page.
919 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
920 * page, <code>false</code> otherwise
922 function isViewSonePage() {
923 return getPageId() == "view-sone";
927 * Returns the ID of the currently shown Sone. This will only return a sensible
928 * value if isViewSonePage() returns <code>true</code>.
930 * @returns The ID of the currently shown Sone
932 function getShownSoneId() {
933 return $("#sone .sone-id").text();
937 * Returns whether the current page is a “view post” page.
939 * @returns {Boolean} <code>true</code> if the current page is a “view post”
940 * page, <code>false</code> otherwise
942 function isViewPostPage() {
943 return getPageId() == "view-post";
947 * Returns the ID of the currently shown post. This will only return a sensible
948 * value if isViewPostPage() returns <code>true</code>.
950 * @returns The ID of the currently shown post
952 function getShownPostId() {
953 return $("#sone .post-id").text();
957 * Returns whether the current page is the “known Sones” page.
959 * @returns {Boolean} <code>true</code> if the current page is the “known
960 * Sones” page, <code>false</code> otherwise
962 function isKnownSonesPage() {
963 return getPageId() == "known-sones";
967 * Returns whether a post with the given ID exists on the current page.
970 * The post ID to check for
971 * @returns {Boolean} <code>true</code> if a post with the given ID already
972 * exists on the page, <code>false</code> otherwise
974 function hasPost(postId) {
975 return $(".post#" + postId).length > 0;
979 * Returns whether a reply with the given ID exists on the current page.
982 * The reply ID to check for
983 * @returns {Boolean} <code>true</code> if a reply with the given ID already
984 * exists on the page, <code>false</code> otherwise
986 function hasReply(replyId) {
987 return $("#sone .reply#" + replyId).length > 0;
990 function loadNewPost(postId, soneId, recipientId, time) {
991 if (hasPost(postId)) {
994 if (!isIndexPage()) {
995 if (!isViewPostPage() || (getShownPostId() != postId)) {
996 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1001 if (getPostTime($("#sone .post").last()) > time) {
1004 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1005 if ((data != null) && data.success) {
1006 if (hasPost(data.post.id)) {
1009 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1012 var firstOlderPost = null;
1013 $("#sone .post").each(function() {
1014 if (getPostTime(this) < data.post.time) {
1015 firstOlderPost = $(this);
1019 newPost = $(data.post.html).addClass("hidden");
1020 if (firstOlderPost != null) {
1021 newPost.insertBefore(firstOlderPost);
1023 ajaxifyPost(newPost);
1024 newPost.slideDown();
1030 function loadNewReply(replyId, soneId, postId, postSoneId) {
1031 if (hasReply(replyId)) {
1034 if (!hasPost(postId)) {
1037 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1039 if ((data != null) && data.success) {
1040 if (hasReply(data.reply.id)) {
1043 $("#sone .post#" + data.reply.postId).each(function() {
1044 var firstNewerReply = null;
1045 $(this).find(".replies .reply").each(function() {
1046 if (getReplyTime(this) > data.reply.time) {
1047 firstNewerReply = $(this);
1051 newReply = $(data.reply.html).addClass("hidden");
1052 if (firstNewerReply != null) {
1053 newReply.insertBefore(firstNewerReply);
1055 if ($(this).find(".replies .create-reply")) {
1056 $(this).find(".replies .create-reply").before(newReply);
1058 $(this).find(".replies").append(newReply);
1061 ajaxifyReply(newReply);
1062 newReply.slideDown();
1071 * Marks the given Sone as known if it is still new.
1073 * @param soneElement
1074 * The Sone to mark as known
1076 function markSoneAsKnown(soneElement) {
1077 if ($(".new", soneElement).length > 0) {
1078 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1079 $(soneElement).removeClass("new");
1084 function markPostAsKnown(postElements) {
1085 $(postElements).each(function() {
1087 if ($(postElement).hasClass("new")) {
1088 (function(postElement) {
1089 $(postElement).removeClass("new");
1090 $(".click-to-show", postElement).removeClass("new");
1091 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1095 markReplyAsKnown($(postElements).find(".reply"));
1098 function markReplyAsKnown(replyElements) {
1099 $(replyElements).each(function() {
1100 replyElement = this;
1101 if ($(replyElement).hasClass("new")) {
1102 (function(replyElement) {
1103 $(replyElement).removeClass("new");
1104 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1110 function resetActivity() {
1111 title = document.title;
1112 if (title.indexOf('(') == 0) {
1113 setTitle(title.substr(title.indexOf(' ') + 1));
1117 function setActivity() {
1119 title = document.title;
1120 if (title.indexOf('(') != 0) {
1121 setTitle("(!) " + title);
1123 if (!iconBlinking) {
1124 setTimeout(toggleIcon, 1500);
1125 iconBlinking = true;
1131 * Sets the window title after a small delay to prevent race-condition issues.
1136 function setTitle(title) {
1137 setTimeout(function() {
1138 document.title = title;
1142 /** Whether the icon is currently showing activity. */
1143 var iconActive = false;
1145 /** Whether the icon is currently supposed to blink. */
1146 var iconBlinking = false;
1149 * Toggles the icon. If the window has gained focus and the icon is still
1150 * showing the activity state, it is returned to normal.
1152 function toggleIcon() {
1155 changeIcon("images/icon.png");
1158 iconBlinking = false;
1160 iconActive = !iconActive;
1161 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1162 setTimeout(toggleIcon, 1500);
1167 * Changes the icon of the page.
1170 * The new URL of the icon
1172 function changeIcon(iconUrl) {
1173 $("link[rel=icon]").remove();
1174 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1175 $("iframe[id=icon-update]")[0].src += "";
1179 * Creates a new notification.
1182 * The ID of the notificaiton
1184 * The text of the notification
1185 * @param dismissable
1186 * <code>true</code> if the notification can be dismissed by the
1189 function createNotification(id, text, dismissable) {
1190 notification = $("<div></div>").addClass("notification").attr("id", id);
1192 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1193 dismissForm.find("input[name=notification]").val(id);
1194 notification.append(dismissForm);
1196 notification.append(text);
1197 return notification;
1201 * Shows the details of the notification with the given ID.
1203 * @param notificationId
1204 * The ID of the notification
1206 function showNotificationDetails(notificationId) {
1207 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1208 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1212 * Deletes the field with the given ID from the profile.
1215 * The ID of the field to delete
1217 function deleteProfileField(fieldId) {
1218 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1219 if (data && data.success) {
1220 $("#sone .profile-field#" + data.field.id).slideUp();
1226 * Renames a profile field.
1229 * The ID of the field to rename
1231 * The new name of the field
1232 * @param successFunction
1233 * Called when the renaming was successful
1235 function editProfileField(fieldId, newName, successFunction) {
1236 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1237 if (data && data.success) {
1244 * Moves the profile field with the given ID one slot in the given direction.
1247 * The ID of the field to move
1249 * The direction to move in (“up” or “down”)
1250 * @param successFunction
1251 * Function to call on success
1253 function moveProfileField(fieldId, direction, successFunction) {
1254 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1255 if (data && data.success) {
1262 * Moves the profile field with the given ID up one slot.
1265 * The ID of the field to move
1266 * @param successFunction
1267 * Function to call on success
1269 function moveProfileFieldUp(fieldId, successFunction) {
1270 moveProfileField(fieldId, "up", successFunction);
1274 * Moves the profile field with the given ID down one slot.
1277 * The ID of the field to move
1278 * @param successFunction
1279 * Function to call on success
1281 function moveProfileFieldDown(fieldId, successFunction) {
1282 moveProfileField(fieldId, "down", successFunction);
1286 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1291 $(document).ready(function() {
1293 /* this initializes the status update input field. */
1294 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1295 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1296 $("#sone #update-status .select-sender").css("display", "inline");
1297 $("#sone #update-status .sender").hide();
1298 $("#sone #update-status .select-sender button").click(function() {
1299 $("#sone #update-status .sender").show();
1300 $("#sone #update-status .select-sender").hide();
1303 $("#sone #update-status").submit(function() {
1304 button = $("button:submit", this);
1305 button.attr("disabled", "disabled");
1306 if ($(this).find(":input.default:enabled").length > 0) {
1309 sender = $(this).find(":input[name=sender]").val();
1310 text = $(this).find(":input[name=text]:enabled").val();
1311 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1312 if ((data != null) && data.success) {
1313 loadNewPost(data.postId, data.sone, data.recipient);
1315 button.removeAttr("disabled");
1317 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1318 $(this).find(":input[name=text]:enabled").val("").blur();
1319 $(this).find(".sender").hide();
1320 $(this).find(".select-sender").show();
1325 /* ajaxify the search input field. */
1326 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1327 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1330 /* ajaxify input field on “view Sone” page. */
1331 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1332 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1333 $("#sone #post-message .select-sender").css("display", "inline");
1334 $("#sone #post-message .sender").hide();
1335 $("#sone #post-message .select-sender button").click(function() {
1336 $("#sone #post-message .sender").show();
1337 $("#sone #post-message .select-sender").hide();
1340 $("#sone #post-message").submit(function() {
1341 sender = $(this).find(":input[name=sender]").val();
1342 text = $(this).find(":input[name=text]:enabled").val();
1343 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text }, function(data, textStatus) {
1344 if ((data != null) && data.success) {
1345 loadNewPost(data.postId, getCurrentSoneId());
1348 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1349 $(this).find(":input[name=text]:enabled").val("").blur();
1350 $(this).find(".sender").hide();
1351 $(this).find(".select-sender").show();
1356 /* Ajaxifies all posts. */
1357 /* calling getTranslation here will cache the necessary values. */
1358 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1359 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1360 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1361 $("#sone .post").each(function() {
1368 /* hides all replies but the latest two. */
1369 if (!isViewPostPage()) {
1370 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1371 $("#sone .post .replies").each(function() {
1372 allReplies = $(this).find(".reply");
1373 if (allReplies.length > 2) {
1375 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1376 $(allReplies[replyIndex]).addClass("hidden");
1377 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1379 clickToShowElement = $("<div></div>").addClass("click-to-show");
1381 clickToShowElement.addClass("new");
1383 (function(clickToShowElement, allReplies, text) {
1384 clickToShowElement.text(text);
1385 clickToShowElement.click(function() {
1386 allReplies.removeClass("hidden");
1387 clickToShowElement.addClass("hidden");
1389 })(clickToShowElement, allReplies, text);
1390 $(allReplies[0]).before(clickToShowElement);
1396 $("#sone .sone").each(function() {
1397 ajaxifySone($(this));
1400 /* process all existing notifications, ajaxify dismiss buttons. */
1401 $("#sone #notification-area .notification").each(function() {
1402 ajaxifyNotification($(this));
1405 /* activate status polling. */
1406 setTimeout(getStatus, 5000);
1408 /* reset activity counter when the page has focus. */
1409 $(window).focus(function() {
1412 }).blur(function() {