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 = {};
|
2017-09-18 18:47:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* time range label today
|
|
|
|
* @type Number
|
|
|
|
*/
|
|
|
|
const TIME_LABEL_TODAY = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* time range label yesterday
|
|
|
|
* @type Number
|
|
|
|
*/
|
|
|
|
const TIME_LABEL_YESTERDAY = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* time range label this month
|
|
|
|
* @type Number
|
|
|
|
*/
|
|
|
|
const TIME_LABEL_THIS_MONTH = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* time range label last month
|
|
|
|
* @type Number
|
|
|
|
*/
|
|
|
|
const TIME_LABEL_LAST_MONTH = 3;
|
2014-10-09 10:21:40 +02:00
|
|
|
|
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('#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()
|
2017-05-15 17:34:27 +02:00
|
|
|
).sendRequest(true);
|
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
|
|
|
|
2017-09-18 18:47:44 +02:00
|
|
|
/**
|
|
|
|
* This function gets created time and current time then finds out if
|
|
|
|
* the time range of the event.
|
|
|
|
*
|
|
|
|
* @param {type} _created
|
|
|
|
* @param {type} _current
|
|
|
|
*
|
|
|
|
* @returns {int} returns type of range
|
|
|
|
*/
|
|
|
|
notifications.prototype.getTimeLabel = function (_created, _current) {
|
|
|
|
var created = typeof _created == 'string'? new Date(_created): _created;
|
|
|
|
var current = new Date(_current.date);
|
|
|
|
var time_diff = (current - created) / 1000;
|
|
|
|
var result = '';
|
|
|
|
if (time_diff < current.getHours() * 3600)
|
|
|
|
{
|
|
|
|
result = TIME_LABEL_TODAY;
|
|
|
|
}
|
|
|
|
else if ((time_diff > current.getHours() * 3600) &&
|
|
|
|
(time_diff < (current.getHours() * 3600 + 86400)))
|
|
|
|
{
|
|
|
|
result = TIME_LABEL_YESTERDAY;
|
|
|
|
}
|
|
|
|
else if (current.getFullYear() == created.getFullYear() &&
|
|
|
|
(current.getMonth() - created.getMonth()) == 0 &&
|
|
|
|
time_diff > (current.getHours() * 3600 + 86400))
|
|
|
|
{
|
|
|
|
result = TIME_LABEL_THIS_MONTH;
|
|
|
|
}
|
|
|
|
else if (current.getFullYear() == created.getFullYear() &&
|
|
|
|
(current.getMonth() - created.getMonth()) == 1)
|
|
|
|
{
|
|
|
|
result = TIME_LABEL_LAST_MONTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
/**
|
|
|
|
* Display notifications window
|
|
|
|
*/
|
|
|
|
notifications.prototype.display = function() {
|
2017-09-15 17:10:11 +02:00
|
|
|
// list container
|
2017-09-18 18:47:44 +02:00
|
|
|
var $egwpopup_list = jQuery("#egwpopup_list");
|
2017-11-20 10:28:24 +01:00
|
|
|
|
|
|
|
// Preserve already poped notifications
|
|
|
|
var poped = [];
|
|
|
|
$egwpopup_list.find('.egwpopup_expanded').each(function(index, item){
|
|
|
|
// store messages ids of poped messages
|
|
|
|
poped.push(item.id.replace('egwpopup_message_', ''));
|
|
|
|
});
|
2017-09-20 17:49:24 +02:00
|
|
|
// clear the list
|
|
|
|
$egwpopup_list.empty();
|
2017-09-18 18:47:44 +02:00
|
|
|
// define time label deviders
|
|
|
|
var $today = jQuery(document.createElement('div'))
|
|
|
|
.addClass('egwpopup_time_label')
|
|
|
|
.text(egw.lang('today'))
|
|
|
|
, $yesterday = jQuery(document.createElement('div'))
|
|
|
|
.addClass('egwpopup_time_label')
|
|
|
|
.text(egw.lang('yesterday'))
|
|
|
|
, $this_month = jQuery(document.createElement('div'))
|
|
|
|
.addClass('egwpopup_time_label')
|
2018-01-23 22:07:27 +01:00
|
|
|
.text(egw.lang('this month'))
|
2017-09-18 18:47:44 +02:00
|
|
|
, $last_month = jQuery(document.createElement('div'))
|
|
|
|
.addClass('egwpopup_time_label')
|
2018-01-23 22:07:27 +01:00
|
|
|
.text(egw.lang('last month'));
|
2017-09-18 18:47:44 +02:00
|
|
|
// reverse indexes to get the latest messages at the top
|
|
|
|
var indexes = Object.keys(notifymessages).reverse()
|
|
|
|
for(var index in indexes)
|
2017-05-15 17:34:27 +02:00
|
|
|
{
|
2017-09-18 18:47:44 +02:00
|
|
|
var id = indexes[index];
|
|
|
|
var $message, $mark, $delete, $inner_container,
|
|
|
|
$more_info, $top_toolbar, $open_entry, $date, $collapse;
|
|
|
|
var message_id = 'egwpopup_message_'+id;
|
|
|
|
var time_label = this.getTimeLabel(notifymessages[id]['created'], notifymessages[id]['current']);
|
2017-05-16 18:03:50 +02:00
|
|
|
if (jQuery('#'+message_id,$egwpopup_list).length > 0)
|
|
|
|
{
|
2017-09-18 18:47:44 +02:00
|
|
|
this.update_message_status(id, notifymessages[id]['status']);
|
2017-05-16 18:03:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-09-18 18:47:44 +02:00
|
|
|
// set the time labels on
|
|
|
|
switch (time_label)
|
|
|
|
{
|
|
|
|
case TIME_LABEL_TODAY:
|
|
|
|
if (!$egwpopup_list.has($today).length) $today.appendTo($egwpopup_list);
|
|
|
|
break;
|
|
|
|
case TIME_LABEL_YESTERDAY:
|
|
|
|
if (!$egwpopup_list.has($yesterday).length) $yesterday.appendTo($egwpopup_list);
|
|
|
|
break;
|
|
|
|
case TIME_LABEL_THIS_MONTH:
|
|
|
|
if (!$egwpopup_list.has($this_month).length) $this_month.appendTo($egwpopup_list);
|
|
|
|
break;
|
|
|
|
case TIME_LABEL_LAST_MONTH:
|
|
|
|
if (!$egwpopup_list.has($last_month).length) $last_month.appendTo($egwpopup_list);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-15 17:10:11 +02:00
|
|
|
// message container
|
2017-05-15 17:34:27 +02:00
|
|
|
$message = jQuery(document.createElement('div'))
|
|
|
|
.addClass('egwpopup_message')
|
2017-05-16 18:03:50 +02:00
|
|
|
.attr('id', message_id);
|
2017-09-15 17:10:11 +02:00
|
|
|
// wrapper for message content
|
|
|
|
$inner_container = jQuery(document.createElement('div'))
|
|
|
|
.addClass('egwpopup_message_inner_container')
|
|
|
|
.appendTo($message);
|
2017-09-18 18:47:44 +02:00
|
|
|
$inner_container[0].innerHTML = notifymessages[id]['message'];
|
2017-09-15 17:10:11 +02:00
|
|
|
|
|
|
|
$more_info = jQuery(document.createElement('div'))
|
|
|
|
.addClass('egwpopup_message_more_info')
|
|
|
|
.text(egw.lang('More info')+'...')
|
|
|
|
.appendTo($message);
|
|
|
|
// top toolbar NODE
|
|
|
|
$top_toolbar = jQuery(document.createElement('div'))
|
|
|
|
.addClass('egwpopup_message_top_toolbar');
|
|
|
|
// Date indicator NODE
|
|
|
|
$date = jQuery(document.createElement('span'))
|
|
|
|
.addClass('egwpopup_message_date')
|
|
|
|
.prependTo($top_toolbar)
|
2017-09-18 18:47:44 +02:00
|
|
|
.text(notifymessages[id]['created']);
|
2018-02-16 12:02:43 +01:00
|
|
|
if (notifymessages[id]['data']['id'] || notifymessages[id]['data']['url']) {
|
|
|
|
// OPEN entry button
|
|
|
|
$open_entry = jQuery(document.createElement('span'))
|
|
|
|
.addClass('egwpopup_message_open')
|
|
|
|
.attr('title',egw.lang('open notified entry'))
|
|
|
|
.click(jQuery.proxy(this.open_entry, this,[$message]))
|
|
|
|
.prependTo($top_toolbar);
|
|
|
|
}
|
2017-09-15 17:10:11 +02:00
|
|
|
// Delete button
|
2017-05-17 14:04:31 +02:00
|
|
|
$delete = jQuery(document.createElement('span'))
|
|
|
|
.addClass('egwpopup_delete')
|
|
|
|
.attr('title',egw.lang('delete this message'))
|
|
|
|
.click(jQuery.proxy(this.button_delete, this,[$message]))
|
2017-09-15 17:10:11 +02:00
|
|
|
.prependTo($top_toolbar);
|
|
|
|
// Mark as read button
|
2017-05-17 14:04:31 +02:00
|
|
|
$mark = jQuery(document.createElement('span'))
|
|
|
|
.addClass('egwpopup_mark')
|
2017-09-15 17:10:11 +02:00
|
|
|
.prependTo($top_toolbar);
|
|
|
|
// Collapse button (close icon)
|
|
|
|
$collapse = jQuery(document.createElement('span'))
|
|
|
|
.addClass('egwpopup_collapse')
|
|
|
|
.click(jQuery.proxy(this.collapseMessage, this, [$message]))
|
|
|
|
.prependTo($top_toolbar);
|
|
|
|
|
2017-09-18 18:47:44 +02:00
|
|
|
if (notifymessages[id]['status'] != 'SEEN')
|
2017-05-17 14:04:31 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
$mark.click(jQuery.proxy(this.message_seen, this,[$message]))
|
|
|
|
.attr('title',egw.lang('mark as read'));
|
|
|
|
}
|
2017-09-15 17:10:11 +02:00
|
|
|
|
2017-05-15 17:34:27 +02:00
|
|
|
// Activate links
|
|
|
|
jQuery('div[data-id],div[data-url]', $message).on('click',
|
|
|
|
function() {
|
|
|
|
if(this.dataset.id)
|
|
|
|
{
|
|
|
|
egw.open(this.dataset.id,this.dataset.app);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
egw.open_link(this.dataset.url,'_blank',this.dataset.popup);
|
|
|
|
}
|
2014-12-15 20:09:22 +01:00
|
|
|
}
|
2017-05-15 17:34:27 +02:00
|
|
|
).addClass('et2_link');
|
2018-01-24 19:02:35 +01:00
|
|
|
if (notifymessages[id]['data'] && notifymessages[id]['data']['actions'] && notifymessages[id]['data']['actions'].length > 0)
|
2017-05-23 16:54:20 +02:00
|
|
|
{
|
|
|
|
var $actions_container = jQuery(document.createElement('div')).addClass('egwpopup_actions_container');
|
2017-09-18 18:47:44 +02:00
|
|
|
for(var action in notifymessages[id].data.actions)
|
2017-05-23 16:54:20 +02:00
|
|
|
{
|
2017-09-18 18:47:44 +02:00
|
|
|
var func = new Function(notifymessages[id].data.actions[action].onExecute);
|
2017-05-23 16:54:20 +02:00
|
|
|
jQuery(document.createElement('button'))
|
|
|
|
.addClass('et2_button')
|
2017-09-18 18:47:44 +02:00
|
|
|
.css({'background-image':'url('+egw.image(notifymessages[id].data.actions[action].icon,notifymessages[id].data.app)+')'})
|
|
|
|
.text(notifymessages[id].data.actions[action].caption)
|
2017-05-23 16:54:20 +02:00
|
|
|
.click(jQuery.proxy(func,this))
|
|
|
|
.prependTo($actions_container);
|
|
|
|
}
|
2017-09-15 17:10:11 +02:00
|
|
|
$actions_container.appendTo($message);
|
2017-05-23 16:54:20 +02:00
|
|
|
}
|
2017-09-15 17:10:11 +02:00
|
|
|
$top_toolbar.prependTo($message);
|
2017-05-15 17:34:27 +02:00
|
|
|
$egwpopup_list.append($message);
|
2017-05-17 14:04:31 +02:00
|
|
|
// bind click handler after the message container is attached
|
2017-09-18 18:47:44 +02:00
|
|
|
$message.click(jQuery.proxy(this.clickOnMessage, this,[$message]));
|
|
|
|
this.update_message_status(id, notifymessages[id]['status']);
|
2013-07-22 15:29:20 +02:00
|
|
|
}
|
2017-11-20 10:28:24 +01:00
|
|
|
|
|
|
|
if (poped.length > 0)
|
|
|
|
{
|
|
|
|
for (var key in poped)
|
|
|
|
{
|
|
|
|
// pop again those where opened before refresh
|
|
|
|
$egwpopup_list.find('#egwpopup_message_'+poped[key]).trigger('click');
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 17:34:27 +02:00
|
|
|
this.counterUpdate();
|
2013-07-22 15:29:20 +02:00
|
|
|
};
|
2017-09-18 18:47:44 +02:00
|
|
|
|
2017-09-15 17:10:11 +02:00
|
|
|
/**
|
|
|
|
* Opens the relavant entry from clicked message
|
|
|
|
*
|
|
|
|
* @param {jquery object} _node
|
|
|
|
* @param {object} _event
|
|
|
|
*/
|
|
|
|
notifications.prototype.open_entry = function (_node, _event){
|
2017-05-22 12:18:53 +02:00
|
|
|
_event.stopPropagation();
|
|
|
|
this.message_seen(_node, _event);
|
|
|
|
var egwpopup_message = _node[0];
|
|
|
|
var id = egwpopup_message[0].id.replace(/egwpopup_message_/ig,'');
|
|
|
|
if (notifymessages[id]['data'])
|
|
|
|
{
|
|
|
|
if (notifymessages[id]['data']['id'])
|
|
|
|
{
|
|
|
|
egw.open(notifymessages[id]['data']['id'], notifymessages[id]['data']['app']);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
egw.open_link(notifymessages[id]['data']['url'],'_blank',notifymessages[id]['data']['popup']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-15 17:10:11 +02:00
|
|
|
/**
|
|
|
|
* Reposition the expanded message back in the list & removes the clone node
|
|
|
|
* @param {jquery object} _node
|
|
|
|
*/
|
2017-09-18 18:47:44 +02:00
|
|
|
notifications.prototype.collapseMessage = function (_node, _event){
|
|
|
|
_event.stopPropagation();
|
2017-09-15 17:10:11 +02:00
|
|
|
var cloned = _node[0].prev();
|
|
|
|
if (cloned.length > 0 && cloned[0].id == _node[0].attr('id')+'_expanded')
|
|
|
|
cloned.remove();
|
|
|
|
_node[0].removeClass('egwpopup_expanded');
|
2017-09-18 18:47:44 +02:00
|
|
|
_node[0].css('z-index', 0);
|
2017-09-15 17:10:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand a clicked message into bigger view
|
|
|
|
* @param {jquery object} _node
|
|
|
|
* @param {object} _event
|
2017-09-18 18:47:44 +02:00
|
|
|
*
|
|
|
|
* @return undefined
|
2017-09-15 17:10:11 +02:00
|
|
|
*/
|
|
|
|
notifications.prototype.clickOnMessage = function (_node, _event){
|
2017-09-18 18:47:44 +02:00
|
|
|
// Do not run the click handler if it's been already expanded
|
2018-01-24 19:02:35 +01:00
|
|
|
if (_node[0].hasClass('egwpopup_expanded') || jQuery(_event.target).hasClass('et2_button')) return;
|
2017-09-15 17:10:11 +02:00
|
|
|
this.message_seen(_node, _event);
|
|
|
|
var $node = jQuery(_node[0][0].cloneNode());
|
|
|
|
if ($node)
|
|
|
|
{
|
|
|
|
$node.attr('id', _node[0].attr('id')+'_expanded')
|
|
|
|
.addClass('egwpopup_message_clone')
|
|
|
|
.insertBefore(_node[0]);
|
|
|
|
}
|
2017-09-18 18:47:44 +02:00
|
|
|
var zindex = jQuery('.egwpopup_expanded').length;
|
|
|
|
_node[0].addClass('egwpopup_expanded').css('z-index', zindex++);
|
2017-09-15 17:10:11 +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
|
|
|
|
*/
|
2017-05-17 14:04:31 +02:00
|
|
|
notifications.prototype.message_seen = function(_node, _event) {
|
|
|
|
_event.stopPropagation();
|
|
|
|
var egwpopup_message = _node[0];
|
|
|
|
var id = egwpopup_message[0].id.replace(/egwpopup_message_/ig,'');
|
2017-05-22 12:18:53 +02:00
|
|
|
if (notifymessages[id]['status'] !='SEEN')
|
|
|
|
{
|
|
|
|
var request = egw.json("notifications.notifications_ajax.update_status", [id, "SEEN"]);
|
|
|
|
request.sendRequest(true);
|
|
|
|
this.update_message_status(id, "SEEN");
|
|
|
|
}
|
2017-05-16 18:03:50 +02:00
|
|
|
};
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2017-05-16 18:03:50 +02:00
|
|
|
notifications.prototype.mark_all_seen = function()
|
|
|
|
{
|
|
|
|
if (!notifymessages || Object.keys(notifymessages).length == 0) return false;
|
|
|
|
egw.json("notifications.notifications_ajax.update_status", [Object.keys(notifymessages), "SEEN"]).sendRequest(true);
|
|
|
|
for (var id in notifymessages)
|
|
|
|
{
|
|
|
|
this.update_message_status(id, "SEEN");
|
|
|
|
}
|
|
|
|
};
|
2014-10-09 10:21:40 +02:00
|
|
|
|
2017-05-16 18:03:50 +02:00
|
|
|
notifications.prototype.update_message_status = function (_id, _status)
|
|
|
|
{
|
|
|
|
var $egwpopup_message = jQuery('#egwpopup_message_'+_id);
|
|
|
|
notifymessages[_id]['status'] = _status;
|
|
|
|
if ($egwpopup_message.length>0)
|
|
|
|
{
|
|
|
|
switch (_status)
|
|
|
|
{
|
|
|
|
case 'SEEN':
|
|
|
|
$egwpopup_message.addClass('egwpopup_message_seen');
|
|
|
|
break;
|
|
|
|
case 'UNSEEN':
|
|
|
|
case 'DISPLAYED':
|
|
|
|
$egwpopup_message.removeClass('egwpopup_message_seen');
|
|
|
|
break;
|
|
|
|
}
|
2013-07-22 15:29:20 +02:00
|
|
|
}
|
2017-05-16 18:03:50 +02:00
|
|
|
this.counterUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
notifications.prototype.delete_all = function () {
|
|
|
|
if (!notifymessages || Object.entries(notifymessages).length == 0) return false;
|
|
|
|
egw.json("notifications.notifications_ajax.delete_message", [Object.keys(notifymessages)]).sendRequest(true);
|
2017-10-26 10:25:57 +02:00
|
|
|
notifymessages = {};
|
2017-05-16 18:03:50 +02:00
|
|
|
jQuery("#egwpopup_list").empty();
|
|
|
|
this.counterUpdate();
|
2013-07-22 15:29:20 +02:00
|
|
|
};
|
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
|
|
|
|
*/
|
2017-05-17 14:04:31 +02:00
|
|
|
notifications.prototype.button_delete = function(_node, _event) {
|
2017-05-16 18:03:50 +02:00
|
|
|
_event.stopPropagation();
|
2017-05-17 14:04:31 +02:00
|
|
|
var egwpopup_message = _node[0];
|
|
|
|
var id = egwpopup_message[0].id.replace(/egwpopup_message_/ig,'');
|
2017-05-16 18:03:50 +02:00
|
|
|
var request = egw.json("notifications.notifications_ajax.delete_message", [id]);
|
|
|
|
request.sendRequest(true);
|
2017-05-15 17:34:27 +02:00
|
|
|
delete (notifymessages[id]);
|
2017-09-18 18:47:44 +02:00
|
|
|
// try to close the dialog if expanded before hidding it
|
|
|
|
this.collapseMessage(_node, _event);
|
2017-05-17 14:04:31 +02:00
|
|
|
egwpopup_message.hide();
|
2013-07-22 15:29:20 +02:00
|
|
|
this.bell("inactive");
|
2017-05-15 17:34:27 +02:00
|
|
|
this.counterUpdate();
|
2013-07-22 15:29:20 +02:00
|
|
|
};
|
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
|
2017-09-18 18:47:44 +02:00
|
|
|
* @param {string} _status
|
|
|
|
* @param {string} _created
|
|
|
|
*
|
|
|
|
* @return undefined
|
2013-07-22 15:29:20 +02:00
|
|
|
*/
|
2017-09-18 18:47:44 +02:00
|
|
|
notifications.prototype.append = function(_id, _message, _browser_notify,
|
2018-01-24 19:02:35 +01:00
|
|
|
_status, _created, _current, _actions) {
|
2016-07-11 11:59:50 +02:00
|
|
|
var data = this.getData(_message);
|
|
|
|
// Prevent the same thing popping up multiple times
|
2017-09-18 18:47:44 +02:00
|
|
|
notifymessages[_id] = {
|
|
|
|
message:_message,
|
|
|
|
data: data,
|
|
|
|
status: _status,
|
|
|
|
created: _created,
|
|
|
|
current: _current
|
|
|
|
};
|
2018-01-24 19:02:35 +01:00
|
|
|
if (_actions && _actions.length > 0) notifymessages[_id]['data']['actions'] = _actions;
|
2013-07-22 15:29:20 +02:00
|
|
|
// Notification API
|
2017-05-16 18:03:50 +02:00
|
|
|
if(_browser_notify && !_status)
|
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(":");
|
2017-05-16 18:03:50 +02:00
|
|
|
// delete the message
|
|
|
|
var request = egw.json("notifications.notifications_ajax.update_status", [id[1], 'DISPLAYED']);
|
2016-07-11 11:59:50 +02:00
|
|
|
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);
|
|
|
|
}
|
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: {};
|
|
|
|
};
|
|
|
|
|
2017-05-15 17:34:27 +02:00
|
|
|
/**
|
|
|
|
* toggle notifications container
|
|
|
|
*/
|
|
|
|
notifications.prototype.toggle = function ()
|
|
|
|
{
|
2018-01-10 12:42:42 +01:00
|
|
|
var $egwpopup = jQuery('#egwpopup');
|
|
|
|
var $body = jQuery('body');
|
|
|
|
var $counter = jQuery('#egwpopup_fw_notifications');
|
|
|
|
|
|
|
|
if (!$egwpopup.is(":visible"))
|
|
|
|
{
|
|
|
|
$body.on('click', function(e){
|
|
|
|
if (!$counter.is(e.target) && e.originalEvent.target.className !== 'popup_note' &&
|
|
|
|
!$egwpopup.is(e.target) && $egwpopup.has(e.target).length == 0)
|
|
|
|
{
|
|
|
|
jQuery(this).off(e);
|
|
|
|
$egwpopup.slideToggle('fast');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$body.off('click');
|
|
|
|
}
|
2017-05-29 15:07:33 +02:00
|
|
|
// Remove popup_note as soon as message list is toggled
|
|
|
|
jQuery('.popup_note', '#egwpopup_fw_notifications').remove();
|
|
|
|
|
2017-05-15 17:34:27 +02:00
|
|
|
if ($egwpopup.length>0) $egwpopup.slideToggle('fast');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set new state of notifications counter
|
|
|
|
*/
|
|
|
|
notifications.prototype.counterUpdate = function ()
|
|
|
|
{
|
|
|
|
var $egwpopup_fw_notifications = jQuery('#egwpopup_fw_notifications');
|
2017-05-29 15:07:33 +02:00
|
|
|
var $popup_note = jQuery(document.createElement('div')).addClass('popup_note');
|
2017-05-16 18:03:50 +02:00
|
|
|
var counter = 0;
|
|
|
|
for (var id in notifymessages)
|
|
|
|
{
|
|
|
|
if (notifymessages[id]['status'] != 'SEEN') counter++;
|
|
|
|
}
|
|
|
|
if (counter > 0)
|
2017-05-15 17:34:27 +02:00
|
|
|
{
|
|
|
|
$egwpopup_fw_notifications.addClass('egwpopup_notify');
|
2017-05-16 18:03:50 +02:00
|
|
|
$egwpopup_fw_notifications.text(counter);
|
2017-05-29 15:07:33 +02:00
|
|
|
$egwpopup_fw_notifications.append($popup_note);
|
2018-01-23 22:07:27 +01:00
|
|
|
$popup_note.text(egw.lang('You have %1 unread notifications', counter));
|
2017-05-29 15:07:33 +02:00
|
|
|
setTimeout(function (){$popup_note.remove();}, 5000);
|
2017-05-15 17:34:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$egwpopup_fw_notifications.text(0);
|
|
|
|
$egwpopup_fw_notifications.removeClass('egwpopup_notify');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-22 15:29:20 +02:00
|
|
|
var lab = egw_LAB || $LAB;
|
|
|
|
var self = notifications;
|
|
|
|
lab.wait(function(){
|
2018-01-11 17:23:58 +01:00
|
|
|
var langRequire = jQuery('#notifications_script_id').attr('data-langRequire');
|
|
|
|
egw.langRequire(window, [JSON.parse(langRequire)], function()
|
|
|
|
{
|
2018-01-25 17:31:15 +01:00
|
|
|
var $egwpopup_fw = jQuery('#egwpopup_fw_notifications');
|
|
|
|
switch (egw.preference('notification_chain','notifications'))
|
|
|
|
{
|
|
|
|
case 'popup_only':
|
|
|
|
case 'popup_and_email':
|
|
|
|
case 'popup_or_email':
|
|
|
|
case 'all':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$egwpopup_fw.hide();
|
|
|
|
return;
|
|
|
|
}
|
2018-01-11 17:23:58 +01:00
|
|
|
if (typeof window.app == 'undefined') window.app = {};
|
2018-02-19 17:17:24 +01:00
|
|
|
|
|
|
|
window.onbeforeunload = function()
|
|
|
|
{
|
|
|
|
if (typeof egw.killAliveNotifications =='function') egw.killAliveNotifications();
|
|
|
|
};
|
2018-01-11 17:23:58 +01:00
|
|
|
window.app.notifications = new self();
|
|
|
|
// toggle notifications bar
|
|
|
|
jQuery('.egwpopup_toggle').click(function(){window.app.notifications.toggle();});
|
2018-01-25 17:31:15 +01:00
|
|
|
$egwpopup_fw.click(function(){window.app.notifications.toggle();});
|
2018-01-11 17:23:58 +01:00
|
|
|
jQuery(".egwpopup_deleteall", '#egwpopup').click(function(){
|
|
|
|
et2_dialog.show_dialog( function(_button){
|
|
|
|
if (_button == 2) window.app.notifications.delete_all();
|
|
|
|
},
|
|
|
|
egw.lang('Are you sure you want to delete all notifications?'),
|
|
|
|
egw.lang('Delete notifications'),
|
|
|
|
null, et2_dialog.BUTTON_YES_NO, et2_dialog.WARNING_MESSAGE, undefined, egw
|
|
|
|
);
|
|
|
|
});
|
|
|
|
jQuery(".egwpopup_seenall", '#egwpopup').click(function(){window.app.notifications.mark_all_seen()});
|
|
|
|
}, this);
|
2013-07-22 15:29:20 +02:00
|
|
|
});
|
|
|
|
})();
|