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 if (!optional && (textarea.val() == "")) {
54 })($(this), textarea);
59 * Adds a “comment” link to all status lines contained in the given element.
64 * The element to add a “comment” link to
66 function addCommentLink(postId, element) {
67 if ($(element).find(".show-reply-form").length > 0) {
70 commentElement = (function(postId) {
71 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
72 markPostAsKnown(getPostElement(this));
73 replyElement = $("#sone .post#" + postId + " .create-reply");
74 replyElement.removeClass("hidden");
75 replyElement.removeClass("light");
76 (function(replyElement) {
77 replyElement.find("input.reply-input").blur(function() {
78 if ($(this).hasClass("default")) {
79 replyElement.addClass("light");
82 replyElement.removeClass("light");
85 replyElement.find("input.reply-input").focus();
87 return commentElement;
89 $(element).find(".status-line .time").each(function() {
90 $(this).after(commentElement.clone(true));
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("ajax/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 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
156 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
160 * Enhances a “delete” button so that the confirmation is done on the same page.
165 * The text to show on the button
166 * @param deleteCallback
167 * The callback that actually deletes something
169 function enhanceDeleteButton(button, text, deleteCallback) {
171 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
172 $(this).fadeOut("slow");
175 }).insertAfter(button);
176 (function(button, newButton) {
177 button.click(function() {
178 button.fadeOut("slow", function() {
179 newButton.fadeIn("slow");
180 $(document).one("click", function() {
181 if (this != newButton.get(0)) {
182 newButton.fadeOut(function() {
190 })(button, newButton);
195 * Enhances a post’s “delete” button.
200 * The ID of the post to delete
202 * The text to replace the button with
204 function enhanceDeletePostButton(button, postId, text) {
205 enhanceDeleteButton(button, text, function() {
206 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
211 $("#sone .post#" + postId).slideUp();
212 } else if (data.error == "invalid-post-id") {
213 alert("Invalid post ID given!");
214 } else if (data.error == "auth-required") {
215 alert("You need to be logged in.");
216 } else if (data.error == "not-authorized") {
217 alert("You are not allowed to delete this post.");
219 }, function(xmlHttpRequest, textStatus, error) {
226 * Enhances a reply’s “delete” button.
231 * The ID of the reply to delete
233 * The text to replace the button with
235 function enhanceDeleteReplyButton(button, replyId, text) {
236 enhanceDeleteButton(button, text, function() {
237 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
242 $("#sone .reply#" + replyId).slideUp();
243 } else if (data.error == "invalid-reply-id") {
244 alert("Invalid reply ID given!");
245 } else if (data.error == "auth-required") {
246 alert("You need to be logged in.");
247 } else if (data.error == "not-authorized") {
248 alert("You are not allowed to delete this reply.");
250 }, function(xmlHttpRequest, textStatus, error) {
256 function getFormPassword() {
257 return $("#sone #formPassword").text();
260 function getSoneElement(element) {
261 return $(element).parents(".sone");
265 * Generates a list of Sones by concatening the names of the given sones with a
266 * new line character (“\n”).
269 * The sones to format
270 * @returns {String} The created string
272 function generateSoneList(sones) {
274 $.each(sones, function() {
275 if (soneList != "") {
278 soneList += this.name;
284 * Returns the ID of the Sone that this element belongs to.
287 * The element to locate the matching Sone ID for
288 * @returns The ID of the Sone, or undefined
290 function getSoneId(element) {
291 return getSoneElement(element).find(".id").text();
294 function getPostElement(element) {
295 return $(element).closest(".post");
298 function getPostId(element) {
299 return getPostElement(element).attr("id");
302 function getPostTime(element) {
303 return getPostElement(element).find(".post-time").text();
306 function getReplyElement(element) {
307 return $(element).closest(".reply");
310 function getReplyId(element) {
311 return getReplyElement(element).attr("id");
314 function getReplyTime(element) {
315 return getReplyElement(element).find(".reply-time").text();
318 function likePost(postId) {
319 $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
320 if ((data == null) || !data.success) {
323 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
324 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
325 updatePostLikes(postId);
326 }, function(xmlHttpRequest, textStatus, error) {
331 function unlikePost(postId) {
332 $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
333 if ((data == null) || !data.success) {
336 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
337 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
338 updatePostLikes(postId);
339 }, function(xmlHttpRequest, textStatus, error) {
344 function updatePostLikes(postId) {
345 $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
346 if ((data != null) && data.success) {
347 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
348 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
349 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
351 }, function(xmlHttpRequest, textStatus, error) {
356 function likeReply(replyId) {
357 $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
358 if ((data == null) || !data.success) {
361 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
362 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
363 updateReplyLikes(replyId);
364 }, function(xmlHttpRequest, textStatus, error) {
369 function unlikeReply(replyId) {
370 $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
371 if ((data == null) || !data.success) {
374 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
375 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
376 updateReplyLikes(replyId);
377 }, function(xmlHttpRequest, textStatus, error) {
382 function updateReplyLikes(replyId) {
383 $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
384 if ((data != null) && data.success) {
385 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
386 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
387 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
389 }, function(xmlHttpRequest, textStatus, error) {
395 * Posts a reply and calls the given callback when the request finishes.
398 * The ID of the post the reply refers to
401 * @param callbackFunction
402 * The callback function to call when the request finishes (takes 3
403 * parameters: success, error, replyId)
405 function postReply(postId, text, callbackFunction) {
406 $.getJSON("ajax/createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
408 /* TODO - show error */
412 callbackFunction(true, null, data.reply);
414 callbackFunction(false, data.error);
416 }, function(xmlHttpRequest, textStatus, error) {
422 * Requests information about the reply with the given ID.
425 * The ID of the reply
426 * @param callbackFunction
427 * A callback function (parameters soneId, soneName, replyTime,
428 * replyDisplayTime, text, html)
430 function getReply(replyId, callbackFunction) {
431 $.getJSON("ajax/getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
432 if ((data != null) && data.success) {
433 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
435 }, function(xmlHttpRequest, textStatus, error) {
441 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
444 * The post element to ajaxify
446 function ajaxifyPost(postElement) {
447 $(postElement).find("form").submit(function() {
450 $(postElement).find(".create-reply button:submit").click(function() {
451 inputField = $(this.form).find(":input:enabled").get(0);
452 postId = getPostId(this);
453 text = $(inputField).val();
454 $(inputField).val("");
455 postReply(postId, text, function(success, error, replyId) {
457 loadNewReply(replyId);
458 markPostAsKnown(getPostElement(inputField));
459 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
467 /* replace all “delete” buttons with javascript. */
468 (function(postElement) {
469 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
470 postId = getPostId(postElement);
471 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
475 /* convert all “like” buttons to javascript functions. */
476 $(postElement).find(".like-post").submit(function() {
477 likePost(getPostId(this));
478 markPostAsKnown(getPostElement(this));
481 $(postElement).find(".unlike-post").submit(function() {
482 unlikePost(getPostId(this));
483 markPostAsKnown(getPostElement(this));
487 /* process all replies. */
488 $(postElement).find(".reply").each(function() {
492 /* process reply input fields. */
493 getTranslation("WebInterface.DefaultText.Reply", function(text) {
494 $(postElement).find("input.reply-input").each(function() {
495 registerInputTextareaSwap(this, text, "text", false, false);
496 addCommentLink(getPostId(postElement), postElement);
500 /* add “comment” link. */
501 addCommentLink(getPostId(postElement), postElement);
503 /* hide reply input field. */
504 $(postElement).find(".create-reply").addClass("hidden");
508 * Ajaxifies the given reply element.
510 * @param replyElement
511 * The reply element to ajaxify
513 function ajaxifyReply(replyElement) {
514 $(replyElement).find(".like-reply").submit(function() {
515 likeReply(getReplyId(this));
516 markPostAsKnown(getPostElement(this));
519 $(replyElement).find(".unlike-reply").submit(function() {
520 unlikeReply(getReplyId(this));
521 markPostAsKnown(getPostElement(this));
524 (function(replyElement) {
525 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
526 $(replyElement).find(".delete-reply button").each(function() {
527 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
531 addCommentLink(getPostId(replyElement), replyElement);
535 * Ajaxifies the given notification by replacing the form with AJAX.
537 * @param notification
538 * jQuery object representing the notification.
540 function ajaxifyNotification(notification) {
541 notification.find("form.dismiss").submit(function() {
544 notification.find("form.dismiss button").click(function() {
545 $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
546 /* dismiss in case of error, too. */
547 notification.slideUp();
548 }, function(xmlHttpRequest, textStatus, error) {
555 function getStatus() {
556 $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) {
557 if ((data != null) && data.success) {
558 /* process Sone information. */
559 $.each(data.sones, function(index, value) {
560 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
562 /* process notifications. */
563 $.each(data.notifications, function(index, value) {
564 oldNotification = $("#sone #notification-area .notification#" + value.id);
565 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
566 if (oldNotification.length != 0) {
567 oldNotification.replaceWith(notification.show());
569 $("#sone #notification-area").append(notification);
570 notification.slideDown();
574 $.each(data.removedNotifications, function(index, value) {
575 $("#sone #notification-area .notification#" + value.id).slideUp();
577 /* process new posts. */
578 $.each(data.newPosts, function(index, value) {
581 /* process new replies. */
582 $.each(data.newReplies, function(index, value) {
585 /* do it again in 5 seconds. */
586 setTimeout(getStatus, 5000);
588 /* data.success was false, wait 30 seconds. */
589 setTimeout(getStatus, 30000);
591 }, function(xmlHttpRequest, textStatus, error) {
592 /* something really bad happend, wait a minute. */
593 setTimeout(getStatus, 60000);
597 var loadedPosts = {};
598 var loadedReplies = {};
600 function loadNewPost(postId) {
601 if (postId in loadedPosts) {
604 loadedPosts[postId] = true;
605 $.getJSON("ajax/getPost.ajax", { "post" : postId }, function(data, textStatus) {
606 if ((data != null) && data.success) {
607 var firstOlderPost = null;
608 $("#sone .post").each(function() {
609 if (getPostTime(this) < data.post.time) {
610 firstOlderPost = $(this);
614 newPost = $(data.post.html).addClass("hidden");
615 if (firstOlderPost != null) {
616 newPost.insertBefore(firstOlderPost);
618 $("#sone #posts").append(newPost);
620 ajaxifyPost(newPost);
627 function loadNewReply(replyId) {
628 if (replyId in loadedReplies) {
631 loadedReplies[replyId] = true;
632 $.getJSON("ajax/getReply.ajax", { "reply": replyId }, function(data, textStatus) {
634 if ((data != null) && data.success) {
635 $("#sone .post#" + data.reply.postId).each(function() {
636 var firstNewerReply = null;
637 $(this).find(".replies .reply").each(function() {
638 if (getReplyTime(this) > data.reply.time) {
639 firstNewerReply = $(this);
643 newReply = $(data.reply.html).addClass("hidden");
644 if (firstNewerReply != null) {
645 newReply.insertBefore(firstNewerReply);
647 if ($(this).find(".replies .create-reply")) {
648 $(this).find(".replies .create-reply").before(newReply);
650 $(this).find(".replies").append(newReply);
653 ajaxifyReply(newReply);
654 newReply.slideDown();
661 function markPostAsKnown(postElements) {
662 $(postElements).each(function() {
664 if ($(postElement).hasClass("new")) {
665 (function(postElement) {
666 $.getJSON("ajax/markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) {
667 $(postElement).removeClass("new");
672 markReplyAsKnown($(postElements).find(".reply"));
675 function markReplyAsKnown(replyElements) {
676 $(replyElements).each(function() {
678 if ($(replyElement).hasClass("new")) {
679 (function(replyElement) {
680 $.getJSON("ajax/markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function(data, textStatus) {
681 $(replyElement).removeClass("new");
688 function resetActivity() {
689 title = document.title;
690 if (title.indexOf('(') == 0) {
691 document.title = title.substr(title.indexOf(' ') + 1);
695 function setActivity() {
696 title = document.title;
697 if (title.indexOf('(') != 0) {
698 document.title = "(!) " + title;
703 * Creates a new notification.
706 * The ID of the notificaiton
708 * The text of the notification
710 * <code>true</code> if the notification can be dismissed by the
713 function createNotification(id, text, dismissable) {
714 notification = $("<div></div>").addClass("notification").attr("id", id);
716 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
717 dismissForm.find("input[name=notification]").val(id);
718 notification.append(dismissForm);
720 notification.append(text);
725 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
728 $(document).ready(function() {
730 /* this initializes the status update input field. */
731 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
732 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
733 $("#sone #update-status").submit(function() {
734 text = $(this).find(":input:enabled").val();
735 $.getJSON("ajax/createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) {
736 if ((data != null) && data.success) {
737 loadNewPost(data.postId);
740 $(this).find(":input:enabled").val("").blur();
745 /* Ajaxifies all posts. */
746 /* calling getTranslation here will cache the necessary values. */
747 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
748 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
749 getTranslation("WebInterface.DefaultText.Reply", function(text) {
750 $("#sone .post").each(function() {
757 /* hides all replies but the latest two. */
758 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
759 $("#sone .post .replies").each(function() {
760 allReplies = $(this).find(".reply");
761 if (allReplies.length > 2) {
763 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
764 $(allReplies[replyIndex]).addClass("hidden");
765 newHidden |= $(allReplies[replyIndex]).hasClass("new");
767 clickToShowElement = $("<div></div>").addClass("click-to-show");
769 clickToShowElement.addClass("new");
771 (function(clickToShowElement, allReplies, text) {
772 clickToShowElement.text(text);
773 clickToShowElement.click(function() {
774 allReplies.removeClass("hidden");
775 clickToShowElement.addClass("hidden");
777 })(clickToShowElement, allReplies, text);
778 $(allReplies[0]).before(clickToShowElement);
784 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
787 $("#sone .follow").submit(function() {
788 var followElement = this;
789 $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
790 $(followElement).addClass("hidden");
791 $(followElement).parent().find(".unfollow").removeClass("hidden");
795 $("#sone .unfollow").submit(function() {
796 var unfollowElement = this;
797 $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
798 $(unfollowElement).addClass("hidden");
799 $(unfollowElement).parent().find(".follow").removeClass("hidden");
803 $("#sone .lock").submit(function() {
804 var lockElement = this;
805 $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
806 $(lockElement).addClass("hidden");
807 $(lockElement).parent().find(".unlock").removeClass("hidden");
811 $("#sone .unlock").submit(function() {
812 var unlockElement = this;
813 $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
814 $(unlockElement).addClass("hidden");
815 $(unlockElement).parent().find(".lock").removeClass("hidden");
820 /* process all existing notifications, ajaxify dismiss buttons. */
821 $("#sone #notification-area .notification").each(function() {
822 ajaxifyNotification($(this));
825 /* activate status polling. */
826 setTimeout(getStatus, 5000);
828 /* reset activity counter when the page has focus. */
829 $(window).focus(function() {