# HG changeset patch # User Thierry Florac # Date 1499854303 -7200 # Node ID 1c9f8ee7c072386b15a6f473561631a637c2ed86 # Parent c05c7737c8da1d3ea01d71f5026578ae53bcab5a Changed notifications API diff -r c05c7737c8da -r 1c9f8ee7c072 src/pyams_notify/skin/resources/js/pyams_notify.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(); }, diff -r c05c7737c8da -r 1c9f8ee7c072 src/pyams_notify/skin/resources/js/pyams_notify.min.js --- 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("
  • ");var f=c("");var g=c("").addClass("msg").attr("href",h.url);if(h.source.avatar){c("").addClass("air air-top-left margin-top-2").attr("src",h.source.avatar).appendTo(g)}else{c("").addClass("fa fa-2x fa-user air air-top-left img margin-left-5 margin-top-2").appendTo(g)}c("").text(h.timestamp).appendTo(g);c("").addClass("from").text(h.source.title).appendTo(g);c("").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");var f=c("");var g=c("").addClass("msg").attr("href",h.url);if(h.source.avatar){c("").addClass("air air-top-left margin-top-2").attr("src",h.source.avatar).appendTo(g)}else{c("").addClass("fa fa-2x fa-user air air-top-left img margin-left-5 margin-top-2").appendTo(g)}c("").text(h.timestamp).appendTo(g);c("").addClass("from").text(h.source.title).appendTo(g);c("").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