Changed notifications API
authorThierry Florac <thierry.florac@onf.fr>
Wed, 12 Jul 2017 12:11:43 +0200
changeset 14 1c9f8ee7c072
parent 13 c05c7737c8da
child 15 eb863d6bc5a9
Changed notifications API
src/pyams_notify/skin/resources/js/pyams_notify.js
src/pyams_notify/skin/resources/js/pyams_notify.min.js
--- a/src/pyams_notify/skin/resources/js/pyams_notify.js	Wed Jul 12 12:11:25 2017 +0200
+++ b/src/pyams_notify/skin/resources/js/pyams_notify.js	Wed Jul 12 12:11:43 2017 +0200
@@ -128,19 +128,29 @@
 		},
 
 		showNotifications: function(data) {
+			var timestamp = data.timestamp,
+				notifications_data = data.notifications;
+			/* create notifications */
 			var badge = $('.badge', '#user-activity >span');
-			badge.text(data.length);
 			var notifications = $('.notification-body', '#user-activity');
+			var old_length = $('li', notifications).length;
 			notifications.empty();
-			if (data.length > 0) {
+			if (notifications_data.length > 0) {
 				notifications.prev('p').hide();
-				for (var index = 0; index < data.length; index++) {
-					var notification = data[index];
+				for (var index = 0; index < notifications_data.length; index++) {
+					var notification = notifications_data[index];
 					PyAMS_notify.createNotification(notification).appendTo(notifications);
 				}
+				var new_length = $('li', notifications).length;
+				badge.text(new_length - old_length);
 			} else {
 				notifications.prev('p').show();
+				badge.text('');
 			}
+			/* update timestamp */
+			var last_update = $('#activity-update');
+			last_update.text(last_update.data('ams-base-label').replace(/\{0\}/, timestamp));
+			/* check notifications badge */
 			MyAMS.skin.checkNotification();
 		},
 
--- a/src/pyams_notify/skin/resources/js/pyams_notify.min.js	Wed Jul 12 12:11:25 2017 +0200
+++ b/src/pyams_notify/skin/resources/js/pyams_notify.min.js	Wed Jul 12 12:11:43 2017 +0200
@@ -1,1 +1,1 @@
-(function(c,b){var d=b.MyAMS;var a={connection:null,initConnection:function(){var f=c("[data-ams-notify-server]","#user-activity").data("ams-notify-server");if(f){var e=c.WebSocket("ws://"+f+"/subscribe");e.onopen=a.onSocketOpened;e.onmessage=a.onSocketMessage;e.onerror=a.onSocketError;e.onclose=a.onSocketClosed;a.connection=e;setInterval(a.checkConnection,30000)}},checkConnection:function(){if((a.connection===null)||(a.connection.readyState===WebSocket.CLOSED)){a.initConnection()}},onSocketOpened:function(e){if(b.console){b.console.debug("WS notifications connection opened")}d.ajax.post("get-notifications-context.json",{},function(f){if(f.principal.id!==""){a.connection.send(JSON.stringify({action:"subscribe",principal:f.principal,context:f.context}));d.ajax.post("get-user-notifications.json",{},a.showNotifications)}})},onSocketMessage:function(e){var f=JSON.parse(e.data);a.notifyOnDesktop(f);a.notifyInWebpage(f)},onSocketError:function(e){if(b.console){b.console.log(e)}},onSocketClosed:function(e){a.connection=null;if(b.console){b.console.debug("WS connection closed")}},notifyOnDesktop:function(e){function f(){var g={title:e.title,body:e.message,icon:e.source.avatar};var h=new Notification(g.title,g);h.onclick=function(){if(e.url){window.open(e.url)}}}if(window.Notification&&(Notification.permission!=="denied")){if(Notification.permission==="default"){Notification.requestPermission(function(g){if(g==="granted"){f()}})}else{f()}}},createNotification:function(h){var e=c("<li></li>");var f=c("<span></span>");var g=c("<a></a>").addClass("msg").attr("href",h.url);if(h.source.avatar){c("<img>").addClass("air air-top-left margin-top-2").attr("src",h.source.avatar).appendTo(g)}else{c("<i></i>").addClass("fa fa-2x fa-user air air-top-left img margin-left-5 margin-top-2").appendTo(g)}c("<time></time>").text(h.timestamp).appendTo(g);c("<span></span>").addClass("from").text(h.source.title).appendTo(g);c("<span></span>").addClass("msg-body").text(h.message).appendTo(g);g.appendTo(f);f.appendTo(e);return e},notifyInWebpage:function(h){var e=c(".badge","#user-activity >span");var g=parseInt(e.text());e.text(g+1);var f=c(".notification-body","#user-activity");a.createNotification(h).prependTo(f);d.skin.checkNotification()},showNotifications:function(h){var e=c(".badge","#user-activity >span");e.text(h.length);var g=c(".notification-body","#user-activity");g.empty();if(h.length>0){g.prev("p").hide();for(var f=0;f<h.length;f++){var i=h[f];a.createNotification(i).appendTo(g)}}else{g.prev("p").show()}d.skin.checkNotification()},refreshNotifications:function(){return function(){d.ajax.post("get-user-notifications.json",{},a.showNotifications)}}};b.PyAMS_notify=a;d.ajax.check(c.WebSocket,"/--static--/pyams_notify/js/jquery-WebSocket"+d.devext+".js",function(){a.initConnection()})})(jQuery,this);
\ No newline at end of file
+(function(c,b){var d=b.MyAMS;var a={connection:null,initConnection:function(){var f=c("[data-ams-notify-server]","#user-activity").data("ams-notify-server");if(f){var e=c.WebSocket("ws://"+f+"/subscribe");e.onopen=a.onSocketOpened;e.onmessage=a.onSocketMessage;e.onerror=a.onSocketError;e.onclose=a.onSocketClosed;a.connection=e;setInterval(a.checkConnection,30000)}},checkConnection:function(){if((a.connection===null)||(a.connection.readyState===WebSocket.CLOSED)){a.initConnection()}},onSocketOpened:function(e){if(b.console){b.console.debug("WS notifications connection opened")}d.ajax.post("get-notifications-context.json",{},function(f){if(f.principal.id!==""){a.connection.send(JSON.stringify({action:"subscribe",principal:f.principal,context:f.context}));d.ajax.post("get-user-notifications.json",{},a.showNotifications)}})},onSocketMessage:function(e){var f=JSON.parse(e.data);a.notifyOnDesktop(f);a.notifyInWebpage(f)},onSocketError:function(e){if(b.console){b.console.log(e)}},onSocketClosed:function(e){a.connection=null;if(b.console){b.console.debug("WS connection closed")}},notifyOnDesktop:function(e){function f(){var g={title:e.title,body:e.message,icon:e.source.avatar};var h=new Notification(g.title,g);h.onclick=function(){if(e.url){window.open(e.url)}}}if(window.Notification&&(Notification.permission!=="denied")){if(Notification.permission==="default"){Notification.requestPermission(function(g){if(g==="granted"){f()}})}else{f()}}},createNotification:function(h){var e=c("<li></li>");var f=c("<span></span>");var g=c("<a></a>").addClass("msg").attr("href",h.url);if(h.source.avatar){c("<img>").addClass("air air-top-left margin-top-2").attr("src",h.source.avatar).appendTo(g)}else{c("<i></i>").addClass("fa fa-2x fa-user air air-top-left img margin-left-5 margin-top-2").appendTo(g)}c("<time></time>").text(h.timestamp).appendTo(g);c("<span></span>").addClass("from").text(h.source.title).appendTo(g);c("<span></span>").addClass("msg-body").text(h.message).appendTo(g);g.appendTo(f);f.appendTo(e);return e},notifyInWebpage:function(h){var e=c(".badge","#user-activity >span");var g=parseInt(e.text());e.text(g+1);var f=c(".notification-body","#user-activity");a.createNotification(h).prependTo(f);d.skin.checkNotification()},showNotifications:function(h){var j=h.timestamp,l=h.notifications;var k=c(".badge","#user-activity >span");var m=c(".notification-body","#user-activity");var n=c("li",m).length;m.empty();if(l.length>0){m.prev("p").hide();for(var i=0;i<l.length;i++){var e=l[i];a.createNotification(e).appendTo(m)}var f=c("li",m).length;k.text(f-n)}else{m.prev("p").show();k.text("")}var g=c("#activity-update");g.text(g.data("ams-base-label").replace(/\{0\}/,j));d.skin.checkNotification()},refreshNotifications:function(){return function(){d.ajax.post("get-user-notifications.json",{},a.showNotifications)}}};b.PyAMS_notify=a;d.ajax.check(c.WebSocket,"/--static--/pyams_notify/js/jquery-WebSocket"+d.devext+".js",function(){a.initConnection()})})(jQuery,this);
\ No newline at end of file