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(inputSelector, defaultText, inputFieldName, optional, dontUseTextarea) {
27 $(inputSelector).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);
58 /* hide all the “create reply” forms until a link is clicked. */
59 function addCommentLinks() {
63 $("#sone .post").each(function() {
64 postId = $(this).attr("id");
65 addCommentLink(postId, $(this));
70 * Adds a “comment” link to all status lines contained in the given element.
75 * The element to add a “comment” link to
77 function addCommentLink(postId, element) {
78 commentElement = (function(postId) {
79 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
80 replyElement = $("#sone .post#" + postId + " .create-reply");
81 replyElement.removeClass("hidden");
82 replyElement.removeClass("light");
83 (function(replyElement) {
84 replyElement.find("input.reply-input").blur(function() {
85 if ($(this).hasClass("default")) {
86 replyElement.addClass("light");
89 replyElement.removeClass("light");
92 replyElement.find("input.reply-input").focus();
94 return commentElement;
96 element.find(".create-reply").addClass("hidden");
97 element.find(".status-line .time").each(function() {
98 $(this).after(commentElement.clone(true));
103 * Retrieves the translation for the given key and calls the callback function.
104 * The callback function takes a single parameter, the translated string.
107 * The key of the translation string
109 * The callback function
111 function getTranslation(key, callback) {
112 $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
113 if ((data != null) && data.success) {
114 callback(data.value);
116 }, function(xmlHttpRequest, textStatus, error) {
122 * Filters the given Sone ID, replacing all “~” characters by an underscore.
125 * The Sone ID to filter
126 * @returns The filtered Sone ID
128 function filterSoneId(soneId) {
129 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
133 * Updates the status of the given Sone.
136 * The ID of the Sone to update
138 * The status of the Sone (“idle”, “unknown”, “inserting”,
141 * Whether the Sone is modified
143 * Whether the Sone is locked
145 * The date and time of the last update (formatted for display)
147 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
148 $("#sone .sone." + filterSoneId(soneId)).
149 toggleClass("unknown", status == "unknown").
150 toggleClass("idle", status == "idle").
151 toggleClass("inserting", status == "inserting").
152 toggleClass("downloading", status == "downloading").
153 toggleClass("modified", modified);
154 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
155 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
156 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
157 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
161 * Enhances a “delete” button so that the confirmation is done on the same page.
164 * The selector of the button
166 * The text to show on the button
167 * @param deleteCallback
168 * The callback that actually deletes something
170 function enhanceDeleteButton(buttonId, text, deleteCallback) {
171 button = $(buttonId);
173 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
174 $(this).fadeOut("slow");
177 }).insertAfter(button);
178 (function(button, newButton) {
179 button.click(function() {
180 button.fadeOut("slow", function() {
181 newButton.fadeIn("slow");
182 $(document).one("click", function() {
183 if (this != newButton.get(0)) {
184 newButton.fadeOut(function() {
192 })(button, newButton);
197 * Enhances a post’s “delete” button.
200 * The selector of the button
202 * The ID of the post to delete
204 * The text to replace the button with
206 function enhanceDeletePostButton(buttonId, postId, text) {
207 enhanceDeleteButton(buttonId, text, function() {
208 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
213 $("#sone .post#" + postId).slideUp();
214 } else if (data.error == "invalid-post-id") {
215 alert("Invalid post ID given!");
216 } else if (data.error == "auth-required") {
217 alert("You need to be logged in.");
218 } else if (data.error == "not-authorized") {
219 alert("You are not allowed to delete this post.");
221 }, function(xmlHttpRequest, textStatus, error) {
228 * Enhances a reply’s “delete” button.
231 * The selector of the button
233 * The ID of the reply to delete
235 * The text to replace the button with
237 function enhanceDeleteReplyButton(buttonId, replyId, text) {
238 enhanceDeleteButton(buttonId, text, function() {
239 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
244 $("#sone .reply#" + replyId).slideUp();
245 } else if (data.error == "invalid-reply-id") {
246 alert("Invalid reply ID given!");
247 } else if (data.error == "auth-required") {
248 alert("You need to be logged in.");
249 } else if (data.error == "not-authorized") {
250 alert("You are not allowed to delete this reply.");
252 }, function(xmlHttpRequest, textStatus, error) {
258 function getFormPassword() {
259 return $("#sone #formPassword").text();
262 function getSoneElement(element) {
263 return $(element).parents(".sone");
267 * Generates a list of Sones by concatening the names of the given sones with a
268 * new line character (“\n”).
271 * The sones to format
272 * @returns {String} The created string
274 function generateSoneList(sones) {
276 $.each(sones, function() {
277 if (soneList != "") {
280 soneList += this.name;
286 * Returns the ID of the Sone that this element belongs to.
289 * The element to locate the matching Sone ID for
290 * @returns The ID of the Sone, or undefined
292 function getSoneId(element) {
293 return getSoneElement(element).find(".id").text();
296 function getPostElement(element) {
297 return $(element).parents(".post");
300 function getPostId(element) {
301 return getPostElement(element).attr("id");
304 function getReplyElement(element) {
305 return $(element).parents(".reply");
308 function getReplyId(element) {
309 return getReplyElement(element).attr("id");
312 function likePost(postId) {
313 $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
314 if ((data == null) || !data.success) {
317 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
318 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
319 updatePostLikes(postId);
320 }, function(xmlHttpRequest, textStatus, error) {
325 function unlikePost(postId) {
326 $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
327 if ((data == null) || !data.success) {
330 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
331 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
332 updatePostLikes(postId);
333 }, function(xmlHttpRequest, textStatus, error) {
338 function updatePostLikes(postId) {
339 $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
340 if ((data != null) && data.success) {
341 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
342 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
343 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
345 }, function(xmlHttpRequest, textStatus, error) {
350 function likeReply(replyId) {
351 $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
352 if ((data == null) || !data.success) {
355 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
356 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
357 updateReplyLikes(replyId);
358 }, function(xmlHttpRequest, textStatus, error) {
363 function unlikeReply(replyId) {
364 $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
365 if ((data == null) || !data.success) {
368 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
369 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
370 updateReplyLikes(replyId);
371 }, function(xmlHttpRequest, textStatus, error) {
376 function updateReplyLikes(replyId) {
377 $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
378 if ((data != null) && data.success) {
379 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
380 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
381 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
383 }, function(xmlHttpRequest, textStatus, error) {
389 * Posts a reply and calls the given callback when the request finishes.
392 * The ID of the post the reply refers to
395 * @param callbackFunction
396 * The callback function to call when the request finishes (takes 3
397 * parameters: success, error, replyId)
399 function postReply(postId, text, callbackFunction) {
400 $.getJSON("ajax/createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
402 /* TODO - show error */
406 callbackFunction(true, null, data.reply);
408 callbackFunction(false, data.error);
410 }, function(xmlHttpRequest, textStatus, error) {
416 * Requests information about the reply with the given ID.
419 * The ID of the reply
420 * @param callbackFunction
421 * A callback function (parameters soneId, soneName, replyTime,
422 * replyDisplayTime, text, html)
424 function getReply(replyId, callbackFunction) {
425 $.getJSON("ajax/getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
426 if ((data != null) && data.success) {
427 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
429 }, function(xmlHttpRequest, textStatus, error) {
435 * Ajaxifies the given notification by replacing the form with AJAX.
437 * @param notification
438 * jQuery object representing the notification.
440 function ajaxifyNotification(notification) {
441 notification.find("form.dismiss").submit(function() {
444 notification.find("form.dismiss button").click(function() {
445 $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
446 /* dismiss in case of error, too. */
447 notification.slideUp();
448 }, function(xmlHttpRequest, textStatus, error) {
455 function getStatus() {
456 $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) {
457 if ((data != null) && data.success) {
458 /* process Sone information. */
459 $.each(data.sones, function(index, value) {
460 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
462 /* process notifications. */
463 $.each(data.notifications, function(index, value) {
464 oldNotification = $("#sone #notification-area .notification#" + value.id);
465 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
466 if (oldNotification.length != 0) {
467 oldNotification.replaceWith(notification.show());
469 $("#sone #notification-area").append(notification);
470 notification.slideDown();
473 $.each(data.removedNotifications, function(index, value) {
474 $("#sone #notification-area .notification#" + value.id).slideUp();
476 /* do it again in 5 seconds. */
477 setTimeout(getStatus, 5000);
479 /* data.success was false, wait 30 seconds. */
480 setTimeout(getStatus, 30000);
482 }, function(xmlHttpRequest, textStatus, error) {
483 /* something really bad happend, wait a minute. */
484 setTimeout(getStatus, 60000);
489 * Creates a new notification.
492 * The ID of the notificaiton
494 * The text of the notification
496 * <code>true</code> if the notification can be dismissed by the
499 function createNotification(id, text, dismissable) {
500 notification = $("<div></div>").addClass("notification").attr("id", id);
502 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
503 dismissForm.find("input[name=notification]").val(id);
504 notification.append(dismissForm);
506 notification.append(text);
511 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
514 $(document).ready(function() {
516 /* this initializes the status update input field. */
517 getTranslation("WebInterface.DefaultText.StatusUpdate", function(text) {
518 registerInputTextareaSwap("#sone #update-status .status-input", text, "text", false, false);
521 /* this initializes all reply input fields. */
522 getTranslation("WebInterface.DefaultText.Reply", function(text) {
523 registerInputTextareaSwap("#sone input.reply-input", text, "text", false, false);
527 /* replaces all “post reply!” forms with AJAX. */
528 $("#sone .create-reply button:submit").click(function() {
529 $(this.form).submit(function() {
532 inputField = $(this.form).find(":input:enabled").get(0);
533 postId = getPostId($(inputField));
534 text = $(inputField).val();
535 $(inputField).val("");
536 postReply(postId, text, function(success, error, replyId) {
538 getReply(replyId, function(soneId, soneName, replyTime, replyDisplayTime, text, html) {
539 newReply = $(html).insertBefore("#sone .post#" + postId + " .create-reply");
540 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
541 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
542 enhanceDeleteReplyButton("#sone .post#" + postId + " .reply#" + replyId + " .delete button", replyId, deleteReplyText);
544 newReply.find(".status-line .like").submit(function() {
545 likeReply(getReplyId(this));
548 newReply.find(".status-line .unlike").submit(function() {
549 unlikeReply(getReplyId(this));
552 addCommentLink(postId, newReply);
561 /* replace all “delete” buttons with javascript. */
562 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
563 deletePostText = text;
564 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
565 deleteReplyText = text;
566 $("#sone .post").each(function() {
567 postId = $(this).attr("id");
568 enhanceDeletePostButton("#sone .post#" + postId + " > .inner-part > .status-line .delete button", postId, deletePostText);
570 $("#sone .post#" + postId + " .reply").each(function() {
571 replyId = $(this).attr("id");
572 (function(postId, reply, replyId) {
573 reply.find(".delete button").each(function() {
574 enhanceDeleteReplyButton("#sone .post#" + postId + " .reply#" + replyId + " .delete button", replyId, deleteReplyText);
576 })(postId, $(this), replyId);
583 /* hides all replies but the latest two. */
584 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
585 $("#sone .post .replies").each(function() {
586 allReplies = $(this).find(".reply");
587 if (allReplies.length > 2) {
589 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
590 $(allReplies[replyIndex]).addClass("hidden");
591 newHidden |= $(allReplies[replyIndex]).hasClass("new");
593 clickToShowElement = $("<div></div>").addClass("click-to-show");
595 clickToShowElement.addClass("new");
597 (function(clickToShowElement, allReplies, text) {
598 clickToShowElement.text(text);
599 clickToShowElement.click(function() {
600 allReplies.removeClass("hidden");
601 clickToShowElement.addClass("hidden");
603 })(clickToShowElement, allReplies, text);
604 $(allReplies[0]).before(clickToShowElement);
610 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
613 $("#sone .follow").submit(function() {
614 var followElement = this;
615 $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
616 $(followElement).addClass("hidden");
617 $(followElement).parent().find(".unfollow").removeClass("hidden");
621 $("#sone .unfollow").submit(function() {
622 var unfollowElement = this;
623 $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
624 $(unfollowElement).addClass("hidden");
625 $(unfollowElement).parent().find(".follow").removeClass("hidden");
629 $("#sone .lock").submit(function() {
630 var lockElement = this;
631 $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
632 $(lockElement).addClass("hidden");
633 $(lockElement).parent().find(".unlock").removeClass("hidden");
637 $("#sone .unlock").submit(function() {
638 var unlockElement = this;
639 $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
640 $(unlockElement).addClass("hidden");
641 $(unlockElement).parent().find(".lock").removeClass("hidden");
646 /* convert all “like” buttons to javascript functions. */
647 $("#sone .post > .inner-part > .status-line .like").submit(function() {
648 likePost(getPostId(this));
651 $("#sone .post > .inner-part > .status-line .unlike").submit(function() {
652 unlikePost(getPostId(this));
655 $("#sone .post .reply .status-line .like").submit(function() {
656 likeReply(getReplyId(this));
659 $("#sone .post .reply .status-line .unlike").submit(function() {
660 unlikeReply(getReplyId(this));
664 /* process all existing notifications, ajaxify dismiss buttons. */
665 $("#sone #notification-area .notification").each(function() {
666 ajaxifyNotification($(this));
669 /* activate status polling. */
670 setTimeout(getStatus, 5000);