src/pyams_notify/skin/resources/js/pyams_notify.js
changeset 52 6d8e4d38f94d
parent 43 19e28ae921da
child 55 15d11232b0dc
--- 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);