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));
102 var translations = {};
105 * Retrieves the translation for the given key and calls the callback function.
106 * The callback function takes a single parameter, the translated string.
109 * The key of the translation string
111 * The callback function
113 function getTranslation(key, callback) {
114 if (key in translations) {
115 callback(translations[key]);
118 $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
119 if ((data != null) && data.success) {
120 translations[key] = data.value;
121 callback(data.value);
123 }, function(xmlHttpRequest, textStatus, error) {
129 * Filters the given Sone ID, replacing all “~” characters by an underscore.
132 * The Sone ID to filter
133 * @returns The filtered Sone ID
135 function filterSoneId(soneId) {
136 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
140 * Updates the status of the given Sone.
143 * The ID of the Sone to update
145 * The status of the Sone (“idle”, “unknown”, “inserting”,
148 * Whether the Sone is modified
150 * Whether the Sone is locked
152 * The date and time of the last update (formatted for display)
154 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
155 $("#sone .sone." + filterSoneId(soneId)).
156 toggleClass("unknown", status == "unknown").
157 toggleClass("idle", status == "idle").
158 toggleClass("inserting", status == "inserting").
159 toggleClass("downloading", status == "downloading").
160 toggleClass("modified", modified);
161 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
162 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
163 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
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.
171 * The selector of the button
173 * The text to show on the button
174 * @param deleteCallback
175 * The callback that actually deletes something
177 function enhanceDeleteButton(buttonId, text, deleteCallback) {
178 button = $(buttonId);
180 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
181 $(this).fadeOut("slow");
184 }).insertAfter(button);
185 (function(button, newButton) {
186 button.click(function() {
187 button.fadeOut("slow", function() {
188 newButton.fadeIn("slow");
189 $(document).one("click", function() {
190 if (this != newButton.get(0)) {
191 newButton.fadeOut(function() {
199 })(button, newButton);
204 * Enhances a post’s “delete” button.
207 * The selector of the button
209 * The ID of the post to delete
211 * The text to replace the button with
213 function enhanceDeletePostButton(buttonId, postId, text) {
214 enhanceDeleteButton(buttonId, text, function() {
215 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
220 $("#sone .post#" + postId).slideUp();
221 } else if (data.error == "invalid-post-id") {
222 alert("Invalid post ID given!");
223 } else if (data.error == "auth-required") {
224 alert("You need to be logged in.");
225 } else if (data.error == "not-authorized") {
226 alert("You are not allowed to delete this post.");
228 }, function(xmlHttpRequest, textStatus, error) {
235 * Enhances a reply’s “delete” button.
238 * The selector of the button
240 * The ID of the reply to delete
242 * The text to replace the button with
244 function enhanceDeleteReplyButton(buttonId, replyId, text) {
245 enhanceDeleteButton(buttonId, text, function() {
246 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
251 $("#sone .reply#" + replyId).slideUp();
252 } else if (data.error == "invalid-reply-id") {
253 alert("Invalid reply ID given!");
254 } else if (data.error == "auth-required") {
255 alert("You need to be logged in.");
256 } else if (data.error == "not-authorized") {
257 alert("You are not allowed to delete this reply.");
259 }, function(xmlHttpRequest, textStatus, error) {
265 function getFormPassword() {
266 return $("#sone #formPassword").text();
269 function getSoneElement(element) {
270 return $(element).parents(".sone");
274 * Generates a list of Sones by concatening the names of the given sones with a
275 * new line character (“\n”).
278 * The sones to format
279 * @returns {String} The created string
281 function generateSoneList(sones) {
283 $.each(sones, function() {
284 if (soneList != "") {
287 soneList += this.name;
293 * Returns the ID of the Sone that this element belongs to.
296 * The element to locate the matching Sone ID for
297 * @returns The ID of the Sone, or undefined
299 function getSoneId(element) {
300 return getSoneElement(element).find(".id").text();
303 function getPostElement(element) {
304 return $(element).hasClass("post") ? $(element) : $(element).parents(".post");
307 function getPostId(element) {
308 return getPostElement(element).attr("id");
311 function getPostTime(element) {
312 return getPostElement(element).find(".storage-time").text();
315 function getReplyElement(element) {
316 return $(element).parents(".reply");
319 function getReplyId(element) {
320 return getReplyElement(element).attr("id");
323 function getReplyTime(element) {
324 return getReplyElement(element).find(".storage-time").text();
327 function likePost(postId) {
328 $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
329 if ((data == null) || !data.success) {
332 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
333 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
334 updatePostLikes(postId);
335 }, function(xmlHttpRequest, textStatus, error) {
340 function unlikePost(postId) {
341 $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
342 if ((data == null) || !data.success) {
345 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
346 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
347 updatePostLikes(postId);
348 }, function(xmlHttpRequest, textStatus, error) {
353 function updatePostLikes(postId) {
354 $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
355 if ((data != null) && data.success) {
356 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
357 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
358 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
360 }, function(xmlHttpRequest, textStatus, error) {
365 function likeReply(replyId) {
366 $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
367 if ((data == null) || !data.success) {
370 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
371 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
372 updateReplyLikes(replyId);
373 }, function(xmlHttpRequest, textStatus, error) {
378 function unlikeReply(replyId) {
379 $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
380 if ((data == null) || !data.success) {
383 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
384 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
385 updateReplyLikes(replyId);
386 }, function(xmlHttpRequest, textStatus, error) {
391 function updateReplyLikes(replyId) {
392 $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
393 if ((data != null) && data.success) {
394 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
395 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
396 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
398 }, function(xmlHttpRequest, textStatus, error) {
404 * Posts a reply and calls the given callback when the request finishes.
407 * The ID of the post the reply refers to
410 * @param callbackFunction
411 * The callback function to call when the request finishes (takes 3
412 * parameters: success, error, replyId)
414 function postReply(postId, text, callbackFunction) {
415 $.getJSON("ajax/createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
417 /* TODO - show error */
421 callbackFunction(true, null, data.reply);
423 callbackFunction(false, data.error);
425 }, function(xmlHttpRequest, textStatus, error) {
431 * Requests information about the reply with the given ID.
434 * The ID of the reply
435 * @param callbackFunction
436 * A callback function (parameters soneId, soneName, replyTime,
437 * replyDisplayTime, text, html)
439 function getReply(replyId, callbackFunction) {
440 $.getJSON("ajax/getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
441 if ((data != null) && data.success) {
442 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
444 }, function(xmlHttpRequest, textStatus, error) {
450 * Ajaxifies the given notification by replacing the form with AJAX.
452 * @param notification
453 * jQuery object representing the notification.
455 function ajaxifyNotification(notification) {
456 notification.find("form.dismiss").submit(function() {
459 notification.find("form.dismiss button").click(function() {
460 $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
461 /* dismiss in case of error, too. */
462 notification.slideUp();
463 }, function(xmlHttpRequest, textStatus, error) {
470 function getStatus() {
471 $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) {
472 if ((data != null) && data.success) {
473 /* process Sone information. */
474 $.each(data.sones, function(index, value) {
475 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
477 /* process notifications. */
478 $.each(data.notifications, function(index, value) {
479 oldNotification = $("#sone #notification-area .notification#" + value.id);
480 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
481 if (oldNotification.length != 0) {
482 oldNotification.replaceWith(notification.show());
484 $("#sone #notification-area").append(notification);
485 notification.slideDown();
488 $.each(data.removedNotifications, function(index, value) {
489 $("#sone #notification-area .notification#" + value.id).slideUp();
491 /* do it again in 5 seconds. */
492 setTimeout(getStatus, 5000);
494 /* data.success was false, wait 30 seconds. */
495 setTimeout(getStatus, 30000);
497 }, function(xmlHttpRequest, textStatus, error) {
498 /* something really bad happend, wait a minute. */
499 setTimeout(getStatus, 60000);
504 * Creates a new notification.
507 * The ID of the notificaiton
509 * The text of the notification
511 * <code>true</code> if the notification can be dismissed by the
514 function createNotification(id, text, dismissable) {
515 notification = $("<div></div>").addClass("notification").attr("id", id);
517 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
518 dismissForm.find("input[name=notification]").val(id);
519 notification.append(dismissForm);
521 notification.append(text);
526 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
529 $(document).ready(function() {
531 /* this initializes the status update input field. */
532 getTranslation("WebInterface.DefaultText.StatusUpdate", function(text) {
533 registerInputTextareaSwap("#sone #update-status .status-input", text, "text", false, false);
536 /* this initializes all reply input fields. */
537 getTranslation("WebInterface.DefaultText.Reply", function(text) {
538 registerInputTextareaSwap("#sone input.reply-input", text, "text", false, false);
542 /* replaces all “post reply!” forms with AJAX. */
543 $("#sone .create-reply button:submit").click(function() {
544 $(this.form).submit(function() {
547 inputField = $(this.form).find(":input:enabled").get(0);
548 postId = getPostId($(inputField));
549 text = $(inputField).val();
550 $(inputField).val("");
551 postReply(postId, text, function(success, error, replyId) {
553 getReply(replyId, function(soneId, soneName, replyTime, replyDisplayTime, text, html) {
554 newReply = $(html).insertBefore("#sone .post#" + postId + " .create-reply");
555 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
556 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
557 enhanceDeleteReplyButton("#sone .post#" + postId + " .reply#" + replyId + " .delete button", replyId, deleteReplyText);
559 newReply.find(".status-line .like").submit(function() {
560 likeReply(getReplyId(this));
563 newReply.find(".status-line .unlike").submit(function() {
564 unlikeReply(getReplyId(this));
567 addCommentLink(postId, newReply);
576 /* replace all “delete” buttons with javascript. */
577 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
578 deletePostText = text;
579 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
580 deleteReplyText = text;
581 $("#sone .post").each(function() {
582 postId = $(this).attr("id");
583 enhanceDeletePostButton("#sone .post#" + postId + " > .inner-part > .status-line .delete button", postId, deletePostText);
585 $("#sone .post#" + postId + " .reply").each(function() {
586 replyId = $(this).attr("id");
587 (function(postId, reply, replyId) {
588 reply.find(".delete button").each(function() {
589 enhanceDeleteReplyButton("#sone .post#" + postId + " .reply#" + replyId + " .delete button", replyId, deleteReplyText);
591 })(postId, $(this), replyId);
598 /* hides all replies but the latest two. */
599 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
600 $("#sone .post .replies").each(function() {
601 allReplies = $(this).find(".reply");
602 if (allReplies.length > 2) {
604 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
605 $(allReplies[replyIndex]).addClass("hidden");
606 newHidden |= $(allReplies[replyIndex]).hasClass("new");
608 clickToShowElement = $("<div></div>").addClass("click-to-show");
610 clickToShowElement.addClass("new");
612 (function(clickToShowElement, allReplies, text) {
613 clickToShowElement.text(text);
614 clickToShowElement.click(function() {
615 allReplies.removeClass("hidden");
616 clickToShowElement.addClass("hidden");
618 })(clickToShowElement, allReplies, text);
619 $(allReplies[0]).before(clickToShowElement);
625 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
628 $("#sone .follow").submit(function() {
629 var followElement = this;
630 $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
631 $(followElement).addClass("hidden");
632 $(followElement).parent().find(".unfollow").removeClass("hidden");
636 $("#sone .unfollow").submit(function() {
637 var unfollowElement = this;
638 $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
639 $(unfollowElement).addClass("hidden");
640 $(unfollowElement).parent().find(".follow").removeClass("hidden");
644 $("#sone .lock").submit(function() {
645 var lockElement = this;
646 $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
647 $(lockElement).addClass("hidden");
648 $(lockElement).parent().find(".unlock").removeClass("hidden");
652 $("#sone .unlock").submit(function() {
653 var unlockElement = this;
654 $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
655 $(unlockElement).addClass("hidden");
656 $(unlockElement).parent().find(".lock").removeClass("hidden");
661 /* convert all “like” buttons to javascript functions. */
662 $("#sone .post > .inner-part > .status-line .like").submit(function() {
663 likePost(getPostId(this));
666 $("#sone .post > .inner-part > .status-line .unlike").submit(function() {
667 unlikePost(getPostId(this));
670 $("#sone .post .reply .status-line .like").submit(function() {
671 likeReply(getReplyId(this));
674 $("#sone .post .reply .status-line .unlike").submit(function() {
675 unlikeReply(getReplyId(this));
679 /* process all existing notifications, ajaxify dismiss buttons. */
680 $("#sone #notification-area .notification").each(function() {
681 ajaxifyNotification($(this));
684 /* activate status polling. */
685 setTimeout(getStatus, 5000);