|
1
|
|
|
/** |
|
2
|
|
|
* EGroupware clientside API object |
|
3
|
|
|
* |
|
4
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
|
5
|
|
|
* @package etemplate |
|
6
|
|
|
* @subpackage api |
|
7
|
|
|
* @link http://www.egroupware.org |
|
8
|
|
|
* @author Hadi Nategh <hn-AT-stylite.de> |
|
9
|
|
|
* @version $Id: $ |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/*egw:uses |
|
13
|
|
|
egw_core; |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Methods to display browser notification |
|
18
|
|
|
* |
|
19
|
|
|
* @augments Class |
|
20
|
|
|
* @param {string} _app application name object is instanciated for |
|
21
|
|
|
* @param {object} _wnd window object is instanciated for |
|
22
|
|
|
* |
|
23
|
|
|
* @return {object} defined functions of module |
|
24
|
|
|
*/ |
|
25
|
|
|
egw.extend('notification', egw.MODULE_WND_LOCAL, function(_app, _wnd) |
|
26
|
|
|
{ |
|
27
|
|
|
"use strict"; |
|
28
|
|
|
|
|
29
|
|
|
// Notification permission, the default value is 'default' which is equivalent to 'denied' |
|
30
|
|
|
var permission = 'default'; |
|
31
|
|
|
|
|
32
|
|
|
if (typeof Notification != 'undefined') |
|
33
|
|
|
{ |
|
34
|
|
|
permission = Notification.permission; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return { |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* @param {string} _title a string to be shown as notification message |
|
42
|
|
|
* @param {object} _options an object of Notification possible options: |
|
43
|
|
|
* options = { |
|
44
|
|
|
* dir: // direction of notification to be shown rtl, ltr or auto |
|
45
|
|
|
* lang: // a valid BCP 47 language tag |
|
46
|
|
|
* body: // DOM body |
|
47
|
|
|
* icon: // parse icon URL, default icon is app icon |
|
48
|
|
|
* tag: // a string value used for tagging an instance of notification, default is app name |
|
49
|
|
|
* onclick: // Callback function dispatches on click on notification message |
|
50
|
|
|
* onshow: // Callback function dispatches when notification is shown |
|
51
|
|
|
* onclose: // Callback function dispateches on notification close |
|
52
|
|
|
* onerror: // Callback function dispatches on error, default is a egw.debug log |
|
53
|
|
|
* } |
|
54
|
|
|
* @return {boolean} false if Notification is not supported by browser |
|
55
|
|
|
*/ |
|
56
|
|
|
notification: function (_title, _options) |
|
57
|
|
|
{ |
|
58
|
|
|
// Check if the notification is supported by browser |
|
59
|
|
|
if (typeof Notification == 'undefined') return false; |
|
60
|
|
|
|
|
61
|
|
|
var self = this; |
|
62
|
|
|
// Check and ask for permission |
|
63
|
|
|
if (Notification && Notification.requestPermission && permission === 'default') Notification.requestPermission (function(_permission){ |
|
64
|
|
|
permission = _permission; |
|
65
|
|
|
if (permission === 'granted') self.notification(_title,_options); |
|
66
|
|
|
}); |
|
67
|
|
|
|
|
68
|
|
|
// All options including callbacks |
|
69
|
|
|
var options = _options || {}; |
|
70
|
|
|
|
|
71
|
|
|
// Options used for creating Notification instane |
|
72
|
|
|
var inst_options = { |
|
73
|
|
|
tag: options.tag || egw.app_name(), |
|
74
|
|
|
dir: options.dir || 'ltr', |
|
75
|
|
|
lang: options.lang || egw.preference('lang', 'common'), |
|
76
|
|
|
body: options.body || '', |
|
77
|
|
|
icon: options.icon || egw.image('navbar', egw.app_name()) |
|
78
|
|
|
}; |
|
79
|
|
|
|
|
80
|
|
|
// Create an instance of Notification object |
|
81
|
|
|
var notification = new Notification(_title, inst_options); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
// Callback function dispatches on click on notification message |
|
84
|
|
|
notification.onclick = options.onclick || ''; |
|
85
|
|
|
// Callback function dispatches when notification is shown |
|
86
|
|
|
notification.onshow = options.onshow || ''; |
|
87
|
|
|
// Callback function dispateches on notification close |
|
88
|
|
|
notification.onclose = options.onclose || ''; |
|
89
|
|
|
// Callback function dispatches on error |
|
90
|
|
|
notification.onerror = options.onerror || function (e) {egw.debug('Notification failed because of ' + e);}; |
|
91
|
|
|
|
|
92
|
|
|
}, |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Check Notification availability by browser |
|
96
|
|
|
* |
|
97
|
|
|
* @returns {Boolean} true if notification is supported and permitted otherwise false |
|
98
|
|
|
*/ |
|
99
|
|
|
checkNotification: function () { |
|
100
|
|
|
// Ask for permission if there's nothing decided yet |
|
101
|
|
|
if (Notification && Notification.requestPermission && permission == 'default') { |
|
102
|
|
|
Notification.requestPermission (function(_permission){ |
|
103
|
|
|
permission = _permission; |
|
104
|
|
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
return (Notification && Notification.requestPermission && permission == 'granted'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
}; |
|
110
|
|
|
}); |