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() == "")) {
55 })($(this), textarea);
60 * Adds a “comment” link to all status lines contained in the given element.
65 * The element to add a “comment” link to
67 function addCommentLink(postId, element, insertAfterThisElement) {
68 if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
71 commentElement = (function(postId) {
72 separator = $("<span> · </span>").addClass("separator");
73 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
74 replyElement = $("#sone .post#" + postId + " .create-reply");
75 replyElement.removeClass("hidden");
76 replyElement.removeClass("light");
77 (function(replyElement) {
78 replyElement.find("input.reply-input").blur(function() {
79 if ($(this).hasClass("default")) {
80 replyElement.addClass("light");
83 replyElement.removeClass("light");
86 replyElement.find("input.reply-input").focus();
88 return commentElement;
90 $(insertAfterThisElement).after(commentElement.clone(true));
91 $(insertAfterThisElement).after(separator);
94 var translations = {};
97 * Retrieves the translation for the given key and calls the callback function.
98 * The callback function takes a single parameter, the translated string.
101 * The key of the translation string
103 * The callback function
105 function getTranslation(key, callback) {
106 if (key in translations) {
107 callback(translations[key]);
110 $.getJSON("getTranslation.ajax", {"key": key}, function(data, textStatus) {
111 if ((data != null) && data.success) {
112 translations[key] = data.value;
113 callback(data.value);
115 }, function(xmlHttpRequest, textStatus, error) {
121 * Filters the given Sone ID, replacing all “~” characters by an underscore.
124 * The Sone ID to filter
125 * @returns The filtered Sone ID
127 function filterSoneId(soneId) {
128 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
132 * Updates the status of the given Sone.
135 * The ID of the Sone to update
137 * The status of the Sone (“idle”, “unknown”, “inserting”,
140 * Whether the Sone is modified
142 * Whether the Sone is locked
144 * The date and time of the last update (formatted for display)
146 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
147 $("#sone .sone." + filterSoneId(soneId)).
148 toggleClass("unknown", status == "unknown").
149 toggleClass("idle", status == "idle").
150 toggleClass("inserting", status == "inserting").
151 toggleClass("downloading", status == "downloading").
152 toggleClass("modified", modified);
153 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
154 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
155 if (lastUpdated != null) {
156 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
158 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
159 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown);
162 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
166 * Enhances a “delete” button so that the confirmation is done on the same page.
171 * The text to show on the button
172 * @param deleteCallback
173 * The callback that actually deletes something
175 function enhanceDeleteButton(button, text, deleteCallback) {
177 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
178 $(this).fadeOut("slow");
181 }).insertAfter(button);
182 (function(button, newButton) {
183 button.click(function() {
184 button.fadeOut("slow", function() {
185 newButton.fadeIn("slow");
186 $(document).one("click", function() {
187 if (this != newButton.get(0)) {
188 newButton.fadeOut(function() {
196 })(button, newButton);
201 * Enhances a post’s “delete” button.
206 * The ID of the post to delete
208 * The text to replace the button with
210 function enhanceDeletePostButton(button, postId, text) {
211 enhanceDeleteButton(button, text, function() {
212 $.getJSON("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
217 $("#sone .post#" + postId).slideUp();
218 } else if (data.error == "invalid-post-id") {
219 alert("Invalid post ID given!");
220 } else if (data.error == "auth-required") {
221 alert("You need to be logged in.");
222 } else if (data.error == "not-authorized") {
223 alert("You are not allowed to delete this post.");
225 }, function(xmlHttpRequest, textStatus, error) {
232 * Enhances a reply’s “delete” button.
237 * The ID of the reply to delete
239 * The text to replace the button with
241 function enhanceDeleteReplyButton(button, replyId, text) {
242 enhanceDeleteButton(button, text, function() {
243 $.getJSON("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
248 $("#sone .reply#" + replyId).slideUp();
249 } else if (data.error == "invalid-reply-id") {
250 alert("Invalid reply ID given!");
251 } else if (data.error == "auth-required") {
252 alert("You need to be logged in.");
253 } else if (data.error == "not-authorized") {
254 alert("You are not allowed to delete this reply.");
256 }, function(xmlHttpRequest, textStatus, error) {
262 function getFormPassword() {
263 return $("#sone #formPassword").text();
266 function getSoneElement(element) {
267 return $(element).closest(".sone");
271 * Generates a list of Sones by concatening the names of the given sones with a
272 * new line character (“\n”).
275 * The sones to format
276 * @returns {String} The created string
278 function generateSoneList(sones) {
280 $.each(sones, function() {
281 if (soneList != "") {
284 soneList += this.name;
290 * Returns the ID of the Sone that this element belongs to.
293 * The element to locate the matching Sone ID for
294 * @returns The ID of the Sone, or undefined
296 function getSoneId(element) {
297 return getSoneElement(element).find(".id").text();
300 function getPostElement(element) {
301 return $(element).closest(".post");
304 function getPostId(element) {
305 return getPostElement(element).attr("id");
308 function getPostTime(element) {
309 return getPostElement(element).find(".post-time").text();
313 * Returns the author of the post the given element belongs to.
316 * The element whose post to get the author for
317 * @returns The ID of the authoring Sone
319 function getPostAuthor(element) {
320 return getPostElement(element).find(".post-author").text();
323 function getReplyElement(element) {
324 return $(element).closest(".reply");
327 function getReplyId(element) {
328 return getReplyElement(element).attr("id");
331 function getReplyTime(element) {
332 return getReplyElement(element).find(".reply-time").text();
336 * Returns the author of the reply the given element belongs to.
339 * The element whose reply to get the author for
340 * @returns The ID of the authoring Sone
342 function getReplyAuthor(element) {
343 return getReplyElement(element).find(".reply-author").text();
346 function likePost(postId) {
347 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
348 if ((data == null) || !data.success) {
351 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
352 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
353 updatePostLikes(postId);
354 }, function(xmlHttpRequest, textStatus, error) {
359 function unlikePost(postId) {
360 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
361 if ((data == null) || !data.success) {
364 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
365 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
366 updatePostLikes(postId);
367 }, function(xmlHttpRequest, textStatus, error) {
372 function updatePostLikes(postId) {
373 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
374 if ((data != null) && data.success) {
375 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
376 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
377 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
379 }, function(xmlHttpRequest, textStatus, error) {
384 function likeReply(replyId) {
385 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
386 if ((data == null) || !data.success) {
389 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
390 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
391 updateReplyLikes(replyId);
392 }, function(xmlHttpRequest, textStatus, error) {
397 function unlikeReply(replyId) {
398 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
399 if ((data == null) || !data.success) {
402 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
403 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
404 updateReplyLikes(replyId);
405 }, function(xmlHttpRequest, textStatus, error) {
411 * Trusts the Sone with the given ID.
414 * The ID of the Sone to trust
416 function trustSone(soneId) {
417 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
418 if ((data != null) && data.success) {
419 updateTrustControls(soneId, data.trustValue);
425 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
428 * The ID of the Sone to distrust
430 function distrustSone(soneId) {
431 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
432 if ((data != null) && data.success) {
433 updateTrustControls(soneId, data.trustValue);
439 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
442 * The ID of the Sone to untrust
444 function untrustSone(soneId) {
445 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
446 if ((data != null) && data.success) {
447 updateTrustControls(soneId, data.trustValue);
453 * Updates the trust controls for all posts and replies of the given Sone,
454 * according to the given trust value.
457 * The ID of the Sone to update all trust controls for
459 * The trust value for the Sone
461 function updateTrustControls(soneId, trustValue) {
462 $("#sone .post").each(function() {
463 if (getPostAuthor(this) == soneId) {
464 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
465 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
466 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
469 $("#sone .reply").each(function() {
470 if (getReplyAuthor(this) == soneId) {
471 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
472 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
473 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
478 function updateReplyLikes(replyId) {
479 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
480 if ((data != null) && data.success) {
481 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
482 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
483 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
485 }, function(xmlHttpRequest, textStatus, error) {
491 * Posts a reply and calls the given callback when the request finishes.
494 * The ID of the sender
496 * The ID of the post the reply refers to
499 * @param callbackFunction
500 * The callback function to call when the request finishes (takes 3
501 * parameters: success, error, replyId)
503 function postReply(sender, postId, text, callbackFunction) {
504 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
506 /* TODO - show error */
510 callbackFunction(true, null, data.reply, data.sone);
512 callbackFunction(false, data.error);
514 }, function(xmlHttpRequest, textStatus, error) {
520 * Requests information about the reply with the given ID.
523 * The ID of the reply
524 * @param callbackFunction
525 * A callback function (parameters soneId, soneName, replyTime,
526 * replyDisplayTime, text, html)
528 function getReply(replyId, callbackFunction) {
529 $.getJSON("getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
530 if ((data != null) && data.success) {
531 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
533 }, function(xmlHttpRequest, textStatus, error) {
539 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
542 * The Sone to ajaxify
544 function ajaxifySone(soneElement) {
546 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
549 $(".follow", soneElement).submit(function() {
550 var followElement = this;
551 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
552 $(followElement).addClass("hidden");
553 $(followElement).parent().find(".unfollow").removeClass("hidden");
557 $(".unfollow", soneElement).submit(function() {
558 var unfollowElement = this;
559 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
560 $(unfollowElement).addClass("hidden");
561 $(unfollowElement).parent().find(".follow").removeClass("hidden");
565 $(".lock", soneElement).submit(function() {
566 var lockElement = this;
567 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
568 $(lockElement).addClass("hidden");
569 $(lockElement).parent().find(".unlock").removeClass("hidden");
573 $(".unlock", soneElement).submit(function() {
574 var unlockElement = this;
575 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
576 $(unlockElement).addClass("hidden");
577 $(unlockElement).parent().find(".lock").removeClass("hidden");
582 /* mark Sone as known when clicking it. */
583 $(soneElement).click(function() {
584 markSoneAsKnown(soneElement);
589 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
592 * The post element to ajaxify
594 function ajaxifyPost(postElement) {
595 $(postElement).find("form").submit(function() {
598 $(postElement).find(".create-reply button:submit").click(function() {
599 sender = $(this.form).find(":input[name=sender]").val();
600 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
601 postId = getPostId(this);
602 text = $(inputField).val();
603 (function(sender, postId, text, inputField) {
604 postReply(sender, postId, text, function(success, error, replyId, soneId) {
606 $(inputField).val("");
607 loadNewReply(replyId, soneId, postId);
608 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
609 $("#sone .post#" + postId + " .create-reply .sender").hide();
610 $("#sone .post#" + postId + " .create-reply .select-sender").show();
611 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
616 })(sender, postId, text, inputField);
620 /* replace all “delete” buttons with javascript. */
621 (function(postElement) {
622 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
623 postId = getPostId(postElement);
624 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
628 /* convert all “like” buttons to javascript functions. */
629 $(postElement).find(".like-post").submit(function() {
630 likePost(getPostId(this));
633 $(postElement).find(".unlike-post").submit(function() {
634 unlikePost(getPostId(this));
638 /* convert trust control buttons to javascript functions. */
639 $(postElement).find(".post-trust").submit(function() {
640 trustSone(getPostAuthor(this));
643 $(postElement).find(".post-distrust").submit(function() {
644 distrustSone(getPostAuthor(this));
647 $(postElement).find(".post-untrust").submit(function() {
648 untrustSone(getPostAuthor(this));
652 /* add “comment” link. */
653 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
655 /* process all replies. */
656 $(postElement).find(".reply").each(function() {
660 /* process reply input fields. */
661 getTranslation("WebInterface.DefaultText.Reply", function(text) {
662 $(postElement).find("input.reply-input").each(function() {
663 registerInputTextareaSwap(this, text, "text", false, false);
667 /* process sender selection. */
668 $(".select-sender", postElement).css("display", "inline");
669 $(".sender", postElement).hide();
670 $(".select-sender button", postElement).click(function() {
671 $(".sender", postElement).show();
672 $(".select-sender", postElement).hide();
676 /* mark everything as known on click. */
677 $(postElement).click(function(event) {
678 if ($(event.target).hasClass("click-to-show")) {
681 markPostAsKnown(this);
684 /* hide reply input field. */
685 $(postElement).find(".create-reply").addClass("hidden");
689 * Ajaxifies the given reply element.
691 * @param replyElement
692 * The reply element to ajaxify
694 function ajaxifyReply(replyElement) {
695 $(replyElement).find(".like-reply").submit(function() {
696 likeReply(getReplyId(this));
699 $(replyElement).find(".unlike-reply").submit(function() {
700 unlikeReply(getReplyId(this));
703 (function(replyElement) {
704 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
705 $(replyElement).find(".delete-reply button").each(function() {
706 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
710 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
712 /* convert trust control buttons to javascript functions. */
713 $(replyElement).find(".reply-trust").submit(function() {
714 trustSone(getReplyAuthor(this));
717 $(replyElement).find(".reply-distrust").submit(function() {
718 distrustSone(getReplyAuthor(this));
721 $(replyElement).find(".reply-untrust").submit(function() {
722 untrustSone(getReplyAuthor(this));
728 * Ajaxifies the given notification by replacing the form with AJAX.
730 * @param notification
731 * jQuery object representing the notification.
733 function ajaxifyNotification(notification) {
734 notification.find("form").submit(function() {
737 notification.find("input[name=returnPage]").val($.url.attr("relative"));
738 if (notification.find(".short-text").length > 0) {
739 notification.find(".short-text").removeClass("hidden");
740 notification.find(".text").addClass("hidden");
742 notification.find("form.mark-as-read button").click(function() {
743 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
745 notification.find("form.dismiss button").click(function() {
746 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
747 /* dismiss in case of error, too. */
748 notification.slideUp();
749 }, function(xmlHttpRequest, textStatus, error) {
756 function getStatus() {
757 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
758 if ((data != null) && data.success) {
759 /* process Sone information. */
760 $.each(data.sones, function(index, value) {
761 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
763 /* process notifications. */
764 $.each(data.notifications, function(index, value) {
765 oldNotification = $("#sone #notification-area .notification#" + value.id);
766 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
767 if (oldNotification.length != 0) {
768 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
769 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
770 notification.find(".short-text").toggleClass("hidden", opened);
771 notification.find(".text").toggleClass("hidden", !opened);
773 oldNotification.replaceWith(notification.show());
775 $("#sone #notification-area").append(notification);
776 notification.slideDown();
780 $.each(data.removedNotifications, function(index, value) {
781 $("#sone #notification-area .notification#" + value.id).slideUp();
783 /* process new posts. */
784 $.each(data.newPosts, function(index, value) {
785 loadNewPost(value.id, value.sone, value.recipient, value.time);
787 /* process new replies. */
788 $.each(data.newReplies, function(index, value) {
789 loadNewReply(value.id, value.sone, value.post, value.postSone);
791 /* do it again in 5 seconds. */
792 setTimeout(getStatus, 5000);
794 /* data.success was false, wait 30 seconds. */
795 setTimeout(getStatus, 30000);
797 }, function(xmlHttpRequest, textStatus, error) {
798 /* something really bad happend, wait a minute. */
799 setTimeout(getStatus, 60000);
804 * Returns the ID of the currently logged in Sone.
806 * @return The ID of the current Sone, or an empty string if no Sone is logged
809 function getCurrentSoneId() {
810 return $("#currentSoneId").text();
814 * Returns the content of the page-id attribute.
816 * @returns The page ID
818 function getPageId() {
819 return $("#sone .page-id").text();
823 * Returns whether the current page is the index page.
825 * @returns {Boolean} <code>true</code> if the current page is the index page,
826 * <code>false</code> otherwise
828 function isIndexPage() {
829 return getPageId() == "index";
833 * Returns whether the current page is a “view Sone” page.
835 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
836 * page, <code>false</code> otherwise
838 function isViewSonePage() {
839 return getPageId() == "view-sone";
843 * Returns the ID of the currently shown Sone. This will only return a sensible
844 * value if isViewSonePage() returns <code>true</code>.
846 * @returns The ID of the currently shown Sone
848 function getShownSoneId() {
849 return $("#sone .sone-id").text();
853 * Returns whether the current page is a “view post” page.
855 * @returns {Boolean} <code>true</code> if the current page is a “view post”
856 * page, <code>false</code> otherwise
858 function isViewPostPage() {
859 return getPageId() == "view-post";
863 * Returns the ID of the currently shown post. This will only return a sensible
864 * value if isViewPostPage() returns <code>true</code>.
866 * @returns The ID of the currently shown post
868 function getShownPostId() {
869 return $("#sone .post-id").text();
873 * Returns whether the current page is the “known Sones” page.
875 * @returns {Boolean} <code>true</code> if the current page is the “known
876 * Sones” page, <code>false</code> otherwise
878 function isKnownSonesPage() {
879 return getPageId() == "known-sones";
883 * Returns whether a post with the given ID exists on the current page.
886 * The post ID to check for
887 * @returns {Boolean} <code>true</code> if a post with the given ID already
888 * exists on the page, <code>false</code> otherwise
890 function hasPost(postId) {
891 return $(".post#" + postId).length > 0;
895 * Returns whether a reply with the given ID exists on the current page.
898 * The reply ID to check for
899 * @returns {Boolean} <code>true</code> if a reply with the given ID already
900 * exists on the page, <code>false</code> otherwise
902 function hasReply(replyId) {
903 return $("#sone .reply#" + replyId).length > 0;
906 function loadNewPost(postId, soneId, recipientId, time) {
907 if (hasPost(postId)) {
910 if (!isIndexPage()) {
911 if (!isViewPostPage() || (getShownPostId() != postId)) {
912 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
917 if (getPostTime($("#sone .post").last()) > time) {
920 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
921 if ((data != null) && data.success) {
922 if (hasPost(data.post.id)) {
925 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
928 var firstOlderPost = null;
929 $("#sone .post").each(function() {
930 if (getPostTime(this) < data.post.time) {
931 firstOlderPost = $(this);
935 newPost = $(data.post.html).addClass("hidden");
936 if (firstOlderPost != null) {
937 newPost.insertBefore(firstOlderPost);
939 ajaxifyPost(newPost);
946 function loadNewReply(replyId, soneId, postId, postSoneId) {
947 if (hasReply(replyId)) {
950 if (!hasPost(postId)) {
953 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
955 if ((data != null) && data.success) {
956 if (hasReply(data.reply.id)) {
959 $("#sone .post#" + data.reply.postId).each(function() {
960 var firstNewerReply = null;
961 $(this).find(".replies .reply").each(function() {
962 if (getReplyTime(this) > data.reply.time) {
963 firstNewerReply = $(this);
967 newReply = $(data.reply.html).addClass("hidden");
968 if (firstNewerReply != null) {
969 newReply.insertBefore(firstNewerReply);
971 if ($(this).find(".replies .create-reply")) {
972 $(this).find(".replies .create-reply").before(newReply);
974 $(this).find(".replies").append(newReply);
977 ajaxifyReply(newReply);
978 newReply.slideDown();
987 * Marks the given Sone as known if it is still new.
990 * The Sone to mark as known
992 function markSoneAsKnown(soneElement) {
993 if ($(".new", soneElement).length > 0) {
994 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
995 $(soneElement).removeClass("new");
1000 function markPostAsKnown(postElements) {
1001 $(postElements).each(function() {
1003 if ($(postElement).hasClass("new")) {
1004 (function(postElement) {
1005 $(postElement).removeClass("new");
1006 $(".click-to-show", postElement).removeClass("new");
1007 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1011 markReplyAsKnown($(postElements).find(".reply"));
1014 function markReplyAsKnown(replyElements) {
1015 $(replyElements).each(function() {
1016 replyElement = this;
1017 if ($(replyElement).hasClass("new")) {
1018 (function(replyElement) {
1019 $(replyElement).removeClass("new");
1020 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1026 function resetActivity() {
1027 title = document.title;
1028 if (title.indexOf('(') == 0) {
1029 document.title = title.substr(title.indexOf(' ') + 1);
1033 function setActivity() {
1035 title = document.title;
1036 if (title.indexOf('(') != 0) {
1037 document.title = "(!) " + title;
1043 * Creates a new notification.
1046 * The ID of the notificaiton
1048 * The text of the notification
1049 * @param dismissable
1050 * <code>true</code> if the notification can be dismissed by the
1053 function createNotification(id, text, dismissable) {
1054 notification = $("<div></div>").addClass("notification").attr("id", id);
1056 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1057 dismissForm.find("input[name=notification]").val(id);
1058 notification.append(dismissForm);
1060 notification.append(text);
1061 return notification;
1065 * Shows the details of the notification with the given ID.
1067 * @param notificationId
1068 * The ID of the notification
1070 function showNotificationDetails(notificationId) {
1071 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1072 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1076 * Deletes the field with the given ID from the profile.
1079 * The ID of the field to delete
1081 function deleteProfileField(fieldId) {
1082 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1083 if (data && data.success) {
1084 $("#sone .profile-field#" + data.field.id).slideUp();
1090 * Renames a profile field.
1093 * The ID of the field to rename
1095 * The new name of the field
1096 * @param successFunction
1097 * Called when the renaming was successful
1099 function editProfileField(fieldId, newName, successFunction) {
1100 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1101 if (data && data.success) {
1108 * Moves the profile field with the given ID one slot in the given direction.
1111 * The ID of the field to move
1113 * The direction to move in (“up” or “down”)
1114 * @param successFunction
1115 * Function to call on success
1117 function moveProfileField(fieldId, direction, successFunction) {
1118 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1119 if (data && data.success) {
1126 * Moves the profile field with the given ID up one slot.
1129 * The ID of the field to move
1130 * @param successFunction
1131 * Function to call on success
1133 function moveProfileFieldUp(fieldId, successFunction) {
1134 moveProfileField(fieldId, "up", successFunction);
1138 * Moves the profile field with the given ID down one slot.
1141 * The ID of the field to move
1142 * @param successFunction
1143 * Function to call on success
1145 function moveProfileFieldDown(fieldId, successFunction) {
1146 moveProfileField(fieldId, "down", successFunction);
1150 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1155 $(document).ready(function() {
1157 /* this initializes the status update input field. */
1158 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1159 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1160 $("#sone #update-status .select-sender").css("display", "inline");
1161 $("#sone #update-status .sender").hide();
1162 $("#sone #update-status .select-sender button").click(function() {
1163 $("#sone #update-status .sender").show();
1164 $("#sone #update-status .select-sender").hide();
1167 $("#sone #update-status").submit(function() {
1168 if ($(this).find(":input.default:enabled").length > 0) {
1171 sender = $(this).find(":input[name=sender]").val();
1172 text = $(this).find(":input[name=text]:enabled").val();
1173 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1174 if ((data != null) && data.success) {
1175 loadNewPost(data.postId, data.sone, data.recipient);
1178 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1179 $(this).find(":input[name=text]:enabled").val("").blur();
1180 $(this).find(".sender").hide();
1181 $(this).find(".select-sender").show();
1186 /* ajaxify input field on “view Sone” page. */
1187 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1188 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1189 $("#sone #post-message").submit(function() {
1190 text = $(this).find(":input:enabled").val();
1191 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "text": text }, function(data, textStatus) {
1192 if ((data != null) && data.success) {
1193 loadNewPost(data.postId, getCurrentSoneId());
1196 $(this).find(":input:enabled").val("").blur();
1201 /* Ajaxifies all posts. */
1202 /* calling getTranslation here will cache the necessary values. */
1203 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1204 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1205 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1206 $("#sone .post").each(function() {
1213 /* hides all replies but the latest two. */
1214 if (!isViewPostPage()) {
1215 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1216 $("#sone .post .replies").each(function() {
1217 allReplies = $(this).find(".reply");
1218 if (allReplies.length > 2) {
1220 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1221 $(allReplies[replyIndex]).addClass("hidden");
1222 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1224 clickToShowElement = $("<div></div>").addClass("click-to-show");
1226 clickToShowElement.addClass("new");
1228 (function(clickToShowElement, allReplies, text) {
1229 clickToShowElement.text(text);
1230 clickToShowElement.click(function() {
1231 allReplies.removeClass("hidden");
1232 clickToShowElement.addClass("hidden");
1234 })(clickToShowElement, allReplies, text);
1235 $(allReplies[0]).before(clickToShowElement);
1241 $("#sone .sone").each(function() {
1242 ajaxifySone($(this));
1245 /* process all existing notifications, ajaxify dismiss buttons. */
1246 $("#sone #notification-area .notification").each(function() {
1247 ajaxifyNotification($(this));
1250 /* activate status polling. */
1251 setTimeout(getStatus, 5000);
1253 /* reset activity counter when the page has focus. */
1254 $(window).focus(function() {
1257 }).blur(function() {