+ $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
+ }
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
+ });
+}
+
+/**
+ * Posts a reply and calls the given callback when the request finishes.
+ *
+ * @param postId
+ * The ID of the post the reply refers to
+ * @param text
+ * The text to post
+ * @param callbackFunction
+ * The callback function to call when the request finishes (takes 3
+ * parameters: success, error, replyId)
+ */
+function postReply(postId, text, callbackFunction) {
+ $.getJSON("createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
+ if (data == null) {
+ /* TODO - show error */
+ return;
+ }
+ if (data.success) {
+ callbackFunction(true, null, data.reply);
+ } else {
+ callbackFunction(false, data.error);
+ }
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
+ });
+}
+
+/**
+ * Requests information about the reply with the given ID.
+ *
+ * @param replyId
+ * The ID of the reply
+ * @param callbackFunction
+ * A callback function (parameters soneId, soneName, replyTime,
+ * replyDisplayTime, text, html)
+ */
+function getReply(replyId, callbackFunction) {
+ $.getJSON("getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
+ if ((data != null) && data.success) {
+ callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
+ }
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
+ });
+}
+
+/**
+ * Ajaxifies the given post by enhancing all eligible elements with AJAX.
+ *
+ * @param postElement
+ * The post element to ajaxify
+ */
+function ajaxifyPost(postElement) {
+ $(postElement).find("form").submit(function() {
+ return false;
+ });
+ $(postElement).find(".create-reply button:submit").click(function() {
+ inputField = $(this.form).find(":input:enabled").get(0);
+ postId = getPostId(this);
+ text = $(inputField).val();
+ (function(postId, text, inputField) {
+ postReply(postId, text, function(success, error, replyId) {
+ if (success) {
+ $(inputField).val("");
+ loadNewReply(replyId, getCurrentSoneId(), postId);
+ markPostAsKnown(getPostElement(inputField));
+ $("#sone .post#" + postId + " .create-reply").addClass("hidden");
+ } else {
+ alert(error);
+ }
+ });
+ })(postId, text, inputField);
+ return false;
+ });
+
+ /* replace all “delete” buttons with javascript. */
+ (function(postElement) {
+ getTranslation("WebInterface.Confirmation.DeletePostButton", function(deletePostText) {
+ postId = getPostId(postElement);
+ enhanceDeletePostButton($(postElement).find(".delete-post button"), postId, deletePostText);
+ });
+ })(postElement);
+
+ /* convert all “like” buttons to javascript functions. */
+ $(postElement).find(".like-post").submit(function() {
+ likePost(getPostId(this));
+ markPostAsKnown(getPostElement(this));
+ return false;
+ });
+ $(postElement).find(".unlike-post").submit(function() {
+ unlikePost(getPostId(this));
+ markPostAsKnown(getPostElement(this));
+ return false;
+ });
+
+ /* add “comment” link. */
+ addCommentLink(getPostId(postElement), postElement, $(postElement).find(".post-status-line .time"));
+
+ /* process all replies. */
+ $(postElement).find(".reply").each(function() {
+ ajaxifyReply(this);
+ });
+
+ /* process reply input fields. */
+ getTranslation("WebInterface.DefaultText.Reply", function(text) {
+ $(postElement).find("input.reply-input").each(function() {
+ registerInputTextareaSwap(this, text, "text", false, false);
+ });
+ });
+
+ /* mark everything as known on click. */
+ $(postElement).click(function() {
+ markPostAsKnown(this);
+ });
+
+ /* hide reply input field. */
+ $(postElement).find(".create-reply").addClass("hidden");
+}
+
+/**
+ * Ajaxifies the given reply element.
+ *
+ * @param replyElement
+ * The reply element to ajaxify
+ */
+function ajaxifyReply(replyElement) {
+ $(replyElement).find(".like-reply").submit(function() {
+ likeReply(getReplyId(this));
+ markPostAsKnown(getPostElement(this));
+ return false;
+ });
+ $(replyElement).find(".unlike-reply").submit(function() {
+ unlikeReply(getReplyId(this));
+ markPostAsKnown(getPostElement(this));
+ return false;
+ });
+ (function(replyElement) {
+ getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
+ $(replyElement).find(".delete-reply button").each(function() {
+ enhanceDeleteReplyButton(this, getReplyId(replyElement), deleteReplyText);
+ });
+ });
+ })(replyElement);
+ addCommentLink(getPostId(replyElement), replyElement, $(replyElement).find(".reply-status-line .time"));
+
+ /* mark post and all replies as known on click. */
+ $(replyElement).click(function() {
+ markPostAsKnown(getPostElement(this));
+ });
+}
+
+/**
+ * Ajaxifies the given notification by replacing the form with AJAX.
+ *
+ * @param notification
+ * jQuery object representing the notification.
+ */
+function ajaxifyNotification(notification) {
+ notification.find("form.dismiss").submit(function() {
+ return false;
+ });
+ notification.find("form.dismiss button").click(function() {
+ $.getJSON("dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
+ /* dismiss in case of error, too. */
+ notification.slideUp();
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
+ });
+ });
+ return notification;
+}
+
+function getStatus() {
+ $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
+ if ((data != null) && data.success) {
+ /* process Sone information. */
+ $.each(data.sones, function(index, value) {
+ updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
+ });
+ /* process notifications. */
+ $.each(data.notifications, function(index, value) {
+ oldNotification = $("#sone #notification-area .notification#" + value.id);
+ notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
+ if (oldNotification.length != 0) {
+ oldNotification.replaceWith(notification.show());
+ } else {
+ $("#sone #notification-area").append(notification);
+ notification.slideDown();
+ }
+ setActivity();
+ });
+ $.each(data.removedNotifications, function(index, value) {
+ $("#sone #notification-area .notification#" + value.id).slideUp();
+ });
+ /* process new posts. */
+ $.each(data.newPosts, function(index, value) {
+ loadNewPost(value.id, value.sone, value.recipient);
+ });
+ /* process new replies. */
+ $.each(data.newReplies, function(index, value) {
+ loadNewReply(value.id, value.sone, value.post, value.postSone);
+ });
+ /* do it again in 5 seconds. */
+ setTimeout(getStatus, 5000);
+ } else {
+ /* data.success was false, wait 30 seconds. */
+ setTimeout(getStatus, 30000);
+ }
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* something really bad happend, wait a minute. */
+ setTimeout(getStatus, 60000);
+ })
+}
+
+/**
+ * Returns the ID of the currently logged in Sone.
+ *
+ * @return The ID of the current Sone, or an empty string if no Sone is logged
+ * in
+ */
+function getCurrentSoneId() {
+ return $("#currentSoneId").text();
+}
+
+/**
+ * Returns the content of the page-id attribute.
+ *
+ * @returns The page ID
+ */
+function getPageId() {
+ return $("#sone .page-id").text();
+}
+
+/**
+ * Returns whether the current page is the index page.
+ *
+ * @returns {Boolean} <code>true</code> if the current page is the index page,
+ * <code>false</code> otherwise
+ */
+function isIndexPage() {
+ return getPageId() == "index";
+}
+
+/**
+ * Returns whether the current page is a “view Sone” page.
+ *
+ * @returns {Boolean} <code>true</code> if the current page is a “view Sone”
+ * page, <code>false</code> otherwise
+ */
+function isViewSonePage() {
+ return getPageId() == "view-sone";
+}
+
+/**
+ * Returns the ID of the currently shown Sone. This will only return a sensible
+ * value if isViewSonePage() returns <code>true</code>.
+ *
+ * @returns The ID of the currently shown Sone
+ */
+function getShownSoneId() {
+ return $("#sone .sone-id").text();
+}
+
+/**
+ * Returns whether the current page is a “view post” page.
+ *
+ * @returns {Boolean} <code>true</code> if the current page is a “view post”
+ * page, <code>false</code> otherwise
+ */
+function isViewPostPage() {
+ return getPageId() == "view-post";
+}
+
+/**
+ * Returns the ID of the currently shown post. This will only return a sensible
+ * value if isViewPostPage() returns <code>true</code>.
+ *
+ * @returns The ID of the currently shown post
+ */
+function getShownPostId() {
+ return $("#sone .post-id").text();
+}
+
+/**
+ * Returns whether the current page is the “known Sones” page.
+ *
+ * @returns {Boolean} <code>true</code> if the current page is the “known
+ * Sones” page, <code>false</code> otherwise
+ */
+function isKnownSonesPage() {
+ return getPageId() == "known-sones";
+}
+
+/**
+ * Returns whether a post with the given ID exists on the current page.
+ *
+ * @param postId
+ * The post ID to check for
+ * @returns {Boolean} <code>true</code> if a post with the given ID already
+ * exists on the page, <code>false</code> otherwise
+ */
+function hasPost(postId) {
+ return $(".post#" + postId).length > 0;
+}
+
+/**
+ * Returns whether a reply with the given ID exists on the current page.
+ *
+ * @param replyId
+ * The reply ID to check for
+ * @returns {Boolean} <code>true</code> if a reply with the given ID already
+ * exists on the page, <code>false</code> otherwise
+ */
+function hasReply(replyId) {
+ return $("#sone .reply#" + replyId).length > 0;
+}
+
+function loadNewPost(postId, soneId, recipientId) {
+ if (hasPost(postId)) {
+ return;
+ }
+ if (!isIndexPage()) {
+ if (!isViewPostPage() || (getShownPostId() != postId)) {
+ if (!isViewSonePage() || ((getShownSoneId() != soneId) && (getShownSoneId() != recipientId))) {
+ return;
+ }
+ }
+ }
+ $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
+ if ((data != null) && data.success) {
+ if (hasPost(data.post.id)) {
+ return;
+ }
+ if (!isIndexPage() && !(isViewSonePage() && ((getShownSoneId() == data.post.sone) || (getShownSoneId() == data.post.recipient)))) {
+ return;
+ }
+ var firstOlderPost = null;
+ $("#sone .post").each(function() {
+ if (getPostTime(this) < data.post.time) {
+ firstOlderPost = $(this);
+ return false;
+ }
+ });
+ newPost = $(data.post.html).addClass("hidden");
+ if (firstOlderPost != null) {
+ newPost.insertBefore(firstOlderPost);
+ } else {
+ $("#sone #posts").append(newPost);
+ }
+ ajaxifyPost(newPost);
+ newPost.slideDown();
+ setActivity();
+ }
+ });
+}
+
+function loadNewReply(replyId, soneId, postId, postSoneId) {
+ if (hasReply(replyId)) {
+ return;
+ }
+ if (!hasPost(postId)) {
+ return;
+ }
+ $.getJSON("getReply.ajax", { "reply": replyId }, function(data, textStatus) {
+ /* find post. */
+ if ((data != null) && data.success) {
+ if (hasReply(data.reply.id)) {
+ return;
+ }
+ $("#sone .post#" + data.reply.postId).each(function() {
+ var firstNewerReply = null;
+ $(this).find(".replies .reply").each(function() {
+ if (getReplyTime(this) > data.reply.time) {
+ firstNewerReply = $(this);
+ return false;
+ }
+ });
+ newReply = $(data.reply.html).addClass("hidden");
+ if (firstNewerReply != null) {
+ newReply.insertBefore(firstNewerReply);
+ } else {
+ if ($(this).find(".replies .create-reply")) {
+ $(this).find(".replies .create-reply").before(newReply);
+ } else {
+ $(this).find(".replies").append(newReply);
+ }
+ }
+ ajaxifyReply(newReply);
+ newReply.slideDown();
+ setActivity();
+ return false;
+ });
+ }
+ });
+}
+
+function markPostAsKnown(postElements) {
+ $(postElements).each(function() {
+ postElement = this;
+ if ($(postElement).hasClass("new")) {
+ (function(postElement) {
+ $.getJSON("markPostAsKnown.ajax", {"formPassword": getFormPassword(), "post": getPostId(postElement)}, function(data, textStatus) {
+ $(postElement).removeClass("new");
+ });
+ })(postElement);
+ }
+ });
+ markReplyAsKnown($(postElements).find(".reply"));
+}
+
+function markReplyAsKnown(replyElements) {
+ $(replyElements).each(function() {
+ replyElement = this;
+ if ($(replyElement).hasClass("new")) {
+ (function(replyElement) {
+ $.getJSON("markReplyAsKnown.ajax", {"formPassword": getFormPassword(), "reply": getReplyId(replyElement)}, function(data, textStatus) {
+ $(replyElement).removeClass("new");
+ });
+ })(replyElement);