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, insertAfterThisElement) {
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 markPostAsKnown(getPostElement(this));
73 replyElement = $("#sone .post#" + postId + " .create-reply");
74 replyElement.removeClass("hidden");
75 replyElement.removeClass("light");
76 (function(replyElement) {
77 replyElement.find("input.reply-input").blur(function() {
78 if ($(this).hasClass("default")) {
79 replyElement.addClass("light");
82 replyElement.removeClass("light");
85 replyElement.find("input.reply-input").focus();
87 return commentElement;
89 $(insertAfterThisElement).after(commentElement.clone(true));
92 var translations = {};
95 * Retrieves the translation for the given key and calls the callback function.
96 * The callback function takes a single parameter, the translated string.
99 * The key of the translation string
101 * The callback function
103 function getTranslation(key, callback) {
104 if (key in translations) {
105 callback(translations[key]);
108 $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
109 if ((data != null) && data.success) {
110 translations[key] = data.value;
111 callback(data.value);
113 }, function(xmlHttpRequest, textStatus, error) {
119 * Filters the given Sone ID, replacing all “~” characters by an underscore.
122 * The Sone ID to filter
123 * @returns The filtered Sone ID
125 function filterSoneId(soneId) {
126 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
130 * Updates the status of the given Sone.
133 * The ID of the Sone to update
135 * The status of the Sone (“idle”, “unknown”, “inserting”,
138 * Whether the Sone is modified
140 * Whether the Sone is locked
142 * The date and time of the last update (formatted for display)
144 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
145 $("#sone .sone." + filterSoneId(soneId)).
146 toggleClass("unknown", status == "unknown").
147 toggleClass("idle", status == "idle").
148 toggleClass("inserting", status == "inserting").
149 toggleClass("downloading", status == "downloading").
150 toggleClass("modified", modified);
151 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
152 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
153 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
154 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
158 * Enhances a “delete” button so that the confirmation is done on the same page.
163 * The text to show on the button
164 * @param deleteCallback
165 * The callback that actually deletes something
167 function enhanceDeleteButton(button, text, deleteCallback) {
169 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
170 $(this).fadeOut("slow");
173 }).insertAfter(button);
174 (function(button, newButton) {
175 button.click(function() {
176 button.fadeOut("slow", function() {
177 newButton.fadeIn("slow");
178 $(document).one("click", function() {
179 if (this != newButton.get(0)) {
180 newButton.fadeOut(function() {
188 })(button, newButton);
193 * Enhances a post’s “delete” button.
198 * The ID of the post to delete
200 * The text to replace the button with
202 function enhanceDeletePostButton(button, postId, text) {
203 enhanceDeleteButton(button, text, function() {
204 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
209 $("#sone .post#" + postId).slideUp();
210 } else if (data.error == "invalid-post-id") {
211 alert("Invalid post ID given!");
212 } else if (data.error == "auth-required") {
213 alert("You need to be logged in.");
214 } else if (data.error == "not-authorized") {
215 alert("You are not allowed to delete this post.");
217 }, function(xmlHttpRequest, textStatus, error) {
224 * Enhances a reply’s “delete” button.
229 * The ID of the reply to delete
231 * The text to replace the button with
233 function enhanceDeleteReplyButton(button, replyId, text) {
234 enhanceDeleteButton(button, text, function() {
235 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
240 $("#sone .reply#" + replyId).slideUp();
241 } else if (data.error == "invalid-reply-id") {
242 alert("Invalid reply ID given!");
243 } else if (data.error == "auth-required") {
244 alert("You need to be logged in.");
245 } else if (data.error == "not-authorized") {
246 alert("You are not allowed to delete this reply.");
248 }, function(xmlHttpRequest, textStatus, error) {
254 function getFormPassword() {
255 return $("#sone #formPassword").text();
258 function getSoneElement(element) {
259 return $(element).parents(".sone");
263 * Generates a list of Sones by concatening the names of the given sones with a
264 * new line character (“\n”).
267 * The sones to format
268 * @returns {String} The created string
270 function generateSoneList(sones) {
272 $.each(sones, function() {
273 if (soneList != "") {
276 soneList += this.name;
282 * Returns the ID of the Sone that this element belongs to.
285 * The element to locate the matching Sone ID for
286 * @returns The ID of the Sone, or undefined
288 function getSoneId(element) {
289 return getSoneElement(element).find(".id").text();
292 function getPostElement(element) {
293 return $(element).closest(".post");
296 function getPostId(element) {
297 return getPostElement(element).attr("id");
300 function getPostTime(element) {
301 return getPostElement(element).find(".post-time").text();
304 function getReplyElement(element) {
305 return $(element).closest(".reply");
308 function getReplyId(element) {
309 return getReplyElement(element).attr("id");
312 function getReplyTime(element) {
313 return getReplyElement(element).find(".reply-time").text();
316 function likePost(postId) {
317 $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
318 if ((data == null) || !data.success) {
321 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
322 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
323 updatePostLikes(postId);
324 }, function(xmlHttpRequest, textStatus, error) {
329 function unlikePost(postId) {
330 $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
331 if ((data == null) || !data.success) {
334 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
335 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
336 updatePostLikes(postId);
337 }, function(xmlHttpRequest, textStatus, error) {
342 function updatePostLikes(postId) {
343 $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
344 if ((data != null) && data.success) {
345 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
346 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
347 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
349 }, function(xmlHttpRequest, textStatus, error) {
354 function likeReply(replyId) {
355 $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
356 if ((data == null) || !data.success) {
359 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
360 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
361 updateReplyLikes(replyId);
362 }, function(xmlHttpRequest, textStatus, error) {
367 function unlikeReply(replyId) {
368 $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
369 if ((data == null) || !data.success) {
372 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
373 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
374 updateReplyLikes(replyId);
375 }, function(xmlHttpRequest, textStatus, error) {
380 function updateReplyLikes(replyId) {
381 $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
382 if ((data != null) && data.success) {
383 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
384 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
385 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
387 }, function(xmlHttpRequest, textStatus, error) {
393 * Posts a reply and calls the given callback when the request finishes.
396 * The ID of the post the reply refers to
399 * @param callbackFunction
400 * The callback function to call when the request finishes (takes 3
401 * parameters: success, error, replyId)
403 function postReply(postId, text, callbackFunction) {
404 $.getJSON("ajax/createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
406 /* TODO - show error */
410 callbackFunction(true, null, data.reply);
412 callbackFunction(false, data.error);
414 }, function(xmlHttpRequest, textStatus, error) {
420 * Requests information about the reply with the given ID.
423 * The ID of the reply
424 * @param callbackFunction
425 * A callback function (parameters soneId, soneName, replyTime,
426 * replyDisplayTime, text, html)
428 function getReply(replyId, callbackFunction) {
429 $.getJSON("ajax/getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
430 if ((data != null) && data.success) {
431 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
433 }, function(xmlHttpRequest, textStatus, error) {
439 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
442 * The post element to ajaxify
444 function ajaxifyPost(postElement) {
445 $(postElement).find("form").submit(function() {
448 $(postElement).find(".create-reply button:submit").click(function() {
449 inputField = $(this.form).find(":input:enabled").get(0);
450 postId = getPostId(this);
451 text = $(inputField).val();
452 $(inputField).val("");
453 postReply(postId, text, function(success, error, replyId) {
455 loadNewReply(replyId);
456 markPostAsKnown(getPostElement(inputField));
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));
476 markPostAsKnown(getPostElement(this));
479 $(postElement).find(".unlike-post").submit(function() {
480 unlikePost(getPostId(this));
481 markPostAsKnown(getPostElement(this));
485 /* add “comment” link. */
486 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
488 /* process all replies. */
489 $(postElement).find(".reply").each(function() {
493 /* process reply input fields. */
494 getTranslation("WebInterface.DefaultText.Reply", function(text) {
495 $(postElement).find("input.reply-input").each(function() {
496 registerInputTextareaSwap(this, text, "text", false, false);
500 /* mark everything as known on click. */
501 $(postElement).click(function() {
502 markPostAsKnown(this);
505 /* hide reply input field. */
506 $(postElement).find(".create-reply").addClass("hidden");
510 * Ajaxifies the given reply element.
512 * @param replyElement
513 * The reply element to ajaxify
515 function ajaxifyReply(replyElement) {
516 $(replyElement).find(".like-reply").submit(function() {
517 likeReply(getReplyId(this));
518 markPostAsKnown(getPostElement(this));
521 $(replyElement).find(".unlike-reply").submit(function() {
522 unlikeReply(getReplyId(this));
523 markPostAsKnown(getPostElement(this));
526 (function(replyElement) {
527 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
528 $(replyElement).find(".delete-reply button").each(function() {
529 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
533 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
535 /* mark post and all replies as known on click. */
536 $(replyElement).click(function() {
537 markPostAsKnown(getPostElement(replyElement));
542 * Ajaxifies the given notification by replacing the form with AJAX.
544 * @param notification
545 * jQuery object representing the notification.
547 function ajaxifyNotification(notification) {
548 notification.find("form.dismiss").submit(function() {
551 notification.find("form.dismiss button").click(function() {
552 $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
553 /* dismiss in case of error, too. */
554 notification.slideUp();
555 }, function(xmlHttpRequest, textStatus, error) {
562 function getStatus() {
563 $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) {
564 if ((data != null) && data.success) {
565 /* process Sone information. */
566 $.each(data.sones, function(index, value) {
567 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
569 /* process notifications. */
570 $.each(data.notifications, function(index, value) {
571 oldNotification = $("#sone #notification-area .notification#" + value.id);
572 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
573 if (oldNotification.length != 0) {
574 oldNotification.replaceWith(notification.show());
576 $("#sone #notification-area").append(notification);
577 notification.slideDown();
581 $.each(data.removedNotifications, function(index, value) {
582 $("#sone #notification-area .notification#" + value.id).slideUp();
584 /* process new posts. */
585 $.each(data.newPosts, function(index, value) {
588 /* process new replies. */
589 $.each(data.newReplies, function(index, value) {
592 /* do it again in 5 seconds. */
593 setTimeout(getStatus, 5000);
595 /* data.success was false, wait 30 seconds. */
596 setTimeout(getStatus, 30000);
598 }, function(xmlHttpRequest, textStatus, error) {
599 /* something really bad happend, wait a minute. */
600 setTimeout(getStatus, 60000);
605 * Returns the content of the page-id attribute.
607 * @returns The page ID
609 function getPageId() {
610 return $("#sone .page-id").text();
614 * Returns whether the current page is the index page.
616 * @returns {Boolean} <code>true</code> if the current page is the index page,
617 * <code>false</code> otherwise
619 function isIndexPage() {
620 return getPageId() == "index";
624 * Returns whether the current page is a “view Sone” page.
626 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
627 * page, <code>false</code> otherwise
629 function isViewSonePage() {
630 return getPageId() == "view-sone";
634 * Returns the ID of the currently shown Sone. This will only return a sensible
635 * value if isViewSonePage() returns <code>true</code>.
637 * @returns The ID of the currently shown Sone
639 function getSoneId() {
640 return $("#sone .sone-id").text();
643 var loadedPosts = {};
644 var loadedReplies = {};
646 function loadNewPost(postId) {
647 if (postId in loadedPosts) {
650 loadedPosts[postId] = true;
651 $.getJSON("ajax/getPost.ajax", { "post" : postId }, function(data, textStatus) {
652 if ((data != null) && data.success) {
653 if (!isIndexPage() && !(isViewSonePage() && (getSoneId() == data.post.sone))) {
656 var firstOlderPost = null;
657 $("#sone .post").each(function() {
658 if (getPostTime(this) < data.post.time) {
659 firstOlderPost = $(this);
663 newPost = $(data.post.html).addClass("hidden");
664 if (firstOlderPost != null) {
665 newPost.insertBefore(firstOlderPost);
667 $("#sone .post:last").append(newPost);
669 ajaxifyPost(newPost);
676 function loadNewReply(replyId) {
677 if (replyId in loadedReplies) {
680 loadedReplies[replyId] = true;
681 $.getJSON("ajax/getReply.ajax", { "reply": replyId }, function(data, textStatus) {
683 if ((data != null) && data.success) {
684 $("#sone .post#" + data.reply.postId).each(function() {
685 var firstNewerReply = null;
686 $(this).find(".replies .reply").each(function() {
687 if (getReplyTime(this) > data.reply.time) {
688 firstNewerReply = $(this);
692 newReply = $(data.reply.html).addClass("hidden");
693 if (firstNewerReply != null) {
694 newReply.insertBefore(firstNewerReply);
696 if ($(this).find(".replies .create-reply")) {
697 $(this).find(".replies .create-reply").before(newReply);
699 $(this).find(".replies").append(newReply);
702 ajaxifyReply(newReply);
703 newReply.slideDown();
710 function markPostAsKnown(postElements) {
711 $(postElements).each(function() {
713 if ($(postElement).hasClass("new")) {
714 (function(postElement) {
715 $.getJSON("ajax/markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) {
716 $(postElement).removeClass("new");
721 markReplyAsKnown($(postElements).find(".reply"));
724 function markReplyAsKnown(replyElements) {
725 $(replyElements).each(function() {
727 if ($(replyElement).hasClass("new")) {
728 (function(replyElement) {
729 $.getJSON("ajax/markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function(data, textStatus) {
730 $(replyElement).removeClass("new");
737 function resetActivity() {
738 title = document.title;
739 if (title.indexOf('(') == 0) {
740 document.title = title.substr(title.indexOf(' ') + 1);
744 function setActivity() {
746 title = document.title;
747 if (title.indexOf('(') != 0) {
748 document.title = "(!) " + title;
754 * Creates a new notification.
757 * The ID of the notificaiton
759 * The text of the notification
761 * <code>true</code> if the notification can be dismissed by the
764 function createNotification(id, text, dismissable) {
765 notification = $("<div></div>").addClass("notification").attr("id", id);
767 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
768 dismissForm.find("input[name=notification]").val(id);
769 notification.append(dismissForm);
771 notification.append(text);
776 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
781 $(document).ready(function() {
783 /* this initializes the status update input field. */
784 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
785 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
786 $("#sone #update-status").submit(function() {
787 text = $(this).find(":input:enabled").val();
788 $.getJSON("ajax/createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) {
789 if ((data != null) && data.success) {
790 loadNewPost(data.postId);
793 $(this).find(":input:enabled").val("").blur();
798 /* Ajaxifies all posts. */
799 /* calling getTranslation here will cache the necessary values. */
800 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
801 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
802 getTranslation("WebInterface.DefaultText.Reply", function(text) {
803 $("#sone .post").each(function() {
810 /* hides all replies but the latest two. */
811 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
812 $("#sone .post .replies").each(function() {
813 allReplies = $(this).find(".reply");
814 if (allReplies.length > 2) {
816 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
817 $(allReplies[replyIndex]).addClass("hidden");
818 newHidden |= $(allReplies[replyIndex]).hasClass("new");
820 clickToShowElement = $("<div></div>").addClass("click-to-show");
822 clickToShowElement.addClass("new");
824 (function(clickToShowElement, allReplies, text) {
825 clickToShowElement.text(text);
826 clickToShowElement.click(function() {
827 allReplies.removeClass("hidden");
828 clickToShowElement.addClass("hidden");
830 })(clickToShowElement, allReplies, text);
831 $(allReplies[0]).before(clickToShowElement);
837 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
840 $("#sone .follow").submit(function() {
841 var followElement = this;
842 $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
843 $(followElement).addClass("hidden");
844 $(followElement).parent().find(".unfollow").removeClass("hidden");
848 $("#sone .unfollow").submit(function() {
849 var unfollowElement = this;
850 $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
851 $(unfollowElement).addClass("hidden");
852 $(unfollowElement).parent().find(".follow").removeClass("hidden");
856 $("#sone .lock").submit(function() {
857 var lockElement = this;
858 $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
859 $(lockElement).addClass("hidden");
860 $(lockElement).parent().find(".unlock").removeClass("hidden");
864 $("#sone .unlock").submit(function() {
865 var unlockElement = this;
866 $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
867 $(unlockElement).addClass("hidden");
868 $(unlockElement).parent().find(".lock").removeClass("hidden");
873 /* process all existing notifications, ajaxify dismiss buttons. */
874 $("#sone #notification-area .notification").each(function() {
875 ajaxifyNotification($(this));
878 /* activate status polling. */
879 setTimeout(getStatus, 5000);
881 /* reset activity counter when the page has focus. */
882 $(window).focus(function() {