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 markPostAsKnown(getPostElement(this));
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();
301 function getPostElement(element) {
302 return $(element).closest(".post");
305 function getPostId(element) {
306 return getPostElement(element).attr("id");
309 function getPostTime(element) {
310 return getPostElement(element).find(".post-time").text();
314 * Returns the author of the post the given element belongs to.
317 * The element whose post to get the author for
318 * @returns The ID of the authoring Sone
320 function getPostAuthor(element) {
321 return getPostElement(element).find(".post-author").text();
324 function getReplyElement(element) {
325 return $(element).closest(".reply");
328 function getReplyId(element) {
329 return getReplyElement(element).attr("id");
332 function getReplyTime(element) {
333 return getReplyElement(element).find(".reply-time").text();
337 * Returns the author of the reply the given element belongs to.
340 * The element whose reply to get the author for
341 * @returns The ID of the authoring Sone
343 function getReplyAuthor(element) {
344 return getReplyElement(element).find(".reply-author").text();
347 function likePost(postId) {
348 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
349 if ((data == null) || !data.success) {
352 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
353 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
354 updatePostLikes(postId);
355 }, function(xmlHttpRequest, textStatus, error) {
360 function unlikePost(postId) {
361 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
362 if ((data == null) || !data.success) {
365 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
366 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
367 updatePostLikes(postId);
368 }, function(xmlHttpRequest, textStatus, error) {
373 function updatePostLikes(postId) {
374 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
375 if ((data != null) && data.success) {
376 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
377 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
378 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
380 }, function(xmlHttpRequest, textStatus, error) {
385 function likeReply(replyId) {
386 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
387 if ((data == null) || !data.success) {
390 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
391 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
392 updateReplyLikes(replyId);
393 }, function(xmlHttpRequest, textStatus, error) {
398 function unlikeReply(replyId) {
399 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
400 if ((data == null) || !data.success) {
403 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
404 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
405 updateReplyLikes(replyId);
406 }, function(xmlHttpRequest, textStatus, error) {
412 * Trusts the Sone with the given ID.
415 * The ID of the Sone to trust
417 function trustSone(soneId) {
418 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
419 if ((data != null) && data.success) {
420 updateTrustControls(soneId, data.trustValue);
426 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
429 * The ID of the Sone to distrust
431 function distrustSone(soneId) {
432 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
433 if ((data != null) && data.success) {
434 updateTrustControls(soneId, data.trustValue);
440 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
443 * The ID of the Sone to untrust
445 function untrustSone(soneId) {
446 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
447 if ((data != null) && data.success) {
448 updateTrustControls(soneId, data.trustValue);
454 * Updates the trust controls for all posts and replies of the given Sone,
455 * according to the given trust value.
458 * The ID of the Sone to update all trust controls for
460 * The trust value for the Sone
462 function updateTrustControls(soneId, trustValue) {
463 $("#sone .post").each(function() {
464 if (getPostAuthor(this) == soneId) {
465 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
466 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
467 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
470 $("#sone .reply").each(function() {
471 if (getReplyAuthor(this) == soneId) {
472 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
473 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
474 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
479 function updateReplyLikes(replyId) {
480 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
481 if ((data != null) && data.success) {
482 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
483 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
484 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
486 }, function(xmlHttpRequest, textStatus, error) {
492 * Posts a reply and calls the given callback when the request finishes.
495 * The ID of the sender
497 * The ID of the post the reply refers to
500 * @param callbackFunction
501 * The callback function to call when the request finishes (takes 3
502 * parameters: success, error, replyId)
504 function postReply(sender, postId, text, callbackFunction) {
505 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
507 /* TODO - show error */
511 callbackFunction(true, null, data.reply, data.sone);
513 callbackFunction(false, data.error);
515 }, function(xmlHttpRequest, textStatus, error) {
521 * Requests information about the reply with the given ID.
524 * The ID of the reply
525 * @param callbackFunction
526 * A callback function (parameters soneId, soneName, replyTime,
527 * replyDisplayTime, text, html)
529 function getReply(replyId, callbackFunction) {
530 $.getJSON("getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
531 if ((data != null) && data.success) {
532 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
534 }, function(xmlHttpRequest, textStatus, error) {
540 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
543 * The Sone to ajaxify
545 function ajaxifySone(soneElement) {
547 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
550 $(".follow", soneElement).submit(function() {
551 var followElement = this;
552 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
553 $(followElement).addClass("hidden");
554 $(followElement).parent().find(".unfollow").removeClass("hidden");
558 $(".unfollow", soneElement).submit(function() {
559 var unfollowElement = this;
560 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
561 $(unfollowElement).addClass("hidden");
562 $(unfollowElement).parent().find(".follow").removeClass("hidden");
566 $(".lock", soneElement).submit(function() {
567 var lockElement = this;
568 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
569 $(lockElement).addClass("hidden");
570 $(lockElement).parent().find(".unlock").removeClass("hidden");
574 $(".unlock", soneElement).submit(function() {
575 var unlockElement = this;
576 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
577 $(unlockElement).addClass("hidden");
578 $(unlockElement).parent().find(".lock").removeClass("hidden");
585 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
588 * The post element to ajaxify
590 function ajaxifyPost(postElement) {
591 $(postElement).find("form").submit(function() {
594 $(postElement).find(".create-reply button:submit").click(function() {
595 sender = $(this.form).find(":input[name=sender]").val();
596 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
597 postId = getPostId(this);
598 text = $(inputField).val();
599 (function(sender, postId, text, inputField) {
600 postReply(sender, postId, text, function(success, error, replyId, soneId) {
602 $(inputField).val("");
603 loadNewReply(replyId, soneId, postId);
604 markPostAsKnown(getPostElement(inputField));
605 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
606 $("#sone .post#" + postId + " .create-reply .sender").hide();
607 $("#sone .post#" + postId + " .create-reply .select-sender").show();
608 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
613 })(sender, postId, text, inputField);
617 /* replace all “delete” buttons with javascript. */
618 (function(postElement) {
619 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
620 postId = getPostId(postElement);
621 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
625 /* convert all “like” buttons to javascript functions. */
626 $(postElement).find(".like-post").submit(function() {
627 likePost(getPostId(this));
628 markPostAsKnown(getPostElement(this));
631 $(postElement).find(".unlike-post").submit(function() {
632 unlikePost(getPostId(this));
633 markPostAsKnown(getPostElement(this));
637 /* convert trust control buttons to javascript functions. */
638 $(postElement).find(".post-trust").submit(function() {
639 trustSone(getPostAuthor(this));
642 $(postElement).find(".post-distrust").submit(function() {
643 distrustSone(getPostAuthor(this));
646 $(postElement).find(".post-untrust").submit(function() {
647 untrustSone(getPostAuthor(this));
651 /* add “comment” link. */
652 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
654 /* process all replies. */
655 $(postElement).find(".reply").each(function() {
659 /* process reply input fields. */
660 getTranslation("WebInterface.DefaultText.Reply", function(text) {
661 $(postElement).find("input.reply-input").each(function() {
662 registerInputTextareaSwap(this, text, "text", false, false);
666 /* process sender selection. */
667 $(".select-sender", postElement).css("display", "inline");
668 $(".sender", postElement).hide();
669 $(".select-sender button", postElement).click(function() {
670 $(".sender", postElement).show();
671 $(".select-sender", postElement).hide();
675 /* mark everything as known on click. */
676 $(postElement).click(function(event) {
677 if ($(event.target).hasClass("click-to-show")) {
680 markPostAsKnown(this);
683 /* hide reply input field. */
684 $(postElement).find(".create-reply").addClass("hidden");
688 * Ajaxifies the given reply element.
690 * @param replyElement
691 * The reply element to ajaxify
693 function ajaxifyReply(replyElement) {
694 $(replyElement).find(".like-reply").submit(function() {
695 likeReply(getReplyId(this));
696 markPostAsKnown(getPostElement(this));
699 $(replyElement).find(".unlike-reply").submit(function() {
700 unlikeReply(getReplyId(this));
701 markPostAsKnown(getPostElement(this));
704 (function(replyElement) {
705 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
706 $(replyElement).find(".delete-reply button").each(function() {
707 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
711 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
713 /* convert trust control buttons to javascript functions. */
714 $(replyElement).find(".reply-trust").submit(function() {
715 trustSone(getReplyAuthor(this));
718 $(replyElement).find(".reply-distrust").submit(function() {
719 distrustSone(getReplyAuthor(this));
722 $(replyElement).find(".reply-untrust").submit(function() {
723 untrustSone(getReplyAuthor(this));
727 /* mark post and all replies as known on click. */
728 $(replyElement).click(function() {
729 markPostAsKnown(getPostElement(this));
734 * Ajaxifies the given notification by replacing the form with AJAX.
736 * @param notification
737 * jQuery object representing the notification.
739 function ajaxifyNotification(notification) {
740 notification.find("form").submit(function() {
743 notification.find("input[name=returnPage]").val($.url.attr("relative"));
744 if (notification.find(".short-text").length > 0) {
745 notification.find(".short-text").removeClass("hidden");
746 notification.find(".text").addClass("hidden");
748 notification.find("form.mark-as-read button").click(function() {
749 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
751 notification.find("form.dismiss button").click(function() {
752 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
753 /* dismiss in case of error, too. */
754 notification.slideUp();
755 }, function(xmlHttpRequest, textStatus, error) {
762 function getStatus() {
763 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
764 if ((data != null) && data.success) {
765 /* process Sone information. */
766 $.each(data.sones, function(index, value) {
767 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
769 /* process notifications. */
770 $.each(data.notifications, function(index, value) {
771 oldNotification = $("#sone #notification-area .notification#" + value.id);
772 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
773 if (oldNotification.length != 0) {
774 if (oldNotification.find(".short-text").length > 0) {
775 opened = !oldNotification.find(".short-text").hasClass("hidden");
776 notification.find(".short-text").toggleClass("hidden", opened);
777 notification.find(".text").toggleClass("hidden", !opened);
779 oldNotification.replaceWith(notification.show());
781 $("#sone #notification-area").append(notification);
782 notification.slideDown();
786 $.each(data.removedNotifications, function(index, value) {
787 $("#sone #notification-area .notification#" + value.id).slideUp();
789 /* process new posts. */
790 $.each(data.newPosts, function(index, value) {
791 loadNewPost(value.id, value.sone, value.recipient, value.time);
793 /* process new replies. */
794 $.each(data.newReplies, function(index, value) {
795 loadNewReply(value.id, value.sone, value.post, value.postSone);
797 /* do it again in 5 seconds. */
798 setTimeout(getStatus, 5000);
800 /* data.success was false, wait 30 seconds. */
801 setTimeout(getStatus, 30000);
803 }, function(xmlHttpRequest, textStatus, error) {
804 /* something really bad happend, wait a minute. */
805 setTimeout(getStatus, 60000);
810 * Returns the ID of the currently logged in Sone.
812 * @return The ID of the current Sone, or an empty string if no Sone is logged
815 function getCurrentSoneId() {
816 return $("#currentSoneId").text();
820 * Returns the content of the page-id attribute.
822 * @returns The page ID
824 function getPageId() {
825 return $("#sone .page-id").text();
829 * Returns whether the current page is the index page.
831 * @returns {Boolean} <code>true</code> if the current page is the index page,
832 * <code>false</code> otherwise
834 function isIndexPage() {
835 return getPageId() == "index";
839 * Returns whether the current page is a “view Sone” page.
841 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
842 * page, <code>false</code> otherwise
844 function isViewSonePage() {
845 return getPageId() == "view-sone";
849 * Returns the ID of the currently shown Sone. This will only return a sensible
850 * value if isViewSonePage() returns <code>true</code>.
852 * @returns The ID of the currently shown Sone
854 function getShownSoneId() {
855 return $("#sone .sone-id").text();
859 * Returns whether the current page is a “view post” page.
861 * @returns {Boolean} <code>true</code> if the current page is a “view post”
862 * page, <code>false</code> otherwise
864 function isViewPostPage() {
865 return getPageId() == "view-post";
869 * Returns the ID of the currently shown post. This will only return a sensible
870 * value if isViewPostPage() returns <code>true</code>.
872 * @returns The ID of the currently shown post
874 function getShownPostId() {
875 return $("#sone .post-id").text();
879 * Returns whether the current page is the “known Sones” page.
881 * @returns {Boolean} <code>true</code> if the current page is the “known
882 * Sones” page, <code>false</code> otherwise
884 function isKnownSonesPage() {
885 return getPageId() == "known-sones";
889 * Returns whether a post with the given ID exists on the current page.
892 * The post ID to check for
893 * @returns {Boolean} <code>true</code> if a post with the given ID already
894 * exists on the page, <code>false</code> otherwise
896 function hasPost(postId) {
897 return $(".post#" + postId).length > 0;
901 * Returns whether a reply with the given ID exists on the current page.
904 * The reply ID to check for
905 * @returns {Boolean} <code>true</code> if a reply with the given ID already
906 * exists on the page, <code>false</code> otherwise
908 function hasReply(replyId) {
909 return $("#sone .reply#" + replyId).length > 0;
912 function loadNewPost(postId, soneId, recipientId, time) {
913 if (hasPost(postId)) {
916 if (!isIndexPage()) {
917 if (!isViewPostPage() || (getShownPostId() != postId)) {
918 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
923 if (getPostTime($("#sone .post").last()) > time) {
926 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
927 if ((data != null) && data.success) {
928 if (hasPost(data.post.id)) {
931 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
934 var firstOlderPost = null;
935 $("#sone .post").each(function() {
936 if (getPostTime(this) < data.post.time) {
937 firstOlderPost = $(this);
941 newPost = $(data.post.html).addClass("hidden");
942 if (firstOlderPost != null) {
943 newPost.insertBefore(firstOlderPost);
945 ajaxifyPost(newPost);
952 function loadNewReply(replyId, soneId, postId, postSoneId) {
953 if (hasReply(replyId)) {
956 if (!hasPost(postId)) {
959 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
961 if ((data != null) && data.success) {
962 if (hasReply(data.reply.id)) {
965 $("#sone .post#" + data.reply.postId).each(function() {
966 var firstNewerReply = null;
967 $(this).find(".replies .reply").each(function() {
968 if (getReplyTime(this) > data.reply.time) {
969 firstNewerReply = $(this);
973 newReply = $(data.reply.html).addClass("hidden");
974 if (firstNewerReply != null) {
975 newReply.insertBefore(firstNewerReply);
977 if ($(this).find(".replies .create-reply")) {
978 $(this).find(".replies .create-reply").before(newReply);
980 $(this).find(".replies").append(newReply);
983 ajaxifyReply(newReply);
984 newReply.slideDown();
993 * Marks the given Sone as known if it is still new.
996 * The Sone to mark as known
998 function markSoneAsKnown(soneElement) {
999 if ($(".new", soneElement).length > 0) {
1000 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1001 $(soneElement).removeClass("new");
1006 function markPostAsKnown(postElements) {
1007 $(postElements).each(function() {
1009 if ($(postElement).hasClass("new")) {
1010 (function(postElement) {
1011 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)}, function(data, textStatus) {
1012 $(postElement).removeClass("new");
1013 $(".click-to-show", postElement).removeClass("new");
1018 markReplyAsKnown($(postElements).find(".reply"));
1021 function markReplyAsKnown(replyElements) {
1022 $(replyElements).each(function() {
1023 replyElement = this;
1024 if ($(replyElement).hasClass("new")) {
1025 (function(replyElement) {
1026 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)}, function(data, textStatus) {
1027 $(replyElement).removeClass("new");
1034 function resetActivity() {
1035 title = document.title;
1036 if (title.indexOf('(') == 0) {
1037 document.title = title.substr(title.indexOf(' ') + 1);
1041 function setActivity() {
1043 title = document.title;
1044 if (title.indexOf('(') != 0) {
1045 document.title = "(!) " + title;
1051 * Creates a new notification.
1054 * The ID of the notificaiton
1056 * The text of the notification
1057 * @param dismissable
1058 * <code>true</code> if the notification can be dismissed by the
1061 function createNotification(id, text, dismissable) {
1062 notification = $("<div></div>").addClass("notification").attr("id", id);
1064 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1065 dismissForm.find("input[name=notification]").val(id);
1066 notification.append(dismissForm);
1068 notification.append(text);
1069 return notification;
1073 * Shows the details of the notification with the given ID.
1075 * @param notificationId
1076 * The ID of the notification
1078 function showNotificationDetails(notificationId) {
1079 $("#sone .notification#" + notificationId + " .text").show();
1080 $("#sone .notification#" + notificationId + " .short-text").hide();
1084 * Deletes the field with the given ID from the profile.
1087 * The ID of the field to delete
1089 function deleteProfileField(fieldId) {
1090 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1091 if (data && data.success) {
1092 $("#sone .profile-field#" + data.field.id).slideUp();
1098 * Renames a profile field.
1101 * The ID of the field to rename
1103 * The new name of the field
1104 * @param successFunction
1105 * Called when the renaming was successful
1107 function editProfileField(fieldId, newName, successFunction) {
1108 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1109 if (data && data.success) {
1116 * Moves the profile field with the given ID one slot in the given direction.
1119 * The ID of the field to move
1121 * The direction to move in (“up” or “down”)
1122 * @param successFunction
1123 * Function to call on success
1125 function moveProfileField(fieldId, direction, successFunction) {
1126 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1127 if (data && data.success) {
1134 * Moves the profile field with the given ID up one slot.
1137 * The ID of the field to move
1138 * @param successFunction
1139 * Function to call on success
1141 function moveProfileFieldUp(fieldId, successFunction) {
1142 moveProfileField(fieldId, "up", successFunction);
1146 * Moves the profile field with the given ID down one slot.
1149 * The ID of the field to move
1150 * @param successFunction
1151 * Function to call on success
1153 function moveProfileFieldDown(fieldId, successFunction) {
1154 moveProfileField(fieldId, "down", successFunction);
1158 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1163 $(document).ready(function() {
1165 /* this initializes the status update input field. */
1166 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1167 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1168 $("#sone #update-status .select-sender").css("display", "inline");
1169 $("#sone #update-status .sender").hide();
1170 $("#sone #update-status .select-sender button").click(function() {
1171 $("#sone #update-status .sender").show();
1172 $("#sone #update-status .select-sender").hide();
1175 $("#sone #update-status").submit(function() {
1176 if ($(this).find(":input.default:enabled").length > 0) {
1179 sender = $(this).find(":input[name=sender]").val();
1180 text = $(this).find(":input[name=text]:enabled").val();
1181 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1182 if ((data != null) && data.success) {
1183 loadNewPost(data.postId, data.sone, data.recipient);
1186 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1187 $(this).find(":input[name=text]:enabled").val("").blur();
1188 $(this).find(".sender").hide();
1189 $(this).find(".select-sender").show();
1194 /* ajaxify input field on “view Sone” page. */
1195 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1196 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1197 $("#sone #post-message").submit(function() {
1198 text = $(this).find(":input:enabled").val();
1199 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "text": text }, function(data, textStatus) {
1200 if ((data != null) && data.success) {
1201 loadNewPost(data.postId, getCurrentSoneId());
1204 $(this).find(":input:enabled").val("").blur();
1209 /* Ajaxifies all posts. */
1210 /* calling getTranslation here will cache the necessary values. */
1211 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1212 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1213 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1214 $("#sone .post").each(function() {
1221 /* hides all replies but the latest two. */
1222 if (!isViewPostPage()) {
1223 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1224 $("#sone .post .replies").each(function() {
1225 allReplies = $(this).find(".reply");
1226 if (allReplies.length > 2) {
1228 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1229 $(allReplies[replyIndex]).addClass("hidden");
1230 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1232 clickToShowElement = $("<div></div>").addClass("click-to-show");
1234 clickToShowElement.addClass("new");
1236 (function(clickToShowElement, allReplies, text) {
1237 clickToShowElement.text(text);
1238 clickToShowElement.click(function() {
1239 allReplies.removeClass("hidden");
1240 clickToShowElement.addClass("hidden");
1242 })(clickToShowElement, allReplies, text);
1243 $(allReplies[0]).before(clickToShowElement);
1249 $("#sone .sone").each(function() {
1250 ajaxifySone($(this));
1253 /* process all existing notifications, ajaxify dismiss buttons. */
1254 $("#sone #notification-area .notification").each(function() {
1255 ajaxifyNotification($(this));
1258 /* activate status polling. */
1259 setTimeout(getStatus, 5000);
1261 /* reset activity counter when the page has focus. */
1262 $(window).focus(function() {
1265 }).blur(function() {