Merge branch 'release-0.9.7'
[Sone.git] / src / main / resources / static / javascript / sone.js
index 030f9be..84be0d4 100644 (file)
@@ -145,7 +145,7 @@ function filterSoneId(soneId) {
  *            The date and time of the last update (formatted for display)
  */
 function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated, lastUpdatedText) {
-       var updateSone = sone.find(".sone." + filterSoneId(soneId));
+    var updateSone = sone.find(".sone." + filterSoneId(soneId));
        updateSone.toggleClass("unknown", status == "unknown").
                toggleClass("idle", status == "idle").
                toggleClass("inserting", status == "inserting").
@@ -718,6 +718,7 @@ function ajaxifyPost(postElement) {
                                        sone.find(".post#post-" + postId + " .create-reply .sender").hide();
                                        sone.find(".post#post-" + postId + " .create-reply .select-sender").show();
                                        sone.find(".post#post-" + postId + " .create-reply :input[name=sender]").val(getCurrentSoneId());
+                                       updateReplyTimes(replyId);
                                } else {
                                        alert(error);
                                }
@@ -1193,7 +1194,13 @@ function checkForRemovedReplies(oldNotification, newNotification) {
 }
 
 function getStatus() {
-       ajaxGet("getStatus.ajax", isViewSonePage() ? {"soneIds": getShownSoneId() } : isKnownSonesPage() ? {"soneIds": getShownSoneIds() } : {}, function(data, textStatus) {
+       var parameters = isViewSonePage() ? {"soneIds": getShownSoneId() } : isKnownSonesPage() ? {"soneIds": getShownSoneIds() } : {};
+       $.extend(parameters, {
+               "elements": JSON.stringify($(".linked-element.not-loaded").map(function () {
+                       return $(this).attr("title");
+               }).toArray())
+       });
+       ajaxGet("getStatus.ajax", parameters, function(data, textStatus) {
                if ((data != null) && data.success) {
                        /* process Sone information. */
                        $.each(data.sones, function(index, value) {
@@ -1215,6 +1222,9 @@ function getStatus() {
                                        loadNewReply(value.id, value.sone, value.post, value.postSone);
                                });
                        }
+                       if (data.linkedElements) {
+                               loadLinkedElements(data.linkedElements)
+                       }
                        /* do it again in 5 seconds. */
                        setTimeout(getStatus, 5000);
                } else {
@@ -1365,7 +1375,7 @@ function getShownSoneId() {
  * @returns The ID of the currently shown Sones
  */
 function getShownSoneIds() {
-       var soneIds = new Array();
+       var soneIds = [];
        sone.find("#known-sones .sone .id").each(function() {
                soneIds.push($(this).text());
        });
@@ -1519,6 +1529,41 @@ function loadNewReply(replyId, soneId, postId, postSoneId) {
        });
 }
 
+function loadLinkedElements(links) {
+       var failedElements = links.filter(function(element) {
+               return element.failed;
+       });
+       if (failedElements.length > 0) {
+               failedElements.forEach(function(element) {
+                       getLinkedElements(element.link).each(function() {
+                               $(this).remove()
+                       });
+               });
+       }
+       var loadedElements = links.filter(function(element) {
+               return !element.loading && !element.failed;
+       });
+       if (loadedElements.length > 0) {
+               ajaxGet("getLinkedElement.ajax", {
+                       "elements": JSON.stringify(loadedElements.map(function(element) {
+                               return element.link;
+                       }))
+               }, function (data, textStatus) {
+                       if ((data != null) && (data.success)) {
+                               data.linkedElements.forEach(function (linkedElement) {
+                                       getLinkedElements(linkedElement.link).each(function() {
+                                               $(this).replaceWith(linkedElement.html);
+                                       });
+                               });
+                       }
+               });
+       }
+}
+
+function getLinkedElements(link) {
+       return $(".linked-element[title='" + link + "']")
+}
+
 /**
  * Marks the given Sone as known if it is still new.
  *
@@ -1601,13 +1646,15 @@ function updatePostTime(postId, timeText, refreshTime, tooltip) {
  *            Comma-separated post IDs
  */
 function updatePostTimes(postIds) {
-       ajaxGet("getTimes.ajax", { "posts" : postIds }, function(data, textStatus) {
-               if ((data != null) && data.success) {
-                       $.each(data.postTimes, function(index, value) {
-                               updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
-                       });
-               }
-       });
+       if (postIds != "") {
+        ajaxGet("getTimes.ajax", {"posts": postIds}, function (data, textStatus) {
+            if ((data != null) && data.success) {
+                $.each(data.postTimes, function (index, value) {
+                    updatePostTime(index, value.timeText, value.refreshTime, value.tooltip);
+                });
+            }
+        });
+    }
 }
 
 /**
@@ -1638,13 +1685,15 @@ function updateReplyTime(replyId, timeText, refreshTime, tooltip) {
  *            Comma-separated post IDs
  */
 function updateReplyTimes(replyIds) {
-       ajaxGet("getTimes.ajax", { "replies" : replyIds }, function(data, textStatus) {
-               if ((data != null) && data.success) {
-                       $.each(data.replyTimes, function(index, value) {
-                               updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
-                       });
-               }
-       });
+       if (replyIds != "") {
+        ajaxGet("getTimes.ajax", {"replies": replyIds}, function (data, textStatus) {
+            if ((data != null) && data.success) {
+                $.each(data.replyTimes, function (index, value) {
+                    updateReplyTime(index, value.timeText, value.refreshTime, value.tooltip);
+                });
+            }
+        });
+    }
 }
 
 function resetActivity() {
@@ -1946,11 +1995,11 @@ $(document).ready(function() {
 
        /* Ajaxifies all posts. */
        /* calling getTranslation here will cache the necessary values. */
-       getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {
-               getTranslation("WebInterface.Confirmation.DeleteReplyButton", function(text) {
-                       getTranslation("WebInterface.DefaultText.Reply", function(text) {
-                               getTranslation("WebInterface.Button.Comment", function(text) {
-                                       sone.find(".post").each(function() {
+       getTranslation("WebInterface.Confirmation.DeletePostButton", function() {
+               getTranslation("WebInterface.Confirmation.DeleteReplyButton", function() {
+                       getTranslation("WebInterface.DefaultText.Reply", function() {
+                getTranslation("WebInterface.Button.Comment", function () {
+                    sone.find(".post").each(function() {
                                                ajaxifyPost(this);
                                        });
                                });