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 getReply(replyId, function(soneId, soneName, replyTime, replyDisplayTime, text, html) {
457 newReply = $(html).insertBefore("#sone .post#" + postId + " .create-reply");
458 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
459 ajaxifyReply(newReply);
468 /* replace all “delete” buttons with javascript. */
469 (function(postElement) {
470 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
471 postId = getPostId(postElement);
472 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
476 /* convert all “like” buttons to javascript functions. */
477 $(postElement).find(".like-post").submit(function() {
478 likePost(getPostId(this));
481 $(postElement).find(".unlike-post").submit(function() {
482 unlikePost(getPostId(this));
486 /* process all replies. */
487 $(postElement).find(".reply").each(function() {
491 /* process reply input fields. */
492 getTranslation("WebInterface.DefaultText.Reply", function(text) {
493 $(postElement).find("input.reply-input").each(function() {
494 registerInputTextareaSwap(this, text, "text", false, false);
495 addCommentLink(getPostId(postElement), postElement);
499 /* add “comment” link. */
500 addCommentLink(getPostId(postElement), postElement);
502 /* hide reply input field. */
503 $(postElement).find(".create-reply").addClass("hidden");
507 * Ajaxifies the given reply element.
509 * @param replyElement
510 * The reply element to ajaxify
512 function ajaxifyReply(replyElement) {
513 $(replyElement).find(".like-reply").submit(function() {
514 likeReply(getReplyId(this));
517 $(replyElement).find(".unlike-reply").submit(function() {
518 unlikeReply(getReplyId(this));
521 (function(replyElement) {
522 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
523 $(replyElement).find(".delete-reply button").each(function() {
524 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
528 addCommentLink(getPostId(replyElement), replyElement);
532 * Ajaxifies the given notification by replacing the form with AJAX.
534 * @param notification
535 * jQuery object representing the notification.
537 function ajaxifyNotification(notification) {
538 notification.find("form.dismiss").submit(function() {
541 notification.find("form.dismiss button").click(function() {
542 $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
543 /* dismiss in case of error, too. */
544 notification.slideUp();
545 }, function(xmlHttpRequest, textStatus, error) {
552 function getStatus() {
553 $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) {
554 if ((data != null) && data.success) {
555 /* process Sone information. */
556 $.each(data.sones, function(index, value) {
557 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
559 /* process notifications. */
560 $.each(data.notifications, function(index, value) {
561 oldNotification = $("#sone #notification-area .notification#" + value.id);
562 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
563 if (oldNotification.length != 0) {
564 oldNotification.replaceWith(notification.show());
566 $("#sone #notification-area").append(notification);
567 notification.slideDown();
570 $.each(data.removedNotifications, function(index, value) {
571 $("#sone #notification-area .notification#" + value.id).slideUp();
573 /* process new posts. */
574 $.each(data.newPosts, function(index, value) {
577 /* process new replies. */
578 $.each(data.newReplies, function(index, value) {
581 /* do it again in 5 seconds. */
582 setTimeout(getStatus, 5000);
584 /* data.success was false, wait 30 seconds. */
585 setTimeout(getStatus, 30000);
587 }, function(xmlHttpRequest, textStatus, error) {
588 /* something really bad happend, wait a minute. */
589 setTimeout(getStatus, 60000);
593 var loadedPosts = {};
594 var loadedReplies = {};
596 function loadNewPost(postId) {
597 if (postId in loadedPosts) {
600 loadedPosts[postId] = true;
601 $.getJSON("ajax/getPost.ajax", { "post" : postId }, function(data, textStatus) {
602 if ((data != null) && data.success) {
603 var firstOlderPost = null;
604 $("#sone #posts .post").each(function() {
605 if (getPostTime(this) < data.post.time) {
606 firstOlderPost = $(this);
610 newPost = $(data.post.html).addClass("hidden");
611 if (firstOlderPost != null) {
612 newPost.insertBefore(firstOlderPost);
614 $("#sone #posts .post:last").after(newPost);
616 ajaxifyPost(newPost);
622 function loadNewReply(replyId) {
623 if (replyId in loadedReplies) {
626 loadedReplies[replyId] = true;
627 $.getJSON("ajax/getReply.ajax", { "reply": replyId }, function(data, textStatus) {
629 $("#sone #posts .post#" + data.reply.postId).each(function() {
630 var firstNewerReply = null;
631 $(this).find(".replies .reply").each(function() {
632 if (getReplyTime(this) > data.reply.time) {
633 firstNewerReply = $(this);
637 newReply = $(data.reply.html);
638 if (firstNewerReply != null) {
639 newReply.insertAfter(firstNewerReply);
641 $(this).find(".replies .reply:last").after(newReply);
643 ajaxifyReply(newReply);
649 * Creates a new notification.
652 * The ID of the notificaiton
654 * The text of the notification
656 * <code>true</code> if the notification can be dismissed by the
659 function createNotification(id, text, dismissable) {
660 notification = $("<div></div>").addClass("notification").attr("id", id);
662 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
663 dismissForm.find("input[name=notification]").val(id);
664 notification.append(dismissForm);
666 notification.append(text);
671 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
674 $(document).ready(function() {
676 /* this initializes the status update input field. */
677 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
678 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
679 $("#sone #update-status").submit(function() {
680 text = $(this).find(":input:enabled").val();
681 $.getJSON("ajax/createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) {
682 if ((data != null) && data.success) {
683 loadNewPost(data.postId);
686 $(this).find(":input:enabled").val("").blur();
691 /* Ajaxifies all posts. */
692 /* calling getTranslation here will cache the necessary values. */
693 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
694 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
695 getTranslation("WebInterface.DefaultText.Reply", function(text) {
696 $("#sone .post").each(function() {
703 /* hides all replies but the latest two. */
704 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
705 $("#sone .post .replies").each(function() {
706 allReplies = $(this).find(".reply");
707 if (allReplies.length > 2) {
709 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
710 $(allReplies[replyIndex]).addClass("hidden");
711 newHidden |= $(allReplies[replyIndex]).hasClass("new");
713 clickToShowElement = $("<div></div>").addClass("click-to-show");
715 clickToShowElement.addClass("new");
717 (function(clickToShowElement, allReplies, text) {
718 clickToShowElement.text(text);
719 clickToShowElement.click(function() {
720 allReplies.removeClass("hidden");
721 clickToShowElement.addClass("hidden");
723 })(clickToShowElement, allReplies, text);
724 $(allReplies[0]).before(clickToShowElement);
730 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
733 $("#sone .follow").submit(function() {
734 var followElement = this;
735 $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
736 $(followElement).addClass("hidden");
737 $(followElement).parent().find(".unfollow").removeClass("hidden");
741 $("#sone .unfollow").submit(function() {
742 var unfollowElement = this;
743 $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
744 $(unfollowElement).addClass("hidden");
745 $(unfollowElement).parent().find(".follow").removeClass("hidden");
749 $("#sone .lock").submit(function() {
750 var lockElement = this;
751 $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
752 $(lockElement).addClass("hidden");
753 $(lockElement).parent().find(".unlock").removeClass("hidden");
757 $("#sone .unlock").submit(function() {
758 var unlockElement = this;
759 $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
760 $(unlockElement).addClass("hidden");
761 $(unlockElement).parent().find(".lock").removeClass("hidden");
766 /* process all existing notifications, ajaxify dismiss buttons. */
767 $("#sone #notification-area .notification").each(function() {
768 ajaxifyNotification($(this));
771 /* activate status polling. */
772 setTimeout(getStatus, 5000);