Completed
Push — 16.1 ( fd1d11...77cd21 )
by Hadi
16:38
created

notifications.append   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
nc 8
nop 3
dl 0
loc 50
rs 8.8571
c 1
b 0
f 0
1
/**
2
 * EGroupware Notifications - clientside javascript
3
 *
4
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
5
 * @package notifications
6
 * @subpackage ajaxpoup
7
 * @link http://www.egroupware.org
8
 * @author Cornelius Weiss <[email protected]>, Christian Binder <[email protected]>, Ralf Becker <[email protected]>
9
 * @version $Id$
10
 */
11
12
/**
13
 * Installs app.notifications used to poll notifications from server and display them
14
 */
15
(function()
16
{
17
	var notifymessages = {};
18
	var EGW_BROWSER_NOTIFY_ALLOWED = 0;
19
20
	/**
21
	 * Constructor inits polling and installs handlers, polling frequence is passed via data-poll-interval of script tag
22
	 */
23
	function notifications() {
24
		var notification_script = document.getElementById('notifications_script_id');
25
		var popup_poll_interval = notification_script && notification_script.getAttribute('data-poll-interval');
26
		this.setTimeout(popup_poll_interval || 60);
27
		jQuery('#egwpopup_ok_button').click(jQuery.proxy(this.button_ok, this));
28
		jQuery('#egwpopup_close_button').click(jQuery.proxy(this.button_close, this));
29
		jQuery('#notificationbell').click(jQuery.proxy(this.display, this));
30
		// query notifictions now
31
		this.get_notifications();
32
	};
33
34
	/**
35
	 * Poll server for new notifications
36
	 */
37
	notifications.prototype.get_notifications = function()
38
	{
39
		egw.json(
40
			"notifications.notifications_ajax.get_notifications",
41
			this.check_browser_notify()
42
		).sendRequest(true);
43
	};
44
45
	/**
46
	 * Poll server in given frequency via Ajax
47
	 * @param _i
48
	 */
49
	notifications.prototype.setTimeout = function(_i) {
50
		var self = this;
51
		window.setTimeout(function(){
52
			self.get_notifications();
53
			self.setTimeout(_i);
54
		}, _i*1000);
55
	};
56
57
	/**
58
	 * Check to see if browser supports / allows desktop notifications
59
	 */
60
	notifications.prototype.check_browser_notify = function() {
61
		return egw.checkNotification();
62
	};
63
64
	/**
65
	 * Display notifications window
66
	 */
67
	notifications.prototype.display = function() {
68
		var egwpopup;
69
		var egwpopup_message;
70
		var Browserwidth;
71
		var Browserheight;
72
		var egwpopup_ok_button;
73
		egwpopup = document.getElementById("egwpopup");
74
		egwpopup_message = document.getElementById("egwpopup_message");
75
		egwpopup_ok_button = document.getElementById("egwpopup_ok_button");
76
		egwpopup.style.display = "block";
77
		egwpopup.style.position = "absolute";
78
		egwpopup.style.width = "500px";
79
		Browserwidth = (window.innerWidth || document.body.clientWidth || 640);
80
		Browserheight = (window.innerHeight || document.body.clientHeight || 480);
81
		egwpopup.style.left = (Browserwidth/2 - 250) + "px";
82
		egwpopup.style.top = (Browserheight/4) + "px";
83
		egwpopup_message.style.maxHeight = (Browserheight/2) + "px";
84
		for(var show in notifymessages) break;
85
		egwpopup_message.innerHTML = notifymessages[show]['message'];
86
87
		// Activate links
88
		jQuery('div[data-id],div[data-url]', egwpopup_message).on('click',
89
			function() {
90
				if(this.dataset.id)
91
				{
92
					egw.open(this.dataset.id,this.dataset.app);
93
				}
94
				else
95
				{
96
					egw.open_link(this.dataset.url,'_blank',this.dataset.popup);
97
				}
98
			}
99
		).addClass('et2_link');
100
		var num = 0;
101
		for(var id in notifymessages) ++num;
102
		if(num-1 > 0 ) {
103
			egwpopup_ok_button.value = "OK (" + (num-1) + ")";
104
		} else {
105
			egwpopup_ok_button.value = "OK";
106
		}
107
		if(window.webkitNotifications && window.webkitNotifications.checkPermission() != EGW_BROWSER_NOTIFY_ALLOWED &&
108
			jQuery('#desktop_perms').length == 0)
109
		{
110
			var label = 'Desktop notifications';
111
			try {
112
				if(egw) label = egw.lang(label);
113
			} catch(err) {}
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
114
			var desktop_button = jQuery('<button id="desktop_perms">' + label + '</button>')
115
				.click(function() {
116
					window.webkitNotifications.requestPermission();
117
					jQuery(this).hide();
118
				});
119
			desktop_button.appendTo(jQuery(egwpopup_ok_button).parent());
120
		}
121
	};
122
123
	/**
124
	 * Display or hide notifcation-bell
125
	 *
126
	 * @param {string} mode "active"
127
	 */
128
	notifications.prototype.bell = function(mode) {
129
		var notificationbell;
130
		notificationbell = document.getElementById("notificationbell");
131
		if(mode == "active") {
132
			notificationbell.style.display = "inline";
133
		} else {
134
			notificationbell.style.display = "none";
135
		}
136
	};
137
138
	/**
139
	 * Callback for OK button: confirms message on server and hides display
140
	 */
141
	notifications.prototype.button_ok = function() {
142
		var egwpopup;
143
		var egwpopup_message;
144
		egwpopup = document.getElementById("egwpopup");
145
		egwpopup_message = document.getElementById("egwpopup_message");
146
		egwpopup_message.scrollTop = 0;
147
148
		for(var confirmed in notifymessages) break;
149
		var request = egw.json("notifications.notifications_ajax.confirm_message", [confirmed]);
150
		request.sendRequest(true);
151
		delete notifymessages[confirmed];
152
153
		for(var id in notifymessages) break;
154
		if (id == undefined) {
155
			egwpopup.style.display = "none";
156
			egwpopup_message.innerHTML = "";
157
			this.bell("inactive");
158
		} else {
159
			this.display();
160
		}
161
	};
162
163
	/**
164
	 * Callback for close button: close and mark all as read
165
	 */
166
	notifications.prototype.button_close = function() {
167
		var ids = new Array();
168
		for(var id in notifymessages) {
169
			ids.push(id);
170
		}
171
		var request = egw.json("notifications.notifications_ajax.confirm_message", [ids]);
172
		request.sendRequest(true);
173
		notifymessages = {};
174
		var egwpopup = document.getElementById("egwpopup");
175
		var egwpopup_message = document.getElementById("egwpopup_message");
176
		egwpopup.style.display = "none";
177
		egwpopup_message.innerHTML = "";
178
		this.bell("inactive");
179
	};
180
181
	/**
182
	 * Add message to internal display-queue
183
	 *
184
	 * @param _id
185
	 * @param _message
186
	 * @param _browser_notify
187
	 */
188
	notifications.prototype.append = function(_id, _message, _browser_notify) {
189
		if(!this.check_browser_notify() || typeof notifymessages[_id] != 'undefined')
190
		{
191
			notifymessages[_id] = {message:_message};
192
			return;
193
		}
194
195
		var data = this.getData(_message);
196
		// Prevent the same thing popping up multiple times
197
		notifymessages[_id] = {message:_message, data: data};
198
		// Notification API
199
		if(_browser_notify)
200
		{
201
			egw.notification(data.title, {
202
				tag: data.app+":"+_id,
203
				body: data.message,
204
				icon: data.icon,
205
				onclose:function(e){
206
					// notification id
207
					var id = this.tag.split(":");
208
					// confirm the message
209
					var request = egw.json("notifications.notifications_ajax.confirm_message", [id[1]]);
210
					request.sendRequest();
211
				},
212
				onclick:function(e){
213
					// notification id
214
					var id = this.tag.split(":");
215
216
					// get the right data from messages object
217
					var notify = notifymessages[id[1]];
218
219
					if (!notifymessages[id[1]]) this.close();
220
221
					if (notify && notify.data && notify.data.id)
222
					{
223
						egw.open(notify.data.id, notify.data.app);
224
					}
225
					else if (notify && notify.data)
226
					{
227
						egw.open_link(notify.data.url,'_blank',notify.data.popup);
228
					}
229
230
					var request = egw.json("notifications.notifications_ajax.confirm_message", [id[1]]);
231
					request.sendRequest();
232
					delete notifymessages[id[1]];
233
					this.close();
234
				}
235
			});
236
		}
237
	};
238
239
	/**
240
	 * Extract useful data out of HTML message
241
	 *
242
	 * @param {type} _message
243
	 * @returns {notificationajaxpopup_L15.notifications.prototype.getData.data}
244
	 */
245
	notifications.prototype.getData = function (_message) {
246
		var dom = jQuery(document.createElement('div')).html(_message);;
247
		var link = dom.find('div[data-id],div[data-url]');
248
		var data = {
249
			message: dom.text(),
250
			title: link.text(),
251
			icon: link.find('img').attr('src')
252
		};
253
		jQuery.extend(data,link.data());
254
		return typeof data == 'object'? data: {};
255
	};
256
257
	var lab = egw_LAB || $LAB;
258
	var self = notifications;
259
	lab.wait(function(){
260
		if (typeof window.app == 'undefined') window.app = {};
261
		window.app.notifications = new self();
262
	});
263
})();
264