Implement browser notification API for notifications

This commit is contained in:
Hadi Nategh 2016-07-11 11:59:50 +02:00
parent c1e43f3324
commit 9befd5fac1

View File

@ -58,7 +58,7 @@
* Check to see if browser supports / allows desktop notifications * Check to see if browser supports / allows desktop notifications
*/ */
notifications.prototype.check_browser_notify = function() { notifications.prototype.check_browser_notify = function() {
return window.webkitNotifications && window.webkitNotifications.checkPermission() == EGW_BROWSER_NOTIFY_ALLOWED; return egw.checkNotification();
}; };
/** /**
@ -82,7 +82,7 @@
egwpopup.style.top = (Browserheight/4) + "px"; egwpopup.style.top = (Browserheight/4) + "px";
egwpopup_message.style.maxHeight = (Browserheight/2) + "px"; egwpopup_message.style.maxHeight = (Browserheight/2) + "px";
for(var show in notifymessages) break; for(var show in notifymessages) break;
egwpopup_message.innerHTML = notifymessages[show]; egwpopup_message.innerHTML = notifymessages[show]['message'];
// Activate links // Activate links
jQuery('div[data-id],div[data-url]', egwpopup_message).on('click', jQuery('div[data-id],div[data-url]', egwpopup_message).on('click',
@ -188,56 +188,72 @@
notifications.prototype.append = function(_id, _message, _browser_notify) { notifications.prototype.append = function(_id, _message, _browser_notify) {
if(!this.check_browser_notify() || typeof notifymessages[_id] != 'undefined') if(!this.check_browser_notify() || typeof notifymessages[_id] != 'undefined')
{ {
notifymessages[_id] = _message; notifymessages[_id] = {message:_message};
return; return;
} }
// Prevent the same thing popping up multiple times
notifymessages[_id] = _message;
var data = this.getData(_message);
// Prevent the same thing popping up multiple times
notifymessages[_id] = {message:_message, data: data};
// Notification API // Notification API
if(_browser_notify) if(_browser_notify)
{ {
var notice = null; egw.notification(data.title, {
if(webkitNotifications.createHTMLNotification) tag: data.app+":"+_id,
{ body: data.message,
notice = webkitNotifications.createHTMLNotification(_browser_notify); icon: data.icon,
} onclose:function(e){
else if (webkitNotifications.createNotification) // notification id
{ var id = this.tag.split(":");
// Pull the subject of the messasge, if possible // confirm the message
var message = /<b>(.*?)<\/b>/.exec(_message); var request = egw.json("notifications.notifications_ajax.confirm_message", [id[1]]);
if(message && message[1]) request.sendRequest();
{ },
_message = message[1]; onclick:function(e){
} // notification id
else var id = this.tag.split(":");
{
_message = _message.replace(/<(?:.|\n)*?>/gm, '');
}
notice = webkitNotifications.createNotification('', "Egroupware",_message);
// When they click, bring up the popup for full info // get the right data from messages object
notice.onclick = function() { var notify = notifymessages[id[1]];
window.focus();
window.app.notifications.display(); if (!notifymessages[id[1]]) this.close();
if (notify && notify.data && notify.data.id)
{
egw.open(notify.data.id, notify.data.app);
}
else if (notify && notify.data)
{
egw.open_link(notify.data.url,'_blank',notify.data.popup);
}
var request = egw.json("notifications.notifications_ajax.confirm_message", [id[1]]);
request.sendRequest();
delete notifymessages[id[1]];
this.close(); this.close();
}; }
} });
if(notice)
{
notice.ondisplay = function() {
// Confirm when user gets to see it - no close needed
// Wait a bit to let it load first, or it might not be there when requested.
window.setTimeout( function() {
var request = egw.json("notifications.notifications_ajax.confirm_message", _id);
request.sendRequest();
}, 2000);
};
notice.show();
}
} }
}; };
/**
* Extract useful data out of HTML message
*
* @param {type} _message
* @returns {notificationajaxpopup_L15.notifications.prototype.getData.data}
*/
notifications.prototype.getData = function (_message) {
var dom = jQuery(document.createElement('div')).html(_message);;
var link = dom.find('div[data-id],div[data-url]');
var data = {
message: dom.text(),
title: link.text(),
icon: link.find('img').attr('src')
};
jQuery.extend(data,link.data());
return typeof data == 'object'? data: {};
};
var lab = egw_LAB || $LAB; var lab = egw_LAB || $LAB;
var self = notifications; var self = notifications;
lab.wait(function(){ lab.wait(function(){