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 inputField.attr("disabled", "disabled");
51 if (!optional && (textarea.val() == "")) {
55 })($(this), textarea);
60 * Adds a “comment” link to all status lines contained in the given element.
65 * The element to add a “comment” link to
67 function addCommentLink(postId, element, insertAfterThisElement) {
68 if ($(element).find(".show-reply-form").length > 0) {
71 commentElement = (function(postId) {
72 separator = $("<span> · </span>").addClass("separator");
73 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
74 markPostAsKnown(getPostElement(this));
75 replyElement = $("#sone .post#" + postId + " .create-reply");
76 replyElement.removeClass("hidden");
77 replyElement.removeClass("light");
78 (function(replyElement) {
79 replyElement.find("input.reply-input").blur(function() {
80 if ($(this).hasClass("default")) {
81 replyElement.addClass("light");
84 replyElement.removeClass("light");
87 replyElement.find("input.reply-input").focus();
89 return commentElement;
91 $(insertAfterThisElement).after(commentElement.clone(true));
92 $(insertAfterThisElement).after(separator);
95 var translations = {};
98 * Retrieves the translation for the given key and calls the callback function.
99 * The callback function takes a single parameter, the translated string.
102 * The key of the translation string
104 * The callback function
106 function getTranslation(key, callback) {
107 if (key in translations) {
108 callback(translations[key]);
111 $.getJSON("getTranslation.ajax", {"key": key}, function(data, textStatus) {
112 if ((data != null) && data.success) {
113 translations[key] = data.value;
114 callback(data.value);
116 }, function(xmlHttpRequest, textStatus, error) {
122 * Filters the given Sone ID, replacing all “~” characters by an underscore.
125 * The Sone ID to filter
126 * @returns The filtered Sone ID
128 function filterSoneId(soneId) {
129 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
133 * Updates the status of the given Sone.
136 * The ID of the Sone to update
138 * The status of the Sone (“idle”, “unknown”, “inserting”,
141 * Whether the Sone is modified
143 * Whether the Sone is locked
145 * The date and time of the last update (formatted for display)
147 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
148 $("#sone .sone." + filterSoneId(soneId)).
149 toggleClass("unknown", status == "unknown").
150 toggleClass("idle", status == "idle").
151 toggleClass("inserting", status == "inserting").
152 toggleClass("downloading", status == "downloading").
153 toggleClass("modified", modified);
154 $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
155 $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
156 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
157 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
161 * Enhances a “delete” button so that the confirmation is done on the same page.
166 * The text to show on the button
167 * @param deleteCallback
168 * The callback that actually deletes something
170 function enhanceDeleteButton(button, text, deleteCallback) {
172 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
173 $(this).fadeOut("slow");
176 }).insertAfter(button);
177 (function(button, newButton) {
178 button.click(function() {
179 button.fadeOut("slow", function() {
180 newButton.fadeIn("slow");
181 $(document).one("click", function() {
182 if (this != newButton.get(0)) {
183 newButton.fadeOut(function() {
191 })(button, newButton);
196 * Enhances a post’s “delete” button.
201 * The ID of the post to delete
203 * The text to replace the button with
205 function enhanceDeletePostButton(button, postId, text) {
206 enhanceDeleteButton(button, text, function() {
207 $.getJSON("deletePost.ajax", { "post": postId, "formPassword": getFormPassword() }, function(data, textStatus) {
212 $("#sone .post#" + postId).slideUp();
213 } else if (data.error == "invalid-post-id") {
214 alert("Invalid post ID given!");
215 } else if (data.error == "auth-required") {
216 alert("You need to be logged in.");
217 } else if (data.error == "not-authorized") {
218 alert("You are not allowed to delete this post.");
220 }, function(xmlHttpRequest, textStatus, error) {
227 * Enhances a reply’s “delete” button.
232 * The ID of the reply to delete
234 * The text to replace the button with
236 function enhanceDeleteReplyButton(button, replyId, text) {
237 enhanceDeleteButton(button, text, function() {
238 $.getJSON("deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
243 $("#sone .reply#" + replyId).slideUp();
244 } else if (data.error == "invalid-reply-id") {
245 alert("Invalid reply ID given!");
246 } else if (data.error == "auth-required") {
247 alert("You need to be logged in.");
248 } else if (data.error == "not-authorized") {
249 alert("You are not allowed to delete this reply.");
251 }, function(xmlHttpRequest, textStatus, error) {
257 function getFormPassword() {
258 return $("#sone #formPassword").text();
261 function getSoneElement(element) {
262 return $(element).closest(".sone");
266 * Generates a list of Sones by concatening the names of the given sones with a
267 * new line character (“\n”).
270 * The sones to format
271 * @returns {String} The created string
273 function generateSoneList(sones) {
275 $.each(sones, function() {
276 if (soneList != "") {
279 soneList += this.name;
285 * Returns the ID of the Sone that this element belongs to.
288 * The element to locate the matching Sone ID for
289 * @returns The ID of the Sone, or undefined
291 function getSoneId(element) {
292 return getSoneElement(element).find(".id").text();
295 function getPostElement(element) {
296 return $(element).closest(".post");
299 function getPostId(element) {
300 return getPostElement(element).attr("id");
303 function getPostTime(element) {
304 return getPostElement(element).find(".post-time").text();
308 * Returns the author of the post the given element belongs to.
311 * The element whose post to get the author for
312 * @returns The ID of the authoring Sone
314 function getPostAuthor(element) {
315 return getPostElement(element).find(".post-author").text();
318 function getReplyElement(element) {
319 return $(element).closest(".reply");
322 function getReplyId(element) {
323 return getReplyElement(element).attr("id");
326 function getReplyTime(element) {
327 return getReplyElement(element).find(".reply-time").text();
331 * Returns the author of the reply the given element belongs to.
334 * The element whose reply to get the author for
335 * @returns The ID of the authoring Sone
337 function getReplyAuthor(element) {
338 return getReplyElement(element).find(".reply-author").text();
341 function likePost(postId) {
342 $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
343 if ((data == null) || !data.success) {
346 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
347 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
348 updatePostLikes(postId);
349 }, function(xmlHttpRequest, textStatus, error) {
354 function unlikePost(postId) {
355 $.getJSON("unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
356 if ((data == null) || !data.success) {
359 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
360 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
361 updatePostLikes(postId);
362 }, function(xmlHttpRequest, textStatus, error) {
367 function updatePostLikes(postId) {
368 $.getJSON("getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
369 if ((data != null) && data.success) {
370 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
371 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
372 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
374 }, function(xmlHttpRequest, textStatus, error) {
379 function likeReply(replyId) {
380 $.getJSON("like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
381 if ((data == null) || !data.success) {
384 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
385 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
386 updateReplyLikes(replyId);
387 }, function(xmlHttpRequest, textStatus, error) {
392 function unlikeReply(replyId) {
393 $.getJSON("unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
394 if ((data == null) || !data.success) {
397 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
398 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
399 updateReplyLikes(replyId);
400 }, function(xmlHttpRequest, textStatus, error) {
406 * Trusts the Sone with the given ID.
409 * The ID of the Sone to trust
411 function trustSone(soneId) {
412 $.getJSON("trustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
413 if ((data != null) && data.success) {
414 updateTrustControls(soneId, data.trustValue);
420 * Distrusts the Sone with the given ID, i.e. assigns a negative trust value.
423 * The ID of the Sone to distrust
425 function distrustSone(soneId) {
426 $.getJSON("distrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
427 if ((data != null) && data.success) {
428 updateTrustControls(soneId, data.trustValue);
434 * Untrusts the Sone with the given ID, i.e. removes any trust assignment.
437 * The ID of the Sone to untrust
439 function untrustSone(soneId) {
440 $.getJSON("untrustSone.ajax", { "formPassword" : getFormPassword(), "sone" : soneId }, function(data, textStatus) {
441 if ((data != null) && data.success) {
442 updateTrustControls(soneId, data.trustValue);
448 * Updates the trust controls for all posts and replies of the given Sone,
449 * according to the given trust value.
452 * The ID of the Sone to update all trust controls for
454 * The trust value for the Sone
456 function updateTrustControls(soneId, trustValue) {
457 $("#sone .post").each(function() {
458 if (getPostAuthor(this) == soneId) {
459 getPostElement(this).find(".post-trust").toggleClass("hidden", trustValue != null);
460 getPostElement(this).find(".post-distrust").toggleClass("hidden", (trustValue != null) && (trustValue < 0));
461 getPostElement(this).find(".post-untrust").toggleClass("hidden", trustValue == null);
464 $("#sone .reply").each(function() {
465 if (getReplyAuthor(this) == soneId) {
466 getReplyElement(this).find(".reply-trust").toggleClass("hidden", trustValue != null);
467 getReplyElement(this).find(".reply-distrust").toggleClass("hidden", (trustValue != null) && (trustValue < 0));
468 getReplyElement(this).find(".reply-untrust").toggleClass("hidden", trustValue == null);
473 function updateReplyLikes(replyId) {
474 $.getJSON("getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
475 if ((data != null) && data.success) {
476 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
477 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
478 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
480 }, function(xmlHttpRequest, textStatus, error) {
486 * Posts a reply and calls the given callback when the request finishes.
489 * The ID of the post the reply refers to
492 * @param callbackFunction
493 * The callback function to call when the request finishes (takes 3
494 * parameters: success, error, replyId)
496 function postReply(postId, text, callbackFunction) {
497 $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
499 /* TODO - show error */
503 callbackFunction(true, null, data.reply);
505 callbackFunction(false, data.error);
507 }, function(xmlHttpRequest, textStatus, error) {
513 * Requests information about the reply with the given ID.
516 * The ID of the reply
517 * @param callbackFunction
518 * A callback function (parameters soneId, soneName, replyTime,
519 * replyDisplayTime, text, html)
521 function getReply(replyId, callbackFunction) {
522 $.getJSON("getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
523 if ((data != null) && data.success) {
524 callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
526 }, function(xmlHttpRequest, textStatus, error) {
532 * Ajaxifies the given post by enhancing all eligible elements with AJAX.
535 * The post element to ajaxify
537 function ajaxifyPost(postElement) {
538 $(postElement).find("form").submit(function() {
541 $(postElement).find(".create-reply button:submit").click(function() {
542 inputField = $(this.form).find(":input:enabled").get(0);
543 postId = getPostId(this);
544 text = $(inputField).val();
545 (function(postId, text, inputField) {
546 postReply(postId, text, function(success, error, replyId) {
548 $(inputField).val("");
549 loadNewReply(replyId, getCurrentSoneId(), postId);
550 markPostAsKnown(getPostElement(inputField));
551 $("#sone .post#" + postId + " .create-reply").addClass("hidden");
556 })(postId, text, inputField);
560 /* replace all “delete” buttons with javascript. */
561 (function(postElement) {
562 getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
563 postId = getPostId(postElement);
564 enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
568 /* convert all “like” buttons to javascript functions. */
569 $(postElement).find(".like-post").submit(function() {
570 likePost(getPostId(this));
571 markPostAsKnown(getPostElement(this));
574 $(postElement).find(".unlike-post").submit(function() {
575 unlikePost(getPostId(this));
576 markPostAsKnown(getPostElement(this));
580 /* convert trust control buttons to javascript functions. */
581 $(postElement).find(".post-trust").submit(function() {
582 trustSone(getPostAuthor(this));
585 $(postElement).find(".post-distrust").submit(function() {
586 distrustSone(getPostAuthor(this));
589 $(postElement).find(".post-untrust").submit(function() {
590 untrustSone(getPostAuthor(this));
594 /* add “comment” link. */
595 addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
597 /* process all replies. */
598 $(postElement).find(".reply").each(function() {
602 /* process reply input fields. */
603 getTranslation("WebInterface.DefaultText.Reply", function(text) {
604 $(postElement).find("input.reply-input").each(function() {
605 registerInputTextareaSwap(this, text, "text", false, false);
609 /* mark everything as known on click. */
610 $(postElement).click(function(event) {
611 if ($(event.target).hasClass("click-to-show")) {
614 markPostAsKnown(this);
617 /* hide reply input field. */
618 $(postElement).find(".create-reply").addClass("hidden");
622 * Ajaxifies the given reply element.
624 * @param replyElement
625 * The reply element to ajaxify
627 function ajaxifyReply(replyElement) {
628 $(replyElement).find(".like-reply").submit(function() {
629 likeReply(getReplyId(this));
630 markPostAsKnown(getPostElement(this));
633 $(replyElement).find(".unlike-reply").submit(function() {
634 unlikeReply(getReplyId(this));
635 markPostAsKnown(getPostElement(this));
638 (function(replyElement) {
639 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
640 $(replyElement).find(".delete-reply button").each(function() {
641 enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
645 addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
647 /* convert trust control buttons to javascript functions. */
648 $(replyElement).find(".reply-trust").submit(function() {
649 trustSone(getReplyAuthor(this));
652 $(replyElement).find(".reply-distrust").submit(function() {
653 distrustSone(getReplyAuthor(this));
656 $(replyElement).find(".reply-untrust").submit(function() {
657 untrustSone(getReplyAuthor(this));
661 /* mark post and all replies as known on click. */
662 $(replyElement).click(function() {
663 markPostAsKnown(getPostElement(this));
668 * Ajaxifies the given notification by replacing the form with AJAX.
670 * @param notification
671 * jQuery object representing the notification.
673 function ajaxifyNotification(notification) {
674 notification.find("form.dismiss").submit(function() {
677 notification.find("form.dismiss button").click(function() {
678 $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
679 /* dismiss in case of error, too. */
680 notification.slideUp();
681 }, function(xmlHttpRequest, textStatus, error) {
688 function getStatus() {
689 $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
690 if ((data != null) && data.success) {
691 /* process Sone information. */
692 $.each(data.sones, function(index, value) {
693 updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
695 /* process notifications. */
696 $.each(data.notifications, function(index, value) {
697 oldNotification = $("#sone #notification-area .notification#" + value.id);
698 notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
699 if (oldNotification.length != 0) {
700 oldNotification.replaceWith(notification.show());
702 $("#sone #notification-area").append(notification);
703 notification.slideDown();
707 $.each(data.removedNotifications, function(index, value) {
708 $("#sone #notification-area .notification#" + value.id).slideUp();
710 /* process new posts. */
711 $.each(data.newPosts, function(index, value) {
712 loadNewPost(value.id, value.sone, value.recipient, value.time);
714 /* process new replies. */
715 $.each(data.newReplies, function(index, value) {
716 loadNewReply(value.id, value.sone, value.post, value.postSone);
718 /* do it again in 5 seconds. */
719 setTimeout(getStatus, 5000);
721 /* data.success was false, wait 30 seconds. */
722 setTimeout(getStatus, 30000);
724 }, function(xmlHttpRequest, textStatus, error) {
725 /* something really bad happend, wait a minute. */
726 setTimeout(getStatus, 60000);
731 * Returns the ID of the currently logged in Sone.
733 * @return The ID of the current Sone, or an empty string if no Sone is logged
736 function getCurrentSoneId() {
737 return $("#currentSoneId").text();
741 * Returns the content of the page-id attribute.
743 * @returns The page ID
745 function getPageId() {
746 return $("#sone .page-id").text();
750 * Returns whether the current page is the index page.
752 * @returns {Boolean} <code>true</code> if the current page is the index page,
753 * <code>false</code> otherwise
755 function isIndexPage() {
756 return getPageId() == "index";
760 * Returns whether the current page is a “view Sone” page.
762 * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
763 * page, <code>false</code> otherwise
765 function isViewSonePage() {
766 return getPageId() == "view-sone";
770 * Returns the ID of the currently shown Sone. This will only return a sensible
771 * value if isViewSonePage() returns <code>true</code>.
773 * @returns The ID of the currently shown Sone
775 function getShownSoneId() {
776 return $("#sone .sone-id").text();
780 * Returns whether the current page is a “view post” page.
782 * @returns {Boolean} <code>true</code> if the current page is a “view post”
783 * page, <code>false</code> otherwise
785 function isViewPostPage() {
786 return getPageId() == "view-post";
790 * Returns the ID of the currently shown post. This will only return a sensible
791 * value if isViewPostPage() returns <code>true</code>.
793 * @returns The ID of the currently shown post
795 function getShownPostId() {
796 return $("#sone .post-id").text();
800 * Returns whether the current page is the “known Sones” page.
802 * @returns {Boolean} <code>true</code> if the current page is the “known
803 * Sones” page, <code>false</code> otherwise
805 function isKnownSonesPage() {
806 return getPageId() == "known-sones";
810 * Returns whether a post with the given ID exists on the current page.
813 * The post ID to check for
814 * @returns {Boolean} <code>true</code> if a post with the given ID already
815 * exists on the page, <code>false</code> otherwise
817 function hasPost(postId) {
818 return $(".post#" + postId).length > 0;
822 * Returns whether a reply with the given ID exists on the current page.
825 * The reply ID to check for
826 * @returns {Boolean} <code>true</code> if a reply with the given ID already
827 * exists on the page, <code>false</code> otherwise
829 function hasReply(replyId) {
830 return $("#sone .reply#" + replyId).length > 0;
833 function loadNewPost(postId, soneId, recipientId, time) {
834 if (hasPost(postId)) {
837 if (!isIndexPage()) {
838 if (!isViewPostPage() || (getShownPostId() != postId)) {
839 if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
844 if (getPostTime($("#sone .post").last()) > time) {
847 $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
848 if ((data != null) && data.success) {
849 if (hasPost(data.post.id)) {
852 if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
855 var firstOlderPost = null;
856 $("#sone .post").each(function() {
857 if (getPostTime(this) < data.post.time) {
858 firstOlderPost = $(this);
862 newPost = $(data.post.html).addClass("hidden");
863 if (firstOlderPost != null) {
864 newPost.insertBefore(firstOlderPost);
866 ajaxifyPost(newPost);
873 function loadNewReply(replyId, soneId, postId, postSoneId) {
874 if (hasReply(replyId)) {
877 if (!hasPost(postId)) {
880 $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
882 if ((data != null) && data.success) {
883 if (hasReply(data.reply.id)) {
886 $("#sone .post#" + data.reply.postId).each(function() {
887 var firstNewerReply = null;
888 $(this).find(".replies .reply").each(function() {
889 if (getReplyTime(this) > data.reply.time) {
890 firstNewerReply = $(this);
894 newReply = $(data.reply.html).addClass("hidden");
895 if (firstNewerReply != null) {
896 newReply.insertBefore(firstNewerReply);
898 if ($(this).find(".replies .create-reply")) {
899 $(this).find(".replies .create-reply").before(newReply);
901 $(this).find(".replies").append(newReply);
904 ajaxifyReply(newReply);
905 newReply.slideDown();
913 function markPostAsKnown(postElements) {
914 $(postElements).each(function() {
916 if ($(postElement).hasClass("new")) {
917 (function(postElement) {
918 $.getJSON("markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) {
919 $(postElement).removeClass("new");
920 $(".click-to-show", postElement).removeClass("new");
925 markReplyAsKnown($(postElements).find(".reply"));
928 function markReplyAsKnown(replyElements) {
929 $(replyElements).each(function() {
931 if ($(replyElement).hasClass("new")) {
932 (function(replyElement) {
933 $.getJSON("markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function(data, textStatus) {
934 $(replyElement).removeClass("new");
941 function resetActivity() {
942 title = document.title;
943 if (title.indexOf('(') == 0) {
944 document.title = title.substr(title.indexOf(' ') + 1);
948 function setActivity() {
950 title = document.title;
951 if (title.indexOf('(') != 0) {
952 document.title = "(!) " + title;
958 * Creates a new notification.
961 * The ID of the notificaiton
963 * The text of the notification
965 * <code>true</code> if the notification can be dismissed by the
968 function createNotification(id, text, dismissable) {
969 notification = $("<div></div>").addClass("notification").attr("id", id);
971 dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
972 dismissForm.find("input[name=notification]").val(id);
973 notification.append(dismissForm);
975 notification.append(text);
980 * Shows the details of the notification with the given ID.
982 * @param notificationId
983 * The ID of the notification
985 function showNotificationDetails(notificationId) {
986 $("#sone .notification#" + notificationId + " .text").show();
987 $("#sone .notification#" + notificationId + " .short-text").hide();
991 * Deletes the field with the given ID from the profile.
994 * The ID of the field to delete
996 function deleteProfileField(fieldId) {
997 $.getJSON("deleteProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId}, function(data, textStatus) {
998 if (data && data.success) {
999 $("#sone .profile-field#" + data.field.id).slideUp();
1005 * Renames a profile field.
1008 * The ID of the field to rename
1010 * The new name of the field
1011 * @param successFunction
1012 * Called when the renaming was successful
1014 function editProfileField(fieldId, newName, successFunction) {
1015 $.getJSON("editProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "name": newName}, function(data, textStatus) {
1016 if (data && data.success) {
1023 * Moves the profile field with the given ID one slot in the given direction.
1026 * The ID of the field to move
1028 * The direction to move in (“up” or “down”)
1029 * @param successFunction
1030 * Function to call on success
1032 function moveProfileField(fieldId, direction, successFunction) {
1033 $.getJSON("moveProfileField.ajax", {"formPassword": getFormPassword(), "field": fieldId, "direction": direction}, function(data, textStatus) {
1034 if (data && data.success) {
1041 * Moves the profile field with the given ID up one slot.
1044 * The ID of the field to move
1045 * @param successFunction
1046 * Function to call on success
1048 function moveProfileFieldUp(fieldId, successFunction) {
1049 moveProfileField(fieldId, "up", successFunction);
1053 * Moves the profile field with the given ID down one slot.
1056 * The ID of the field to move
1057 * @param successFunction
1058 * Function to call on success
1060 function moveProfileFieldDown(fieldId, successFunction) {
1061 moveProfileField(fieldId, "down", successFunction);
1065 // EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
1070 $(document).ready(function() {
1072 /* this initializes the status update input field. */
1073 getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
1074 registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
1075 $("#sone #update-status").submit(function() {
1076 if ($(this).find(":input.default:enabled").length > 0) {
1079 text = $(this).find(":input:enabled").val();
1080 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "text": text }, function(data, textStatus) {
1081 if ((data != null) && data.success) {
1082 loadNewPost(data.postId, getCurrentSoneId());
1085 $(this).find(":input:enabled").val("").blur();
1090 /* ajaxify input field on “view Sone” page. */
1091 getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
1092 registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
1093 $("#sone #post-message").submit(function() {
1094 text = $(this).find(":input:enabled").val();
1095 $.getJSON("createPost.ajax", { "formPassword": getFormPassword(), "recipient": getShownSoneId(), "text": text }, function(data, textStatus) {
1096 if ((data != null) && data.success) {
1097 loadNewPost(data.postId, getCurrentSoneId());
1100 $(this).find(":input:enabled").val("").blur();
1105 /* Ajaxifies all posts. */
1106 /* calling getTranslation here will cache the necessary values. */
1107 getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
1108 getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
1109 getTranslation("WebInterface.DefaultText.Reply", function(text) {
1110 $("#sone .post").each(function() {
1117 /* hides all replies but the latest two. */
1118 if (!isViewPostPage()) {
1119 getTranslation("WebInterface.ClickToShow.Replies", function(text) {
1120 $("#sone .post .replies").each(function() {
1121 allReplies = $(this).find(".reply");
1122 if (allReplies.length > 2) {
1124 for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
1125 $(allReplies[replyIndex]).addClass("hidden");
1126 newHidden |= $(allReplies[replyIndex]).hasClass("new");
1128 clickToShowElement = $("<div></div>").addClass("click-to-show");
1130 clickToShowElement.addClass("new");
1132 (function(clickToShowElement, allReplies, text) {
1133 clickToShowElement.text(text);
1134 clickToShowElement.click(function() {
1135 allReplies.removeClass("hidden");
1136 clickToShowElement.addClass("hidden");
1138 })(clickToShowElement, allReplies, text);
1139 $(allReplies[0]).before(clickToShowElement);
1146 * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
1149 $("#sone .follow").submit(function() {
1150 var followElement = this;
1151 $.getJSON("followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
1152 $(followElement).addClass("hidden");
1153 $(followElement).parent().find(".unfollow").removeClass("hidden");
1157 $("#sone .unfollow").submit(function() {
1158 var unfollowElement = this;
1159 $.getJSON("unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
1160 $(unfollowElement).addClass("hidden");
1161 $(unfollowElement).parent().find(".follow").removeClass("hidden");
1165 $("#sone .lock").submit(function() {
1166 var lockElement = this;
1167 $.getJSON("lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
1168 $(lockElement).addClass("hidden");
1169 $(lockElement).parent().find(".unlock").removeClass("hidden");
1173 $("#sone .unlock").submit(function() {
1174 var unlockElement = this;
1175 $.getJSON("unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
1176 $(unlockElement).addClass("hidden");
1177 $(unlockElement).parent().find(".lock").removeClass("hidden");
1182 /* process all existing notifications, ajaxify dismiss buttons. */
1183 $("#sone #notification-area .notification").each(function() {
1184 ajaxifyNotification($(this));
1187 /* activate status polling. */
1188 setTimeout(getStatus, 5000);
1190 /* reset activity counter when the page has focus. */
1191 $(window).focus(function() {
1194 }).blur(function() {