--- a/src/pyams_notify/skin/resources/js/pyams_notify.js Fri Dec 14 20:07:50 2018 +0100
+++ b/src/pyams_notify/skin/resources/js/pyams_notify.js Thu Jun 20 12:56:04 2019 +0200
@@ -36,7 +36,7 @@
if (globals.console) {
globals.console.debug("WS notifications connection opened");
}
- MyAMS.ajax.post('get-notifications-context.json', {}, function(result) {
+ MyAMS.ajax && MyAMS.ajax.post('get-notifications-context.json', {}, function(result) {
if (result.principal.id !== '') {
PyAMS_notify.connection.send(JSON.stringify({
action: 'subscribe',
@@ -49,9 +49,20 @@
},
onSocketMessage: function(event) {
- var data = JSON.parse(event.data);
- PyAMS_notify.notifyOnDesktop(data);
- PyAMS_notify.notifyInWebpage(data);
+ var data = JSON.parse(event.data),
+ status = data.status;
+ if (status === 'error') {
+ if (globals.console) {
+ globals.console.error(event.data);
+ }
+ MyAMS.skin && MyAMS.skin.smallBox(status, {
+ title: data.error,
+ content: data.message
+ })
+ } else {
+ PyAMS_notify.notifyOnDesktop(data);
+ PyAMS_notify.notifyInWebpage(data);
+ }
},
onSocketError: function(event) {
@@ -128,7 +139,7 @@
badge.text(count + 1);
var notifications = $('.notification-body', '#user-activity');
PyAMS_notify.createNotification(data).prependTo(notifications);
- MyAMS.skin.checkNotification();
+ MyAMS.skin && MyAMS.skin.checkNotification();
},
showNotifications: function(data) {
@@ -155,21 +166,21 @@
var last_update = $('#activity-update');
last_update.text(last_update.data('ams-base-label').replace(/\{0\}/, timestamp));
/* check notifications badge */
- MyAMS.skin.checkNotification();
+ MyAMS.skin && MyAMS.skin.checkNotification();
},
refreshNotifications: function() {
return function() {
- MyAMS.ajax.post('get-user-notifications.json', {}, PyAMS_notify.showNotifications);
+ MyAMS.ajax && MyAMS.ajax.post('get-user-notifications.json', {}, PyAMS_notify.showNotifications);
};
}
};
globals.PyAMS_notify = PyAMS_notify;
- MyAMS.ajax.check($.WebSocket,
- '/--static--/pyams_notify/js/jquery-WebSocket' + MyAMS.devext + '.js',
- function() {
- PyAMS_notify.initConnection();
- });
+ MyAMS.ajax && MyAMS.ajax.check($.WebSocket,
+ '/--static--/pyams_notify/js/jquery-WebSocket' + MyAMS.devext + '.js',
+ function() {
+ PyAMS_notify.initConnection();
+ });
})(jQuery, this);
--- a/src/pyams_notify/skin/resources/js/pyams_notify.min.js Fri Dec 14 20:07:50 2018 +0100
+++ b/src/pyams_notify/skin/resources/js/pyams_notify.min.js Thu Jun 20 12:56:04 2019 +0200
@@ -1,1 +1,1 @@
-!function(t,n){"use strict";var o=n.MyAMS,i={connection:null,initConnection:function(){var n=t("[data-ams-notify-server]","#user-activity").data("ams-notify-server");if(n){var o="ws";"https:"===window.location.protocol&&(o+="s");var e=t.WebSocket(o+"://"+n+"/subscribe");e.onopen=i.onSocketOpened,e.onmessage=i.onSocketMessage,e.onerror=i.onSocketError,e.onclose=i.onSocketClosed,i.connection=e,setInterval(i.checkConnection,3e4)}},checkConnection:function(){null!==i.connection&&i.connection.readyState!==WebSocket.CLOSED||i.initConnection()},onSocketOpened:function(t){n.console&&n.console.debug("WS notifications connection opened"),o.ajax.post("get-notifications-context.json",{},function(t){""!==t.principal.id&&(i.connection.send(JSON.stringify({action:"subscribe",principal:t.principal,context:t.context})),o.ajax.post("get-user-notifications.json",{},i.showNotifications))})},onSocketMessage:function(t){var n=JSON.parse(t.data);i.notifyOnDesktop(n),i.notifyInWebpage(n)},onSocketError:function(t){n.console&&n.console.log(t)},onSocketClosed:function(t){i.connection=null,n.console&&n.console.debug("WS connection closed")},notifyOnDesktop:function(t){function n(){var n={title:t.title,body:t.message,icon:t.source.avatar};new Notification(n.title,n).onclick=function(){t.url&&window.open(t.url)}}window.Notification&&"denied"!==Notification.permission&&("default"===Notification.permission?Notification.requestPermission(function(t){"granted"===t&&n()}):n())},createNotification:function(n){var o=t("<li></li>"),i=t("<span></span>"),e=t("<a></a>").addClass("msg").attr("href",n.url);return n.source.avatar?t("<img>").addClass("air air-top-left margin-top-2").attr("src",n.source.avatar).appendTo(e):t("<i></i>").addClass("fa fa-2x fa-user air air-top-left img margin-left-5 margin-top-2").appendTo(e),t("<time></time>").text(n.timestamp).appendTo(e),t("<span></span>").addClass("from").text(n.source.title).appendTo(e),t("<span></span>").addClass("msg-body").text(n.message).appendTo(e),e.appendTo(i),i.appendTo(o),o},notifyInWebpage:function(n){var e=t(".badge","#user-activity >span"),a=parseInt(e.text());e.text(a+1);var c=t(".notification-body","#user-activity");i.createNotification(n).prependTo(c),o.skin.checkNotification()},showNotifications:function(n){var e=n.timestamp,a=n.notifications,c=t(".badge","#user-activity >span"),s=t(".notification-body","#user-activity"),r=t("li",s).length;if(s.empty(),a.length>0){s.prev("p").hide();for(var p=0;p<a.length;p++){var f=a[p];i.createNotification(f).appendTo(s)}var l=t("li",s).length;c.text(l-r)}else s.prev("p").show(),c.text("");var d=t("#activity-update");d.text(d.data("ams-base-label").replace(/\{0\}/,e)),o.skin.checkNotification()},refreshNotifications:function(){return function(){o.ajax.post("get-user-notifications.json",{},i.showNotifications)}}};n.PyAMS_notify=i,o.ajax.check(t.WebSocket,"/--static--/pyams_notify/js/jquery-WebSocket"+o.devext+".js",function(){i.initConnection()})}(jQuery,this);
+!function(t,n){"use strict";var o=n.MyAMS,e={connection:null,initConnection:function(){var n=t("[data-ams-notify-server]","#user-activity").data("ams-notify-server");if(n){var o="ws";"https:"===window.location.protocol&&(o+="s");var i=t.WebSocket(o+"://"+n+"/subscribe");i.onopen=e.onSocketOpened,i.onmessage=e.onSocketMessage,i.onerror=e.onSocketError,i.onclose=e.onSocketClosed,e.connection=i,setInterval(e.checkConnection,3e4)}},checkConnection:function(){null!==e.connection&&e.connection.readyState!==WebSocket.CLOSED||e.initConnection()},onSocketOpened:function(t){n.console&&n.console.debug("WS notifications connection opened"),o.ajax&&o.ajax.post("get-notifications-context.json",{},function(t){""!==t.principal.id&&(e.connection.send(JSON.stringify({action:"subscribe",principal:t.principal,context:t.context})),o.ajax.post("get-user-notifications.json",{},e.showNotifications))})},onSocketMessage:function(t){var i=JSON.parse(t.data),a=i.status;"error"===a?(n.console&&n.console.error(t.data),o.skin&&o.skin.smallBox(a,{title:i.error,content:i.message})):(e.notifyOnDesktop(i),e.notifyInWebpage(i))},onSocketError:function(t){n.console&&n.console.log(t)},onSocketClosed:function(t){e.connection=null,n.console&&n.console.debug("WS connection closed")},notifyOnDesktop:function(t){function n(){var n={title:t.title,body:t.message,icon:t.source.avatar};new Notification(n.title,n).onclick=function(){t.url&&window.open(t.url)}}window.Notification&&"denied"!==Notification.permission&&("default"===Notification.permission?Notification.requestPermission(function(t){"granted"===t&&n()}):n())},createNotification:function(n){var o=t("<li></li>"),e=t("<span></span>"),i=t("<a></a>").addClass("msg").attr("href",n.url);return n.source.avatar?t("<img>").addClass("air air-top-left margin-top-2").attr("src",n.source.avatar).appendTo(i):t("<i></i>").addClass("fa fa-2x fa-user air air-top-left img margin-left-5 margin-top-2").appendTo(i),t("<time></time>").text(n.timestamp).appendTo(i),t("<span></span>").addClass("from").text(n.source.title).appendTo(i),t("<span></span>").addClass("msg-body").text(n.message).appendTo(i),i.appendTo(e),e.appendTo(o),o},notifyInWebpage:function(n){var i=t(".badge","#user-activity >span"),a=parseInt(i.text());i.text(a+1);var c=t(".notification-body","#user-activity");e.createNotification(n).prependTo(c),o.skin&&o.skin.checkNotification()},showNotifications:function(n){var i=n.timestamp,a=n.notifications,c=t(".badge","#user-activity >span"),s=t(".notification-body","#user-activity"),r=t("li",s).length;if(s.empty(),a.length>0){s.prev("p").hide();for(var p=0;p<a.length;p++){var f=a[p];e.createNotification(f).appendTo(s)}var l=t("li",s).length;c.text(l-r)}else s.prev("p").show(),c.text("");var d=t("#activity-update");d.text(d.data("ams-base-label").replace(/\{0\}/,i)),o.skin&&o.skin.checkNotification()},refreshNotifications:function(){return function(){o.ajax&&o.ajax.post("get-user-notifications.json",{},e.showNotifications)}}};n.PyAMS_notify=e,o.ajax&&o.ajax.check(t.WebSocket,"/--static--/pyams_notify/js/jquery-WebSocket"+o.devext+".js",function(){e.initConnection()})}(jQuery,this);