Add methods to retrieve new notification and create their HTML.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 15 Nov 2010 05:34:42 +0000 (06:34 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 15 Nov 2010 05:34:42 +0000 (06:34 +0100)
src/main/resources/static/javascript/sone.js

index 8c6db31..5c3415b 100644 (file)
@@ -425,3 +425,47 @@ function ajaxifyNotification(notification) {
        });
        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();
+                       });
+               }
+               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;
+}