Fix comparisons that were assignments.
[Sone.git] / src / main / resources / static / javascript / sone.js
index b819333..accfabc 100644 (file)
@@ -67,30 +67,31 @@ function addCommentLink(postId, author, element, insertAfterThisElement) {
        if (($(element).find(".show-reply-form").length > 0) || (getPostElement(element).find(".create-reply").length == 0)) {
                return;
        }
-       commentElement = (function(postId, author) {
+       (function(postId, author, insertAfterThisElement) {
                separator = $("<span> · </span>").addClass("separator");
-               var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
-                       replyElement = $("#sone .post#post-" + postId + " .create-reply");
-                       replyElement.removeClass("hidden");
-                       replyElement.removeClass("light");
-                       (function(replyElement) {
-                               replyElement.find("input.reply-input").blur(function() {
-                                       if ($(this).hasClass("default")) {
-                                               replyElement.addClass("light");
-                                       }
-                               }).focus(function() {
-                                       replyElement.removeClass("light");
-                               });
-                       })(replyElement);
-                       textArea = replyElement.find("input.reply-input").focus().data("textarea");
-                       if (author != getCurrentSoneId()) {
-                               textArea.val(textArea.val() + "@sone://" + author + " ");
-                       }
+               getTranslation("WebInterface.Button.Comment", function(text) {
+                       commentElement = $("<div><span>" + text + "</span></div>").addClass("show-reply-form").click(function() {
+                               replyElement = $("#sone .post#post-" + postId + " .create-reply");
+                               replyElement.removeClass("hidden");
+                               replyElement.removeClass("light");
+                               (function(replyElement) {
+                                       replyElement.find("input.reply-input").blur(function() {
+                                               if ($(this).hasClass("default")) {
+                                                       replyElement.addClass("light");
+                                               }
+                                       }).focus(function() {
+                                               replyElement.removeClass("light");
+                                       });
+                               })(replyElement);
+                               textArea = replyElement.find("input.reply-input").focus().data("textarea");
+                               if (author != getCurrentSoneId()) {
+                                       textArea.val(textArea.val() + "@sone://" + author + " ");
+                               }
+                       });
+                       $(insertAfterThisElement).after(commentElement.clone(true));
+                       $(insertAfterThisElement).after(separator);
                });
-               return commentElement;
-       })(postId, author);
-       $(insertAfterThisElement).after(commentElement.clone(true));
-       $(insertAfterThisElement).after(separator);
+       })(postId, author, insertAfterThisElement);
 }
 
 var translations = {};
@@ -806,7 +807,7 @@ function ajaxifyPost(postElement) {
                        $(".expand-post-text", getPostElement(this)).toggleClass("hidden");
                        $(".shrink-post-text", getPostElement(this)).toggleClass("hidden");
                        return false;
-               })
+               });
        });
 
        /* ajaxify author/post links */
@@ -846,12 +847,14 @@ function ajaxifyPost(postElement) {
        });
 
        /* mark everything as known on click. */
-       $(postElement).click(function(event) {
-               if ($(event.target).hasClass("click-to-show")) {
-                       return false;
-               }
-               markPostAsKnown(this);
-       });
+       (function(postElement) {
+               $(postElement).click(function(event) {
+                       if ($(event.target).hasClass("click-to-show")) {
+                               return false;
+                       }
+                       markPostAsKnown(postElement, false);
+               });
+       })(postElement);
 
        /* hide reply input field. */
        $(postElement).find(".create-reply").addClass("hidden");
@@ -869,12 +872,12 @@ function ajaxifyPost(postElement) {
                        }).fadeIn();
                }, 1000);
        }).mouseleave(function() {
-               if (currentSoneMenuId = getPostId(this)) {
+               if (currentSoneMenuId == getPostId(this)) {
                        clearTimeout(currentSoneMenuTimeoutHandler);
                }
        });
        (function(postElement) {
-               var soneId = $(".sone-menu-id", postElement).text();
+               var soneId = $(".sone-menu-id:first", postElement).text();
                $(".sone-post-menu .follow", postElement).click(function() {
                        var followElement = this;
                        ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
@@ -1008,7 +1011,7 @@ function ajaxifyReply(replyElement) {
                        }).fadeIn();
                }, 1000);
        }).mouseleave(function() {
-               if (currentSoneMenuId = getPostId(this) + "-" + getReplyId(this)) {
+               if (currentSoneMenuId == getPostId(this) + "-" + getReplyId(this)) {
                        clearTimeout(currentSoneMenuTimeoutHandler);
                }
        });
@@ -1062,7 +1065,7 @@ function ajaxifyNotification(notification) {
        }
        notification.find("form.mark-as-read button").click(function() {
                allIds = $(":input[name=id]", this.form).val().split(" ");
-               for (index = 0; index < allIds.length; index += 16) {
+               for (var index = 0; index < allIds.length; index += 16) {
                        ids = allIds.slice(index, index + 16).join(" ");
                        ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": $(":input[name=type]", this.form).val(), "id": ids});
                }
@@ -1088,6 +1091,24 @@ function ajaxifyNotification(notification) {
 }
 
 /**
+ * Returns the notification hash. This hash is used in {@link #getStatus()} to
+ * determine whether the notifications changed and need to be reloaded.
+ */
+function getNotificationHash() {
+       return $("#sone #notification-area #notification-hash").text();
+}
+
+/**
+ * Sets the notification hash.
+ *
+ * @param notificationHash
+ *            The new notification hash
+ */
+function setNotificationHash(notificationHash) {
+       $("#sone #notification-area #notification-hash").text(notificationHash);
+}
+
+/**
  * Retrieves element IDs from notification elements.
  *
  * @param notification
@@ -1172,7 +1193,7 @@ function checkForRemovedReplies(oldNotification, newNotification) {
 }
 
 function getStatus() {
-       ajaxGet("getStatus.ajax", isViewSonePage() ? {"soneIds": getShownSoneId() } : {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
+       ajaxGet("getStatus.ajax", isViewSonePage() ? {"soneIds": getShownSoneId() } : isKnownSonesPage() ? {"soneIds": getShownSoneIds() } : {}, function(data, textStatus) {
                if ((data != null) && data.success) {
                        /* process Sone information. */
                        $.each(data.sones, function(index, value) {
@@ -1182,6 +1203,33 @@ function getStatus() {
                        if (!notLoggedIn) {
                                showOfflineMarker(!online);
                        }
+                       if (data.notificationHash != getNotificationHash()) {
+                               console.log("Old hash: ", getNotificationHash(), ", new hash: ", data.notificationHash);
+                               requestNotifications();
+                               /* process new posts. */
+                               $.each(data.newPosts, function(index, value) {
+                                       loadNewPost(value.id, value.sone, value.recipient, value.time);
+                               });
+                               /* 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() {
+               statusRequestQueued = false;
+               ajaxError();
+       });
+}
+
+function requestNotifications() {
+       ajaxGet("getNotifications.ajax", {}, function(data, textStatus) {
+               if (data && data.success) {
                        /* search for removed notifications. */
                        $("#sone #notification-area .notification").each(function() {
                                notificationId = $(this).attr("id");
@@ -1219,69 +1267,29 @@ function getStatus() {
                                }
                        });
                        /* process notifications. */
-                       notificationIds = [];
                        $.each(data.notifications, function(index, value) {
                                oldNotification = getNotification(value.id);
-                               if ((oldNotification.length == 0) || (value.lastUpdatedTime > getNotificationLastUpdatedTime(oldNotification))) {
-                                       notificationIds.push(value.id);
+                               notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide();
+                               if (oldNotification.length != 0) {
+                                       if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
+                                               opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
+                                               notification.find(".short-text").toggleClass("hidden", opened);
+                                               notification.find(".text").toggleClass("hidden", !opened);
+                                       }
+                                       checkForRemovedSones(oldNotification, notification);
+                                       checkForRemovedPosts(oldNotification, notification);
+                                       checkForRemovedReplies(oldNotification, notification);
+                                       oldNotification.replaceWith(notification.show());
+                               } else {
+                                       $("#sone #notification-area").append(notification);
+                                       if (value.id.substring(0, 5) != "local") {
+                                               notification.slideDown();
+                                               setActivity();
+                                       }
                                }
                        });
-                       if (notificationIds.length > 0) {
-                               loadNotifications(notificationIds);
-                       }
-                       /* process new posts. */
-                       $.each(data.newPosts, function(index, value) {
-                               loadNewPost(value.id, value.sone, value.recipient, value.time);
-                       });
-                       /* 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);
+                       setNotificationHash(data.notificationHash);
                }
-       }, function() {
-               statusRequestQueued = false;
-               ajaxError();
-       });
-}
-
-/**
- * Requests multiple notifications from Sone and displays them.
- *
- * @param notificationIds
- *            Array of IDs of the notifications to load
- */
-function loadNotifications(notificationIds) {
-       ajaxGet("getNotification.ajax", {"notifications": notificationIds.join(",")}, function(data, textStatus) {
-               if (!data || !data.success) {
-                       // TODO - show error
-                       return;
-               }
-               $.each(data.notifications, function(index, value) {
-                       oldNotification = getNotification(value.id);
-                       notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide();
-                       if (oldNotification.length != 0) {
-                               if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) {
-                                       opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden");
-                                       notification.find(".short-text").toggleClass("hidden", opened);
-                                       notification.find(".text").toggleClass("hidden", !opened);
-                               }
-                               checkForRemovedSones(oldNotification, notification);
-                               checkForRemovedPosts(oldNotification, notification);
-                               checkForRemovedReplies(oldNotification, notification);
-                               oldNotification.replaceWith(notification.show());
-                       } else {
-                               $("#sone #notification-area").append(notification);
-                               if (value.id.substring(0, 5) != "local") {
-                                       notification.slideDown();
-                                       setActivity();
-                               }
-                       }
-               });
        });
 }
 
@@ -1347,7 +1355,21 @@ function isViewSonePage() {
  * @returns The ID of the currently shown Sone
  */
 function getShownSoneId() {
-       return $("#sone .sone-id").text();
+       return $("#sone .sone-id").first().text();
+}
+
+/**
+ * Returns the ID of all currently visible Sones. This is mainly used on the
+ * “Known Sones” page.
+ *
+ * @returns The ID of the currently shown Sones
+ */
+function getShownSoneIds() {
+       var soneIds = new Array();
+       $("#sone #known-sones .sone .id").each(function() {
+               soneIds.push($(this).text());
+       });
+       return soneIds.join(",");
 }
 
 /**
@@ -1507,10 +1529,11 @@ function loadNewReply(replyId, soneId, postId, postSoneId) {
  *            request
  */
 function markSoneAsKnown(soneElement, skipRequest) {
-       if ($(soneElement).is(".new")) {
+       if ($(soneElement).hasClass("new")) {
                $(soneElement).removeClass("new");
                if ((typeof skipRequest == "undefined") || !skipRequest) {
                        ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)});
+                       requestNotifications();
                }
        }
 }
@@ -1523,12 +1546,13 @@ function markPostAsKnown(postElements, skipRequest) {
                                $(postElement).removeClass("new");
                                if ((typeof skipRequest == "undefined") || !skipRequest) {
                                        ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
+                                       requestNotifications();
                                }
                        })(postElement);
                }
                $(".click-to-show", postElement).removeClass("new");
        });
-       markReplyAsKnown($(postElements).find(".reply"));
+       markReplyAsKnown($(postElements).find(".reply"), true);
 }
 
 function markReplyAsKnown(replyElements, skipRequest) {
@@ -1539,6 +1563,7 @@ function markReplyAsKnown(replyElements, skipRequest) {
                                $(replyElement).removeClass("new");
                                if ((typeof skipRequest == "undefined") || !skipRequest) {
                                        ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
+                                       requestNotifications();
                                }
                        })(replyElement);
                }
@@ -1854,6 +1879,14 @@ var currentSoneMenuTimeoutHandler;
 
 $(document).ready(function() {
 
+       /* rip out the status update textarea. */
+       $("#sone .rip-out").each(function() {
+               var oldElement = $(this);
+               newElement = $("<input type='text'/>");
+               newElement.attr("class", oldElement.attr("class")).attr("name", oldElement.attr("name"));
+               oldElement.before(newElement).remove();
+       });
+
        /* this initializes the status update input field. */
        getTranslation("WebInterface.DefaultText.StatusUpdate", function(defaultText) {
                registerInputTextareaSwap("#sone #update-status .status-input", defaultText, "text", false, false);
@@ -1915,8 +1948,10 @@ $(document).ready(function() {
        getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
                getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
                        getTranslation("WebInterface.DefaultText.Reply", function(text) {
-                               $("#sone .post").each(function() {
-                                       ajaxifyPost(this);
+                               getTranslation("WebInterface.Button.Comment", function(text) {
+                                       $("#sone .post").each(function() {
+                                               ajaxifyPost(this);
+                                       });
                                });
                        });
                });
@@ -1936,7 +1971,7 @@ $(document).ready(function() {
                                allReplies = $(this).find(".reply");
                                if (allReplies.length > 2) {
                                        newHidden = false;
-                                       for (replyIndex = 0; !newHidden && (replyIndex < (allReplies.length - 2)); ++replyIndex) {
+                                       for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
                                                $(allReplies[replyIndex]).addClass("hidden");
                                                newHidden |= $(allReplies[replyIndex]).hasClass("new");
                                        }