--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_notify/skin/resources/js/pyams_notify.js Thu Jun 02 15:52:52 2016 +0200
@@ -0,0 +1,155 @@
+(function($, globals) {
+
+ "use strict";
+
+ var MyAMS = globals.MyAMS;
+
+ var PyAMS_notify = {
+
+ connection: null,
+
+ initConnection: function() {
+ var tcp_handler = $('[data-ams-notify-server]', '#user-activity').data('ams-notify-server');
+ if (tcp_handler) {
+ var ws = $.WebSocket('ws://' + tcp_handler + '/subscribe');
+ ws.onopen = PyAMS_notify.onSocketOpened;
+ ws.onmessage = PyAMS_notify.onSocketMessage;
+ ws.onerror = PyAMS_notify.onSocketError;
+ ws.onclose = PyAMS_notify.onSocketClosed;
+ PyAMS_notify.connection = ws;
+ setInterval(PyAMS_notify.checkConnection, 30000);
+ }
+ },
+
+ checkConnection: function() {
+ if ((PyAMS_notify.connection === null) ||
+ (PyAMS_notify.connection.readyState === WebSocket.CLOSED)) {
+ PyAMS_notify.initConnection();
+ }
+ },
+
+ onSocketOpened: function(event) {
+ console.debug("WS subscription connection opened");
+ MyAMS.ajax.post('get-notifications-context.json', {}, function(result) {
+ if (result.principal.id !== '') {
+ PyAMS_notify.connection.send(JSON.stringify({
+ action: 'subscribe',
+ principal: result.principal,
+ context: result.context
+ }));
+ MyAMS.ajax.post('get-user-notifications.json', {}, PyAMS_notify.showNotifications);
+ }
+ });
+ },
+
+ onSocketMessage: function(event) {
+ var data = JSON.parse(event.data);
+ PyAMS_notify.notifyOnDesktop(data);
+ PyAMS_notify.notifyInWebpage(data);
+ },
+
+ onSocketError: function(event) {
+ console.log(event);
+ },
+
+ onSocketClosed: function(event) {
+ PyAMS_notify.connection = null;
+ console.debug("WS connection closed");
+ },
+
+ notifyOnDesktop: function(data) {
+
+ function do_notify() {
+ var options = {
+ title: data.title,
+ body: data.message,
+ icon: data.source.avatar
+ };
+ var notification = new Notification(options.title, options);
+ notification.onclick = function() {
+ if (data.url) {
+ window.open(data.url);
+ }
+ };
+ }
+
+ if (window.Notification && (Notification.permission !== 'denied')) {
+ if (Notification.permission === 'default') {
+ Notification.requestPermission(function (status) {
+ if (status === 'granted') {
+ do_notify();
+ }
+ });
+ } else {
+ do_notify();
+ }
+ }
+ },
+
+ createNotification: function(notification) {
+ var li = $('<li></li>');
+ var span = $('<span></span>');
+ var link = $('<a></a>').addClass('msg')
+ .attr('href', notification.url);
+ if (notification.source.avatar) {
+ $('<img>').addClass('air air-top-left margin-top-2')
+ .attr('src', notification.source.avatar)
+ .appendTo(link);
+ } else {
+ $('<i></i>').addClass('fa fa-2x fa-user air air-top-left img margin-left-5 margin-top-2')
+ .appendTo(link);
+ }
+ $('<time></time>').text(notification.timestamp)
+ .appendTo(link);
+ $('<span></span>').addClass('from')
+ .text(notification.source.title)
+ .appendTo(link);
+ $('<span></span>').addClass('msg-body')
+ .text(notification.message)
+ .appendTo(link);
+ link.appendTo(span);
+ span.appendTo(li);
+ return li;
+ },
+
+ notifyInWebpage: function(data) {
+ var badge = $('.badge', '#user-activity >span');
+ var count = parseInt(badge.text());
+ badge.text(count + 1);
+ var notifications = $('.notification-body', '#user-activity');
+ PyAMS_notify.createNotification(data).prependTo(notifications);
+ MyAMS.skin.checkNotification();
+ },
+
+ showNotifications: function(data) {
+ var badge = $('.badge', '#user-activity >span');
+ badge.text(data.length);
+ var notifications = $('.notification-body', '#user-activity');
+ notifications.empty();
+ if (data.length > 0) {
+ notifications.prev('p').hide();
+ for (var index = 0; index < data.length; index++) {
+ var notification = data[index];
+ PyAMS_notify.createNotification(notification).appendTo(notifications);
+ }
+ } else {
+ notifications.prev('p').show();
+ }
+ MyAMS.skin.checkNotification();
+ },
+
+ refreshNotifications: function() {
+ return function() {
+ 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();
+ });
+
+})(jQuery, this);