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 replyElement = $("#sone .post#" + postId + " .create-reply");
73 replyElement.removeClass("hidden");
74 replyElement.removeClass("light");
75 (function(replyElement) {
76 replyElement.find("input.reply-input").blur(function() {
77 if ($(this).hasClass("default")) {
78 replyElement.addClass("light");
81 replyElement.removeClass("light");
84 replyElement.find("input.reply-input").focus();
86 return commentElement;
88 $(element).find(".status-line .time").each(function() {
89 $(this).after(commentElement.clone(true));
93 var translations = {};
96 * Retrieves the translation for the given key and calls the callback function.
97 * The callback function takes a single parameter, the translated string.
100 * The key of the translation string
102 * The callback function
104 function getTranslation(key, callback) {
105 if (key in translations) {
106 callback(translations[key]);
109 $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
110 if ((data != null) && data.success) {
111 translations[key] = data.value;
112 callback(data.value);
114 }, function(xmlHttpRequest, textStatus, error) {
120 * Filters the given Sone ID, replacing all “~” characters by an underscore.
123 * The Sone ID to filter
124 * @returns The filtered Sone ID
126 function filterSoneId(soneId) {
127 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
131 * Updates the status of the given Sone.
134 * The ID of the Sone to update
136 * The status of the Sone (“idle”, “unknown”, “inserting”,
139 * Whether the Sone is modified
141 * Whether the Sone is locked
143 * The date and time of the last update (formatted for display)
145 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
146 $("#sone .sone." + filterSoneId(soneId)).
147 toggleClass("unknown", status == "unknown").
148 toggleClass("idle", status == "idle").
149 toggleClass("inserting", status == "inserting").
150 toggleClass("downloading", status == "downloading").
151 toggleClass("modified", modified);
152 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
153 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
154 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
155 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
159 * Enhances a “delete” button so that the confirmation is done on the same page.
164 * The text to show on the button
165 * @param deleteCallback
166 * The callback that actually deletes something
168 function enhanceDeleteButton(button, text, deleteCallback) {
170 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
171 $(this).fadeOut("slow");
174 }).insertAfter(button);
175 (function(button, newButton) {
176 button.click(function() {
177 button.fadeOut("slow", function() {
178 newButton.fadeIn("slow");
179 $(document).one("click", function() {
180 if (this != newButton.get(0)) {
181 newButton.fadeOut(function() {
189 })(button, newButton);
194 * Enhances a post’s “delete” button.
199 * The ID of the post to delete
201 * The text to replace the button with
203 function enhanceDeletePostButton(button, postId, text) {
204 enhanceDeleteButton(button, text, function() {
205 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
210 $("#sone .post#" + postId).slideUp();
211 } else if (data.error == "invalid-post-id") {
212 alert("Invalid post ID given!");
213 } else if (data.error == "auth-required") {
214 alert("You need to be logged in.");
215 } else if (data.error == "not-authorized") {
216 alert("You are not allowed to delete this post.");
218 }, function(xmlHttpRequest, textStatus, error) {
225 * Enhances a reply’s “delete” button.
230 * The ID of the reply to delete
232 * The text to replace the button with
234 function enhanceDeleteReplyButton(button, replyId, text) {
235 enhanceDeleteButton(button, text, function() {
236 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
241 $("#sone .reply#" + replyId).slideUp();
242 } else if (data.error == "invalid-reply-id") {
243 alert("Invalid reply ID given!");
244 } else if (data.error == "auth-required") {
245 alert("You need to be logged in.");
246 } else if (data.error == "not-authorized") {
247 alert("You are not allowed to delete this reply.");
249 }, function(xmlHttpRequest, textStatus, error) {
255 function getFormPassword() {
256 return $("#sone #formPassword").text();
259 function getSoneElement(element) {
260 return $(element).parents(".sone");
264 * Generates a list of Sones by concatening the names of the given sones with a
265 * new line character (“\n”).
268 * The sones to format
269 * @returns {String} The created string
271 function generateSoneList(sones) {
273 $.each(sones, function() {
274 if (soneList != "") {
277 soneList += this.name;
283 * Returns the ID of the Sone that this element belongs to.
286 * The element to locate the matching Sone ID for
287 * @returns The ID of the Sone, or undefined
289 function getSoneId(element) {
290 return getSoneElement(element).find(".id").text();
293 function getPostElement(element) {
294 return $(element).closest(".post");
297 function getPostId(element) {
298 return getPostElement(element).attr("id");
301 function getPostTime(element) {
302 return getPostElement(element).find(".storage-time").text();
305 function getReplyElement(element) {
306 return $(element).closest(".reply");
309 function getReplyId(element) {
310 return getReplyElement(element).attr("id");
313 function getReplyTime(element) {
314 return getReplyElement(element).find(".storage-time").text();
317 function likePost(postId) {
318 $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
319 if ((data == null) || !data.success) {
322 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
323 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
324 updatePostLikes(postId);
325 }, function(xmlHttpRequest, textStatus, error) {
330 function unlikePost(postId) {
331 $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
332 if ((data == null) || !data.success) {
335 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
336 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
337 updatePostLikes(postId);
338 }, function(xmlHttpRequest, textStatus, error) {
343 function updatePostLikes(postId) {
344 $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
345 if ((data != null) && data.success) {
346 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
347 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
348 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
350 }, function(xmlHttpRequest, textStatus, error) {
355 function likeReply(replyId) {
356 $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
357 if ((data == null) || !data.success) {
360 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
361 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
362 updateReplyLikes(replyId);
363 }, function(xmlHttpRequest, textStatus, error) {
368 function unlikeReply(replyId) {
369 $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
370 if ((data == null) || !data.success) {
373 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
374 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
375 updateReplyLikes(replyId);
376 }, function(xmlHttpRequest, textStatus, error) {
381 function updateReplyLikes(replyId) {
382 $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
383 if ((data != null) && data.success) {
384 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
385 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
386 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
388 }, function(xmlHttpRequest, textStatus, error) {
394 * Posts a reply and calls the given callback when the request finishes.
397 * The ID of the post the reply refers to
400 * @param callbackFunction
401 * The callback function to call when the request finishes (takes 3
402 * parameters: success, error, replyId)
404 function postReply(postId, text, callbackFunction) {
405 $.getJSON("ajax/createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
407 /* TODO - show error */
411 callbackFunction(true, null, data.reply);
413 callbackFunction(false, data.error);
415 }, function(xmlHttpRequest, textStatus, error) {
421 * Requests information about the reply with the given ID.
424 * The ID of the reply
425 * @param callbackFunction
426 * A callback function (parameters soneId, soneName, replyTime,
427 * replyDisplayTime, text, html)
429 function getReply(replyId, callbackFunction) {
430 $.getJSON("ajax/getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
431 if ((data != null) && data.success) {
432 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
434 }, function(xmlHttpRequest, textStatus, error) {
440 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
443 * The post element to ajaxify
445 function ajaxifyPost(postElement) {
446 $(postElement).find("form").submit(function() {
449 $(postElement).find(".create-reply button:submit").click(function() {
450 inputField = $(this.form).find(":input:enabled").get(0);
451 postId = getPostId(this);
452 text = $(inputField).val();
453 $(inputField).val("");
454 postReply(postId, text, function(success, error, replyId) {
456 loadNewReply(replyId);
457 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
465 /* replace all “delete” buttons with javascript. */
466 (function(postElement) {
467 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
468 postId = getPostId(postElement);
469 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
473 /* convert all “like” buttons to javascript functions. */
474 $(postElement).find(".like-post").submit(function() {
475 likePost(getPostId(this));
478 $(postElement).find(".unlike-post").submit(function() {
479 unlikePost(getPostId(this));
483 /* process all replies. */
484 $(postElement).find(".reply").each(function() {
488 /* process reply input fields. */
489 getTranslation("WebInterface.DefaultText.Reply", function(text) {
490 $(postElement).find("input.reply-input").each(function() {
491 registerInputTextareaSwap(this, text, "text", false, false);
492 addCommentLink(getPostId(postElement), postElement);
496 /* add “comment” link. */
497 addCommentLink(getPostId(postElement), postElement);
499 /* hide reply input field. */
500 $(postElement).find(".create-reply").addClass("hidden");
504 * Ajaxifies the given reply element.
506 * @param replyElement
507 * The reply element to ajaxify
509 function ajaxifyReply(replyElement) {
510 $(replyElement).find(".like-reply").submit(function() {
511 likeReply(getReplyId(this));
514 $(replyElement).find(".unlike-reply").submit(function() {
515 unlikeReply(getReplyId(this));
518 (function(replyElement) {
519 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
520 $(replyElement).find(".delete-reply button").each(function() {
521 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
525 addCommentLink(getPostId(replyElement), replyElement);
529 * Ajaxifies the given notification by replacing the form with AJAX.
531 * @param notification
532 * jQuery object representing the notification.
534 function ajaxifyNotification(notification) {
535 notification.find("form.dismiss").submit(function() {
538 notification.find("form.dismiss button").click(function() {
539 $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
540 /* dismiss in case of error, too. */
541 notification.slideUp();
542 }, function(xmlHttpRequest, textStatus, error) {
549 function getStatus() {
550 $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) {
551 if ((data != null) && data.success) {
552 /* process Sone information. */
553 $.each(data.sones, function(index, value) {
554 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
556 /* process notifications. */
557 $.each(data.notifications, function(index, value) {
558 oldNotification = $("#sone #notification-area .notification#" + value.id);
559 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
560 if (oldNotification.length != 0) {
561 oldNotification.replaceWith(notification.show());
563 $("#sone #notification-area").append(notification);
564 notification.slideDown();
567 $.each(data.removedNotifications, function(index, value) {
568 $("#sone #notification-area .notification#" + value.id).slideUp();
570 /* process new posts. */
571 $.each(data.newPosts, function(index, value) {
574 /* process new replies. */
575 $.each(data.newReplies, function(index, value) {
578 /* do it again in 5 seconds. */
579 setTimeout(getStatus, 5000);
581 /* data.success was false, wait 30 seconds. */
582 setTimeout(getStatus, 30000);
584 }, function(xmlHttpRequest, textStatus, error) {
585 /* something really bad happend, wait a minute. */
586 setTimeout(getStatus, 60000);
590 var loadedPosts = {};
591 var loadedReplies = {};
593 function loadNewPost(postId) {
594 if (postId in loadedPosts) {
597 loadedPosts[postId] = true;
598 $.getJSON("ajax/getPost.ajax", { "post" : postId }, function(data, textStatus) {
599 if ((data != null) && data.success) {
600 var firstOlderPost = null;
601 $("#sone .post").each(function() {
602 if (getPostTime(this) < data.post.time) {
603 firstOlderPost = $(this);
607 newPost = $(data.post.html).addClass("hidden");
608 if (firstOlderPost != null) {
609 newPost.insertBefore(firstOlderPost);
611 $("#sone #posts").append(newPost);
613 ajaxifyPost(newPost);
619 function loadNewReply(replyId) {
620 if (replyId in loadedReplies) {
623 loadedReplies[replyId] = true;
624 $.getJSON("ajax/getReply.ajax", { "reply": replyId }, function(data, textStatus) {
626 if ((data != null) && data.success) {
627 $("#sone .post#" + data.reply.postId).each(function() {
628 var firstNewerReply = null;
629 $(this).find(".replies .reply").each(function() {
630 if (getReplyTime(this) > data.reply.time) {
631 firstNewerReply = $(this);
635 newReply = $(data.reply.html).addClass("hidden");
636 if (firstNewerReply != null) {
637 newReply.insertAfter(firstNewerReply);
639 if ($(this).find(".replies .create-reply")) {
640 $(this).find(".replies .create-reply").before(newReply);
642 $(this).find(".replies").append(newReply);
645 ajaxifyReply(newReply);
646 newReply.slideDown();
653 * Creates a new notification.
656 * The ID of the notificaiton
658 * The text of the notification
660 * <code>true</code> if the notification can be dismissed by the
663 function createNotification(id, text, dismissable) {
664 notification = $("<div></div>").addClass("notification").attr("id", id);
666 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
667 dismissForm.find("input[name=notification]").val(id);
668 notification.append(dismissForm);
670 notification.append(text);
675 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
678 $(document).ready(function() {
680 /* this initializes the status update input field. */
681 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
682 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
683 $("#sone #update-status").submit(function() {
684 text = $(this).find(":input:enabled").val();
685 $.getJSON("ajax/createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) {
686 if ((data != null) && data.success) {
687 loadNewPost(data.postId);
690 $(this).find(":input:enabled").val("").blur();
695 /* Ajaxifies all posts. */
696 /* calling getTranslation here will cache the necessary values. */
697 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
698 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
699 getTranslation("WebInterface.DefaultText.Reply", function(text) {
700 $("#sone .post").each(function() {
707 /* hides all replies but the latest two. */
708 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
709 $("#sone .post .replies").each(function() {
710 allReplies = $(this).find(".reply");
711 if (allReplies.length > 2) {
713 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
714 $(allReplies[replyIndex]).addClass("hidden");
715 newHidden |= $(allReplies[replyIndex]).hasClass("new");
717 clickToShowElement = $("<div></div>").addClass("click-to-show");
719 clickToShowElement.addClass("new");
721 (function(clickToShowElement, allReplies, text) {
722 clickToShowElement.text(text);
723 clickToShowElement.click(function() {
724 allReplies.removeClass("hidden");
725 clickToShowElement.addClass("hidden");
727 })(clickToShowElement, allReplies, text);
728 $(allReplies[0]).before(clickToShowElement);
734 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
737 $("#sone .follow").submit(function() {
738 var followElement = this;
739 $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
740 $(followElement).addClass("hidden");
741 $(followElement).parent().find(".unfollow").removeClass("hidden");
745 $("#sone .unfollow").submit(function() {
746 var unfollowElement = this;
747 $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
748 $(unfollowElement).addClass("hidden");
749 $(unfollowElement).parent().find(".follow").removeClass("hidden");
753 $("#sone .lock").submit(function() {
754 var lockElement = this;
755 $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
756 $(lockElement).addClass("hidden");
757 $(lockElement).parent().find(".unlock").removeClass("hidden");
761 $("#sone .unlock").submit(function() {
762 var unlockElement = this;
763 $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
764 $(unlockElement).addClass("hidden");
765 $(unlockElement).parent().find(".lock").removeClass("hidden");
770 /* process all existing notifications, ajaxify dismiss buttons. */
771 $("#sone #notification-area .notification").each(function() {
772 ajaxifyNotification($(this));
775 /* activate status polling. */
776 setTimeout(getStatus, 5000);