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

egw.extend(ꞌnotificationꞌ)   B

Complexity

Conditions 2
Paths > 20000

Size

Total Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 86
rs 8.6583
c 2
b 0
f 0
cc 2
nc 55296
nop 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
B ��) 0 37 5
A ��) 0 9 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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);
0 ignored issues
show
Bug introduced by
The variable Notification does not seem to be initialized in case Notification && Notifica...ermission === "default" on line 63 is false. Are you sure this can never be the case?
Loading history...
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
});