1 /* Sone JavaScript functions. */
3 /* jQuery overrides. */
4 oldGetJson = jQuery.prototype.getJSON;
5 jQuery.prototype.getJSON = function(url, data, successCallback, errorCallback) {
6 if (typeof errorCallback == "undefined") {
7 return oldGetJson(url, data, successCallback);
9 if (jQuery.isFunction(data)) {
10 errorCallback = successCallback;
11 successCallback = data;
17 success: successCallback,
23 return $("#sone").hasClass("online");
26 function registerInputTextareaSwap(inputElement, defaultText, inputFieldName, optional, dontUseTextarea) {
27 $(inputElement).each(function() {
28 textarea = $(dontUseTextarea ? "<input type=\"text\" name=\"" + inputFieldName + "\">" : "<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
29 if ($(this).val() == "") {
31 inputField = $(this).data("inputField");
32 inputField.show().removeAttr("disabled").addClass("default");
33 inputField.val(defaultText);
35 }).hide().data("inputField", $(this)).val($(this).val());
36 $(this).after(textarea);
37 (function(inputField, textarea) {
38 inputField.focus(function() {
39 $(this).hide().attr("disabled", "disabled");
40 textarea.show().focus();
42 if (inputField.val() == "") {
43 inputField.addClass("default");
44 inputField.val(defaultText);
46 inputField.hide().attr("disabled", "disabled");
49 $(inputField.get(0).form).submit(function() {
50 inputField.attr("disabled", "disabled");
51 if (!optional && (textarea.val() == "")) {
55 })($(this), textarea);
60 * Adds a “comment” link to all status lines contained in the given element.
65 * The element to add a “comment” link to
67 function addCommentLink(postId, element, insertAfterThisElement) {
68 if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
71 commentElement = (function(postId) {
72 separator = $("<span> · </span>").addClass("separator");
73 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
74 replyElement = $("#sone .post#" + postId + " .create-reply");
75 replyElement.removeClass("hidden");
76 replyElement.removeClass("light");
77 (function(replyElement) {
78 replyElement.find("input.reply-input").blur(function() {
79 if ($(this).hasClass("default")) {
80 replyElement.addClass("light");
83 replyElement.removeClass("light");
86 replyElement.find("input.reply-input").focus();
88 return commentElement;
90 $(insertAfterThisElement).after(commentElement.clone(true));
91 $(insertAfterThisElement).after(separator);
94 var translations = {};
97 * Retrieves the translation for the given key and calls the callback function.
98 * The callback function takes a single parameter, the translated string.
101 * The key of the translation string
103 * The callback function
105 function getTranslation(key, callback) {
106 if (key in translations) {
107 callback(translations[key]);
110 $.getJSON("getTranslation.ajax", {"key": key}, function(data, textStatus) {
111 if ((data != null) && data.success) {
112 translations[key] = data.value;
113 callback(data.value);
115 }, function(xmlHttpRequest, textStatus, error) {
121 * Filters the given Sone ID, replacing all “~” characters by an underscore.
124 * The Sone ID to filter
125 * @returns The filtered Sone ID
127 function filterSoneId(soneId) {
128 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
132 * Updates the status of the given Sone.
135 * The ID of the Sone to update
137 * The status of the Sone (“idle”, “unknown”, “inserting”,
140 * Whether the Sone is modified
142 * Whether the Sone is locked
144 * The date and time of the last update (formatted for display)
146 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
147 $("#sone .sone." + filterSoneId(soneId)).
148 toggleClass("unknown", status == "unknown").
149 toggleClass("idle", status == "idle").
150 toggleClass("inserting", status == "inserting").
151 toggleClass("downloading", status == "downloading").
152 toggleClass("modified", modified);
153 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
154 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
155 if (lastUpdated != null) {
156 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
158 getTranslation("View.Sone.Text.UnknownDate", function(unknown) {
159 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(unknown);
162 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
166 * Enhances a “delete” button so that the confirmation is done on the same page.
171 * The text to show on the button
172 * @param deleteCallback
173 * The callback that actually deletes something
175 function enhanceDeleteButton(button, text, deleteCallback) {
177 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
178 $(this).fadeOut("slow");
181 }).insertAfter(button);
182 (function(button, newButton) {
183 button.click(function() {
184 button.fadeOut("slow", function() {
185 newButton.fadeIn("slow");
186 $(document).one("click", function() {
187 if (this != newButton.get(0)) {
188 newButton.fadeOut(function() {
196 })(button, newButton);
201 * Enhances a post’s “delete” button.
206 * The ID of the post to delete
208 * The text to replace the button with
210 function enhanceDeletePostButton(button, postId, text) {
211 enhanceDeleteButton(button, text, function() {
212 $.getJSON("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
217 $("#sone .post#" + postId).slideUp();
218 } else if (data.error == "invalid-post-id") {
219 alert("Invalid post ID given!");
220 } else if (data.error == "auth-required") {
221 alert("You need to be logged in.");
222 } else if (data.error == "not-authorized") {
223 alert("You are not allowed to delete this post.");
225 }, function(xmlHttpRequest, textStatus, error) {
232 * Enhances a reply’s “delete” button.
237 * The ID of the reply to delete
239 * The text to replace the button with
241 function enhanceDeleteReplyButton(button, replyId, text) {
242 enhanceDeleteButton(button, text, function() {
243 $.getJSON("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
248 $("#sone .reply#" + replyId).slideUp();
249 } else if (data.error == "invalid-reply-id") {
250 alert("Invalid reply ID given!");
251 } else if (data.error == "auth-required") {
252 alert("You need to be logged in.");
253 } else if (data.error == "not-authorized") {
254 alert("You are not allowed to delete this reply.");
256 }, function(xmlHttpRequest, textStatus, error) {
262 function getFormPassword() {
263 return $("#sone #formPassword").text();
266 function getSoneElement(element) {
267 return $(element).closest(".sone");
271 * Generates a list of Sones by concatening the names of the given sones with a
272 * new line character (“\n”).
275 * The sones to format
276 * @returns {String} The created string
278 function generateSoneList(sones) {
280 $.each(sones, function() {
281 if (soneList != "") {
284 soneList += this.name;
290 * Returns the ID of the Sone that this element belongs to.
293 * The element to locate the matching Sone ID for
294 * @returns The ID of the Sone, or undefined
296 function getSoneId(element) {
297 return getSoneElement(element).find(".id").text();
301 * Returns the element of the post with the given ID.
305 * @returns The element of the post
307 function getPost(postId) {
308 return $("#sone .post#" + postId);
311 function getPostElement(element) {
312 return $(element).closest(".post");
315 function getPostId(element) {
316 return getPostElement(element).attr("id");
319 function getPostTime(element) {
320 return getPostElement(element).find(".post-time").text();
324 * Returns the author of the post the given element belongs to.
327 * The element whose post to get the author for
328 * @returns The ID of the authoring Sone
330 function getPostAuthor(element) {
331 return getPostElement(element).find(".post-author").text();
334 function getReplyElement(element) {
335 return $(element).closest(".reply");
338 function getReplyId(element) {
339 return getReplyElement(element).attr("id");
342 function getReplyTime(element) {
343 return getReplyElement(element).find(".reply-time").text();
347 * Returns the author of the reply the given element belongs to.
350 * The element whose reply to get the author for
351 * @returns The ID of the authoring Sone
353 function getReplyAuthor(element) {
354 return getReplyElement(element).find(".reply-author").text();
357 function likePost(postId) {
358 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
359 if ((data == null) || !data.success) {
362 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
363 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
364 updatePostLikes(postId);
365 }, function(xmlHttpRequest, textStatus, error) {
370 function unlikePost(postId) {
371 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
372 if ((data == null) || !data.success) {
375 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
376 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
377 updatePostLikes(postId);
378 }, function(xmlHttpRequest, textStatus, error) {
383 function updatePostLikes(postId) {
384 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
385 if ((data != null) && data.success) {
386 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
387 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
388 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
390 }, function(xmlHttpRequest, textStatus, error) {
395 function likeReply(replyId) {
396 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
397 if ((data == null) || !data.success) {
400 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
401 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
402 updateReplyLikes(replyId);
403 }, function(xmlHttpRequest, textStatus, error) {
408 function unlikeReply(replyId) {
409 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
410 if ((data == null) || !data.success) {
413 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
414 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
415 updateReplyLikes(replyId);
416 }, function(xmlHttpRequest, textStatus, error) {
422 * Trusts the Sone with the given ID.
425 * The ID of the Sone to trust
427 function trustSone(soneId) {
428 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
429 if ((data != null) && data.success) {
430 updateTrustControls(soneId, data.trustValue);
436 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
439 * The ID of the Sone to distrust
441 function distrustSone(soneId) {
442 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
443 if ((data != null) && data.success) {
444 updateTrustControls(soneId, data.trustValue);
450 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
453 * The ID of the Sone to untrust
455 function untrustSone(soneId) {
456 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
457 if ((data != null) && data.success) {
458 updateTrustControls(soneId, data.trustValue);
464 * Updates the trust controls for all posts and replies of the given Sone,
465 * according to the given trust value.
468 * The ID of the Sone to update all trust controls for
470 * The trust value for the Sone
472 function updateTrustControls(soneId, trustValue) {
473 $("#sone .post").each(function() {
474 if (getPostAuthor(this) == soneId) {
475 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
476 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
477 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
480 $("#sone .reply").each(function() {
481 if (getReplyAuthor(this) == soneId) {
482 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
483 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
484 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
490 * Bookmarks the post with the given ID.
493 * The ID of the post to bookmark
495 function bookmarkPost(postId) {
497 $.getJSON("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
498 if ((data != null) && data.success) {
499 getPost(postId).find(".bookmark").toggleClass("hidden", true);
500 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
507 * Unbookmarks the post with the given ID.
510 * The ID of the post to unbookmark
512 function unbookmarkPost(postId) {
513 $.getJSON("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
514 if ((data != null) && data.success) {
515 getPost(postId).find(".bookmark").toggleClass("hidden", false);
516 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
521 function updateReplyLikes(replyId) {
522 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
523 if ((data != null) && data.success) {
524 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
525 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
526 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
528 }, function(xmlHttpRequest, textStatus, error) {
534 * Posts a reply and calls the given callback when the request finishes.
537 * The ID of the sender
539 * The ID of the post the reply refers to
542 * @param callbackFunction
543 * The callback function to call when the request finishes (takes 3
544 * parameters: success, error, replyId)
546 function postReply(sender, postId, text, callbackFunction) {
547 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
549 /* TODO - show error */
553 callbackFunction(true, null, data.reply, data.sone);
555 callbackFunction(false, data.error);
557 }, function(xmlHttpRequest, textStatus, error) {
563 * Requests information about the reply with the given ID.
566 * The ID of the reply
567 * @param callbackFunction
568 * A callback function (parameters soneId, soneName, replyTime,
569 * replyDisplayTime, text, html)
571 function getReply(replyId, callbackFunction) {
572 $.getJSON("getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
573 if ((data != null) && data.success) {
574 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
576 }, function(xmlHttpRequest, textStatus, error) {
582 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
585 * The Sone to ajaxify
587 function ajaxifySone(soneElement) {
589 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
592 $(".follow", soneElement).submit(function() {
593 var followElement = this;
594 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
595 $(followElement).addClass("hidden");
596 $(followElement).parent().find(".unfollow").removeClass("hidden");
600 $(".unfollow", soneElement).submit(function() {
601 var unfollowElement = this;
602 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
603 $(unfollowElement).addClass("hidden");
604 $(unfollowElement).parent().find(".follow").removeClass("hidden");
608 $(".lock", soneElement).submit(function() {
609 var lockElement = this;
610 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
611 $(lockElement).addClass("hidden");
612 $(lockElement).parent().find(".unlock").removeClass("hidden");
616 $(".unlock", soneElement).submit(function() {
617 var unlockElement = this;
618 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
619 $(unlockElement).addClass("hidden");
620 $(unlockElement).parent().find(".lock").removeClass("hidden");
625 /* mark Sone as known when clicking it. */
626 $(soneElement).click(function() {
627 markSoneAsKnown(soneElement);
632 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
635 * The post element to ajaxify
637 function ajaxifyPost(postElement) {
638 $(postElement).find("form").submit(function() {
641 $(postElement).find(".create-reply button:submit").click(function() {
643 button.attr("disabled", "disabled");
644 sender = $(this.form).find(":input[name=sender]").val();
645 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
646 postId = getPostId(this);
647 text = $(inputField).val();
648 (function(sender, postId, text, inputField) {
649 postReply(sender, postId, text, function(success, error, replyId, soneId) {
651 $(inputField).val("");
652 loadNewReply(replyId, soneId, postId);
653 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
654 $("#sone .post#" + postId + " .create-reply .sender").hide();
655 $("#sone .post#" + postId + " .create-reply .select-sender").show();
656 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
660 button.removeAttr("disabled");
662 })(sender, postId, text, inputField);
666 /* replace all “delete” buttons with javascript. */
667 (function(postElement) {
668 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
669 postId = getPostId(postElement);
670 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
674 /* convert all “like” buttons to javascript functions. */
675 $(postElement).find(".like-post").submit(function() {
676 likePost(getPostId(this));
679 $(postElement).find(".unlike-post").submit(function() {
680 unlikePost(getPostId(this));
684 /* convert trust control buttons to javascript functions. */
685 $(postElement).find(".post-trust").submit(function() {
686 trustSone(getPostAuthor(this));
689 $(postElement).find(".post-distrust").submit(function() {
690 distrustSone(getPostAuthor(this));
693 $(postElement).find(".post-untrust").submit(function() {
694 untrustSone(getPostAuthor(this));
698 /* convert bookmark/unbookmark buttons to javascript functions. */
699 $(postElement).find(".bookmark").submit(function() {
700 bookmarkPost(getPostId(this));
703 $(postElement).find(".unbookmark").submit(function() {
704 unbookmarkPost(getPostId(this));
708 /* convert “show source” link into javascript function. */
709 $(postElement).find(".show-source").each(function() {
710 $("a", this).click(function() {
711 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
712 $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
717 /* add “comment” link. */
718 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
720 /* process all replies. */
721 $(postElement).find(".reply").each(function() {
725 /* process reply input fields. */
726 getTranslation("WebInterface.DefaultText.Reply", function(text) {
727 $(postElement).find("input.reply-input").each(function() {
728 registerInputTextareaSwap(this, text, "text", false, false);
732 /* process sender selection. */
733 $(".select-sender", postElement).css("display", "inline");
734 $(".sender", postElement).hide();
735 $(".select-sender button", postElement).click(function() {
736 $(".sender", postElement).show();
737 $(".select-sender", postElement).hide();
741 /* mark everything as known on click. */
742 $(postElement).click(function(event) {
743 if ($(event.target).hasClass("click-to-show")) {
746 markPostAsKnown(this);
749 /* hide reply input field. */
750 $(postElement).find(".create-reply").addClass("hidden");
754 * Ajaxifies the given reply element.
756 * @param replyElement
757 * The reply element to ajaxify
759 function ajaxifyReply(replyElement) {
760 $(replyElement).find(".like-reply").submit(function() {
761 likeReply(getReplyId(this));
764 $(replyElement).find(".unlike-reply").submit(function() {
765 unlikeReply(getReplyId(this));
768 (function(replyElement) {
769 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
770 $(replyElement).find(".delete-reply button").each(function() {
771 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
775 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
777 /* convert “show source” link into javascript function. */
778 $(replyElement).find(".show-reply-source").each(function() {
779 $("a", this).click(function() {
780 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
781 $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
786 /* convert trust control buttons to javascript functions. */
787 $(replyElement).find(".reply-trust").submit(function() {
788 trustSone(getReplyAuthor(this));
791 $(replyElement).find(".reply-distrust").submit(function() {
792 distrustSone(getReplyAuthor(this));
795 $(replyElement).find(".reply-untrust").submit(function() {
796 untrustSone(getReplyAuthor(this));
802 * Ajaxifies the given notification by replacing the form with AJAX.
804 * @param notification
805 * jQuery object representing the notification.
807 function ajaxifyNotification(notification) {
808 notification.find("form").submit(function() {
811 notification.find("input[name=returnPage]").val($.url.attr("relative"));
812 if (notification.find(".short-text").length > 0) {
813 notification.find(".short-text").removeClass("hidden");
814 notification.find(".text").addClass("hidden");
816 notification.find("form.mark-as-read button").click(function() {
817 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
819 notification.find("a[class^='link-']").each(function() {
820 linkElement = $(this);
821 if (linkElement.is("[href^='viewPost']")) {
822 id = linkElement.attr("class").substr(5);
824 linkElement.attr("href", "#post-" + id);
828 notification.find("form.dismiss button").click(function() {
829 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
830 /* dismiss in case of error, too. */
831 notification.slideUp();
832 }, function(xmlHttpRequest, textStatus, error) {
839 function getStatus() {
840 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
841 if ((data != null) && data.success) {
842 /* process Sone information. */
843 $.each(data.sones, function(index, value) {
844 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
846 /* process notifications. */
847 $.each(data.notifications, function(index, value) {
848 oldNotification = $("#sone #notification-area .notification#" + value.id);
849 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
850 if (oldNotification.length != 0) {
851 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
852 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
853 notification.find(".short-text").toggleClass("hidden", opened);
854 notification.find(".text").toggleClass("hidden", !opened);
856 oldNotification.replaceWith(notification.show());
858 $("#sone #notification-area").append(notification);
859 notification.slideDown();
863 $.each(data.removedNotifications, function(index, value) {
864 $("#sone #notification-area .notification#" + value.id).slideUp();
866 /* process new posts. */
867 $.each(data.newPosts, function(index, value) {
868 loadNewPost(value.id, value.sone, value.recipient, value.time);
870 /* process new replies. */
871 $.each(data.newReplies, function(index, value) {
872 loadNewReply(value.id, value.sone, value.post, value.postSone);
874 /* do it again in 5 seconds. */
875 setTimeout(getStatus, 5000);
877 /* data.success was false, wait 30 seconds. */
878 setTimeout(getStatus, 30000);
880 }, function(xmlHttpRequest, textStatus, error) {
881 /* something really bad happend, wait a minute. */
882 setTimeout(getStatus, 60000);
887 * Returns the ID of the currently logged in Sone.
889 * @return The ID of the current Sone, or an empty string if no Sone is logged
892 function getCurrentSoneId() {
893 return $("#currentSoneId").text();
897 * Returns the content of the page-id attribute.
899 * @returns The page ID
901 function getPageId() {
902 return $("#sone .page-id").text();
906 * Returns whether the current page is the index page.
908 * @returns {Boolean} <code>true</code> if the current page is the index page,
909 * <code>false</code> otherwise
911 function isIndexPage() {
912 return getPageId() == "index";
916 * Returns whether the current page is a “view Sone” page.
918 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
919 * page, <code>false</code> otherwise
921 function isViewSonePage() {
922 return getPageId() == "view-sone";
926 * Returns the ID of the currently shown Sone. This will only return a sensible
927 * value if isViewSonePage() returns <code>true</code>.
929 * @returns The ID of the currently shown Sone
931 function getShownSoneId() {
932 return $("#sone .sone-id").text();
936 * Returns whether the current page is a “view post” page.
938 * @returns {Boolean} <code>true</code> if the current page is a “view post”
939 * page, <code>false</code> otherwise
941 function isViewPostPage() {
942 return getPageId() == "view-post";
946 * Returns the ID of the currently shown post. This will only return a sensible
947 * value if isViewPostPage() returns <code>true</code>.
949 * @returns The ID of the currently shown post
951 function getShownPostId() {
952 return $("#sone .post-id").text();
956 * Returns whether the current page is the “known Sones” page.
958 * @returns {Boolean} <code>true</code> if the current page is the “known
959 * Sones” page, <code>false</code> otherwise
961 function isKnownSonesPage() {
962 return getPageId() == "known-sones";
966 * Returns whether a post with the given ID exists on the current page.
969 * The post ID to check for
970 * @returns {Boolean} <code>true</code> if a post with the given ID already
971 * exists on the page, <code>false</code> otherwise
973 function hasPost(postId) {
974 return $(".post#" + postId).length > 0;
978 * Returns whether a reply with the given ID exists on the current page.
981 * The reply ID to check for
982 * @returns {Boolean} <code>true</code> if a reply with the given ID already
983 * exists on the page, <code>false</code> otherwise
985 function hasReply(replyId) {
986 return $("#sone .reply#" + replyId).length > 0;
989 function loadNewPost(postId, soneId, recipientId, time) {
990 if (hasPost(postId)) {
993 if (!isIndexPage()) {
994 if (!isViewPostPage() || (getShownPostId() != postId)) {
995 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1000 if (getPostTime($("#sone .post").last()) > time) {
1003 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1004 if ((data != null) && data.success) {
1005 if (hasPost(data.post.id)) {
1008 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1011 var firstOlderPost = null;
1012 $("#sone .post").each(function() {
1013 if (getPostTime(this) < data.post.time) {
1014 firstOlderPost = $(this);
1018 newPost = $(data.post.html).addClass("hidden");
1019 if (firstOlderPost != null) {
1020 newPost.insertBefore(firstOlderPost);
1022 ajaxifyPost(newPost);
1023 newPost.slideDown();
1029 function loadNewReply(replyId, soneId, postId, postSoneId) {
1030 if (hasReply(replyId)) {
1033 if (!hasPost(postId)) {
1036 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1038 if ((data != null) && data.success) {
1039 if (hasReply(data.reply.id)) {
1042 $("#sone .post#" + data.reply.postId).each(function() {
1043 var firstNewerReply = null;
1044 $(this).find(".replies .reply").each(function() {
1045 if (getReplyTime(this) > data.reply.time) {
1046 firstNewerReply = $(this);
1050 newReply = $(data.reply.html).addClass("hidden");
1051 if (firstNewerReply != null) {
1052 newReply.insertBefore(firstNewerReply);
1054 if ($(this).find(".replies .create-reply")) {
1055 $(this).find(".replies .create-reply").before(newReply);
1057 $(this).find(".replies").append(newReply);
1060 ajaxifyReply(newReply);
1061 newReply.slideDown();
1070 * Marks the given Sone as known if it is still new.
1072 * @param soneElement
1073 * The Sone to mark as known
1075 function markSoneAsKnown(soneElement) {
1076 if ($(".new", soneElement).length > 0) {
1077 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1078 $(soneElement).removeClass("new");
1083 function markPostAsKnown(postElements) {
1084 $(postElements).each(function() {
1086 if ($(postElement).hasClass("new")) {
1087 (function(postElement) {
1088 $(postElement).removeClass("new");
1089 $(".click-to-show", postElement).removeClass("new");
1090 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1094 markReplyAsKnown($(postElements).find(".reply"));
1097 function markReplyAsKnown(replyElements) {
1098 $(replyElements).each(function() {
1099 replyElement = this;
1100 if ($(replyElement).hasClass("new")) {
1101 (function(replyElement) {
1102 $(replyElement).removeClass("new");
1103 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1109 function resetActivity() {
1110 title = document.title;
1111 if (title.indexOf('(') == 0) {
1112 setTitle(title.substr(title.indexOf(' ') + 1));
1116 function setActivity() {
1118 title = document.title;
1119 if (title.indexOf('(') != 0) {
1120 setTitle("(!) " + title);
1122 if (!iconBlinking) {
1123 setTimeout(toggleIcon, 1500);
1124 iconBlinking = true;
1130 * Sets the window title after a small delay to prevent race-condition issues.
1135 function setTitle(title) {
1136 setTimeout(function() {
1137 document.title = title;
1141 /** Whether the icon is currently showing activity. */
1142 var iconActive = false;
1144 /** Whether the icon is currently supposed to blink. */
1145 var iconBlinking = false;
1148 * Toggles the icon. If the window has gained focus and the icon is still
1149 * showing the activity state, it is returned to normal.
1151 function toggleIcon() {
1154 changeIcon("images/icon.png");
1157 iconBlinking = false;
1159 iconActive = !iconActive;
1160 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1161 setTimeout(toggleIcon, 1500);
1166 * Changes the icon of the page.
1169 * The new URL of the icon
1171 function changeIcon(iconUrl) {
1172 $("link[rel=icon]").remove();
1173 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1174 $("iframe[id=icon-update]")[0].src += "";
1178 * Creates a new notification.
1181 * The ID of the notificaiton
1183 * The text of the notification
1184 * @param dismissable
1185 * <code>true</code> if the notification can be dismissed by the
1188 function createNotification(id, text, dismissable) {
1189 notification = $("<div></div>").addClass("notification").attr("id", id);
1191 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1192 dismissForm.find("input[name=notification]").val(id);
1193 notification.append(dismissForm);
1195 notification.append(text);
1196 return notification;
1200 * Shows the details of the notification with the given ID.
1202 * @param notificationId
1203 * The ID of the notification
1205 function showNotificationDetails(notificationId) {
1206 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1207 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1211 * Deletes the field with the given ID from the profile.
1214 * The ID of the field to delete
1216 function deleteProfileField(fieldId) {
1217 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1218 if (data && data.success) {
1219 $("#sone .profile-field#" + data.field.id).slideUp();
1225 * Renames a profile field.
1228 * The ID of the field to rename
1230 * The new name of the field
1231 * @param successFunction
1232 * Called when the renaming was successful
1234 function editProfileField(fieldId, newName, successFunction) {
1235 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1236 if (data && data.success) {
1243 * Moves the profile field with the given ID one slot in the given direction.
1246 * The ID of the field to move
1248 * The direction to move in (“up” or “down”)
1249 * @param successFunction
1250 * Function to call on success
1252 function moveProfileField(fieldId, direction, successFunction) {
1253 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1254 if (data && data.success) {
1261 * Moves the profile field with the given ID up one slot.
1264 * The ID of the field to move
1265 * @param successFunction
1266 * Function to call on success
1268 function moveProfileFieldUp(fieldId, successFunction) {
1269 moveProfileField(fieldId, "up", successFunction);
1273 * Moves the profile field with the given ID down one slot.
1276 * The ID of the field to move
1277 * @param successFunction
1278 * Function to call on success
1280 function moveProfileFieldDown(fieldId, successFunction) {
1281 moveProfileField(fieldId, "down", successFunction);
1285 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1290 $(document).ready(function() {
1292 /* this initializes the status update input field. */
1293 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1294 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1295 $("#sone #update-status .select-sender").css("display", "inline");
1296 $("#sone #update-status .sender").hide();
1297 $("#sone #update-status .select-sender button").click(function() {
1298 $("#sone #update-status .sender").show();
1299 $("#sone #update-status .select-sender").hide();
1302 $("#sone #update-status").submit(function() {
1303 button = $("button:submit", this);
1304 button.attr("disabled", "disabled");
1305 if ($(this).find(":input.default:enabled").length > 0) {
1308 sender = $(this).find(":input[name=sender]").val();
1309 text = $(this).find(":input[name=text]:enabled").val();
1310 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1311 if ((data != null) && data.success) {
1312 loadNewPost(data.postId, data.sone, data.recipient);
1314 button.removeAttr("disabled");
1316 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1317 $(this).find(":input[name=text]:enabled").val("").blur();
1318 $(this).find(".sender").hide();
1319 $(this).find(".select-sender").show();
1324 /* ajaxify the search input field. */
1325 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1326 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1329 /* ajaxify input field on “view Sone” page. */
1330 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1331 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1332 $("#sone #post-message .select-sender").css("display", "inline");
1333 $("#sone #post-message .sender").hide();
1334 $("#sone #post-message .select-sender button").click(function() {
1335 $("#sone #post-message .sender").show();
1336 $("#sone #post-message .select-sender").hide();
1339 $("#sone #post-message").submit(function() {
1340 sender = $(this).find(":input[name=sender]").val();
1341 text = $(this).find(":input[name=text]:enabled").val();
1342 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text }, function(data, textStatus) {
1343 if ((data != null) && data.success) {
1344 loadNewPost(data.postId, getCurrentSoneId());
1347 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1348 $(this).find(":input[name=text]:enabled").val("").blur();
1349 $(this).find(".sender").hide();
1350 $(this).find(".select-sender").show();
1355 /* Ajaxifies all posts. */
1356 /* calling getTranslation here will cache the necessary values. */
1357 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1358 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1359 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1360 $("#sone .post").each(function() {
1367 /* hides all replies but the latest two. */
1368 if (!isViewPostPage()) {
1369 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1370 $("#sone .post .replies").each(function() {
1371 allReplies = $(this).find(".reply");
1372 if (allReplies.length > 2) {
1374 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1375 $(allReplies[replyIndex]).addClass("hidden");
1376 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1378 clickToShowElement = $("<div></div>").addClass("click-to-show");
1380 clickToShowElement.addClass("new");
1382 (function(clickToShowElement, allReplies, text) {
1383 clickToShowElement.text(text);
1384 clickToShowElement.click(function() {
1385 allReplies.removeClass("hidden");
1386 clickToShowElement.addClass("hidden");
1388 })(clickToShowElement, allReplies, text);
1389 $(allReplies[0]).before(clickToShowElement);
1395 $("#sone .sone").each(function() {
1396 ajaxifySone($(this));
1399 /* process all existing notifications, ajaxify dismiss buttons. */
1400 $("#sone #notification-area .notification").each(function() {
1401 ajaxifyNotification($(this));
1404 /* activate status polling. */
1405 setTimeout(getStatus, 5000);
1407 /* reset activity counter when the page has focus. */
1408 $(window).focus(function() {
1411 }).blur(function() {