src/pyams_notify/skin/resources/js/pyams_notify.js
changeset 52 6d8e4d38f94d
parent 43 19e28ae921da
child 55 15d11232b0dc
equal deleted inserted replaced
51:7f0a34ddeb4b 52:6d8e4d38f94d
    34 
    34 
    35 		onSocketOpened: function(event) {
    35 		onSocketOpened: function(event) {
    36 			if (globals.console) {
    36 			if (globals.console) {
    37 				globals.console.debug("WS notifications connection opened");
    37 				globals.console.debug("WS notifications connection opened");
    38 			}
    38 			}
    39 			MyAMS.ajax.post('get-notifications-context.json', {}, function(result) {
    39 			MyAMS.ajax && MyAMS.ajax.post('get-notifications-context.json', {}, function(result) {
    40 				if (result.principal.id !== '') {
    40 				if (result.principal.id !== '') {
    41 					PyAMS_notify.connection.send(JSON.stringify({
    41 					PyAMS_notify.connection.send(JSON.stringify({
    42 						action: 'subscribe',
    42 						action: 'subscribe',
    43 						principal: result.principal,
    43 						principal: result.principal,
    44 						context: result.context
    44 						context: result.context
    47 				}
    47 				}
    48 			});
    48 			});
    49 		},
    49 		},
    50 
    50 
    51 		onSocketMessage: function(event) {
    51 		onSocketMessage: function(event) {
    52 			var data = JSON.parse(event.data);
    52 			var data = JSON.parse(event.data),
    53 			PyAMS_notify.notifyOnDesktop(data);
    53 				status = data.status;
    54 			PyAMS_notify.notifyInWebpage(data);
    54 			if (status === 'error') {
       
    55 				if (globals.console) {
       
    56 					globals.console.error(event.data);
       
    57 				}
       
    58 				MyAMS.skin && MyAMS.skin.smallBox(status, {
       
    59 					title: data.error,
       
    60 					content: data.message
       
    61 				})
       
    62 			} else {
       
    63 				PyAMS_notify.notifyOnDesktop(data);
       
    64 				PyAMS_notify.notifyInWebpage(data);
       
    65 			}
    55 		},
    66 		},
    56 
    67 
    57 		onSocketError: function(event) {
    68 		onSocketError: function(event) {
    58 			if (globals.console) {
    69 			if (globals.console) {
    59 				globals.console.log(event);
    70 				globals.console.log(event);
   126 			var badge = $('.badge', '#user-activity >span');
   137 			var badge = $('.badge', '#user-activity >span');
   127 			var count = parseInt(badge.text());
   138 			var count = parseInt(badge.text());
   128 			badge.text(count + 1);
   139 			badge.text(count + 1);
   129 			var notifications = $('.notification-body', '#user-activity');
   140 			var notifications = $('.notification-body', '#user-activity');
   130 			PyAMS_notify.createNotification(data).prependTo(notifications);
   141 			PyAMS_notify.createNotification(data).prependTo(notifications);
   131 			MyAMS.skin.checkNotification();
   142 			MyAMS.skin && MyAMS.skin.checkNotification();
   132 		},
   143 		},
   133 
   144 
   134 		showNotifications: function(data) {
   145 		showNotifications: function(data) {
   135 			var timestamp = data.timestamp,
   146 			var timestamp = data.timestamp,
   136 				notifications_data = data.notifications;
   147 				notifications_data = data.notifications;
   153 			}
   164 			}
   154 			/* update timestamp */
   165 			/* update timestamp */
   155 			var last_update = $('#activity-update');
   166 			var last_update = $('#activity-update');
   156 			last_update.text(last_update.data('ams-base-label').replace(/\{0\}/, timestamp));
   167 			last_update.text(last_update.data('ams-base-label').replace(/\{0\}/, timestamp));
   157 			/* check notifications badge */
   168 			/* check notifications badge */
   158 			MyAMS.skin.checkNotification();
   169 			MyAMS.skin && MyAMS.skin.checkNotification();
   159 		},
   170 		},
   160 
   171 
   161 		refreshNotifications: function() {
   172 		refreshNotifications: function() {
   162 			return function() {
   173 			return function() {
   163 				MyAMS.ajax.post('get-user-notifications.json', {}, PyAMS_notify.showNotifications);
   174 				MyAMS.ajax && MyAMS.ajax.post('get-user-notifications.json', {}, PyAMS_notify.showNotifications);
   164 			};
   175 			};
   165 		}
   176 		}
   166 	};
   177 	};
   167 	globals.PyAMS_notify = PyAMS_notify;
   178 	globals.PyAMS_notify = PyAMS_notify;
   168 
   179 
   169 	MyAMS.ajax.check($.WebSocket,
   180 	MyAMS.ajax && MyAMS.ajax.check($.WebSocket,
   170 					 '/--static--/pyams_notify/js/jquery-WebSocket' + MyAMS.devext + '.js',
   181 								   '/--static--/pyams_notify/js/jquery-WebSocket' + MyAMS.devext + '.js',
   171 					 function() {
   182 								   function() {
   172 						PyAMS_notify.initConnection();
   183 										PyAMS_notify.initConnection();
   173 					 });
   184 								   });
   174 
   185 
   175 })(jQuery, this);
   186 })(jQuery, this);