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 /* no, show(), “display: block” is not what I need. */
41 textarea.attr("style", "display: inline").focus();
43 if (inputField.val() == "") {
44 inputField.addClass("default");
45 inputField.val(defaultText);
47 inputField.hide().attr("disabled", "disabled");
50 $(inputField.get(0).form).submit(function() {
51 inputField.attr("disabled", "disabled");
52 if (!optional && (textarea.val() == "")) {
53 inputField.removeAttr("disabled").focus();
57 })($(this), textarea);
62 * Adds a “comment” link to all status lines contained in the given element.
67 * The element to add a “comment” link to
69 function addCommentLink(postId, element, insertAfterThisElement) {
70 if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
73 commentElement = (function(postId) {
74 separator = $("<span> · </span>").addClass("separator");
75 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
76 replyElement = $("#sone .post#" + postId + " .create-reply");
77 replyElement.removeClass("hidden");
78 replyElement.removeClass("light");
79 (function(replyElement) {
80 replyElement.find("input.reply-input").blur(function() {
81 if ($(this).hasClass("default")) {
82 replyElement.addClass("light");
85 replyElement.removeClass("light");
88 replyElement.find("input.reply-input").focus();
90 return commentElement;
92 $(insertAfterThisElement).after(commentElement.clone(true));
93 $(insertAfterThisElement).after(separator);
96 var translations = {};
99 * Retrieves the translation for the given key and calls the callback function.
100 * The callback function takes a single parameter, the translated string.
103 * The key of the translation string
105 * The callback function
107 function getTranslation(key, callback) {
108 if (key in translations) {
109 callback(translations[key]);
112 $.getJSON("getTranslation.ajax", {"key": key}, function(data, textStatus) {
113 if ((data != null) && data.success) {
114 translations[key] = data.value;
115 callback(data.value);
117 }, function(xmlHttpRequest, textStatus, error) {
123 * Filters the given Sone ID, replacing all “~” characters by an underscore.
126 * The Sone ID to filter
127 * @returns The filtered Sone ID
129 function filterSoneId(soneId) {
130 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
134 * Updates the status of the given Sone.
137 * The ID of the Sone to update
139 * The status of the Sone (“idle”, “unknown”, “inserting”,
142 * Whether the Sone is modified
144 * Whether the Sone is locked
146 * The date and time of the last update (formatted for display)
148 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
149 $("#sone .sone." + filterSoneId(soneId)).
150 toggleClass("unknown", status == "unknown").
151 toggleClass("idle", status == "idle").
152 toggleClass("inserting", status == "inserting").
153 toggleClass("downloading", status == "downloading").
154 toggleClass("modified", modified);
155 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
156 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
157 if (lastUpdated != null) {
158 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
160 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
161 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown);
164 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
168 * Enhances a “delete” button so that the confirmation is done on the same page.
173 * The text to show on the button
174 * @param deleteCallback
175 * The callback that actually deletes something
177 function enhanceDeleteButton(button, text, deleteCallback) {
179 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
180 $(this).fadeOut("slow");
183 }).insertAfter(button);
184 (function(button, newButton) {
185 button.click(function() {
186 button.fadeOut("slow", function() {
187 newButton.fadeIn("slow");
188 $(document).one("click", function() {
189 if (this != newButton.get(0)) {
190 newButton.fadeOut(function() {
198 })(button, newButton);
203 * Enhances a post’s “delete” button.
208 * The ID of the post to delete
210 * The text to replace the button with
212 function enhanceDeletePostButton(button, postId, text) {
213 enhanceDeleteButton(button, text, function() {
214 $.getJSON("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
219 $("#sone .post#" + postId).slideUp();
220 } else if (data.error == "invalid-post-id") {
221 alert("Invalid post ID given!");
222 } else if (data.error == "auth-required") {
223 alert("You need to be logged in.");
224 } else if (data.error == "not-authorized") {
225 alert("You are not allowed to delete this post.");
227 }, function(xmlHttpRequest, textStatus, error) {
234 * Enhances a reply’s “delete” button.
239 * The ID of the reply to delete
241 * The text to replace the button with
243 function enhanceDeleteReplyButton(button, replyId, text) {
244 enhanceDeleteButton(button, text, function() {
245 $.getJSON("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
250 $("#sone .reply#" + replyId).slideUp();
251 } else if (data.error == "invalid-reply-id") {
252 alert("Invalid reply ID given!");
253 } else if (data.error == "auth-required") {
254 alert("You need to be logged in.");
255 } else if (data.error == "not-authorized") {
256 alert("You are not allowed to delete this reply.");
258 }, function(xmlHttpRequest, textStatus, error) {
264 function getFormPassword() {
265 return $("#sone #formPassword").text();
268 function getSoneElement(element) {
269 return $(element).closest(".sone");
273 * Generates a list of Sones by concatening the names of the given sones with a
274 * new line character (“\n”).
277 * The sones to format
278 * @returns {String} The created string
280 function generateSoneList(sones) {
282 $.each(sones, function() {
283 if (soneList != "") {
286 soneList += this.name;
292 * Returns the ID of the Sone that this element belongs to.
295 * The element to locate the matching Sone ID for
296 * @returns The ID of the Sone, or undefined
298 function getSoneId(element) {
299 return getSoneElement(element).find(".id").text();
303 * Returns the element of the post with the given ID.
307 * @returns The element of the post
309 function getPost(postId) {
310 return $("#sone .post#" + postId);
313 function getPostElement(element) {
314 return $(element).closest(".post");
317 function getPostId(element) {
318 return getPostElement(element).attr("id");
321 function getPostTime(element) {
322 return getPostElement(element).find(".post-time").text();
326 * Returns the author of the post the given element belongs to.
329 * The element whose post to get the author for
330 * @returns The ID of the authoring Sone
332 function getPostAuthor(element) {
333 return getPostElement(element).find(".post-author").text();
336 function getReplyElement(element) {
337 return $(element).closest(".reply");
340 function getReplyId(element) {
341 return getReplyElement(element).attr("id");
344 function getReplyTime(element) {
345 return getReplyElement(element).find(".reply-time").text();
349 * Returns the author of the reply the given element belongs to.
352 * The element whose reply to get the author for
353 * @returns The ID of the authoring Sone
355 function getReplyAuthor(element) {
356 return getReplyElement(element).find(".reply-author").text();
359 function likePost(postId) {
360 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
361 if ((data == null) || !data.success) {
364 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
365 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
366 updatePostLikes(postId);
367 }, function(xmlHttpRequest, textStatus, error) {
372 function unlikePost(postId) {
373 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
374 if ((data == null) || !data.success) {
377 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
378 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
379 updatePostLikes(postId);
380 }, function(xmlHttpRequest, textStatus, error) {
385 function updatePostLikes(postId) {
386 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
387 if ((data != null) && data.success) {
388 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
389 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
390 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
392 }, function(xmlHttpRequest, textStatus, error) {
397 function likeReply(replyId) {
398 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
399 if ((data == null) || !data.success) {
402 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
403 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
404 updateReplyLikes(replyId);
405 }, function(xmlHttpRequest, textStatus, error) {
410 function unlikeReply(replyId) {
411 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
412 if ((data == null) || !data.success) {
415 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
416 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
417 updateReplyLikes(replyId);
418 }, function(xmlHttpRequest, textStatus, error) {
424 * Trusts the Sone with the given ID.
427 * The ID of the Sone to trust
429 function trustSone(soneId) {
430 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
431 if ((data != null) && data.success) {
432 updateTrustControls(soneId, data.trustValue);
438 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
441 * The ID of the Sone to distrust
443 function distrustSone(soneId) {
444 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
445 if ((data != null) && data.success) {
446 updateTrustControls(soneId, data.trustValue);
452 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
455 * The ID of the Sone to untrust
457 function untrustSone(soneId) {
458 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
459 if ((data != null) && data.success) {
460 updateTrustControls(soneId, data.trustValue);
466 * Updates the trust controls for all posts and replies of the given Sone,
467 * according to the given trust value.
470 * The ID of the Sone to update all trust controls for
472 * The trust value for the Sone
474 function updateTrustControls(soneId, trustValue) {
475 $("#sone .post").each(function() {
476 if (getPostAuthor(this) == soneId) {
477 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
478 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
479 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
482 $("#sone .reply").each(function() {
483 if (getReplyAuthor(this) == soneId) {
484 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
485 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
486 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
492 * Bookmarks the post with the given ID.
495 * The ID of the post to bookmark
497 function bookmarkPost(postId) {
499 $.getJSON("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
500 if ((data != null) && data.success) {
501 getPost(postId).find(".bookmark").toggleClass("hidden", true);
502 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
509 * Unbookmarks the post with the given ID.
512 * The ID of the post to unbookmark
514 function unbookmarkPost(postId) {
515 $.getJSON("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
516 if ((data != null) && data.success) {
517 getPost(postId).find(".bookmark").toggleClass("hidden", false);
518 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
523 function updateReplyLikes(replyId) {
524 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
525 if ((data != null) && data.success) {
526 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
527 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
528 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
530 }, function(xmlHttpRequest, textStatus, error) {
536 * Posts a reply and calls the given callback when the request finishes.
539 * The ID of the sender
541 * The ID of the post the reply refers to
544 * @param callbackFunction
545 * The callback function to call when the request finishes (takes 3
546 * parameters: success, error, replyId)
548 function postReply(sender, postId, text, callbackFunction) {
549 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
551 /* TODO - show error */
555 callbackFunction(true, null, data.reply, data.sone);
557 callbackFunction(false, data.error);
559 }, function(xmlHttpRequest, textStatus, error) {
565 * Requests information about the reply with the given ID.
568 * The ID of the reply
569 * @param callbackFunction
570 * A callback function (parameters soneId, soneName, replyTime,
571 * replyDisplayTime, text, html)
573 function getReply(replyId, callbackFunction) {
574 $.getJSON("getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
575 if ((data != null) && data.success) {
576 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
578 }, function(xmlHttpRequest, textStatus, error) {
584 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
587 * The Sone to ajaxify
589 function ajaxifySone(soneElement) {
591 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
594 $(".follow", soneElement).submit(function() {
595 var followElement = this;
596 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
597 $(followElement).addClass("hidden");
598 $(followElement).parent().find(".unfollow").removeClass("hidden");
602 $(".unfollow", soneElement).submit(function() {
603 var unfollowElement = this;
604 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
605 $(unfollowElement).addClass("hidden");
606 $(unfollowElement).parent().find(".follow").removeClass("hidden");
610 $(".lock", soneElement).submit(function() {
611 var lockElement = this;
612 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
613 $(lockElement).addClass("hidden");
614 $(lockElement).parent().find(".unlock").removeClass("hidden");
618 $(".unlock", soneElement).submit(function() {
619 var unlockElement = this;
620 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
621 $(unlockElement).addClass("hidden");
622 $(unlockElement).parent().find(".lock").removeClass("hidden");
627 /* mark Sone as known when clicking it. */
628 $(soneElement).click(function() {
629 markSoneAsKnown(soneElement);
634 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
637 * The post element to ajaxify
639 function ajaxifyPost(postElement) {
640 $(postElement).find("form").submit(function() {
643 $(postElement).find(".create-reply button:submit").click(function() {
645 button.attr("disabled", "disabled");
646 sender = $(this.form).find(":input[name=sender]").val();
647 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
648 postId = getPostId(this);
649 text = $(inputField).val();
650 (function(sender, postId, text, inputField) {
651 postReply(sender, postId, text, function(success, error, replyId, soneId) {
653 $(inputField).val("");
654 loadNewReply(replyId, soneId, postId);
655 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
656 $("#sone .post#" + postId + " .create-reply .sender").hide();
657 $("#sone .post#" + postId + " .create-reply .select-sender").show();
658 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
662 button.removeAttr("disabled");
664 })(sender, postId, text, inputField);
668 /* replace all “delete” buttons with javascript. */
669 (function(postElement) {
670 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
671 postId = getPostId(postElement);
672 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
676 /* convert all “like” buttons to javascript functions. */
677 $(postElement).find(".like-post").submit(function() {
678 likePost(getPostId(this));
681 $(postElement).find(".unlike-post").submit(function() {
682 unlikePost(getPostId(this));
686 /* convert trust control buttons to javascript functions. */
687 $(postElement).find(".post-trust").submit(function() {
688 trustSone(getPostAuthor(this));
691 $(postElement).find(".post-distrust").submit(function() {
692 distrustSone(getPostAuthor(this));
695 $(postElement).find(".post-untrust").submit(function() {
696 untrustSone(getPostAuthor(this));
700 /* convert bookmark/unbookmark buttons to javascript functions. */
701 $(postElement).find(".bookmark").submit(function() {
702 bookmarkPost(getPostId(this));
705 $(postElement).find(".unbookmark").submit(function() {
706 unbookmarkPost(getPostId(this));
710 /* convert “show source” link into javascript function. */
711 $(postElement).find(".show-source").each(function() {
712 $("a", this).click(function() {
713 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
714 $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
719 /* add “comment” link. */
720 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
722 /* process all replies. */
723 $(postElement).find(".reply").each(function() {
727 /* process reply input fields. */
728 getTranslation("WebInterface.DefaultText.Reply", function(text) {
729 $(postElement).find("input.reply-input").each(function() {
730 registerInputTextareaSwap(this, text, "text", false, false);
734 /* process sender selection. */
735 $(".select-sender", postElement).css("display", "inline");
736 $(".sender", postElement).hide();
737 $(".select-sender button", postElement).click(function() {
738 $(".sender", postElement).show();
739 $(".select-sender", postElement).hide();
743 /* mark everything as known on click. */
744 $(postElement).click(function(event) {
745 if ($(event.target).hasClass("click-to-show")) {
748 markPostAsKnown(this);
751 /* hide reply input field. */
752 $(postElement).find(".create-reply").addClass("hidden");
756 * Ajaxifies the given reply element.
758 * @param replyElement
759 * The reply element to ajaxify
761 function ajaxifyReply(replyElement) {
762 $(replyElement).find(".like-reply").submit(function() {
763 likeReply(getReplyId(this));
766 $(replyElement).find(".unlike-reply").submit(function() {
767 unlikeReply(getReplyId(this));
770 (function(replyElement) {
771 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
772 $(replyElement).find(".delete-reply button").each(function() {
773 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
777 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
779 /* convert “show source” link into javascript function. */
780 $(replyElement).find(".show-reply-source").each(function() {
781 $("a", this).click(function() {
782 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
783 $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
788 /* convert trust control buttons to javascript functions. */
789 $(replyElement).find(".reply-trust").submit(function() {
790 trustSone(getReplyAuthor(this));
793 $(replyElement).find(".reply-distrust").submit(function() {
794 distrustSone(getReplyAuthor(this));
797 $(replyElement).find(".reply-untrust").submit(function() {
798 untrustSone(getReplyAuthor(this));
804 * Ajaxifies the given notification by replacing the form with AJAX.
806 * @param notification
807 * jQuery object representing the notification.
809 function ajaxifyNotification(notification) {
810 notification.find("form").submit(function() {
813 notification.find("input[name=returnPage]").val($.url.attr("relative"));
814 if (notification.find(".short-text").length > 0) {
815 notification.find(".short-text").removeClass("hidden");
816 notification.find(".text").addClass("hidden");
818 notification.find("form.mark-as-read button").click(function() {
819 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
821 notification.find("a[class^='link-']").each(function() {
822 linkElement = $(this);
823 if (linkElement.is("[href^='viewPost']")) {
824 id = linkElement.attr("class").substr(5);
826 linkElement.attr("href", "#post-" + id);
830 notification.find("form.dismiss button").click(function() {
831 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
832 /* dismiss in case of error, too. */
833 notification.slideUp();
834 }, function(xmlHttpRequest, textStatus, error) {
841 function getStatus() {
842 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
843 if ((data != null) && data.success) {
844 /* process Sone information. */
845 $.each(data.sones, function(index, value) {
846 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
848 /* search for removed notifications. */
849 $("#sone #notification-area .notification").each(function() {
850 notificationId = $(this).attr("id");
851 foundNotification = false;
852 $.each(data.notifications, function(index, value) {
853 if (value.id == notificationId) {
854 foundNotification = true;
858 if (!foundNotification) {
859 $(this).slideUp("normal", function() {
864 /* process notifications. */
865 $.each(data.notifications, function(index, value) {
866 oldNotification = $("#sone #notification-area .notification#" + value.id);
867 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
868 if (oldNotification.length != 0) {
869 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
870 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
871 notification.find(".short-text").toggleClass("hidden", opened);
872 notification.find(".text").toggleClass("hidden", !opened);
874 oldNotification.replaceWith(notification.show());
876 $("#sone #notification-area").append(notification);
877 notification.slideDown();
881 /* process new posts. */
882 $.each(data.newPosts, function(index, value) {
883 loadNewPost(value.id, value.sone, value.recipient, value.time);
885 /* process new replies. */
886 $.each(data.newReplies, function(index, value) {
887 loadNewReply(value.id, value.sone, value.post, value.postSone);
889 /* do it again in 5 seconds. */
890 setTimeout(getStatus, 5000);
892 /* data.success was false, wait 30 seconds. */
893 setTimeout(getStatus, 30000);
895 }, function(xmlHttpRequest, textStatus, error) {
896 /* something really bad happend, wait a minute. */
897 setTimeout(getStatus, 60000);
902 * Returns the ID of the currently logged in Sone.
904 * @return The ID of the current Sone, or an empty string if no Sone is logged
907 function getCurrentSoneId() {
908 return $("#currentSoneId").text();
912 * Returns the content of the page-id attribute.
914 * @returns The page ID
916 function getPageId() {
917 return $("#sone .page-id").text();
921 * Returns whether the current page is the index page.
923 * @returns {Boolean} <code>true</code> if the current page is the index page,
924 * <code>false</code> otherwise
926 function isIndexPage() {
927 return getPageId() == "index";
931 * Returns whether the current page is a “view Sone” page.
933 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
934 * page, <code>false</code> otherwise
936 function isViewSonePage() {
937 return getPageId() == "view-sone";
941 * Returns the ID of the currently shown Sone. This will only return a sensible
942 * value if isViewSonePage() returns <code>true</code>.
944 * @returns The ID of the currently shown Sone
946 function getShownSoneId() {
947 return $("#sone .sone-id").text();
951 * Returns whether the current page is a “view post” page.
953 * @returns {Boolean} <code>true</code> if the current page is a “view post”
954 * page, <code>false</code> otherwise
956 function isViewPostPage() {
957 return getPageId() == "view-post";
961 * Returns the ID of the currently shown post. This will only return a sensible
962 * value if isViewPostPage() returns <code>true</code>.
964 * @returns The ID of the currently shown post
966 function getShownPostId() {
967 return $("#sone .post-id").text();
971 * Returns whether the current page is the “known Sones” page.
973 * @returns {Boolean} <code>true</code> if the current page is the “known
974 * Sones” page, <code>false</code> otherwise
976 function isKnownSonesPage() {
977 return getPageId() == "known-sones";
981 * Returns whether a post with the given ID exists on the current page.
984 * The post ID to check for
985 * @returns {Boolean} <code>true</code> if a post with the given ID already
986 * exists on the page, <code>false</code> otherwise
988 function hasPost(postId) {
989 return $(".post#" + postId).length > 0;
993 * Returns whether a reply with the given ID exists on the current page.
996 * The reply ID to check for
997 * @returns {Boolean} <code>true</code> if a reply with the given ID already
998 * exists on the page, <code>false</code> otherwise
1000 function hasReply(replyId) {
1001 return $("#sone .reply#" + replyId).length > 0;
1004 function loadNewPost(postId, soneId, recipientId, time) {
1005 if (hasPost(postId)) {
1008 if (!isIndexPage()) {
1009 if (!isViewPostPage() || (getShownPostId() != postId)) {
1010 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1015 if (getPostTime($("#sone .post").last()) > time) {
1018 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1019 if ((data != null) && data.success) {
1020 if (hasPost(data.post.id)) {
1023 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1026 var firstOlderPost = null;
1027 $("#sone .post").each(function() {
1028 if (getPostTime(this) < data.post.time) {
1029 firstOlderPost = $(this);
1033 newPost = $(data.post.html).addClass("hidden");
1034 if (firstOlderPost != null) {
1035 newPost.insertBefore(firstOlderPost);
1037 ajaxifyPost(newPost);
1038 newPost.slideDown();
1044 function loadNewReply(replyId, soneId, postId, postSoneId) {
1045 if (hasReply(replyId)) {
1048 if (!hasPost(postId)) {
1051 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1053 if ((data != null) && data.success) {
1054 if (hasReply(data.reply.id)) {
1057 $("#sone .post#" + data.reply.postId).each(function() {
1058 var firstNewerReply = null;
1059 $(this).find(".replies .reply").each(function() {
1060 if (getReplyTime(this) > data.reply.time) {
1061 firstNewerReply = $(this);
1065 newReply = $(data.reply.html).addClass("hidden");
1066 if (firstNewerReply != null) {
1067 newReply.insertBefore(firstNewerReply);
1069 if ($(this).find(".replies .create-reply")) {
1070 $(this).find(".replies .create-reply").before(newReply);
1072 $(this).find(".replies").append(newReply);
1075 ajaxifyReply(newReply);
1076 newReply.slideDown();
1085 * Marks the given Sone as known if it is still new.
1087 * @param soneElement
1088 * The Sone to mark as known
1090 function markSoneAsKnown(soneElement) {
1091 if ($(".new", soneElement).length > 0) {
1092 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1093 $(soneElement).removeClass("new");
1098 function markPostAsKnown(postElements) {
1099 $(postElements).each(function() {
1101 if ($(postElement).hasClass("new")) {
1102 (function(postElement) {
1103 $(postElement).removeClass("new");
1104 $(".click-to-show", postElement).removeClass("new");
1105 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1109 markReplyAsKnown($(postElements).find(".reply"));
1112 function markReplyAsKnown(replyElements) {
1113 $(replyElements).each(function() {
1114 replyElement = this;
1115 if ($(replyElement).hasClass("new")) {
1116 (function(replyElement) {
1117 $(replyElement).removeClass("new");
1118 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1124 function resetActivity() {
1125 title = document.title;
1126 if (title.indexOf('(') == 0) {
1127 setTitle(title.substr(title.indexOf(' ') + 1));
1131 function setActivity() {
1133 title = document.title;
1134 if (title.indexOf('(') != 0) {
1135 setTitle("(!) " + title);
1137 if (!iconBlinking) {
1138 setTimeout(toggleIcon, 1500);
1139 iconBlinking = true;
1145 * Sets the window title after a small delay to prevent race-condition issues.
1150 function setTitle(title) {
1151 setTimeout(function() {
1152 document.title = title;
1156 /** Whether the icon is currently showing activity. */
1157 var iconActive = false;
1159 /** Whether the icon is currently supposed to blink. */
1160 var iconBlinking = false;
1163 * Toggles the icon. If the window has gained focus and the icon is still
1164 * showing the activity state, it is returned to normal.
1166 function toggleIcon() {
1169 changeIcon("images/icon.png");
1172 iconBlinking = false;
1174 iconActive = !iconActive;
1175 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1176 setTimeout(toggleIcon, 1500);
1181 * Changes the icon of the page.
1184 * The new URL of the icon
1186 function changeIcon(iconUrl) {
1187 $("link[rel=icon]").remove();
1188 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1189 $("iframe[id=icon-update]")[0].src += "";
1193 * Creates a new notification.
1196 * The ID of the notificaiton
1198 * The text of the notification
1199 * @param dismissable
1200 * <code>true</code> if the notification can be dismissed by the
1203 function createNotification(id, text, dismissable) {
1204 notification = $("<div></div>").addClass("notification").attr("id", id);
1206 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1207 dismissForm.find("input[name=notification]").val(id);
1208 notification.append(dismissForm);
1210 notification.append(text);
1211 return notification;
1215 * Shows the details of the notification with the given ID.
1217 * @param notificationId
1218 * The ID of the notification
1220 function showNotificationDetails(notificationId) {
1221 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1222 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1226 * Deletes the field with the given ID from the profile.
1229 * The ID of the field to delete
1231 function deleteProfileField(fieldId) {
1232 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1233 if (data && data.success) {
1234 $("#sone .profile-field#" + data.field.id).slideUp();
1240 * Renames a profile field.
1243 * The ID of the field to rename
1245 * The new name of the field
1246 * @param successFunction
1247 * Called when the renaming was successful
1249 function editProfileField(fieldId, newName, successFunction) {
1250 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1251 if (data && data.success) {
1258 * Moves the profile field with the given ID one slot in the given direction.
1261 * The ID of the field to move
1263 * The direction to move in (“up” or “down”)
1264 * @param successFunction
1265 * Function to call on success
1267 function moveProfileField(fieldId, direction, successFunction) {
1268 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1269 if (data && data.success) {
1276 * Moves the profile field with the given ID up one slot.
1279 * The ID of the field to move
1280 * @param successFunction
1281 * Function to call on success
1283 function moveProfileFieldUp(fieldId, successFunction) {
1284 moveProfileField(fieldId, "up", successFunction);
1288 * Moves the profile field with the given ID down one slot.
1291 * The ID of the field to move
1292 * @param successFunction
1293 * Function to call on success
1295 function moveProfileFieldDown(fieldId, successFunction) {
1296 moveProfileField(fieldId, "down", successFunction);
1300 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1305 $(document).ready(function() {
1307 /* this initializes the status update input field. */
1308 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1309 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1310 $("#sone #update-status .select-sender").css("display", "inline");
1311 $("#sone #update-status .sender").hide();
1312 $("#sone #update-status .select-sender button").click(function() {
1313 $("#sone #update-status .sender").show();
1314 $("#sone #update-status .select-sender").hide();
1317 $("#sone #update-status").submit(function() {
1318 button = $("button:submit", this);
1319 button.attr("disabled", "disabled");
1320 if ($(this).find(":input.default:enabled").length > 0) {
1323 sender = $(this).find(":input[name=sender]").val();
1324 text = $(this).find(":input[name=text]:enabled").val();
1325 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1326 if ((data != null) && data.success) {
1327 loadNewPost(data.postId, data.sone, data.recipient);
1329 button.removeAttr("disabled");
1331 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1332 $(this).find(":input[name=text]:enabled").val("").blur();
1333 $(this).find(".sender").hide();
1334 $(this).find(".select-sender").show();
1339 /* ajaxify the search input field. */
1340 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1341 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1344 /* ajaxify input field on “view Sone” page. */
1345 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1346 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1347 $("#sone #post-message .select-sender").css("display", "inline");
1348 $("#sone #post-message .sender").hide();
1349 $("#sone #post-message .select-sender button").click(function() {
1350 $("#sone #post-message .sender").show();
1351 $("#sone #post-message .select-sender").hide();
1354 $("#sone #post-message").submit(function() {
1355 sender = $(this).find(":input[name=sender]").val();
1356 text = $(this).find(":input[name=text]:enabled").val();
1357 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text }, function(data, textStatus) {
1358 if ((data != null) && data.success) {
1359 loadNewPost(data.postId, getCurrentSoneId());
1362 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1363 $(this).find(":input[name=text]:enabled").val("").blur();
1364 $(this).find(".sender").hide();
1365 $(this).find(".select-sender").show();
1370 /* Ajaxifies all posts. */
1371 /* calling getTranslation here will cache the necessary values. */
1372 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1373 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1374 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1375 $("#sone .post").each(function() {
1382 /* hides all replies but the latest two. */
1383 if (!isViewPostPage()) {
1384 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1385 $("#sone .post .replies").each(function() {
1386 allReplies = $(this).find(".reply");
1387 if (allReplies.length > 2) {
1389 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1390 $(allReplies[replyIndex]).addClass("hidden");
1391 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1393 clickToShowElement = $("<div></div>").addClass("click-to-show");
1395 clickToShowElement.addClass("new");
1397 (function(clickToShowElement, allReplies, text) {
1398 clickToShowElement.text(text);
1399 clickToShowElement.click(function() {
1400 allReplies.removeClass("hidden");
1401 clickToShowElement.addClass("hidden");
1403 })(clickToShowElement, allReplies, text);
1404 $(allReplies[0]).before(clickToShowElement);
1410 $("#sone .sone").each(function() {
1411 ajaxifySone($(this));
1414 /* process all existing notifications, ajaxify dismiss buttons. */
1415 $("#sone #notification-area .notification").each(function() {
1416 ajaxifyNotification($(this));
1419 /* activate status polling. */
1420 setTimeout(getStatus, 5000);
1422 /* reset activity counter when the page has focus. */
1423 $(window).focus(function() {
1426 }).blur(function() {