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();
269 * Returns the element of the Sone with the given ID.
273 * @returns All Sone elements with the given ID
275 function getSone(soneId) {
276 return $("#sone .sone").filter(function(index) {
277 return $(".id").text() == soneId;
281 function getSoneElement(element) {
282 return $(element).closest(".sone");
286 * Generates a list of Sones by concatening the names of the given sones with a
287 * new line character (“\n”).
290 * The sones to format
291 * @returns {String} The created string
293 function generateSoneList(sones) {
295 $.each(sones, function() {
296 if (soneList != "") {
299 soneList += this.name;
305 * Returns the ID of the Sone that this element belongs to.
308 * The element to locate the matching Sone ID for
309 * @returns The ID of the Sone, or undefined
311 function getSoneId(element) {
312 return getSoneElement(element).find(".id").text();
316 * Returns the element of the post with the given ID.
320 * @returns The element of the post
322 function getPost(postId) {
323 return $("#sone .post#" + postId);
326 function getPostElement(element) {
327 return $(element).closest(".post");
330 function getPostId(element) {
331 return getPostElement(element).attr("id");
334 function getPostTime(element) {
335 return getPostElement(element).find(".post-time").text();
339 * Returns the author of the post the given element belongs to.
342 * The element whose post to get the author for
343 * @returns The ID of the authoring Sone
345 function getPostAuthor(element) {
346 return getPostElement(element).find(".post-author").text();
350 * Returns the element of the reply with the given ID.
353 * The ID of the reply
354 * @returns The element of the reply
356 function getReply(replyId) {
357 return $("#sone .reply#" + replyId);
360 function getReplyElement(element) {
361 return $(element).closest(".reply");
364 function getReplyId(element) {
365 return getReplyElement(element).attr("id");
368 function getReplyTime(element) {
369 return getReplyElement(element).find(".reply-time").text();
373 * Returns the author of the reply the given element belongs to.
376 * The element whose reply to get the author for
377 * @returns The ID of the authoring Sone
379 function getReplyAuthor(element) {
380 return getReplyElement(element).find(".reply-author").text();
384 * Returns the notification with the given ID.
386 * @param notificationId
387 * The ID of the notification
388 * @returns The notification element
390 function getNotification(notificationId) {
391 return $("#sone #notification-area .notification#" + notificationId);
395 * Returns the notification element closest to the given element.
398 * The element to get the closest notification of
399 * @return The closest notification element
401 function getNotificationElement(element) {
402 return $(element).closest(".notification");
406 * Returns the ID of the notification element.
408 * @param notificationElement
409 * The notification element
410 * @returns The ID of the notification
412 function getNotificationId(notificationElement) {
413 return $(notificationElement).attr("id");
416 function likePost(postId) {
417 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
418 if ((data == null) || !data.success) {
421 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
422 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
423 updatePostLikes(postId);
424 }, function(xmlHttpRequest, textStatus, error) {
429 function unlikePost(postId) {
430 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
431 if ((data == null) || !data.success) {
434 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
435 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
436 updatePostLikes(postId);
437 }, function(xmlHttpRequest, textStatus, error) {
442 function updatePostLikes(postId) {
443 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
444 if ((data != null) && data.success) {
445 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
446 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
447 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
449 }, function(xmlHttpRequest, textStatus, error) {
454 function likeReply(replyId) {
455 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
456 if ((data == null) || !data.success) {
459 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
460 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
461 updateReplyLikes(replyId);
462 }, function(xmlHttpRequest, textStatus, error) {
467 function unlikeReply(replyId) {
468 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
469 if ((data == null) || !data.success) {
472 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
473 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
474 updateReplyLikes(replyId);
475 }, function(xmlHttpRequest, textStatus, error) {
481 * Trusts the Sone with the given ID.
484 * The ID of the Sone to trust
486 function trustSone(soneId) {
487 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
488 if ((data != null) && data.success) {
489 updateTrustControls(soneId, data.trustValue);
495 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
498 * The ID of the Sone to distrust
500 function distrustSone(soneId) {
501 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
502 if ((data != null) && data.success) {
503 updateTrustControls(soneId, data.trustValue);
509 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
512 * The ID of the Sone to untrust
514 function untrustSone(soneId) {
515 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
516 if ((data != null) && data.success) {
517 updateTrustControls(soneId, data.trustValue);
523 * Updates the trust controls for all posts and replies of the given Sone,
524 * according to the given trust value.
527 * The ID of the Sone to update all trust controls for
529 * The trust value for the Sone
531 function updateTrustControls(soneId, trustValue) {
532 $("#sone .post").each(function() {
533 if (getPostAuthor(this) == soneId) {
534 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
535 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
536 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
539 $("#sone .reply").each(function() {
540 if (getReplyAuthor(this) == soneId) {
541 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
542 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
543 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
549 * Bookmarks the post with the given ID.
552 * The ID of the post to bookmark
554 function bookmarkPost(postId) {
556 $.getJSON("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
557 if ((data != null) && data.success) {
558 getPost(postId).find(".bookmark").toggleClass("hidden", true);
559 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
566 * Unbookmarks the post with the given ID.
569 * The ID of the post to unbookmark
571 function unbookmarkPost(postId) {
572 $.getJSON("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
573 if ((data != null) && data.success) {
574 getPost(postId).find(".bookmark").toggleClass("hidden", false);
575 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
580 function updateReplyLikes(replyId) {
581 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
582 if ((data != null) && data.success) {
583 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
584 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
585 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
587 }, function(xmlHttpRequest, textStatus, error) {
593 * Posts a reply and calls the given callback when the request finishes.
596 * The ID of the sender
598 * The ID of the post the reply refers to
601 * @param callbackFunction
602 * The callback function to call when the request finishes (takes 3
603 * parameters: success, error, replyId)
605 function postReply(sender, postId, text, callbackFunction) {
606 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
608 /* TODO - show error */
612 callbackFunction(true, null, data.reply, data.sone);
614 callbackFunction(false, data.error);
616 }, function(xmlHttpRequest, textStatus, error) {
622 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
625 * The Sone to ajaxify
627 function ajaxifySone(soneElement) {
629 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
632 $(".follow", soneElement).submit(function() {
633 var followElement = this;
634 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
635 $(followElement).addClass("hidden");
636 $(followElement).parent().find(".unfollow").removeClass("hidden");
640 $(".unfollow", soneElement).submit(function() {
641 var unfollowElement = this;
642 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
643 $(unfollowElement).addClass("hidden");
644 $(unfollowElement).parent().find(".follow").removeClass("hidden");
648 $(".lock", soneElement).submit(function() {
649 var lockElement = this;
650 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
651 $(lockElement).addClass("hidden");
652 $(lockElement).parent().find(".unlock").removeClass("hidden");
656 $(".unlock", soneElement).submit(function() {
657 var unlockElement = this;
658 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
659 $(unlockElement).addClass("hidden");
660 $(unlockElement).parent().find(".lock").removeClass("hidden");
665 /* mark Sone as known when clicking it. */
666 $(soneElement).click(function() {
667 markSoneAsKnown(soneElement);
672 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
675 * The post element to ajaxify
677 function ajaxifyPost(postElement) {
678 $(postElement).find("form").submit(function() {
681 $(postElement).find(".create-reply button:submit").click(function() {
683 button.attr("disabled", "disabled");
684 sender = $(this.form).find(":input[name=sender]").val();
685 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
686 postId = getPostId(this);
687 text = $(inputField).val();
688 (function(sender, postId, text, inputField) {
689 postReply(sender, postId, text, function(success, error, replyId, soneId) {
691 $(inputField).val("");
692 loadNewReply(replyId, soneId, postId);
693 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
694 $("#sone .post#" + postId + " .create-reply .sender").hide();
695 $("#sone .post#" + postId + " .create-reply .select-sender").show();
696 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
700 button.removeAttr("disabled");
702 })(sender, postId, text, inputField);
706 /* replace all “delete” buttons with javascript. */
707 (function(postElement) {
708 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
709 postId = getPostId(postElement);
710 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
714 /* convert all “like” buttons to javascript functions. */
715 $(postElement).find(".like-post").submit(function() {
716 likePost(getPostId(this));
719 $(postElement).find(".unlike-post").submit(function() {
720 unlikePost(getPostId(this));
724 /* convert trust control buttons to javascript functions. */
725 $(postElement).find(".post-trust").submit(function() {
726 trustSone(getPostAuthor(this));
729 $(postElement).find(".post-distrust").submit(function() {
730 distrustSone(getPostAuthor(this));
733 $(postElement).find(".post-untrust").submit(function() {
734 untrustSone(getPostAuthor(this));
738 /* convert bookmark/unbookmark buttons to javascript functions. */
739 $(postElement).find(".bookmark").submit(function() {
740 bookmarkPost(getPostId(this));
743 $(postElement).find(".unbookmark").submit(function() {
744 unbookmarkPost(getPostId(this));
748 /* convert “show source” link into javascript function. */
749 $(postElement).find(".show-source").each(function() {
750 $("a", this).click(function() {
751 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
752 $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
757 /* add “comment” link. */
758 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
760 /* process all replies. */
762 $(postElement).find(".reply").each(function() {
763 replyIds.push(getReplyId(this));
766 updateReplyTimes(replyIds.join(","));
768 /* process reply input fields. */
769 getTranslation("WebInterface.DefaultText.Reply", function(text) {
770 $(postElement).find("input.reply-input").each(function() {
771 registerInputTextareaSwap(this, text, "text", false, false);
775 /* process sender selection. */
776 $(".select-sender", postElement).css("display", "inline");
777 $(".sender", postElement).hide();
778 $(".select-sender button", postElement).click(function() {
779 $(".sender", postElement).show();
780 $(".select-sender", postElement).hide();
784 /* mark everything as known on click. */
785 $(postElement).click(function(event) {
786 if ($(event.target).hasClass("click-to-show")) {
789 markPostAsKnown(this);
792 /* hide reply input field. */
793 $(postElement).find(".create-reply").addClass("hidden");
797 * Ajaxifies the given reply element.
799 * @param replyElement
800 * The reply element to ajaxify
802 function ajaxifyReply(replyElement) {
803 $(replyElement).find(".like-reply").submit(function() {
804 likeReply(getReplyId(this));
807 $(replyElement).find(".unlike-reply").submit(function() {
808 unlikeReply(getReplyId(this));
811 (function(replyElement) {
812 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
813 $(replyElement).find(".delete-reply button").each(function() {
814 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
818 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
820 /* convert “show source” link into javascript function. */
821 $(replyElement).find(".show-reply-source").each(function() {
822 $("a", this).click(function() {
823 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
824 $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
829 /* convert trust control buttons to javascript functions. */
830 $(replyElement).find(".reply-trust").submit(function() {
831 trustSone(getReplyAuthor(this));
834 $(replyElement).find(".reply-distrust").submit(function() {
835 distrustSone(getReplyAuthor(this));
838 $(replyElement).find(".reply-untrust").submit(function() {
839 untrustSone(getReplyAuthor(this));
845 * Ajaxifies the given notification by replacing the form with AJAX.
847 * @param notification
848 * jQuery object representing the notification.
850 function ajaxifyNotification(notification) {
851 notification.find("form").submit(function() {
854 notification.find("input[name=returnPage]").val($.url.attr("relative"));
855 if (notification.find(".short-text").length > 0) {
856 notification.find(".short-text").removeClass("hidden");
857 notification.find(".text").addClass("hidden");
859 notification.find("form.mark-as-read button").click(function() {
860 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
862 notification.find("a[class^='link-']").each(function() {
863 linkElement = $(this);
864 if (linkElement.is("[href^='viewPost']")) {
865 id = linkElement.attr("class").substr(5);
867 linkElement.attr("href", "#post-" + id);
871 notification.find("form.dismiss button").click(function() {
872 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
873 /* dismiss in case of error, too. */
874 notification.slideUp();
875 }, function(xmlHttpRequest, textStatus, error) {
882 function getStatus() {
883 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
884 if ((data != null) && data.success) {
885 /* process Sone information. */
886 $.each(data.sones, function(index, value) {
887 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
889 /* search for removed notifications. */
890 $("#sone #notification-area .notification").each(function() {
891 notificationId = $(this).attr("id");
892 foundNotification = false;
893 $.each(data.notifications, function(index, value) {
894 if (value.id == notificationId) {
895 foundNotification = true;
899 if (!foundNotification) {
900 $(this).slideUp("normal", function() {
905 /* process notifications. */
906 $.each(data.notifications, function(index, value) {
907 oldNotification = $("#sone #notification-area .notification#" + value.id);
908 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
909 if (oldNotification.length != 0) {
910 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
911 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
912 notification.find(".short-text").toggleClass("hidden", opened);
913 notification.find(".text").toggleClass("hidden", !opened);
915 oldNotification.replaceWith(notification.show());
917 $("#sone #notification-area").append(notification);
918 notification.slideDown();
922 /* process new posts. */
923 $.each(data.newPosts, function(index, value) {
924 loadNewPost(value.id, value.sone, value.recipient, value.time);
926 /* process new replies. */
927 $.each(data.newReplies, function(index, value) {
928 loadNewReply(value.id, value.sone, value.post, value.postSone);
930 /* do it again in 5 seconds. */
931 setTimeout(getStatus, 5000);
933 /* data.success was false, wait 30 seconds. */
934 setTimeout(getStatus, 30000);
936 }, function(xmlHttpRequest, textStatus, error) {
937 /* something really bad happend, wait a minute. */
938 setTimeout(getStatus, 60000);
943 * Returns the ID of the currently logged in Sone.
945 * @return The ID of the current Sone, or an empty string if no Sone is logged
948 function getCurrentSoneId() {
949 return $("#currentSoneId").text();
953 * Returns the content of the page-id attribute.
955 * @returns The page ID
957 function getPageId() {
958 return $("#sone .page-id").text();
962 * Returns whether the current page is the index page.
964 * @returns {Boolean} <code>true</code> if the current page is the index page,
965 * <code>false</code> otherwise
967 function isIndexPage() {
968 return getPageId() == "index";
972 * Returns whether the current page is a “view Sone” page.
974 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
975 * page, <code>false</code> otherwise
977 function isViewSonePage() {
978 return getPageId() == "view-sone";
982 * Returns the ID of the currently shown Sone. This will only return a sensible
983 * value if isViewSonePage() returns <code>true</code>.
985 * @returns The ID of the currently shown Sone
987 function getShownSoneId() {
988 return $("#sone .sone-id").text();
992 * Returns whether the current page is a “view post” page.
994 * @returns {Boolean} <code>true</code> if the current page is a “view post”
995 * page, <code>false</code> otherwise
997 function isViewPostPage() {
998 return getPageId() == "view-post";
1002 * Returns the ID of the currently shown post. This will only return a sensible
1003 * value if isViewPostPage() returns <code>true</code>.
1005 * @returns The ID of the currently shown post
1007 function getShownPostId() {
1008 return $("#sone .post-id").text();
1012 * Returns whether the current page is the “known Sones” page.
1014 * @returns {Boolean} <code>true</code> if the current page is the “known
1015 * Sones” page, <code>false</code> otherwise
1017 function isKnownSonesPage() {
1018 return getPageId() == "known-sones";
1022 * Returns whether a post with the given ID exists on the current page.
1025 * The post ID to check for
1026 * @returns {Boolean} <code>true</code> if a post with the given ID already
1027 * exists on the page, <code>false</code> otherwise
1029 function hasPost(postId) {
1030 return $(".post#" + postId).length > 0;
1034 * Returns whether a reply with the given ID exists on the current page.
1037 * The reply ID to check for
1038 * @returns {Boolean} <code>true</code> if a reply with the given ID already
1039 * exists on the page, <code>false</code> otherwise
1041 function hasReply(replyId) {
1042 return $("#sone .reply#" + replyId).length > 0;
1045 function loadNewPost(postId, soneId, recipientId, time) {
1046 if (hasPost(postId)) {
1049 if (!isIndexPage()) {
1050 if (!isViewPostPage() || (getShownPostId() != postId)) {
1051 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1056 if (getPostTime($("#sone .post").last()) > time) {
1059 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1060 if ((data != null) && data.success) {
1061 if (hasPost(data.post.id)) {
1064 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1067 var firstOlderPost = null;
1068 $("#sone .post").each(function() {
1069 if (getPostTime(this) < data.post.time) {
1070 firstOlderPost = $(this);
1074 newPost = $(data.post.html).addClass("hidden");
1075 if (firstOlderPost != null) {
1076 newPost.insertBefore(firstOlderPost);
1078 ajaxifyPost(newPost);
1079 updatePostTimes(data.post.id);
1080 newPost.slideDown();
1086 function loadNewReply(replyId, soneId, postId, postSoneId) {
1087 if (hasReply(replyId)) {
1090 if (!hasPost(postId)) {
1093 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1095 if ((data != null) && data.success) {
1096 if (hasReply(data.reply.id)) {
1099 $("#sone .post#" + data.reply.postId).each(function() {
1100 var firstNewerReply = null;
1101 $(this).find(".replies .reply").each(function() {
1102 if (getReplyTime(this) > data.reply.time) {
1103 firstNewerReply = $(this);
1107 newReply = $(data.reply.html).addClass("hidden");
1108 if (firstNewerReply != null) {
1109 newReply.insertBefore(firstNewerReply);
1111 if ($(this).find(".replies .create-reply")) {
1112 $(this).find(".replies .create-reply").before(newReply);
1114 $(this).find(".replies").append(newReply);
1117 ajaxifyReply(newReply);
1118 updateReplyTimes(data.reply.id);
1119 newReply.slideDown();
1128 * Marks the given Sone as known if it is still new.
1130 * @param soneElement
1131 * The Sone to mark as known
1133 function markSoneAsKnown(soneElement) {
1134 if ($(".new", soneElement).length > 0) {
1135 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1136 $(soneElement).removeClass("new");
1141 function markPostAsKnown(postElements) {
1142 $(postElements).each(function() {
1144 if ($(postElement).hasClass("new")) {
1145 (function(postElement) {
1146 $(postElement).removeClass("new");
1147 $(".click-to-show", postElement).removeClass("new");
1148 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1152 markReplyAsKnown($(postElements).find(".reply"));
1155 function markReplyAsKnown(replyElements) {
1156 $(replyElements).each(function() {
1157 replyElement = this;
1158 if ($(replyElement).hasClass("new")) {
1159 (function(replyElement) {
1160 $(replyElement).removeClass("new");
1161 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1168 * Updates the time of the post with the given ID.
1171 * The ID of the post to update
1173 * The text of the time to show
1174 * @param refreshTime
1175 * The refresh time after which to request a new time (in seconds)
1177 * The tooltip to show
1179 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1180 if (!getPost(postId).is(":visible")) {
1183 getPost(postId).find(".post-status-line > .time a").html(timeText).attr("title", tooltip);
1184 (function(postId, refreshTime) {
1185 setTimeout(function() {
1186 updatePostTimes(postId);
1187 }, refreshTime * 1000);
1188 })(postId, refreshTime);
1192 * Requests new rendered times for the posts with the given IDs.
1195 * Comma-separated post IDs
1197 function updatePostTimes(postIds) {
1198 $.getJSON("getTimes.ajax", { "posts" : postIds }, function(data, textStatus) {
1199 if ((data != null) && data.success) {
1200 $.each(data.postTimes, function(index, value) {
1201 updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1208 * Updates the time of the reply with the given ID.
1211 * The ID of the reply to update
1213 * The text of the time to show
1214 * @param refreshTime
1215 * The refresh time after which to request a new time (in seconds)
1217 * The tooltip to show
1219 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1220 if (!getReply(replyId).is(":visible")) {
1223 getReply(replyId).find(".reply-status-line > .time").html(timeText).attr("title", tooltip);
1224 (function(replyId, refreshTime) {
1225 setTimeout(function() {
1226 updateReplyTimes(replyId);
1227 }, refreshTime * 1000);
1228 })(replyId, refreshTime);
1232 * Requests new rendered times for the posts with the given IDs.
1235 * Comma-separated post IDs
1237 function updateReplyTimes(replyIds) {
1238 $.getJSON("getTimes.ajax", { "replies" : replyIds }, function(data, textStatus) {
1239 if ((data != null) && data.success) {
1240 $.each(data.replyTimes, function(index, value) {
1241 updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1247 function resetActivity() {
1248 title = document.title;
1249 if (title.indexOf('(') == 0) {
1250 setTitle(title.substr(title.indexOf(' ') + 1));
1254 function setActivity() {
1256 title = document.title;
1257 if (title.indexOf('(') != 0) {
1258 setTitle("(!) " + title);
1260 if (!iconBlinking) {
1261 setTimeout(toggleIcon, 1500);
1262 iconBlinking = true;
1268 * Sets the window title after a small delay to prevent race-condition issues.
1273 function setTitle(title) {
1274 setTimeout(function() {
1275 document.title = title;
1279 /** Whether the icon is currently showing activity. */
1280 var iconActive = false;
1282 /** Whether the icon is currently supposed to blink. */
1283 var iconBlinking = false;
1286 * Toggles the icon. If the window has gained focus and the icon is still
1287 * showing the activity state, it is returned to normal.
1289 function toggleIcon() {
1292 changeIcon("images/icon.png");
1295 iconBlinking = false;
1297 iconActive = !iconActive;
1298 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1299 setTimeout(toggleIcon, 1500);
1304 * Changes the icon of the page.
1307 * The new URL of the icon
1309 function changeIcon(iconUrl) {
1310 $("link[rel=icon]").remove();
1311 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1312 $("iframe[id=icon-update]")[0].src += "";
1316 * Creates a new notification.
1319 * The ID of the notificaiton
1321 * The text of the notification
1322 * @param dismissable
1323 * <code>true</code> if the notification can be dismissed by the
1326 function createNotification(id, text, dismissable) {
1327 notification = $("<div></div>").addClass("notification").attr("id", id);
1329 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1330 dismissForm.find("input[name=notification]").val(id);
1331 notification.append(dismissForm);
1333 notification.append(text);
1334 return notification;
1338 * Shows the details of the notification with the given ID.
1340 * @param notificationId
1341 * The ID of the notification
1343 function showNotificationDetails(notificationId) {
1344 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1345 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1349 * Deletes the field with the given ID from the profile.
1352 * The ID of the field to delete
1354 function deleteProfileField(fieldId) {
1355 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1356 if (data && data.success) {
1357 $("#sone .profile-field#" + data.field.id).slideUp();
1363 * Renames a profile field.
1366 * The ID of the field to rename
1368 * The new name of the field
1369 * @param successFunction
1370 * Called when the renaming was successful
1372 function editProfileField(fieldId, newName, successFunction) {
1373 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1374 if (data && data.success) {
1381 * Moves the profile field with the given ID one slot in the given direction.
1384 * The ID of the field to move
1386 * The direction to move in (“up” or “down”)
1387 * @param successFunction
1388 * Function to call on success
1390 function moveProfileField(fieldId, direction, successFunction) {
1391 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1392 if (data && data.success) {
1399 * Moves the profile field with the given ID up one slot.
1402 * The ID of the field to move
1403 * @param successFunction
1404 * Function to call on success
1406 function moveProfileFieldUp(fieldId, successFunction) {
1407 moveProfileField(fieldId, "up", successFunction);
1411 * Moves the profile field with the given ID down one slot.
1414 * The ID of the field to move
1415 * @param successFunction
1416 * Function to call on success
1418 function moveProfileFieldDown(fieldId, successFunction) {
1419 moveProfileField(fieldId, "down", successFunction);
1423 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1428 $(document).ready(function() {
1430 /* this initializes the status update input field. */
1431 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1432 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1433 $("#sone #update-status .select-sender").css("display", "inline");
1434 $("#sone #update-status .sender").hide();
1435 $("#sone #update-status .select-sender button").click(function() {
1436 $("#sone #update-status .sender").show();
1437 $("#sone #update-status .select-sender").hide();
1440 $("#sone #update-status").submit(function() {
1441 button = $("button:submit", this);
1442 button.attr("disabled", "disabled");
1443 if ($(this).find(":input.default:enabled").length > 0) {
1446 sender = $(this).find(":input[name=sender]").val();
1447 text = $(this).find(":input[name=text]:enabled").val();
1448 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1449 if ((data != null) && data.success) {
1450 loadNewPost(data.postId, data.sone, data.recipient);
1452 button.removeAttr("disabled");
1454 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1455 $(this).find(":input[name=text]:enabled").val("").blur();
1456 $(this).find(".sender").hide();
1457 $(this).find(".select-sender").show();
1462 /* ajaxify the search input field. */
1463 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1464 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1467 /* ajaxify input field on “view Sone” page. */
1468 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1469 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1470 $("#sone #post-message .select-sender").css("display", "inline");
1471 $("#sone #post-message .sender").hide();
1472 $("#sone #post-message .select-sender button").click(function() {
1473 $("#sone #post-message .sender").show();
1474 $("#sone #post-message .select-sender").hide();
1477 $("#sone #post-message").submit(function() {
1478 sender = $(this).find(":input[name=sender]").val();
1479 text = $(this).find(":input[name=text]:enabled").val();
1480 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text }, function(data, textStatus) {
1481 if ((data != null) && data.success) {
1482 loadNewPost(data.postId, getCurrentSoneId());
1485 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1486 $(this).find(":input[name=text]:enabled").val("").blur();
1487 $(this).find(".sender").hide();
1488 $(this).find(".select-sender").show();
1493 /* Ajaxifies all posts. */
1494 /* calling getTranslation here will cache the necessary values. */
1495 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1496 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1497 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1498 $("#sone .post").each(function() {
1505 /* update post times. */
1507 $("#sone .post").each(function() {
1508 postIds.push(getPostId(this));
1510 updatePostTimes(postIds.join(","));
1512 /* hides all replies but the latest two. */
1513 if (!isViewPostPage()) {
1514 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1515 $("#sone .post .replies").each(function() {
1516 allReplies = $(this).find(".reply");
1517 if (allReplies.length > 2) {
1519 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1520 $(allReplies[replyIndex]).addClass("hidden");
1521 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1523 clickToShowElement = $("<div></div>").addClass("click-to-show");
1525 clickToShowElement.addClass("new");
1527 (function(clickToShowElement, allReplies, text) {
1528 clickToShowElement.text(text);
1529 clickToShowElement.click(function() {
1530 allReplies.removeClass("hidden");
1531 clickToShowElement.addClass("hidden");
1533 })(clickToShowElement, allReplies, text);
1534 $(allReplies[0]).before(clickToShowElement);
1540 $("#sone .sone").each(function() {
1541 ajaxifySone($(this));
1544 /* process all existing notifications, ajaxify dismiss buttons. */
1545 $("#sone #notification-area .notification").each(function() {
1546 ajaxifyNotification($(this));
1549 /* activate status polling. */
1550 setTimeout(getStatus, 5000);
1552 /* reset activity counter when the page has focus. */
1553 $(window).focus(function() {
1556 }).blur(function() {