Hide all removed notifications.
[Sone.git] / src / main / resources / static / javascript / sone.js
index 361fc53..20386f7 100644 (file)
@@ -100,17 +100,20 @@ function getTranslation(key, callback) {
  *
  * @param soneId
  *            The ID of the Sone
+ * @param local
+ *            <code>true</code> if the Sone is local, <code>false</code>
+ *            otherwise
  */
-function getSoneStatus(soneId) {
+function getSoneStatus(soneId, local) {
        $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
                updateSoneStatus(soneId, data.name, data.status, data.modified, data.lastUpdated);
                /* seconds! */
                updateInterval = 60;
-               if (data.modified || (data.status == "downloading") || (data.status == "inserting")) {
+               if (local || data.modified || (data.status == "downloading") || (data.status == "inserting")) {
                        updateInterval = 5;
                }
                setTimeout(function() {
-                       getSoneStatus(soneId);
+                       getSoneStatus(soneId, local);
                }, updateInterval * 1000);
        });
 }
@@ -157,15 +160,18 @@ var watchedSones = {};
  *
  * @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) {
+function watchSone(soneId, local) {
        if (watchedSones[soneId]) {
                return;
        }
        watchedSones[soneId] = true;
        (function(soneId) {
                setTimeout(function() {
-                       getSoneStatus(soneId);
+                       getSoneStatus(soneId, local);
                }, 5000);
        })(soneId);
 }
@@ -401,3 +407,68 @@ function getReply(replyId, callbackFunction) {
                }
        });
 }
+
+/**
+ * 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("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
+                       notification.slideUp();
+               });
+       });
+       return notification;
+}
+
+/**
+ * Retrieves all changed notifications.
+ */
+function getNotifications() {
+       $.getJSON("ajax/getNotifications.ajax", {}, function(data, textStatus) {
+               if (data.success) {
+                       $.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);
+                               } else {
+                                       $("#sone #notification-area").append(notification);
+                               }
+                               notification.slideDown();
+                       });
+                       $.each(data.removedNotifications, function(index, value) {
+                               $("#sone #notification-area .notification#" + value.id).slideUp();
+                       });
+               }
+               setTimeout(getNotifications, 5000);
+       });
+}
+
+/**
+ * Creates a new notification.
+ *
+ * @param id
+ *            The ID of the notificaiton
+ * @param text
+ *            The text of the notification
+ * @param dismissable
+ *            <code>true</code> if the notification can be dismissed by the
+ *            user
+ */
+function createNotification(id, text, dismissable) {
+       notification = $("<div></div>").addClass("notification").attr("id", id);
+       if (dismissable) {
+               dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id")
+               dismissForm.find("input[name=notification]").val(id);
+               notification.append(dismissForm);
+       }
+       notification.append(text);
+       return notification;
+}