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).parents(".post");
307 function getPostId(element) {
308 return getPostElement(element).attr("id");
311 function getReplyElement(element) {
312 return $(element).parents(".reply");
315 function getReplyId(element) {
316 return getReplyElement(element).attr("id");
319 function likePost(postId) {
320 $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
321 if ((data == null) || !data.success) {
324 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
325 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
326 updatePostLikes(postId);
327 }, function(xmlHttpRequest, textStatus, error) {
332 function unlikePost(postId) {
333 $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
334 if ((data == null) || !data.success) {
337 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
338 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
339 updatePostLikes(postId);
340 }, function(xmlHttpRequest, textStatus, error) {
345 function updatePostLikes(postId) {
346 $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
347 if ((data != null) && data.success) {
348 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
349 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
350 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
352 }, function(xmlHttpRequest, textStatus, error) {
357 function likeReply(replyId) {
358 $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
359 if ((data == null) || !data.success) {
362 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
363 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
364 updateReplyLikes(replyId);
365 }, function(xmlHttpRequest, textStatus, error) {
370 function unlikeReply(replyId) {
371 $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
372 if ((data == null) || !data.success) {
375 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
376 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
377 updateReplyLikes(replyId);
378 }, function(xmlHttpRequest, textStatus, error) {
383 function updateReplyLikes(replyId) {
384 $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
385 if ((data != null) && data.success) {
386 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
387 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
388 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
390 }, function(xmlHttpRequest, textStatus, error) {
396 * Posts a reply and calls the given callback when the request finishes.
399 * The ID of the post the reply refers to
402 * @param callbackFunction
403 * The callback function to call when the request finishes (takes 3
404 * parameters: success, error, replyId)
406 function postReply(postId, text, callbackFunction) {
407 $.getJSON("ajax/createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
409 /* TODO - show error */
413 callbackFunction(true, null, data.reply);
415 callbackFunction(false, data.error);
417 }, function(xmlHttpRequest, textStatus, error) {
423 * Requests information about the reply with the given ID.
426 * The ID of the reply
427 * @param callbackFunction
428 * A callback function (parameters soneId, soneName, replyTime,
429 * replyDisplayTime, text, html)
431 function getReply(replyId, callbackFunction) {
432 $.getJSON("ajax/getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
433 if ((data != null) && data.success) {
434 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
436 }, function(xmlHttpRequest, textStatus, error) {
442 * Ajaxifies the given notification by replacing the form with AJAX.
444 * @param notification
445 * jQuery object representing the notification.
447 function ajaxifyNotification(notification) {
448 notification.find("form.dismiss").submit(function() {
451 notification.find("form.dismiss button").click(function() {
452 $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
453 /* dismiss in case of error, too. */
454 notification.slideUp();
455 }, function(xmlHttpRequest, textStatus, error) {
462 function getStatus() {
463 $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) {
464 if ((data != null) && data.success) {
465 /* process Sone information. */
466 $.each(data.sones, function(index, value) {
467 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
469 /* process notifications. */
470 $.each(data.notifications, function(index, value) {
471 oldNotification = $("#sone #notification-area .notification#" + value.id);
472 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
473 if (oldNotification.length != 0) {
474 oldNotification.replaceWith(notification.show());
476 $("#sone #notification-area").append(notification);
477 notification.slideDown();
480 $.each(data.removedNotifications, function(index, value) {
481 $("#sone #notification-area .notification#" + value.id).slideUp();
483 /* do it again in 5 seconds. */
484 setTimeout(getStatus, 5000);
486 /* data.success was false, wait 30 seconds. */
487 setTimeout(getStatus, 30000);
489 }, function(xmlHttpRequest, textStatus, error) {
490 /* something really bad happend, wait a minute. */
491 setTimeout(getStatus, 60000);
496 * Creates a new notification.
499 * The ID of the notificaiton
501 * The text of the notification
503 * <code>true</code> if the notification can be dismissed by the
506 function createNotification(id, text, dismissable) {
507 notification = $("<div></div>").addClass("notification").attr("id", id);
509 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
510 dismissForm.find("input[name=notification]").val(id);
511 notification.append(dismissForm);
513 notification.append(text);
518 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
521 $(document).ready(function() {
523 /* this initializes the status update input field. */
524 getTranslation("WebInterface.DefaultText.StatusUpdate", function(text) {
525 registerInputTextareaSwap("#sone #update-status .status-input", text, "text", false, false);
528 /* this initializes all reply input fields. */
529 getTranslation("WebInterface.DefaultText.Reply", function(text) {
530 registerInputTextareaSwap("#sone input.reply-input", text, "text", false, false);
534 /* replaces all “post reply!” forms with AJAX. */
535 $("#sone .create-reply button:submit").click(function() {
536 $(this.form).submit(function() {
539 inputField = $(this.form).find(":input:enabled").get(0);
540 postId = getPostId($(inputField));
541 text = $(inputField).val();
542 $(inputField).val("");
543 postReply(postId, text, function(success, error, replyId) {
545 getReply(replyId, function(soneId, soneName, replyTime, replyDisplayTime, text, html) {
546 newReply = $(html).insertBefore("#sone .post#" + postId + " .create-reply");
547 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
548 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
549 enhanceDeleteReplyButton("#sone .post#" + postId + " .reply#" + replyId + " .delete button", replyId, deleteReplyText);
551 newReply.find(".status-line .like").submit(function() {
552 likeReply(getReplyId(this));
555 newReply.find(".status-line .unlike").submit(function() {
556 unlikeReply(getReplyId(this));
559 addCommentLink(postId, newReply);
568 /* replace all “delete” buttons with javascript. */
569 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
570 deletePostText = text;
571 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
572 deleteReplyText = text;
573 $("#sone .post").each(function() {
574 postId = $(this).attr("id");
575 enhanceDeletePostButton("#sone .post#" + postId + " > .inner-part > .status-line .delete button", postId, deletePostText);
577 $("#sone .post#" + postId + " .reply").each(function() {
578 replyId = $(this).attr("id");
579 (function(postId, reply, replyId) {
580 reply.find(".delete button").each(function() {
581 enhanceDeleteReplyButton("#sone .post#" + postId + " .reply#" + replyId + " .delete button", replyId, deleteReplyText);
583 })(postId, $(this), replyId);
590 /* hides all replies but the latest two. */
591 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
592 $("#sone .post .replies").each(function() {
593 allReplies = $(this).find(".reply");
594 if (allReplies.length > 2) {
596 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
597 $(allReplies[replyIndex]).addClass("hidden");
598 newHidden |= $(allReplies[replyIndex]).hasClass("new");
600 clickToShowElement = $("<div></div>").addClass("click-to-show");
602 clickToShowElement.addClass("new");
604 (function(clickToShowElement, allReplies, text) {
605 clickToShowElement.text(text);
606 clickToShowElement.click(function() {
607 allReplies.removeClass("hidden");
608 clickToShowElement.addClass("hidden");
610 })(clickToShowElement, allReplies, text);
611 $(allReplies[0]).before(clickToShowElement);
617 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
620 $("#sone .follow").submit(function() {
621 var followElement = this;
622 $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
623 $(followElement).addClass("hidden");
624 $(followElement).parent().find(".unfollow").removeClass("hidden");
628 $("#sone .unfollow").submit(function() {
629 var unfollowElement = this;
630 $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
631 $(unfollowElement).addClass("hidden");
632 $(unfollowElement).parent().find(".follow").removeClass("hidden");
636 $("#sone .lock").submit(function() {
637 var lockElement = this;
638 $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
639 $(lockElement).addClass("hidden");
640 $(lockElement).parent().find(".unlock").removeClass("hidden");
644 $("#sone .unlock").submit(function() {
645 var unlockElement = this;
646 $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
647 $(unlockElement).addClass("hidden");
648 $(unlockElement).parent().find(".lock").removeClass("hidden");
653 /* convert all “like” buttons to javascript functions. */
654 $("#sone .post > .inner-part > .status-line .like").submit(function() {
655 likePost(getPostId(this));
658 $("#sone .post > .inner-part > .status-line .unlike").submit(function() {
659 unlikePost(getPostId(this));
662 $("#sone .post .reply .status-line .like").submit(function() {
663 likeReply(getReplyId(this));
666 $("#sone .post .reply .status-line .unlike").submit(function() {
667 unlikeReply(getReplyId(this));
671 /* process all existing notifications, ajaxify dismiss buttons. */
672 $("#sone #notification-area .notification").each(function() {
673 ajaxifyNotification($(this));
676 /* activate status polling. */
677 setTimeout(getStatus, 5000);