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 separator = $("<span> · </span>").addClass("separator");
72 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
73 markPostAsKnown(getPostElement(this));
74 replyElement = $("#sone .post#" + postId + " .create-reply");
75 replyElement.removeClass("hidden");
76 replyElement.removeClass("light");
77 (function(replyElement) {
78 replyElement.find("input.reply-input").blur(function() {
79 if ($(this).hasClass("default")) {
80 replyElement.addClass("light");
83 replyElement.removeClass("light");
86 replyElement.find("input.reply-input").focus();
88 return commentElement;
90 $(insertAfterThisElement).after(commentElement.clone(true));
91 $(insertAfterThisElement).after(separator);
94 var translations = {};
97 * Retrieves the translation for the given key and calls the callback function.
98 * The callback function takes a single parameter, the translated string.
101 * The key of the translation string
103 * The callback function
105 function getTranslation(key, callback) {
106 if (key in translations) {
107 callback(translations[key]);
110 $.getJSON("getTranslation.ajax", {"key": key}, function(data, textStatus) {
111 if ((data != null) && data.success) {
112 translations[key] = data.value;
113 callback(data.value);
115 }, function(xmlHttpRequest, textStatus, error) {
121 * Filters the given Sone ID, replacing all “~” characters by an underscore.
124 * The Sone ID to filter
125 * @returns The filtered Sone ID
127 function filterSoneId(soneId) {
128 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
132 * Updates the status of the given Sone.
135 * The ID of the Sone to update
137 * The status of the Sone (“idle”, “unknown”, “inserting”,
140 * Whether the Sone is modified
142 * Whether the Sone is locked
144 * The date and time of the last update (formatted for display)
146 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
147 $("#sone .sone." + filterSoneId(soneId)).
148 toggleClass("unknown", status == "unknown").
149 toggleClass("idle", status == "idle").
150 toggleClass("inserting", status == "inserting").
151 toggleClass("downloading", status == "downloading").
152 toggleClass("modified", modified);
153 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
154 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
155 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
156 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
160 * Enhances a “delete” button so that the confirmation is done on the same page.
165 * The text to show on the button
166 * @param deleteCallback
167 * The callback that actually deletes something
169 function enhanceDeleteButton(button, text, deleteCallback) {
171 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
172 $(this).fadeOut("slow");
175 }).insertAfter(button);
176 (function(button, newButton) {
177 button.click(function() {
178 button.fadeOut("slow", function() {
179 newButton.fadeIn("slow");
180 $(document).one("click", function() {
181 if (this != newButton.get(0)) {
182 newButton.fadeOut(function() {
190 })(button, newButton);
195 * Enhances a post’s “delete” button.
200 * The ID of the post to delete
202 * The text to replace the button with
204 function enhanceDeletePostButton(button, postId, text) {
205 enhanceDeleteButton(button, text, function() {
206 $.getJSON("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
211 $("#sone .post#" + postId).slideUp();
212 } else if (data.error == "invalid-post-id") {
213 alert("Invalid post ID given!");
214 } else if (data.error == "auth-required") {
215 alert("You need to be logged in.");
216 } else if (data.error == "not-authorized") {
217 alert("You are not allowed to delete this post.");
219 }, function(xmlHttpRequest, textStatus, error) {
226 * Enhances a reply’s “delete” button.
231 * The ID of the reply to delete
233 * The text to replace the button with
235 function enhanceDeleteReplyButton(button, replyId, text) {
236 enhanceDeleteButton(button, text, function() {
237 $.getJSON("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
242 $("#sone .reply#" + replyId).slideUp();
243 } else if (data.error == "invalid-reply-id") {
244 alert("Invalid reply ID given!");
245 } else if (data.error == "auth-required") {
246 alert("You need to be logged in.");
247 } else if (data.error == "not-authorized") {
248 alert("You are not allowed to delete this reply.");
250 }, function(xmlHttpRequest, textStatus, error) {
256 function getFormPassword() {
257 return $("#sone #formPassword").text();
260 function getSoneElement(element) {
261 return $(element).closest(".sone");
265 * Generates a list of Sones by concatening the names of the given sones with a
266 * new line character (“\n”).
269 * The sones to format
270 * @returns {String} The created string
272 function generateSoneList(sones) {
274 $.each(sones, function() {
275 if (soneList != "") {
278 soneList += this.name;
284 * Returns the ID of the Sone that this element belongs to.
287 * The element to locate the matching Sone ID for
288 * @returns The ID of the Sone, or undefined
290 function getSoneId(element) {
291 return getSoneElement(element).find(".id").text();
294 function getPostElement(element) {
295 return $(element).closest(".post");
298 function getPostId(element) {
299 return getPostElement(element).attr("id");
302 function getPostTime(element) {
303 return getPostElement(element).find(".post-time").text();
307 * Returns the author of the post the given element belongs to.
310 * The element whose post to get the author for
311 * @returns The ID of the authoring Sone
313 function getPostAuthor(element) {
314 return getPostElement(element).find(".post-author").text();
317 function getReplyElement(element) {
318 return $(element).closest(".reply");
321 function getReplyId(element) {
322 return getReplyElement(element).attr("id");
325 function getReplyTime(element) {
326 return getReplyElement(element).find(".reply-time").text();
330 * Returns the author of the reply the given element belongs to.
333 * The element whose reply to get the author for
334 * @returns The ID of the authoring Sone
336 function getReplyAuthor(element) {
337 return getReplyElement(element).find(".reply-author").text();
340 function likePost(postId) {
341 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
342 if ((data == null) || !data.success) {
345 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
346 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
347 updatePostLikes(postId);
348 }, function(xmlHttpRequest, textStatus, error) {
353 function unlikePost(postId) {
354 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
355 if ((data == null) || !data.success) {
358 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
359 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
360 updatePostLikes(postId);
361 }, function(xmlHttpRequest, textStatus, error) {
366 function updatePostLikes(postId) {
367 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
368 if ((data != null) && data.success) {
369 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
370 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
371 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
373 }, function(xmlHttpRequest, textStatus, error) {
378 function likeReply(replyId) {
379 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
380 if ((data == null) || !data.success) {
383 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
384 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
385 updateReplyLikes(replyId);
386 }, function(xmlHttpRequest, textStatus, error) {
391 function unlikeReply(replyId) {
392 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
393 if ((data == null) || !data.success) {
396 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
397 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
398 updateReplyLikes(replyId);
399 }, function(xmlHttpRequest, textStatus, error) {
405 * Trusts the Sone with the given ID.
408 * The ID of the Sone to trust
410 function trustSone(soneId) {
411 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
412 if ((data != null) && data.success) {
413 updateTrustControls(soneId, data.trustValue);
419 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
422 * The ID of the Sone to distrust
424 function distrustSone(soneId) {
425 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
426 if ((data != null) && data.success) {
427 updateTrustControls(soneId, data.trustValue);
433 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
436 * The ID of the Sone to untrust
438 function untrustSone(soneId) {
439 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
440 if ((data != null) && data.success) {
441 updateTrustControls(soneId, data.trustValue);
447 * Updates the trust controls for all posts and replies of the given Sone,
448 * according to the given trust value.
451 * The ID of the Sone to update all trust controls for
453 * The trust value for the Sone
455 function updateTrustControls(soneId, trustValue) {
456 $("#sone .post").each(function() {
457 if (getPostAuthor(this) == soneId) {
458 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
459 getPostElement(this).find(".post-distrust").toggleClass("hidden", (trustValue != null) && (trustValue < 0));
460 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
463 $("#sone .reply").each(function() {
464 if (getReplyAuthor(this) == soneId) {
465 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
466 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", (trustValue != null) && (trustValue < 0));
467 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
472 function updateReplyLikes(replyId) {
473 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
474 if ((data != null) && data.success) {
475 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
476 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
477 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
479 }, function(xmlHttpRequest, textStatus, error) {
485 * Posts a reply and calls the given callback when the request finishes.
488 * The ID of the post the reply refers to
491 * @param callbackFunction
492 * The callback function to call when the request finishes (takes 3
493 * parameters: success, error, replyId)
495 function postReply(postId, text, callbackFunction) {
496 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
498 /* TODO - show error */
502 callbackFunction(true, null, data.reply);
504 callbackFunction(false, data.error);
506 }, function(xmlHttpRequest, textStatus, error) {
512 * Requests information about the reply with the given ID.
515 * The ID of the reply
516 * @param callbackFunction
517 * A callback function (parameters soneId, soneName, replyTime,
518 * replyDisplayTime, text, html)
520 function getReply(replyId, callbackFunction) {
521 $.getJSON("getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
522 if ((data != null) && data.success) {
523 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
525 }, function(xmlHttpRequest, textStatus, error) {
531 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
534 * The post element to ajaxify
536 function ajaxifyPost(postElement) {
537 $(postElement).find("form").submit(function() {
540 $(postElement).find(".create-reply button:submit").click(function() {
541 inputField = $(this.form).find(":input:enabled").get(0);
542 postId = getPostId(this);
543 text = $(inputField).val();
544 (function(postId, text, inputField) {
545 postReply(postId, text, function(success, error, replyId) {
547 $(inputField).val("");
548 loadNewReply(replyId);
549 markPostAsKnown(getPostElement(inputField));
550 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
555 })(postId, text, inputField);
559 /* replace all “delete” buttons with javascript. */
560 (function(postElement) {
561 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
562 postId = getPostId(postElement);
563 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
567 /* convert all “like” buttons to javascript functions. */
568 $(postElement).find(".like-post").submit(function() {
569 likePost(getPostId(this));
570 markPostAsKnown(getPostElement(this));
573 $(postElement).find(".unlike-post").submit(function() {
574 unlikePost(getPostId(this));
575 markPostAsKnown(getPostElement(this));
579 /* convert trust control buttons to javascript functions. */
580 $(postElement).find(".post-trust").submit(function() {
581 trustSone(getPostAuthor(this));
584 $(postElement).find(".post-distrust").submit(function() {
585 distrustSone(getPostAuthor(this));
588 $(postElement).find(".post-untrust").submit(function() {
589 untrustSone(getPostAuthor(this));
593 /* add “comment” link. */
594 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
596 /* process all replies. */
597 $(postElement).find(".reply").each(function() {
601 /* process reply input fields. */
602 getTranslation("WebInterface.DefaultText.Reply", function(text) {
603 $(postElement).find("input.reply-input").each(function() {
604 registerInputTextareaSwap(this, text, "text", false, false);
608 /* mark everything as known on click. */
609 $(postElement).click(function() {
610 markPostAsKnown(this);
613 /* hide reply input field. */
614 $(postElement).find(".create-reply").addClass("hidden");
618 * Ajaxifies the given reply element.
620 * @param replyElement
621 * The reply element to ajaxify
623 function ajaxifyReply(replyElement) {
624 $(replyElement).find(".like-reply").submit(function() {
625 likeReply(getReplyId(this));
626 markPostAsKnown(getPostElement(this));
629 $(replyElement).find(".unlike-reply").submit(function() {
630 unlikeReply(getReplyId(this));
631 markPostAsKnown(getPostElement(this));
634 (function(replyElement) {
635 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
636 $(replyElement).find(".delete-reply button").each(function() {
637 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
641 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
643 /* convert trust control buttons to javascript functions. */
644 $(replyElement).find(".reply-trust").submit(function() {
645 trustSone(getReplyAuthor(this));
648 $(replyElement).find(".reply-distrust").submit(function() {
649 distrustSone(getReplyAuthor(this));
652 $(replyElement).find(".reply-untrust").submit(function() {
653 untrustSone(getReplyAuthor(this));
657 /* mark post and all replies as known on click. */
658 $(replyElement).click(function() {
659 markPostAsKnown(getPostElement(this));
664 * Ajaxifies the given notification by replacing the form with AJAX.
666 * @param notification
667 * jQuery object representing the notification.
669 function ajaxifyNotification(notification) {
670 notification.find("form.dismiss").submit(function() {
673 notification.find("form.dismiss button").click(function() {
674 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
675 /* dismiss in case of error, too. */
676 notification.slideUp();
677 }, function(xmlHttpRequest, textStatus, error) {
684 function getStatus() {
685 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
686 if ((data != null) && data.success) {
687 /* process Sone information. */
688 $.each(data.sones, function(index, value) {
689 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
691 /* process notifications. */
692 $.each(data.notifications, function(index, value) {
693 oldNotification = $("#sone #notification-area .notification#" + value.id);
694 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
695 if (oldNotification.length != 0) {
696 oldNotification.replaceWith(notification.show());
698 $("#sone #notification-area").append(notification);
699 notification.slideDown();
703 $.each(data.removedNotifications, function(index, value) {
704 $("#sone #notification-area .notification#" + value.id).slideUp();
706 /* process new posts. */
707 $.each(data.newPosts, function(index, value) {
710 /* process new replies. */
711 $.each(data.newReplies, function(index, value) {
714 /* do it again in 5 seconds. */
715 setTimeout(getStatus, 5000);
717 /* data.success was false, wait 30 seconds. */
718 setTimeout(getStatus, 30000);
720 }, function(xmlHttpRequest, textStatus, error) {
721 /* something really bad happend, wait a minute. */
722 setTimeout(getStatus, 60000);
727 * Returns the content of the page-id attribute.
729 * @returns The page ID
731 function getPageId() {
732 return $("#sone .page-id").text();
736 * Returns whether the current page is the index page.
738 * @returns {Boolean} <code>true</code> if the current page is the index page,
739 * <code>false</code> otherwise
741 function isIndexPage() {
742 return getPageId() == "index";
746 * Returns whether the current page is a “view Sone” page.
748 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
749 * page, <code>false</code> otherwise
751 function isViewSonePage() {
752 return getPageId() == "view-sone";
756 * Returns the ID of the currently shown Sone. This will only return a sensible
757 * value if isViewSonePage() returns <code>true</code>.
759 * @returns The ID of the currently shown Sone
761 function getShownSoneId() {
762 return $("#sone .sone-id").text();
766 * Returns whether the current page is a “view post” page.
768 * @returns {Boolean} <code>true</code> if the current page is a “view post”
769 * page, <code>false</code> otherwise
771 function isViewPostPage() {
772 return getPageId() == "view-post";
776 * Returns the ID of the currently shown post. This will only return a sensible
777 * value if isViewPostPage() returns <code>true</code>.
779 * @returns The ID of the currently shown post
781 function getShownPostId() {
782 return $("#sone .post-id").text();
786 * Returns whether the current page is the “known Sones” page.
788 * @returns {Boolean} <code>true</code> if the current page is the “known
789 * Sones” page, <code>false</code> otherwise
791 function isKnownSonesPage() {
792 return getPageId() == "known-sones";
796 * Returns whether a post with the given ID exists on the current page.
799 * The post ID to check for
800 * @returns {Boolean} <code>true</code> if a post with the given ID already
801 * exists on the page, <code>false</code> otherwise
803 function hasPost(postId) {
804 return $(".post#" + postId).length > 0;
808 * Returns whether a reply with the given ID exists on the current page.
811 * The reply ID to check for
812 * @returns {Boolean} <code>true</code> if a reply with the given ID already
813 * exists on the page, <code>false</code> otherwise
815 function hasReply(replyId) {
816 return $("#sone .reply#" + replyId).length > 0;
819 function loadNewPost(postId) {
820 if (hasPost(postId)) {
823 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
824 if ((data != null) && data.success) {
825 if (hasPost(data.post.id)) {
828 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
831 var firstOlderPost = null;
832 $("#sone .post").each(function() {
833 if (getPostTime(this) < data.post.time) {
834 firstOlderPost = $(this);
838 newPost = $(data.post.html).addClass("hidden");
839 if (firstOlderPost != null) {
840 newPost.insertBefore(firstOlderPost);
842 $("#sone #posts").append(newPost);
844 ajaxifyPost(newPost);
851 function loadNewReply(replyId) {
852 if (hasReply(replyId)) {
855 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
857 if ((data != null) && data.success) {
858 if (hasReply(data.reply.id)) {
861 $("#sone .post#" + data.reply.postId).each(function() {
862 var firstNewerReply = null;
863 $(this).find(".replies .reply").each(function() {
864 if (getReplyTime(this) > data.reply.time) {
865 firstNewerReply = $(this);
869 newReply = $(data.reply.html).addClass("hidden");
870 if (firstNewerReply != null) {
871 newReply.insertBefore(firstNewerReply);
873 if ($(this).find(".replies .create-reply")) {
874 $(this).find(".replies .create-reply").before(newReply);
876 $(this).find(".replies").append(newReply);
879 ajaxifyReply(newReply);
880 newReply.slideDown();
888 function markPostAsKnown(postElements) {
889 $(postElements).each(function() {
891 if ($(postElement).hasClass("new")) {
892 (function(postElement) {
893 $.getJSON("markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) {
894 $(postElement).removeClass("new");
899 markReplyAsKnown($(postElements).find(".reply"));
902 function markReplyAsKnown(replyElements) {
903 $(replyElements).each(function() {
905 if ($(replyElement).hasClass("new")) {
906 (function(replyElement) {
907 $.getJSON("markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function(data, textStatus) {
908 $(replyElement).removeClass("new");
915 function resetActivity() {
916 title = document.title;
917 if (title.indexOf('(') == 0) {
918 document.title = title.substr(title.indexOf(' ') + 1);
922 function setActivity() {
924 title = document.title;
925 if (title.indexOf('(') != 0) {
926 document.title = "(!) " + title;
932 * Creates a new notification.
935 * The ID of the notificaiton
937 * The text of the notification
939 * <code>true</code> if the notification can be dismissed by the
942 function createNotification(id, text, dismissable) {
943 notification = $("<div></div>").addClass("notification").attr("id", id);
945 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
946 dismissForm.find("input[name=notification]").val(id);
947 notification.append(dismissForm);
949 notification.append(text);
954 * Shows the details of the notification with the given ID.
956 * @param notificationId
957 * The ID of the notification
959 function showNotificationDetails(notificationId) {
960 $("#sone .notification#" + notificationId + " .text").show();
961 $("#sone .notification#" + notificationId + " .short-text").hide();
965 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
970 $(document).ready(function() {
972 /* this initializes the status update input field. */
973 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
974 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
975 $("#sone #update-status").submit(function() {
976 if ($(this).find(":input.default:enabled").length > 0) {
979 text = $(this).find(":input:enabled").val();
980 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) {
981 if ((data != null) && data.success) {
982 loadNewPost(data.postId);
985 $(this).find(":input:enabled").val("").blur();
990 /* ajaxify input field on “view Sone” page. */
991 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
992 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
993 $("#sone #post-message").submit(function() {
994 text = $(this).find(":input:enabled").val();
995 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "text": text }, function(data, textStatus) {
996 if ((data != null) && data.success) {
997 loadNewPost(data.postId);
1000 $(this).find(":input:enabled").val("").blur();
1005 /* Ajaxifies all posts. */
1006 /* calling getTranslation here will cache the necessary values. */
1007 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1008 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1009 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1010 $("#sone .post").each(function() {
1017 /* hides all replies but the latest two. */
1018 if (!isViewPostPage()) {
1019 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1020 $("#sone .post .replies").each(function() {
1021 allReplies = $(this).find(".reply");
1022 if (allReplies.length > 2) {
1024 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1025 $(allReplies[replyIndex]).addClass("hidden");
1026 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1028 clickToShowElement = $("<div></div>").addClass("click-to-show");
1030 clickToShowElement.addClass("new");
1032 (function(clickToShowElement, allReplies, text) {
1033 clickToShowElement.text(text);
1034 clickToShowElement.click(function() {
1035 allReplies.removeClass("hidden");
1036 clickToShowElement.addClass("hidden");
1038 })(clickToShowElement, allReplies, text);
1039 $(allReplies[0]).before(clickToShowElement);
1046 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
1049 $("#sone .follow").submit(function() {
1050 var followElement = this;
1051 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
1052 $(followElement).addClass("hidden");
1053 $(followElement).parent().find(".unfollow").removeClass("hidden");
1057 $("#sone .unfollow").submit(function() {
1058 var unfollowElement = this;
1059 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
1060 $(unfollowElement).addClass("hidden");
1061 $(unfollowElement).parent().find(".follow").removeClass("hidden");
1065 $("#sone .lock").submit(function() {
1066 var lockElement = this;
1067 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
1068 $(lockElement).addClass("hidden");
1069 $(lockElement).parent().find(".unlock").removeClass("hidden");
1073 $("#sone .unlock").submit(function() {
1074 var unlockElement = this;
1075 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
1076 $(unlockElement).addClass("hidden");
1077 $(unlockElement).parent().find(".lock").removeClass("hidden");
1082 /* process all existing notifications, ajaxify dismiss buttons. */
1083 $("#sone #notification-area .notification").each(function() {
1084 ajaxifyNotification($(this));
1087 /* activate status polling. */
1088 setTimeout(getStatus, 5000);
1090 /* reset activity counter when the page has focus. */
1091 $(window).focus(function() {
1094 }).blur(function() {