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 /* process notifications. */
849 $.each(data.notifications, function(index, value) {
850 oldNotification = $("#sone #notification-area .notification#" + value.id);
851 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
852 if (oldNotification.length != 0) {
853 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
854 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
855 notification.find(".short-text").toggleClass("hidden", opened);
856 notification.find(".text").toggleClass("hidden", !opened);
858 oldNotification.replaceWith(notification.show());
860 $("#sone #notification-area").append(notification);
861 notification.slideDown();
865 $.each(data.removedNotifications, function(index, value) {
866 $("#sone #notification-area .notification#" + value.id).slideUp();
868 /* process new posts. */
869 $.each(data.newPosts, function(index, value) {
870 loadNewPost(value.id, value.sone, value.recipient, value.time);
872 /* process new replies. */
873 $.each(data.newReplies, function(index, value) {
874 loadNewReply(value.id, value.sone, value.post, value.postSone);
876 /* do it again in 5 seconds. */
877 setTimeout(getStatus, 5000);
879 /* data.success was false, wait 30 seconds. */
880 setTimeout(getStatus, 30000);
882 }, function(xmlHttpRequest, textStatus, error) {
883 /* something really bad happend, wait a minute. */
884 setTimeout(getStatus, 60000);
889 * Returns the ID of the currently logged in Sone.
891 * @return The ID of the current Sone, or an empty string if no Sone is logged
894 function getCurrentSoneId() {
895 return $("#currentSoneId").text();
899 * Returns the content of the page-id attribute.
901 * @returns The page ID
903 function getPageId() {
904 return $("#sone .page-id").text();
908 * Returns whether the current page is the index page.
910 * @returns {Boolean} <code>true</code> if the current page is the index page,
911 * <code>false</code> otherwise
913 function isIndexPage() {
914 return getPageId() == "index";
918 * Returns whether the current page is a “view Sone” page.
920 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
921 * page, <code>false</code> otherwise
923 function isViewSonePage() {
924 return getPageId() == "view-sone";
928 * Returns the ID of the currently shown Sone. This will only return a sensible
929 * value if isViewSonePage() returns <code>true</code>.
931 * @returns The ID of the currently shown Sone
933 function getShownSoneId() {
934 return $("#sone .sone-id").text();
938 * Returns whether the current page is a “view post” page.
940 * @returns {Boolean} <code>true</code> if the current page is a “view post”
941 * page, <code>false</code> otherwise
943 function isViewPostPage() {
944 return getPageId() == "view-post";
948 * Returns the ID of the currently shown post. This will only return a sensible
949 * value if isViewPostPage() returns <code>true</code>.
951 * @returns The ID of the currently shown post
953 function getShownPostId() {
954 return $("#sone .post-id").text();
958 * Returns whether the current page is the “known Sones” page.
960 * @returns {Boolean} <code>true</code> if the current page is the “known
961 * Sones” page, <code>false</code> otherwise
963 function isKnownSonesPage() {
964 return getPageId() == "known-sones";
968 * Returns whether a post with the given ID exists on the current page.
971 * The post ID to check for
972 * @returns {Boolean} <code>true</code> if a post with the given ID already
973 * exists on the page, <code>false</code> otherwise
975 function hasPost(postId) {
976 return $(".post#" + postId).length > 0;
980 * Returns whether a reply with the given ID exists on the current page.
983 * The reply ID to check for
984 * @returns {Boolean} <code>true</code> if a reply with the given ID already
985 * exists on the page, <code>false</code> otherwise
987 function hasReply(replyId) {
988 return $("#sone .reply#" + replyId).length > 0;
991 function loadNewPost(postId, soneId, recipientId, time) {
992 if (hasPost(postId)) {
995 if (!isIndexPage()) {
996 if (!isViewPostPage() || (getShownPostId() != postId)) {
997 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1002 if (getPostTime($("#sone .post").last()) > time) {
1005 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1006 if ((data != null) && data.success) {
1007 if (hasPost(data.post.id)) {
1010 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1013 var firstOlderPost = null;
1014 $("#sone .post").each(function() {
1015 if (getPostTime(this) < data.post.time) {
1016 firstOlderPost = $(this);
1020 newPost = $(data.post.html).addClass("hidden");
1021 if (firstOlderPost != null) {
1022 newPost.insertBefore(firstOlderPost);
1024 ajaxifyPost(newPost);
1025 newPost.slideDown();
1031 function loadNewReply(replyId, soneId, postId, postSoneId) {
1032 if (hasReply(replyId)) {
1035 if (!hasPost(postId)) {
1038 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1040 if ((data != null) && data.success) {
1041 if (hasReply(data.reply.id)) {
1044 $("#sone .post#" + data.reply.postId).each(function() {
1045 var firstNewerReply = null;
1046 $(this).find(".replies .reply").each(function() {
1047 if (getReplyTime(this) > data.reply.time) {
1048 firstNewerReply = $(this);
1052 newReply = $(data.reply.html).addClass("hidden");
1053 if (firstNewerReply != null) {
1054 newReply.insertBefore(firstNewerReply);
1056 if ($(this).find(".replies .create-reply")) {
1057 $(this).find(".replies .create-reply").before(newReply);
1059 $(this).find(".replies").append(newReply);
1062 ajaxifyReply(newReply);
1063 newReply.slideDown();
1072 * Marks the given Sone as known if it is still new.
1074 * @param soneElement
1075 * The Sone to mark as known
1077 function markSoneAsKnown(soneElement) {
1078 if ($(".new", soneElement).length > 0) {
1079 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1080 $(soneElement).removeClass("new");
1085 function markPostAsKnown(postElements) {
1086 $(postElements).each(function() {
1088 if ($(postElement).hasClass("new")) {
1089 (function(postElement) {
1090 $(postElement).removeClass("new");
1091 $(".click-to-show", postElement).removeClass("new");
1092 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1096 markReplyAsKnown($(postElements).find(".reply"));
1099 function markReplyAsKnown(replyElements) {
1100 $(replyElements).each(function() {
1101 replyElement = this;
1102 if ($(replyElement).hasClass("new")) {
1103 (function(replyElement) {
1104 $(replyElement).removeClass("new");
1105 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1111 function resetActivity() {
1112 title = document.title;
1113 if (title.indexOf('(') == 0) {
1114 setTitle(title.substr(title.indexOf(' ') + 1));
1118 function setActivity() {
1120 title = document.title;
1121 if (title.indexOf('(') != 0) {
1122 setTitle("(!) " + title);
1124 if (!iconBlinking) {
1125 setTimeout(toggleIcon, 1500);
1126 iconBlinking = true;
1132 * Sets the window title after a small delay to prevent race-condition issues.
1137 function setTitle(title) {
1138 setTimeout(function() {
1139 document.title = title;
1143 /** Whether the icon is currently showing activity. */
1144 var iconActive = false;
1146 /** Whether the icon is currently supposed to blink. */
1147 var iconBlinking = false;
1150 * Toggles the icon. If the window has gained focus and the icon is still
1151 * showing the activity state, it is returned to normal.
1153 function toggleIcon() {
1156 changeIcon("images/icon.png");
1159 iconBlinking = false;
1161 iconActive = !iconActive;
1162 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1163 setTimeout(toggleIcon, 1500);
1168 * Changes the icon of the page.
1171 * The new URL of the icon
1173 function changeIcon(iconUrl) {
1174 $("link[rel=icon]").remove();
1175 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1176 $("iframe[id=icon-update]")[0].src += "";
1180 * Creates a new notification.
1183 * The ID of the notificaiton
1185 * The text of the notification
1186 * @param dismissable
1187 * <code>true</code> if the notification can be dismissed by the
1190 function createNotification(id, text, dismissable) {
1191 notification = $("<div></div>").addClass("notification").attr("id", id);
1193 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1194 dismissForm.find("input[name=notification]").val(id);
1195 notification.append(dismissForm);
1197 notification.append(text);
1198 return notification;
1202 * Shows the details of the notification with the given ID.
1204 * @param notificationId
1205 * The ID of the notification
1207 function showNotificationDetails(notificationId) {
1208 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1209 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1213 * Deletes the field with the given ID from the profile.
1216 * The ID of the field to delete
1218 function deleteProfileField(fieldId) {
1219 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1220 if (data && data.success) {
1221 $("#sone .profile-field#" + data.field.id).slideUp();
1227 * Renames a profile field.
1230 * The ID of the field to rename
1232 * The new name of the field
1233 * @param successFunction
1234 * Called when the renaming was successful
1236 function editProfileField(fieldId, newName, successFunction) {
1237 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1238 if (data && data.success) {
1245 * Moves the profile field with the given ID one slot in the given direction.
1248 * The ID of the field to move
1250 * The direction to move in (“up” or “down”)
1251 * @param successFunction
1252 * Function to call on success
1254 function moveProfileField(fieldId, direction, successFunction) {
1255 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1256 if (data && data.success) {
1263 * Moves the profile field with the given ID up one slot.
1266 * The ID of the field to move
1267 * @param successFunction
1268 * Function to call on success
1270 function moveProfileFieldUp(fieldId, successFunction) {
1271 moveProfileField(fieldId, "up", successFunction);
1275 * Moves the profile field with the given ID down one slot.
1278 * The ID of the field to move
1279 * @param successFunction
1280 * Function to call on success
1282 function moveProfileFieldDown(fieldId, successFunction) {
1283 moveProfileField(fieldId, "down", successFunction);
1287 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1292 $(document).ready(function() {
1294 /* this initializes the status update input field. */
1295 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1296 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1297 $("#sone #update-status .select-sender").css("display", "inline");
1298 $("#sone #update-status .sender").hide();
1299 $("#sone #update-status .select-sender button").click(function() {
1300 $("#sone #update-status .sender").show();
1301 $("#sone #update-status .select-sender").hide();
1304 $("#sone #update-status").submit(function() {
1305 button = $("button:submit", this);
1306 button.attr("disabled", "disabled");
1307 if ($(this).find(":input.default:enabled").length > 0) {
1310 sender = $(this).find(":input[name=sender]").val();
1311 text = $(this).find(":input[name=text]:enabled").val();
1312 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1313 if ((data != null) && data.success) {
1314 loadNewPost(data.postId, data.sone, data.recipient);
1316 button.removeAttr("disabled");
1318 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1319 $(this).find(":input[name=text]:enabled").val("").blur();
1320 $(this).find(".sender").hide();
1321 $(this).find(".select-sender").show();
1326 /* ajaxify the search input field. */
1327 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1328 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1331 /* ajaxify input field on “view Sone” page. */
1332 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1333 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1334 $("#sone #post-message .select-sender").css("display", "inline");
1335 $("#sone #post-message .sender").hide();
1336 $("#sone #post-message .select-sender button").click(function() {
1337 $("#sone #post-message .sender").show();
1338 $("#sone #post-message .select-sender").hide();
1341 $("#sone #post-message").submit(function() {
1342 sender = $(this).find(":input[name=sender]").val();
1343 text = $(this).find(":input[name=text]:enabled").val();
1344 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text }, function(data, textStatus) {
1345 if ((data != null) && data.success) {
1346 loadNewPost(data.postId, getCurrentSoneId());
1349 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1350 $(this).find(":input[name=text]:enabled").val("").blur();
1351 $(this).find(".sender").hide();
1352 $(this).find(".select-sender").show();
1357 /* Ajaxifies all posts. */
1358 /* calling getTranslation here will cache the necessary values. */
1359 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1360 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1361 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1362 $("#sone .post").each(function() {
1369 /* hides all replies but the latest two. */
1370 if (!isViewPostPage()) {
1371 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1372 $("#sone .post .replies").each(function() {
1373 allReplies = $(this).find(".reply");
1374 if (allReplies.length > 2) {
1376 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1377 $(allReplies[replyIndex]).addClass("hidden");
1378 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1380 clickToShowElement = $("<div></div>").addClass("click-to-show");
1382 clickToShowElement.addClass("new");
1384 (function(clickToShowElement, allReplies, text) {
1385 clickToShowElement.text(text);
1386 clickToShowElement.click(function() {
1387 allReplies.removeClass("hidden");
1388 clickToShowElement.addClass("hidden");
1390 })(clickToShowElement, allReplies, text);
1391 $(allReplies[0]).before(clickToShowElement);
1397 $("#sone .sone").each(function() {
1398 ajaxifySone($(this));
1401 /* process all existing notifications, ajaxify dismiss buttons. */
1402 $("#sone #notification-area .notification").each(function() {
1403 ajaxifyNotification($(this));
1406 /* activate status polling. */
1407 setTimeout(getStatus, 5000);
1409 /* reset activity counter when the page has focus. */
1410 $(window).focus(function() {
1413 }).blur(function() {