get notifications-popup ready for content-security, install as object in app.notifications and use data-poll-intervall of script tag to pass poll frequency

This commit is contained in:
Ralf Becker 2013-07-22 13:29:20 +00:00
parent 5842f5fd07
commit 4ed52a2b16
4 changed files with 218 additions and 184 deletions

View File

@ -7,16 +7,13 @@
* @subpackage ajaxpopup * @subpackage ajaxpopup
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>, Christian Binder <christian@jaytraxx.de> * @author Cornelius Weiss <nelius@cwtech.de>, Christian Binder <christian@jaytraxx.de>
* @version $Id$
*/ */
/** /**
* Ajax methods for notifications * Ajax methods for notifications
*/ */
class notifications_ajax { class notifications_ajax {
public $public_functions = array(
'get_notification' => true
);
/** /**
* Appname * Appname
*/ */
@ -108,12 +105,6 @@ class notifications_ajax {
$this->db = $GLOBALS['egw']->db; $this->db = $GLOBALS['egw']->db;
} }
/**
* destructor
*
*/
public function __destruct() {}
/** /**
* public AJAX trigger function to be called by the JavaScript client * public AJAX trigger function to be called by the JavaScript client
* *
@ -283,20 +274,20 @@ class notifications_ajax {
$message = 'data:text/html;charset=' . translation::charset() .';base64,'.base64_encode($message); $message = 'data:text/html;charset=' . translation::charset() .';base64,'.base64_encode($message);
} }
$this->response->addScriptCall('append_notification_message',$notification['notify_id'],$notification['notify_message'],$message); $this->response->addScriptCall('app.notifications.append',$notification['notify_id'],$notification['notify_message'],$message);
} }
switch($this->preferences[self::_appname]['egwpopup_verbosity']) { switch($this->preferences[self::_appname]['egwpopup_verbosity']) {
case 'low': case 'low':
$this->response->addScript('notificationbell_switch("active");'); $this->response->addScriptCall('app.notifications.bell', 'active');
break; break;
case 'high': case 'high':
$this->response->addAlert(lang('eGroupWare has notifications for you')); $this->response->addAlert(lang('EGroupware has notifications for you'));
$this->response->addScript('egwpopup_display();'); $this->response->addScriptCall('app.notifications.display');
break; break;
case 'medium': case 'medium':
default: default:
$this->response->addScript('egwpopup_display();'); $this->response->addScriptCall('app.notifications.display');
break; break;
} }
} }

View File

@ -12,24 +12,23 @@
* @author Cornelius Weiss <nelius@cwtech.de> * @author Cornelius Weiss <nelius@cwtech.de>
* @version $Id$ * @version $Id$
*/ */
$notification_config = config::read('notifications');
if ($GLOBALS['egw_info']['user']['apps']['notifications']) if ($GLOBALS['egw_info']['user']['apps']['notifications'])
{ {
$GLOBALS['egw']->translation->add_app('notifications'); $notification_config = config::read('notifications');
translation::add_app('notifications');
$popup_poll_interval = empty($notification_config['popup_poll_interval']) ? 60 : $notification_config['popup_poll_interval']; $popup_poll_interval = empty($notification_config['popup_poll_interval']) ? 60 : $notification_config['popup_poll_interval'];
echo '<script src="'. $GLOBALS['egw_info']['server']['webserver_url']. '/notifications/js/notificationajaxpopup.js?'. echo '<script src="'. $GLOBALS['egw_info']['server']['webserver_url']. '/notifications/js/notificationajaxpopup.js?'.
filemtime(EGW_SERVER_ROOT.'/notifications/js/notificationajaxpopup.js'). '" type="text/javascript"></script>'; filemtime(EGW_SERVER_ROOT.'/notifications/js/notificationajaxpopup.js'). '" type="text/javascript" id="notifications_script_id" data-poll-interval="'.$popup_poll_interval.'"></script>';
echo '<script type="text/javascript">var lab = egw_LAB || $LAB; lab.wait(function(){egwpopup_init("'.$popup_poll_interval.'");});</script>';
echo ' echo '
<div id="egwpopup" style="display: none; z-index: 999;"> <div id="egwpopup" style="display: none; z-index: 999;">
<div id="egwpopup_header">'.lang('Notification'). '<span style="float:right;">'. <div id="egwpopup_header">'.lang('Notification'). '<span style="float:right;">'.
html::submit_button('egwpopup_close_button', 'X', 'egwpopup_button_close();',true,'', 'close.button') . html::submit_button('egwpopup_close_button', 'X', '', true, 'id="egwpopup_close_button"', 'close.button') .
'</span></div> '</span></div>
<div id="egwpopup_message"></div> <div id="egwpopup_message"></div>
<div id="egwpopup_footer"> <div id="egwpopup_footer">
<input id="egwpopup_ok_button" type="submit" value="'. lang('ok'). '" onClick="egwpopup_button_ok();"> <input id="egwpopup_ok_button" type="button" value="'. lang('ok'). '">
</div> </div>
</div> </div>
'; ';
unset($notification_config);
} }
unset($notification_config);

View File

@ -1,174 +1,225 @@
/** /**
* eGroupWare - Notifications * EGroupware Notifications - clientside javascript
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package notifications * @package notifications
* @subpackage ajaxpoup * @subpackage ajaxpoup
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>, Christian Binder <christian@jaytraxx.de> * @author Cornelius Weiss <nelius@cwtech.de>, Christian Binder <christian@jaytraxx.de>, Ralf Becker <rb@stylite.de>
* @version $Id$ * @version $Id$
*/ */
var notifymessages = {};
var EGW_BROWSER_NOTIFY_ALLOWED = 0;
function egwpopup_init(_i) {
window.setTimeout("egwpopup_refresh(" + _i + ");", 1000);
}
function egwpopup_setTimeout(_i) {
window.setTimeout("egwpopup_refresh(" + _i + ");", _i*1000);
}
function egwpopup_refresh(_i) {
var request = xajax_doXMLHTTP("notifications.notifications_ajax.get_notifications", check_browser_notify());
request.request.error = function(_xmlhttp,_err){if(console) {console.log(request);console.log(_err)}};
egwpopup_setTimeout(_i);
}
/** /**
* Check to see if browser supports / allows desktop notifications * Installs app.notifications used to poll notifications from server and display them
*/ */
function check_browser_notify() { (function()
return window.webkitNotifications && window.webkitNotifications.checkPermission() == EGW_BROWSER_NOTIFY_ALLOWED; {
} var notifymessages = {};
var EGW_BROWSER_NOTIFY_ALLOWED = 0;
function egwpopup_display() {
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;
egwpopup_message.innerHTML = notifymessages[show];
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() {
window.webkitNotifications.requestPermission();
jQuery(this).hide();
});
desktop_button.appendTo(jQuery(egwpopup_ok_button).parent());
}
}
function notificationbell_switch(mode) {
var notificationbell;
notificationbell = document.getElementById("notificationbell");
if(mode == "active") {
notificationbell.style.display = "inline";
} else {
notificationbell.style.display = "none";
}
}
function egwpopup_button_ok() {
var egwpopup;
var egwpopup_message;
egwpopup = document.getElementById("egwpopup");
egwpopup_message = document.getElementById("egwpopup_message");
egwpopup_message.scrollTop = 0;
for(var confirmed in notifymessages) break;
xajax_doXMLHTTP("notifications.notifications_ajax.confirm_message", confirmed);
delete notifymessages[confirmed];
for(var id in notifymessages) break; /**
if (id == undefined) { * 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);
var self = this;
jQuery('#egwpopup_ok_button').click(function() { self.button_ok.apply(self); });
jQuery('#egwpopup_close_button').click(function() { self.button_close.apply(self); });
jQuery('#notificationbell').click(function() { self.display.apply(self); });
};
/**
* Poll server in given frequency via Ajax
* @param _i
*/
notifications.prototype.setTimeout = function(_i) {
var self = this;
window.setTimeout(function(){
var request = xajax_doXMLHTTP("notifications.notifications_ajax.get_notifications", self.check_browser_notify());
request.request.error = function(_xmlhttp,_err){
if(console) {
console.log(request);
console.log(_err);
}
};
self.setTimeout(_i);
}, _i*1000);
};
/**
* Check to see if browser supports / allows desktop notifications
*/
notifications.prototype.check_browser_notify = function() {
return window.webkitNotifications && window.webkitNotifications.checkPermission() == EGW_BROWSER_NOTIFY_ALLOWED;
};
/**
* 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;
egwpopup_message.innerHTML = notifymessages[show];
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() {
window.webkitNotifications.requestPermission();
jQuery(this).hide();
});
desktop_button.appendTo(jQuery(egwpopup_ok_button).parent());
}
};
/**
* Display or hide notifcation-bell
*
* @param String mode "active"
*/
notifications.prototype.bell = function(mode) {
var notificationbell;
notificationbell = document.getElementById("notificationbell");
if(mode == "active") {
notificationbell.style.display = "inline";
} else {
notificationbell.style.display = "none";
}
};
/**
* 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;
for(var confirmed in notifymessages) break;
xajax_doXMLHTTP("notifications.notifications_ajax.confirm_message", confirmed);
delete notifymessages[confirmed];
for(var id in notifymessages) break;
if (id == undefined) {
egwpopup.style.display = "none";
egwpopup_message.innerHTML = "";
this.bell("inactive");
} else {
this.display();
}
};
/**
* 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);
}
xajax_doXMLHTTP("notifications.notifications_ajax.confirm_message", ids);
notifymessages = {};
var egwpopup = document.getElementById("egwpopup");
var egwpopup_message = document.getElementById("egwpopup_message");
egwpopup.style.display = "none"; egwpopup.style.display = "none";
egwpopup_message.innerHTML = ""; egwpopup_message.innerHTML = "";
notificationbell_switch("inactive"); this.bell("inactive");
} else { };
egwpopup_display();
} /**
} * Add message to internal display-queue
*
// Close and mark all as read * @param _id
function egwpopup_button_close() { * @param _message
var ids = new Array(); * @param _browser_notify
for(var id in notifymessages) { */
ids.push(id); notifications.prototype.append = function(_id, _message, _browser_notify) {
} if(!this.check_browser_notify() || typeof notifymessages[_id] != 'undefined')
xajax_doXMLHTTP("notifications.notifications_ajax.confirm_message", ids); {
notifymessages[_id] = _message;
notifymessages = {}; return;
var egwpopup = document.getElementById("egwpopup"); }
var egwpopup_message = document.getElementById("egwpopup_message"); // Prevent the same thing popping up multiple times
egwpopup.style.display = "none";
egwpopup_message.innerHTML = "";
notificationbell_switch("inactive");
}
function append_notification_message(_id, _message, _browser_notify) {
if(!check_browser_notify() || typeof notifymessages[_id] != 'undefined')
{
notifymessages[_id] = _message; notifymessages[_id] = _message;
return;
} // Notification API
// Prevent the same thing popping up multiple times if(_browser_notify)
notifymessages[_id] = _message;
// Notification API
if(_browser_notify)
{
var notice;
if(webkitNotifications.createHTMLNotification)
{ {
notice = webkitNotifications.createHTMLNotification(_browser_notify); var notice = null;
} if(webkitNotifications.createHTMLNotification)
else if (webkitNotifications.createNotification)
{
// Pull the subject of the messasge, if possible
var message = /<b>(.*?)<\/b>/.exec(_message);
if(message && message[1])
{ {
_message = message[1]; notice = webkitNotifications.createHTMLNotification(_browser_notify);
} }
else else if (webkitNotifications.createNotification)
{ {
_message = _message.replace(/<(?:.|\n)*?>/gm, ''); // Pull the subject of the messasge, if possible
var message = /<b>(.*?)<\/b>/.exec(_message);
if(message && message[1])
{
_message = message[1];
}
else
{
_message = _message.replace(/<(?:.|\n)*?>/gm, '');
}
notice = webkitNotifications.createNotification('', "Egroupware",_message);
// When they click, bring up the popup for full info
notice.onclick = function() {
window.focus();
window.app.notifications.display();
this.close();
};
} }
notice = webkitNotifications.createNotification('', "Egroupware",_message); if(notice)
{
// When they click, bring up the popup for full info notice.ondisplay = function() {
notice.onclick = function() { // Confirm when user gets to see it - no close needed
window.focus(); // Wait a bit to let it load first, or it might not be there when requested.
egwpopup_display(); window.setTimeout( function() {
this.close(); xajax_doXMLHTTP("notifications.notifications_ajax.confirm_message", _id);
}, 2000);
};
notice.show();
} }
} }
if(notice) };
{
notice.ondisplay = function() { var lab = egw_LAB || $LAB;
// Confirm when user gets to see it - no close needed var self = notifications;
// Wait a bit to let it load first, or it might not be there when requested. lab.wait(function(){
window.setTimeout( function() { if (typeof window.app == 'undefined') window.app = {};
xajax_doXMLHTTP("notifications.notifications_ajax.confirm_message", _id); window.app.notifications = new self();
}, 2000); });
}; })();
notice.show();
}
}
}

View File

@ -510,15 +510,8 @@ abstract class egw_framework
*/ */
protected static function _get_notification_bell() protected static function _get_notification_bell()
{ {
return html::div( return html::image('notifications', 'notificationbell', lang('notifications'),
html::a_href( 'id="notificationbell" style="display: none"');
html::image('notifications','notificationbell',lang('notifications')),
'javascript: egwpopup_display();'
),
'id="notificationbell"', // options
'', // class
'display: none' //style
);
} }
/** /**