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();
383 function likePost(postId) {
384 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
385 if ((data == null) || !data.success) {
388 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
389 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
390 updatePostLikes(postId);
391 }, function(xmlHttpRequest, textStatus, error) {
396 function unlikePost(postId) {
397 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
398 if ((data == null) || !data.success) {
401 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
402 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
403 updatePostLikes(postId);
404 }, function(xmlHttpRequest, textStatus, error) {
409 function updatePostLikes(postId) {
410 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
411 if ((data != null) && data.success) {
412 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
413 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
414 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
416 }, function(xmlHttpRequest, textStatus, error) {
421 function likeReply(replyId) {
422 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
423 if ((data == null) || !data.success) {
426 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
427 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
428 updateReplyLikes(replyId);
429 }, function(xmlHttpRequest, textStatus, error) {
434 function unlikeReply(replyId) {
435 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
436 if ((data == null) || !data.success) {
439 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
440 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
441 updateReplyLikes(replyId);
442 }, function(xmlHttpRequest, textStatus, error) {
448 * Trusts the Sone with the given ID.
451 * The ID of the Sone to trust
453 function trustSone(soneId) {
454 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
455 if ((data != null) && data.success) {
456 updateTrustControls(soneId, data.trustValue);
462 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
465 * The ID of the Sone to distrust
467 function distrustSone(soneId) {
468 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
469 if ((data != null) && data.success) {
470 updateTrustControls(soneId, data.trustValue);
476 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
479 * The ID of the Sone to untrust
481 function untrustSone(soneId) {
482 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
483 if ((data != null) && data.success) {
484 updateTrustControls(soneId, data.trustValue);
490 * Updates the trust controls for all posts and replies of the given Sone,
491 * according to the given trust value.
494 * The ID of the Sone to update all trust controls for
496 * The trust value for the Sone
498 function updateTrustControls(soneId, trustValue) {
499 $("#sone .post").each(function() {
500 if (getPostAuthor(this) == soneId) {
501 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
502 getPostElement(this).find(".post-distrust").toggleClass("hidden", trustValue != null);
503 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
506 $("#sone .reply").each(function() {
507 if (getReplyAuthor(this) == soneId) {
508 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
509 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", trustValue != null);
510 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
516 * Bookmarks the post with the given ID.
519 * The ID of the post to bookmark
521 function bookmarkPost(postId) {
523 $.getJSON("bookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
524 if ((data != null) && data.success) {
525 getPost(postId).find(".bookmark").toggleClass("hidden", true);
526 getPost(postId).find(".unbookmark").toggleClass("hidden", false);
533 * Unbookmarks the post with the given ID.
536 * The ID of the post to unbookmark
538 function unbookmarkPost(postId) {
539 $.getJSON("unbookmark.ajax", {"formPassword": getFormPassword(), "type": "post", "post": postId}, function(data, textStatus) {
540 if ((data != null) && data.success) {
541 getPost(postId).find(".bookmark").toggleClass("hidden", false);
542 getPost(postId).find(".unbookmark").toggleClass("hidden", true);
547 function updateReplyLikes(replyId) {
548 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
549 if ((data != null) && data.success) {
550 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
551 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
552 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
554 }, function(xmlHttpRequest, textStatus, error) {
560 * Posts a reply and calls the given callback when the request finishes.
563 * The ID of the sender
565 * The ID of the post the reply refers to
568 * @param callbackFunction
569 * The callback function to call when the request finishes (takes 3
570 * parameters: success, error, replyId)
572 function postReply(sender, postId, text, callbackFunction) {
573 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "sender": sender, "post" : postId, "text": text }, function(data, textStatus) {
575 /* TODO - show error */
579 callbackFunction(true, null, data.reply, data.sone);
581 callbackFunction(false, data.error);
583 }, function(xmlHttpRequest, textStatus, error) {
589 * Ajaxifies the given Sone by enhancing all eligible elements with AJAX.
592 * The Sone to ajaxify
594 function ajaxifySone(soneElement) {
596 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
599 $(".follow", soneElement).submit(function() {
600 var followElement = this;
601 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
602 $(followElement).addClass("hidden");
603 $(followElement).parent().find(".unfollow").removeClass("hidden");
607 $(".unfollow", soneElement).submit(function() {
608 var unfollowElement = this;
609 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
610 $(unfollowElement).addClass("hidden");
611 $(unfollowElement).parent().find(".follow").removeClass("hidden");
615 $(".lock", soneElement).submit(function() {
616 var lockElement = this;
617 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
618 $(lockElement).addClass("hidden");
619 $(lockElement).parent().find(".unlock").removeClass("hidden");
623 $(".unlock", soneElement).submit(function() {
624 var unlockElement = this;
625 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
626 $(unlockElement).addClass("hidden");
627 $(unlockElement).parent().find(".lock").removeClass("hidden");
632 /* mark Sone as known when clicking it. */
633 $(soneElement).click(function() {
634 markSoneAsKnown(soneElement);
639 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
642 * The post element to ajaxify
644 function ajaxifyPost(postElement) {
645 $(postElement).find("form").submit(function() {
648 $(postElement).find(".create-reply button:submit").click(function() {
650 button.attr("disabled", "disabled");
651 sender = $(this.form).find(":input[name=sender]").val();
652 inputField = $(this.form).find(":input[name=text]:enabled").get(0);
653 postId = getPostId(this);
654 text = $(inputField).val();
655 (function(sender, postId, text, inputField) {
656 postReply(sender, postId, text, function(success, error, replyId, soneId) {
658 $(inputField).val("");
659 loadNewReply(replyId, soneId, postId);
660 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
661 $("#sone .post#" + postId + " .create-reply .sender").hide();
662 $("#sone .post#" + postId + " .create-reply .select-sender").show();
663 $("#sone .post#" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
667 button.removeAttr("disabled");
669 })(sender, postId, text, inputField);
673 /* replace all “delete” buttons with javascript. */
674 (function(postElement) {
675 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
676 postId = getPostId(postElement);
677 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
681 /* convert all “like” buttons to javascript functions. */
682 $(postElement).find(".like-post").submit(function() {
683 likePost(getPostId(this));
686 $(postElement).find(".unlike-post").submit(function() {
687 unlikePost(getPostId(this));
691 /* convert trust control buttons to javascript functions. */
692 $(postElement).find(".post-trust").submit(function() {
693 trustSone(getPostAuthor(this));
696 $(postElement).find(".post-distrust").submit(function() {
697 distrustSone(getPostAuthor(this));
700 $(postElement).find(".post-untrust").submit(function() {
701 untrustSone(getPostAuthor(this));
705 /* convert bookmark/unbookmark buttons to javascript functions. */
706 $(postElement).find(".bookmark").submit(function() {
707 bookmarkPost(getPostId(this));
710 $(postElement).find(".unbookmark").submit(function() {
711 unbookmarkPost(getPostId(this));
715 /* convert “show source” link into javascript function. */
716 $(postElement).find(".show-source").each(function() {
717 $("a", this).click(function() {
718 $(".post-text.text", getPostElement(this)).toggleClass("hidden");
719 $(".post-text.raw-text", getPostElement(this)).toggleClass("hidden");
724 /* add “comment” link. */
725 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
727 /* process all replies. */
729 $(postElement).find(".reply").each(function() {
730 replyIds.push(getReplyId(this));
733 updateReplyTimes(replyIds.join(","));
735 /* process reply input fields. */
736 getTranslation("WebInterface.DefaultText.Reply", function(text) {
737 $(postElement).find("input.reply-input").each(function() {
738 registerInputTextareaSwap(this, text, "text", false, false);
742 /* process sender selection. */
743 $(".select-sender", postElement).css("display", "inline");
744 $(".sender", postElement).hide();
745 $(".select-sender button", postElement).click(function() {
746 $(".sender", postElement).show();
747 $(".select-sender", postElement).hide();
751 /* mark everything as known on click. */
752 $(postElement).click(function(event) {
753 if ($(event.target).hasClass("click-to-show")) {
756 markPostAsKnown(this);
759 /* hide reply input field. */
760 $(postElement).find(".create-reply").addClass("hidden");
764 * Ajaxifies the given reply element.
766 * @param replyElement
767 * The reply element to ajaxify
769 function ajaxifyReply(replyElement) {
770 $(replyElement).find(".like-reply").submit(function() {
771 likeReply(getReplyId(this));
774 $(replyElement).find(".unlike-reply").submit(function() {
775 unlikeReply(getReplyId(this));
778 (function(replyElement) {
779 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
780 $(replyElement).find(".delete-reply button").each(function() {
781 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
785 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
787 /* convert “show source” link into javascript function. */
788 $(replyElement).find(".show-reply-source").each(function() {
789 $("a", this).click(function() {
790 $(".reply-text.text", getReplyElement(this)).toggleClass("hidden");
791 $(".reply-text.raw-text", getReplyElement(this)).toggleClass("hidden");
796 /* convert trust control buttons to javascript functions. */
797 $(replyElement).find(".reply-trust").submit(function() {
798 trustSone(getReplyAuthor(this));
801 $(replyElement).find(".reply-distrust").submit(function() {
802 distrustSone(getReplyAuthor(this));
805 $(replyElement).find(".reply-untrust").submit(function() {
806 untrustSone(getReplyAuthor(this));
812 * Ajaxifies the given notification by replacing the form with AJAX.
814 * @param notification
815 * jQuery object representing the notification.
817 function ajaxifyNotification(notification) {
818 notification.find("form").submit(function() {
821 notification.find("input[name=returnPage]").val($.url.attr("relative"));
822 if (notification.find(".short-text").length > 0) {
823 notification.find(".short-text").removeClass("hidden");
824 notification.find(".text").addClass("hidden");
826 notification.find("form.mark-as-read button").click(function() {
827 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": $(":input[name=id]", this.form).val()});
829 notification.find("a[class^='link-']").each(function() {
830 linkElement = $(this);
831 if (linkElement.is("[href^='viewPost']")) {
832 id = linkElement.attr("class").substr(5);
834 linkElement.attr("href", "#post-" + id);
838 notification.find("form.dismiss button").click(function() {
839 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
840 /* dismiss in case of error, too. */
841 notification.slideUp();
842 }, function(xmlHttpRequest, textStatus, error) {
849 function getStatus() {
850 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
851 if ((data != null) && data.success) {
852 /* process Sone information. */
853 $.each(data.sones, function(index, value) {
854 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated);
856 /* search for removed notifications. */
857 $("#sone #notification-area .notification").each(function() {
858 notificationId = $(this).attr("id");
859 foundNotification = false;
860 $.each(data.notifications, function(index, value) {
861 if (value.id == notificationId) {
862 foundNotification = true;
866 if (!foundNotification) {
867 $(this).slideUp("normal", function() {
872 /* process notifications. */
873 $.each(data.notifications, function(index, value) {
874 oldNotification = $("#sone #notification-area .notification#" + value.id);
875 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
876 if (oldNotification.length != 0) {
877 if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
878 opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
879 notification.find(".short-text").toggleClass("hidden", opened);
880 notification.find(".text").toggleClass("hidden", !opened);
882 oldNotification.replaceWith(notification.show());
884 $("#sone #notification-area").append(notification);
885 notification.slideDown();
889 /* process new posts. */
890 $.each(data.newPosts, function(index, value) {
891 loadNewPost(value.id, value.sone, value.recipient, value.time);
893 /* process new replies. */
894 $.each(data.newReplies, function(index, value) {
895 loadNewReply(value.id, value.sone, value.post, value.postSone);
897 /* do it again in 5 seconds. */
898 setTimeout(getStatus, 5000);
900 /* data.success was false, wait 30 seconds. */
901 setTimeout(getStatus, 30000);
903 }, function(xmlHttpRequest, textStatus, error) {
904 /* something really bad happend, wait a minute. */
905 setTimeout(getStatus, 60000);
910 * Returns the ID of the currently logged in Sone.
912 * @return The ID of the current Sone, or an empty string if no Sone is logged
915 function getCurrentSoneId() {
916 return $("#currentSoneId").text();
920 * Returns the content of the page-id attribute.
922 * @returns The page ID
924 function getPageId() {
925 return $("#sone .page-id").text();
929 * Returns whether the current page is the index page.
931 * @returns {Boolean} <code>true</code> if the current page is the index page,
932 * <code>false</code> otherwise
934 function isIndexPage() {
935 return getPageId() == "index";
939 * Returns whether the current page is a “view Sone” page.
941 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
942 * page, <code>false</code> otherwise
944 function isViewSonePage() {
945 return getPageId() == "view-sone";
949 * Returns the ID of the currently shown Sone. This will only return a sensible
950 * value if isViewSonePage() returns <code>true</code>.
952 * @returns The ID of the currently shown Sone
954 function getShownSoneId() {
955 return $("#sone .sone-id").text();
959 * Returns whether the current page is a “view post” page.
961 * @returns {Boolean} <code>true</code> if the current page is a “view post”
962 * page, <code>false</code> otherwise
964 function isViewPostPage() {
965 return getPageId() == "view-post";
969 * Returns the ID of the currently shown post. This will only return a sensible
970 * value if isViewPostPage() returns <code>true</code>.
972 * @returns The ID of the currently shown post
974 function getShownPostId() {
975 return $("#sone .post-id").text();
979 * Returns whether the current page is the “known Sones” page.
981 * @returns {Boolean} <code>true</code> if the current page is the “known
982 * Sones” page, <code>false</code> otherwise
984 function isKnownSonesPage() {
985 return getPageId() == "known-sones";
989 * Returns whether a post with the given ID exists on the current page.
992 * The post ID to check for
993 * @returns {Boolean} <code>true</code> if a post with the given ID already
994 * exists on the page, <code>false</code> otherwise
996 function hasPost(postId) {
997 return $(".post#" + postId).length > 0;
1001 * Returns whether a reply with the given ID exists on the current page.
1004 * The reply ID to check for
1005 * @returns {Boolean} <code>true</code> if a reply with the given ID already
1006 * exists on the page, <code>false</code> otherwise
1008 function hasReply(replyId) {
1009 return $("#sone .reply#" + replyId).length > 0;
1012 function loadNewPost(postId, soneId, recipientId, time) {
1013 if (hasPost(postId)) {
1016 if (!isIndexPage()) {
1017 if (!isViewPostPage() || (getShownPostId() != postId)) {
1018 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
1023 if (getPostTime($("#sone .post").last()) > time) {
1026 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
1027 if ((data != null) && data.success) {
1028 if (hasPost(data.post.id)) {
1031 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
1034 var firstOlderPost = null;
1035 $("#sone .post").each(function() {
1036 if (getPostTime(this) < data.post.time) {
1037 firstOlderPost = $(this);
1041 newPost = $(data.post.html).addClass("hidden");
1042 if (firstOlderPost != null) {
1043 newPost.insertBefore(firstOlderPost);
1045 ajaxifyPost(newPost);
1046 updatePostTimes(data.post.id);
1047 newPost.slideDown();
1053 function loadNewReply(replyId, soneId, postId, postSoneId) {
1054 if (hasReply(replyId)) {
1057 if (!hasPost(postId)) {
1060 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
1062 if ((data != null) && data.success) {
1063 if (hasReply(data.reply.id)) {
1066 $("#sone .post#" + data.reply.postId).each(function() {
1067 var firstNewerReply = null;
1068 $(this).find(".replies .reply").each(function() {
1069 if (getReplyTime(this) > data.reply.time) {
1070 firstNewerReply = $(this);
1074 newReply = $(data.reply.html).addClass("hidden");
1075 if (firstNewerReply != null) {
1076 newReply.insertBefore(firstNewerReply);
1078 if ($(this).find(".replies .create-reply")) {
1079 $(this).find(".replies .create-reply").before(newReply);
1081 $(this).find(".replies").append(newReply);
1084 ajaxifyReply(newReply);
1085 updateReplyTimes(data.reply.id);
1086 newReply.slideDown();
1095 * Marks the given Sone as known if it is still new.
1097 * @param soneElement
1098 * The Sone to mark as known
1100 function markSoneAsKnown(soneElement) {
1101 if ($(".new", soneElement).length > 0) {
1102 $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
1103 $(soneElement).removeClass("new");
1108 function markPostAsKnown(postElements) {
1109 $(postElements).each(function() {
1111 if ($(postElement).hasClass("new")) {
1112 (function(postElement) {
1113 $(postElement).removeClass("new");
1114 $(".click-to-show", postElement).removeClass("new");
1115 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
1119 markReplyAsKnown($(postElements).find(".reply"));
1122 function markReplyAsKnown(replyElements) {
1123 $(replyElements).each(function() {
1124 replyElement = this;
1125 if ($(replyElement).hasClass("new")) {
1126 (function(replyElement) {
1127 $(replyElement).removeClass("new");
1128 $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
1135 * Updates the time of the post with the given ID.
1138 * The ID of the post to update
1140 * The text of the time to show
1141 * @param refreshTime
1142 * The refresh time after which to request a new time (in seconds)
1144 * The tooltip to show
1146 function updatePostTime(postId, timeText, refreshTime, tooltip) {
1147 if (!getPost(postId).is(":visible")) {
1150 getPost(postId).find(".post-status-line > .time a").html(timeText).attr("title", tooltip);
1151 (function(postId, refreshTime) {
1152 setTimeout(function() {
1153 updatePostTimes(postId);
1154 }, refreshTime * 1000);
1155 })(postId, refreshTime);
1159 * Requests new rendered times for the posts with the given IDs.
1162 * Comma-separated post IDs
1164 function updatePostTimes(postIds) {
1165 $.getJSON("getTimes.ajax", { "posts" : postIds }, function(data, textStatus) {
1166 if ((data != null) && data.success) {
1167 $.each(data.postTimes, function(index, value) {
1168 updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
1175 * Updates the time of the reply with the given ID.
1178 * The ID of the reply to update
1180 * The text of the time to show
1181 * @param refreshTime
1182 * The refresh time after which to request a new time (in seconds)
1184 * The tooltip to show
1186 function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
1187 if (!getReply(replyId).is(":visible")) {
1190 getReply(replyId).find(".reply-status-line > .time").html(timeText).attr("title", tooltip);
1191 (function(replyId, refreshTime) {
1192 setTimeout(function() {
1193 updateReplyTimes(replyId);
1194 }, refreshTime * 1000);
1195 })(replyId, refreshTime);
1199 * Requests new rendered times for the posts with the given IDs.
1202 * Comma-separated post IDs
1204 function updateReplyTimes(replyIds) {
1205 $.getJSON("getTimes.ajax", { "replies" : replyIds }, function(data, textStatus) {
1206 if ((data != null) && data.success) {
1207 $.each(data.replyTimes, function(index, value) {
1208 updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
1214 function resetActivity() {
1215 title = document.title;
1216 if (title.indexOf('(') == 0) {
1217 setTitle(title.substr(title.indexOf(' ') + 1));
1221 function setActivity() {
1223 title = document.title;
1224 if (title.indexOf('(') != 0) {
1225 setTitle("(!) " + title);
1227 if (!iconBlinking) {
1228 setTimeout(toggleIcon, 1500);
1229 iconBlinking = true;
1235 * Sets the window title after a small delay to prevent race-condition issues.
1240 function setTitle(title) {
1241 setTimeout(function() {
1242 document.title = title;
1246 /** Whether the icon is currently showing activity. */
1247 var iconActive = false;
1249 /** Whether the icon is currently supposed to blink. */
1250 var iconBlinking = false;
1253 * Toggles the icon. If the window has gained focus and the icon is still
1254 * showing the activity state, it is returned to normal.
1256 function toggleIcon() {
1259 changeIcon("images/icon.png");
1262 iconBlinking = false;
1264 iconActive = !iconActive;
1265 changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
1266 setTimeout(toggleIcon, 1500);
1271 * Changes the icon of the page.
1274 * The new URL of the icon
1276 function changeIcon(iconUrl) {
1277 $("link[rel=icon]").remove();
1278 $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
1279 $("iframe[id=icon-update]")[0].src += "";
1283 * Creates a new notification.
1286 * The ID of the notificaiton
1288 * The text of the notification
1289 * @param dismissable
1290 * <code>true</code> if the notification can be dismissed by the
1293 function createNotification(id, text, dismissable) {
1294 notification = $("<div></div>").addClass("notification").attr("id", id);
1296 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
1297 dismissForm.find("input[name=notification]").val(id);
1298 notification.append(dismissForm);
1300 notification.append(text);
1301 return notification;
1305 * Shows the details of the notification with the given ID.
1307 * @param notificationId
1308 * The ID of the notification
1310 function showNotificationDetails(notificationId) {
1311 $("#sone .notification#" + notificationId + " .text").removeClass("hidden");
1312 $("#sone .notification#" + notificationId + " .short-text").addClass("hidden");
1316 * Deletes the field with the given ID from the profile.
1319 * The ID of the field to delete
1321 function deleteProfileField(fieldId) {
1322 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
1323 if (data && data.success) {
1324 $("#sone .profile-field#" + data.field.id).slideUp();
1330 * Renames a profile field.
1333 * The ID of the field to rename
1335 * The new name of the field
1336 * @param successFunction
1337 * Called when the renaming was successful
1339 function editProfileField(fieldId, newName, successFunction) {
1340 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1341 if (data && data.success) {
1348 * Moves the profile field with the given ID one slot in the given direction.
1351 * The ID of the field to move
1353 * The direction to move in (“up” or “down”)
1354 * @param successFunction
1355 * Function to call on success
1357 function moveProfileField(fieldId, direction, successFunction) {
1358 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1359 if (data && data.success) {
1366 * Moves the profile field with the given ID up one slot.
1369 * The ID of the field to move
1370 * @param successFunction
1371 * Function to call on success
1373 function moveProfileFieldUp(fieldId, successFunction) {
1374 moveProfileField(fieldId, "up", successFunction);
1378 * Moves the profile field with the given ID down one slot.
1381 * The ID of the field to move
1382 * @param successFunction
1383 * Function to call on success
1385 function moveProfileFieldDown(fieldId, successFunction) {
1386 moveProfileField(fieldId, "down", successFunction);
1390 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1395 $(document).ready(function() {
1397 /* this initializes the status update input field. */
1398 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1399 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1400 $("#sone #update-status .select-sender").css("display", "inline");
1401 $("#sone #update-status .sender").hide();
1402 $("#sone #update-status .select-sender button").click(function() {
1403 $("#sone #update-status .sender").show();
1404 $("#sone #update-status .select-sender").hide();
1407 $("#sone #update-status").submit(function() {
1408 button = $("button:submit", this);
1409 button.attr("disabled", "disabled");
1410 if ($(this).find(":input.default:enabled").length > 0) {
1413 sender = $(this).find(":input[name=sender]").val();
1414 text = $(this).find(":input[name=text]:enabled").val();
1415 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "sender": sender, "text": text }, function(data, textStatus) {
1416 if ((data != null) && data.success) {
1417 loadNewPost(data.postId, data.sone, data.recipient);
1419 button.removeAttr("disabled");
1421 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1422 $(this).find(":input[name=text]:enabled").val("").blur();
1423 $(this).find(".sender").hide();
1424 $(this).find(".select-sender").show();
1429 /* ajaxify the search input field. */
1430 getTranslation("WebInterface.DefaultText.Search", function(defaultText) {
1431 registerInputTextareaSwap("#sone #search input[name=query]", defaultText, "query", false, true);
1434 /* ajaxify input field on “view Sone” page. */
1435 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1436 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1437 $("#sone #post-message .select-sender").css("display", "inline");
1438 $("#sone #post-message .sender").hide();
1439 $("#sone #post-message .select-sender button").click(function() {
1440 $("#sone #post-message .sender").show();
1441 $("#sone #post-message .select-sender").hide();
1444 $("#sone #post-message").submit(function() {
1445 sender = $(this).find(":input[name=sender]").val();
1446 text = $(this).find(":input[name=text]:enabled").val();
1447 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "sender": sender, "text": text }, function(data, textStatus) {
1448 if ((data != null) && data.success) {
1449 loadNewPost(data.postId, getCurrentSoneId());
1452 $(this).find(":input[name=sender]").val(getCurrentSoneId());
1453 $(this).find(":input[name=text]:enabled").val("").blur();
1454 $(this).find(".sender").hide();
1455 $(this).find(".select-sender").show();
1460 /* Ajaxifies all posts. */
1461 /* calling getTranslation here will cache the necessary values. */
1462 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1463 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1464 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1465 $("#sone .post").each(function() {
1472 /* update post times. */
1474 $("#sone .post").each(function() {
1475 postIds.push(getPostId(this));
1477 updatePostTimes(postIds.join(","));
1479 /* hides all replies but the latest two. */
1480 if (!isViewPostPage()) {
1481 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1482 $("#sone .post .replies").each(function() {
1483 allReplies = $(this).find(".reply");
1484 if (allReplies.length > 2) {
1486 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1487 $(allReplies[replyIndex]).addClass("hidden");
1488 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1490 clickToShowElement = $("<div></div>").addClass("click-to-show");
1492 clickToShowElement.addClass("new");
1494 (function(clickToShowElement, allReplies, text) {
1495 clickToShowElement.text(text);
1496 clickToShowElement.click(function() {
1497 allReplies.removeClass("hidden");
1498 clickToShowElement.addClass("hidden");
1500 })(clickToShowElement, allReplies, text);
1501 $(allReplies[0]).before(clickToShowElement);
1507 $("#sone .sone").each(function() {
1508 ajaxifySone($(this));
1511 /* process all existing notifications, ajaxify dismiss buttons. */
1512 $("#sone #notification-area .notification").each(function() {
1513 ajaxifyNotification($(this));
1516 /* activate status polling. */
1517 setTimeout(getStatus, 5000);
1519 /* reset activity counter when the page has focus. */
1520 $(window).focus(function() {
1523 }).blur(function() {