Use a comma to separate the identities in the tooltips.
[Sone.git] / src / main / resources / static / javascript / sone.js
index 310e422..149520f 100644 (file)
@@ -99,6 +99,8 @@ function addCommentLink(postId, element) {
        });
 }
 
+var translations = {};
+
 /**
  * Retrieves the translation for the given key and calls the callback function.
  * The callback function takes a single parameter, the translated string.
@@ -109,37 +111,15 @@ function addCommentLink(postId, element) {
  *            The callback function
  */
 function getTranslation(key, callback) {
+       if (key in translations) {
+               callback(translations[key]);
+               return;
+       }
        $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
-               if (data != null) {
-                       callback(data.value);
-               }
-       }, function(xmlHttpRequest, textStatus, error) {
-               /* ignore error. */
-       });
-}
-
-/**
- * Fires off an AJAX request to retrieve the current status of a Sone.
- *
- * @param soneId
- *            The ID of the Sone
- * @param local
- *            <code>true</code> if the Sone is local, <code>false</code>
- *            otherwise
- */
-function getSoneStatus(soneId, local) {
-       $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
                if ((data != null) && data.success) {
-                       updateSoneStatus(soneId, data.name, data.status, data.modified, data.lastUpdated);
-               }
-               /* seconds! */
-               updateInterval = 60;
-               if (local || (data!= null) && (data.modified || (data.status == "downloading") || (data.status == "inserting"))) {
-                       updateInterval = 5;
+                       translations[key] = data.value;
+                       callback(data.value);
                }
-               setTimeout(function() {
-                       getSoneStatus(soneId, local);
-               }, updateInterval * 1000);
        }, function(xmlHttpRequest, textStatus, error) {
                /* ignore error. */
        });
@@ -166,43 +146,24 @@ function filterSoneId(soneId) {
  *            “downloading”)
  * @param modified
  *            Whether the Sone is modified
+ * @param locked
+ *            Whether the Sone is locked
  * @param lastUpdated
  *            The date and time of the last update (formatted for display)
  */
-function updateSoneStatus(soneId, name, status, modified, lastUpdated) {
+function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
        $("#sone .sone." + filterSoneId(soneId)).
                toggleClass("unknown", status == "unknown").
                toggleClass("idle", status == "idle").
                toggleClass("inserting", status == "inserting").
                toggleClass("downloading", status == "downloading").
                toggleClass("modified", modified);
+       $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
+       $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
        $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
        $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
 }
 
-var watchedSones = {};
-
-/**
- * Watches this Sone for updates to its status.
- *
- * @param soneId
- *            The ID of the Sone to watch
- * @param local
- *            <code>true</code> if the Sone is local, <code>false</code>
- *            otherwise
- */
-function watchSone(soneId, local) {
-       if (watchedSones[soneId]) {
-               return;
-       }
-       watchedSones[soneId] = true;
-       (function(soneId) {
-               setTimeout(function() {
-                       getSoneStatus(soneId, local);
-               }, 5000);
-       })(soneId);
-}
-
 /**
  * Enhances a “delete” button so that the confirmation is done on the same page.
  *
@@ -321,7 +282,7 @@ function generateSoneList(sones) {
        var soneList = "";
        $.each(sones, function() {
                if (soneList != "") {
-                       soneList += "\n";
+                       soneList += "";
                }
                soneList += this.name;
        });
@@ -498,33 +459,37 @@ function ajaxifyNotification(notification) {
        return notification;
 }
 
-/**
- * Retrieves all changed notifications.
- */
-function getNotifications() {
-       $.getJSON("ajax/getNotifications.ajax", {}, function(data, textStatus) {
+function getStatus() {
+       $.getJSON("ajax/getStatus.ajax", {}, 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.slideUp();
-                                       notification.insertBefore(oldNotification);
+                                       oldNotification.replaceWith(notification.show());
                                } else {
                                        $("#sone #notification-area").append(notification);
+                                       notification.slideDown();
                                }
-                               notification.slideDown();
                        });
                        $.each(data.removedNotifications, function(index, value) {
                                $("#sone #notification-area .notification#" + value.id).slideUp();
                        });
-                       setTimeout(getNotifications, 5000);
+                       /* do it again in 5 seconds. */
+                       setTimeout(getStatus, 5000);
                } else {
-                       setTimeout(getNotifications, 30000);
+                       /* data.success was false, wait 30 seconds. */
+                       setTimeout(getStatus, 30000);
                }
        }, function(xmlHttpRequest, textStatus, error) {
-               /* ignore error. */
-       });
+               /* something really bad happend, wait a minute. */
+               setTimeout(getStatus, 60000);
+       })
 }
 
 /**
@@ -548,3 +513,166 @@ function createNotification(id, text, dismissable) {
        notification.append(text);
        return notification;
 }
+
+//
+// EVERYTHING BELOW HERE IS EXECUTED AFTER LOADING THE PAGE
+//
+
+$(document).ready(function() {
+
+       /* this initializes the status update input field. */
+       getTranslation("WebInterface.DefaultText.StatusUpdate", function(text) {
+               registerInputTextareaSwap("#sone #update-status .status-input", text, "text", false, false);
+       })
+
+       /* this initializes all reply input fields. */
+       getTranslation("WebInterface.DefaultText.Reply", function(text) {
+               registerInputTextareaSwap("#sone input.reply-input", text, "text", false, false);
+               addCommentLinks();
+       })
+
+       /* replaces all “post reply!” forms with AJAX. */
+       $("#sone .create-reply button:submit").click(function() {
+               $(this.form).submit(function() {
+                       return false;
+               });
+               inputField = $(this.form).find(":input:enabled").get(0);
+               postId = getPostId($(inputField));
+               text = $(inputField).val();
+               $(inputField).val("");
+               postReply(postId, text, function(success, error, replyId) {
+                       if (success) {
+                               getReply(replyId, function(soneId, soneName, replyTime, replyDisplayTime, text, html) {
+                                       newReply = $(html).insertBefore("#sone .post#" + postId + " .create-reply");
+                                       $("#sone .post#" + postId + " .create-reply").addClass("hidden");
+                                       getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(deleteReplyText) {
+                                               enhanceDeleteReplyButton("#sone .post#" + postId + " .reply#" + replyId + " .delete button", replyId, deleteReplyText);
+                                       });
+                                       newReply.find(".status-line .like").submit(function() {
+                                               likeReply(getReplyId(this));
+                                               return false;
+                                       });
+                                       newReply.find(".status-line .unlike").submit(function() {
+                                               unlikeReply(getReplyId(this));
+                                               return false;
+                                       });
+                                       addCommentLink(postId, newReply);
+                               });
+                       } else {
+                               alert(error);
+                       }
+               });
+               return false;
+       });
+
+       /* replace all “delete” buttons with javascript. */
+       getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
+               deletePostText = text;
+               getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
+                       deleteReplyText = text;
+                       $("#sone .post").each(function() {
+                               postId = $(this).attr("id");
+                               enhanceDeletePostButton("#sone .post#" + postId + " > .inner-part > .status-line .delete button", postId, deletePostText);
+                               (function(postId) {
+                                       $("#sone .post#" + postId + " .reply").each(function() {
+                                               replyId = $(this).attr("id");
+                                               (function(postId, reply, replyId) {
+                                                       reply.find(".delete button").each(function() {
+                                                               enhanceDeleteReplyButton("#sone .post#" + postId + " .reply#" + replyId + " .delete button", replyId, deleteReplyText);
+                                                       })
+                                               })(postId, $(this), replyId);
+                                       });
+                               })(postId);
+                       });
+               });
+       });
+
+       /* hides all replies but the latest two. */
+       getTranslation("WebInterface.ClickToShow.Replies", function(text) {
+               $("#sone .post .replies").each(function() {
+                       allReplies = $(this).find(".reply");
+                       if (allReplies.length > 2) {
+                               newHidden = false;
+                               for (replyIndex = 0; replyIndex < (allReplies.length - 2); ++replyIndex) {
+                                       $(allReplies[replyIndex]).addClass("hidden");
+                                       newHidden |= $(allReplies[replyIndex]).hasClass("new");
+                               }
+                               clickToShowElement = $("<div></div>").addClass("click-to-show");
+                               if (newHidden) {
+                                       clickToShowElement.addClass("new");
+                               }
+                               (function(clickToShowElement, allReplies, text) {
+                                       clickToShowElement.text(text);
+                                       clickToShowElement.click(function() {
+                                               allReplies.removeClass("hidden");
+                                               clickToShowElement.addClass("hidden");
+                                       });
+                               })(clickToShowElement, allReplies, text);
+                               $(allReplies[0]).before(clickToShowElement);
+                       }
+               });
+       });
+
+       /*
+        * convert all “follow”, “unfollow”, “lock”, and “unlock” links to something
+        * nicer.
+        */
+       $("#sone .follow").submit(function() {
+               var followElement = this;
+               $.getJSON("ajax/followSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
+                       $(followElement).addClass("hidden");
+                       $(followElement).parent().find(".unfollow").removeClass("hidden");
+               });
+               return false;
+       });
+       $("#sone .unfollow").submit(function() {
+               var unfollowElement = this;
+               $.getJSON("ajax/unfollowSone.ajax", { "sone": getSoneId(this), "formPassword": getFormPassword() }, function() {
+                       $(unfollowElement).addClass("hidden");
+                       $(unfollowElement).parent().find(".follow").removeClass("hidden");
+               });
+               return false;
+       });
+       $("#sone .lock").submit(function() {
+               var lockElement = this;
+               $.getJSON("ajax/lockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
+                       $(lockElement).addClass("hidden");
+                       $(lockElement).parent().find(".unlock").removeClass("hidden");
+               });
+               return false;
+       });
+       $("#sone .unlock").submit(function() {
+               var unlockElement = this;
+               $.getJSON("ajax/unlockSone.ajax", { "sone" : getSoneId(this), "formPassword" : getFormPassword() }, function() {
+                       $(unlockElement).addClass("hidden");
+                       $(unlockElement).parent().find(".lock").removeClass("hidden");
+               });
+               return false;
+       });
+
+       /* convert all “like” buttons to javascript functions. */
+       $("#sone .post > .inner-part > .status-line .like").submit(function() {
+               likePost(getPostId(this));
+               return false;
+       });
+       $("#sone .post > .inner-part > .status-line .unlike").submit(function() {
+               unlikePost(getPostId(this));
+               return false;
+       });
+       $("#sone .post .reply .status-line .like").submit(function() {
+               likeReply(getReplyId(this));
+               return false;
+       });
+       $("#sone .post .reply .status-line .unlike").submit(function() {
+               unlikeReply(getReplyId(this));
+               return false;
+       });
+
+       /* process all existing notifications, ajaxify dismiss buttons. */
+       $("#sone #notification-area .notification").each(function() {
+               ajaxifyNotification($(this));
+       });
+
+       /* activate status polling. */
+       setTimeout(getStatus, 5000);
+});