2006-09-07 09:04:15 +02:00
|
|
|
/**
|
2013-07-22 15:29:20 +02:00
|
|
|
* EGroupware Notifications - clientside javascript
|
2014-10-09 10:21:40 +02:00
|
|
|
*
|
2006-09-07 09:04:15 +02:00
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package notifications
|
|
|
|
* @subpackage ajaxpoup
|
|
|
|
* @link http://www.egroupware.org
|
2013-07-22 15:29:20 +02:00
|
|
|
* @author Cornelius Weiss <nelius@cwtech.de>, Christian Binder <christian@jaytraxx.de>, Ralf Becker <rb@stylite.de>
|
2006-09-25 12:20:46 +02:00
|
|
|
* @version $Id$
|
2006-09-07 09:04:15 +02:00
|
|
|
*/
|
|
|
|
|
2011-11-24 00:48:58 +01:00
|
|
|
/**
|
2013-07-22 15:29:20 +02:00
|
|
|
* Installs app.notifications used to poll notifications from server and display them
|
2011-11-24 00:48:58 +01:00
|
|
|
*/
|
2013-07-22 15:29:20 +02:00
|
|
|
(function()
|
2014-10-09 10:21:40 +02:00
|
|
|
{
|
2013-07-22 15:29:20 +02:00
|
|
|
var notifymessages = {};
|
2014-10-09 10:21:40 +02:00
|
|
|
var EGW_BROWSER_NOTIFY_ALLOWED = 0;
|
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
/**
|
|
|
|
* Constructor inits polling and installs handlers, polling frequence is passed via data-poll-interval of script tag
|
|
|
|
*/
|
|
|
|
function notifications() {
|
|
|
|
var notification_script = document.getElementById('notifications_script_id');
|
|
|
|
var popup_poll_interval = notification_script && notification_script.getAttribute('data-poll-interval');
|
|
|
|
this.setTimeout(popup_poll_interval || 60);
|
2014-10-09 10:21:40 +02:00
|
|
|
jQuery('#egwpopup_ok_button').click(jQuery.proxy(this.button_ok, this));
|
|
|
|
jQuery('#egwpopup_close_button').click(jQuery.proxy(this.button_close, this));
|
|
|
|
jQuery('#notificationbell').click(jQuery.proxy(this.display, this));
|
|
|
|
// query notifictions now
|
|
|
|
this.get_notifications();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Poll server for new notifications
|
|
|
|
*/
|
|
|
|
notifications.prototype.get_notifications = function()
|
|
|
|
{
|
|
|
|
egw.json(
|
|
|
|
"notifications.notifications_ajax.get_notifications",
|
|
|
|
this.check_browser_notify()
|
|
|
|
).sendRequest();
|
2013-07-22 15:29:20 +02:00
|
|
|
};
|
2011-04-14 15:43:16 +02:00
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
/**
|
|
|
|
* Poll server in given frequency via Ajax
|
|
|
|
* @param _i
|
|
|
|
*/
|
|
|
|
notifications.prototype.setTimeout = function(_i) {
|
|
|
|
var self = this;
|
|
|
|
window.setTimeout(function(){
|
2014-10-09 10:21:40 +02:00
|
|
|
self.get_notifications();
|
2013-07-22 15:29:20 +02:00
|
|
|
self.setTimeout(_i);
|
|
|
|
}, _i*1000);
|
|
|
|
};
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
/**
|
|
|
|
* Check to see if browser supports / allows desktop notifications
|
|
|
|
*/
|
|
|
|
notifications.prototype.check_browser_notify = function() {
|
2016-07-11 11:59:50 +02:00
|
|
|
return egw.checkNotification();
|
2013-07-22 15:29:20 +02:00
|
|
|
};
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
/**
|
|
|
|
* Display notifications window
|
|
|
|
*/
|
|
|
|
notifications.prototype.display = function() {
|
|
|
|
var egwpopup;
|
|
|
|
var egwpopup_message;
|
|
|
|
var Browserwidth;
|
|
|
|
var Browserheight;
|
|
|
|
var egwpopup_ok_button;
|
|
|
|
egwpopup = document.getElementById("egwpopup");
|
|
|
|
egwpopup_message = document.getElementById("egwpopup_message");
|
|
|
|
egwpopup_ok_button = document.getElementById("egwpopup_ok_button");
|
|
|
|
egwpopup.style.display = "block";
|
|
|
|
egwpopup.style.position = "absolute";
|
|
|
|
egwpopup.style.width = "500px";
|
|
|
|
Browserwidth = (window.innerWidth || document.body.clientWidth || 640);
|
|
|
|
Browserheight = (window.innerHeight || document.body.clientHeight || 480);
|
|
|
|
egwpopup.style.left = (Browserwidth/2 - 250) + "px";
|
|
|
|
egwpopup.style.top = (Browserheight/4) + "px";
|
|
|
|
egwpopup_message.style.maxHeight = (Browserheight/2) + "px";
|
|
|
|
for(var show in notifymessages) break;
|
2016-07-11 11:59:50 +02:00
|
|
|
egwpopup_message.innerHTML = notifymessages[show]['message'];
|
2014-12-15 20:09:22 +01:00
|
|
|
|
|
|
|
// Activate links
|
2016-06-02 16:51:15 +02:00
|
|
|
jQuery('div[data-id],div[data-url]', egwpopup_message).on('click',
|
2014-12-15 20:09:22 +01:00
|
|
|
function() {
|
|
|
|
if(this.dataset.id)
|
|
|
|
{
|
|
|
|
egw.open(this.dataset.id,this.dataset.app);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
egw.open_link(this.dataset.url,'_blank',this.dataset.popup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).addClass('et2_link');
|
2013-07-22 15:29:20 +02:00
|
|
|
var num = 0;
|
|
|
|
for(var id in notifymessages) ++num;
|
|
|
|
if(num-1 > 0 ) {
|
|
|
|
egwpopup_ok_button.value = "OK (" + (num-1) + ")";
|
|
|
|
} else {
|
|
|
|
egwpopup_ok_button.value = "OK";
|
|
|
|
}
|
|
|
|
if(window.webkitNotifications && window.webkitNotifications.checkPermission() != EGW_BROWSER_NOTIFY_ALLOWED &&
|
|
|
|
jQuery('#desktop_perms').length == 0)
|
|
|
|
{
|
|
|
|
var label = 'Desktop notifications';
|
|
|
|
try {
|
|
|
|
if(egw) label = egw.lang(label);
|
|
|
|
} catch(err) {}
|
|
|
|
var desktop_button = jQuery('<button id="desktop_perms">' + label + '</button>')
|
|
|
|
.click(function() {
|
2014-10-09 10:21:40 +02:00
|
|
|
window.webkitNotifications.requestPermission();
|
2013-07-22 15:29:20 +02:00
|
|
|
jQuery(this).hide();
|
|
|
|
});
|
|
|
|
desktop_button.appendTo(jQuery(egwpopup_ok_button).parent());
|
|
|
|
}
|
|
|
|
};
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
/**
|
|
|
|
* Display or hide notifcation-bell
|
2014-10-09 10:21:40 +02:00
|
|
|
*
|
|
|
|
* @param {string} mode "active"
|
2013-07-22 15:29:20 +02:00
|
|
|
*/
|
|
|
|
notifications.prototype.bell = function(mode) {
|
|
|
|
var notificationbell;
|
|
|
|
notificationbell = document.getElementById("notificationbell");
|
|
|
|
if(mode == "active") {
|
|
|
|
notificationbell.style.display = "inline";
|
|
|
|
} else {
|
|
|
|
notificationbell.style.display = "none";
|
|
|
|
}
|
|
|
|
};
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
/**
|
|
|
|
* Callback for OK button: confirms message on server and hides display
|
|
|
|
*/
|
|
|
|
notifications.prototype.button_ok = function() {
|
|
|
|
var egwpopup;
|
|
|
|
var egwpopup_message;
|
|
|
|
egwpopup = document.getElementById("egwpopup");
|
|
|
|
egwpopup_message = document.getElementById("egwpopup_message");
|
|
|
|
egwpopup_message.scrollTop = 0;
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
for(var confirmed in notifymessages) break;
|
2013-09-05 00:58:29 +02:00
|
|
|
var request = egw.json("notifications.notifications_ajax.confirm_message", [confirmed]);
|
2013-09-05 00:52:43 +02:00
|
|
|
request.sendRequest();
|
2013-07-22 15:29:20 +02:00
|
|
|
delete notifymessages[confirmed];
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
for(var id in notifymessages) break;
|
|
|
|
if (id == undefined) {
|
|
|
|
egwpopup.style.display = "none";
|
|
|
|
egwpopup_message.innerHTML = "";
|
|
|
|
this.bell("inactive");
|
2014-10-09 10:21:40 +02:00
|
|
|
} else {
|
2013-07-22 15:29:20 +02:00
|
|
|
this.display();
|
|
|
|
}
|
|
|
|
};
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
/**
|
|
|
|
* Callback for close button: close and mark all as read
|
|
|
|
*/
|
|
|
|
notifications.prototype.button_close = function() {
|
|
|
|
var ids = new Array();
|
|
|
|
for(var id in notifymessages) {
|
|
|
|
ids.push(id);
|
|
|
|
}
|
2014-12-15 20:09:22 +01:00
|
|
|
var request = egw.json("notifications.notifications_ajax.confirm_message", [ids]);
|
2013-09-05 00:52:43 +02:00
|
|
|
request.sendRequest();
|
2013-07-22 15:29:20 +02:00
|
|
|
notifymessages = {};
|
|
|
|
var egwpopup = document.getElementById("egwpopup");
|
|
|
|
var egwpopup_message = document.getElementById("egwpopup_message");
|
2009-02-23 21:45:31 +01:00
|
|
|
egwpopup.style.display = "none";
|
|
|
|
egwpopup_message.innerHTML = "";
|
2013-07-22 15:29:20 +02:00
|
|
|
this.bell("inactive");
|
|
|
|
};
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
/**
|
|
|
|
* Add message to internal display-queue
|
2014-10-09 10:21:40 +02:00
|
|
|
*
|
2013-07-22 15:29:20 +02:00
|
|
|
* @param _id
|
|
|
|
* @param _message
|
|
|
|
* @param _browser_notify
|
|
|
|
*/
|
|
|
|
notifications.prototype.append = function(_id, _message, _browser_notify) {
|
|
|
|
if(!this.check_browser_notify() || typeof notifymessages[_id] != 'undefined')
|
2012-11-05 19:32:54 +01:00
|
|
|
{
|
2016-07-11 11:59:50 +02:00
|
|
|
notifymessages[_id] = {message:_message};
|
2013-07-22 15:29:20 +02:00
|
|
|
return;
|
2012-11-05 19:32:54 +01:00
|
|
|
}
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2016-07-11 11:59:50 +02:00
|
|
|
var data = this.getData(_message);
|
|
|
|
// Prevent the same thing popping up multiple times
|
|
|
|
notifymessages[_id] = {message:_message, data: data};
|
2013-07-22 15:29:20 +02:00
|
|
|
// Notification API
|
|
|
|
if(_browser_notify)
|
2012-11-05 19:32:54 +01:00
|
|
|
{
|
2016-07-11 11:59:50 +02:00
|
|
|
egw.notification(data.title, {
|
|
|
|
tag: data.app+":"+_id,
|
|
|
|
body: data.message,
|
|
|
|
icon: data.icon,
|
|
|
|
onclose:function(e){
|
|
|
|
// notification id
|
|
|
|
var id = this.tag.split(":");
|
|
|
|
// confirm the message
|
|
|
|
var request = egw.json("notifications.notifications_ajax.confirm_message", [id[1]]);
|
|
|
|
request.sendRequest();
|
|
|
|
},
|
|
|
|
onclick:function(e){
|
|
|
|
// notification id
|
|
|
|
var id = this.tag.split(":");
|
|
|
|
|
|
|
|
// get the right data from messages object
|
|
|
|
var notify = notifymessages[id[1]];
|
|
|
|
|
|
|
|
if (!notifymessages[id[1]]) this.close();
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2016-07-11 11:59:50 +02:00
|
|
|
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]];
|
2013-07-22 15:29:20 +02:00
|
|
|
this.close();
|
2016-07-11 11:59:50 +02:00
|
|
|
}
|
|
|
|
});
|
2012-11-05 19:32:54 +01:00
|
|
|
}
|
2013-07-22 15:29:20 +02:00
|
|
|
};
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2016-07-11 11:59:50 +02:00
|
|
|
/**
|
|
|
|
* 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: {};
|
|
|
|
};
|
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
var lab = egw_LAB || $LAB;
|
|
|
|
var self = notifications;
|
|
|
|
lab.wait(function(){
|
|
|
|
if (typeof window.app == 'undefined') window.app = {};
|
|
|
|
window.app.notifications = new self();
|
|
|
|
});
|
|
|
|
})();
|