This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
/
Notification.js
251 lines (204 loc) · 8 KB
/
Notification.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/** @namespace window */
/** @namespace window.webkitNotifications */
/** @namespace window.external */
;(function(win, undefined) {
/*
Safari native methods required for Notifications do NOT run in strict mode.
*/
//"use strict";
// local variables
var PERMISSION_DEFAULT = 'default'; // The user decision is unknown; in this case the application will act as if permission was denied.
var PERMISSION_GRANTED = 'granted'; // The user has explicitly granted permission for the current origin to display system notifications.
var PERMISSION_DENIED = 'denied'; // The user has explicitly denied permission for the current origin to display system notifications.
var PERMISSION_NOTSUPPORTED = 'notsupported'; // The Notification API is not supported on current environment
// map for the old permission values
var PERMISSIONS = [PERMISSION_GRANTED, PERMISSION_DEFAULT, PERMISSION_DENIED, PERMISSION_NOTSUPPORTED];
var DIRESCTIONS = ['auto', 'ltr', 'rtl']
/*
* Keep the original/constructor defined requestPermission() method in local variable.
* The public requestPermission() method will be defind last and will call this private method.
*/
var requestPermission;
/*
IE does not support Notifications in the same meaning as other modern browsers.
On the other side, IE9+(except MS Edge) implement flashing pinned site taskbar buttons.
Each time new IE Notification is create, previous flashing and icon overlay is cleared.
So, we need to keep track of the notification that calls close method.
*/
var IENotificationIndex = -1;
/**
* Notification
* @constructor
*/
function Notification(title, options) {
var dir;
if (!arguments.length) {
throw TypeError('Failed to construct \'Notification\': 1 argument required, but only 0 present.');
}
if (options && 'object' !== typeof options) {
throw TypeError('Failed to construct \'Notification\': parameter 2 (\'options\') is not an object.');
}
dir = Object(options).dir;
if (dir && DIRESCTIONS[dir] === undefined) {
throw TypeError('Failed to construct \'Notification\': The provided value \'' + dir +'\' is not a valid enum value of type NotificationDirection.')
}
options = Object(options);
Object.defineProperties(this, {
'body': { value: String(options.body || '') },
'data': { value: options.data || null },
'dir': { value: dir },
'icon': { value: String(options.icon || '') },
'lang': { value: String(options.lang || '') },
'requireInteraction': { value: Boolean(options.requireInteraction) },
'silent': { value: Boolean(options.silent) },
'tag': { value: String(options.tag || '') },
'title': { value: String(title) },
'timestamp': { value: (new Date).getTime() }
});
}
Object.defineProperty(Notification, 'permission', {
enumerable: true,
get: function() {
return PERMISSION_NOTSUPPORTED;
}
});
Object.defineProperty(Notification, 'requestPermission', {
enumerable: true,
writable: true,
value: function(callback) {
callback(this.permission);
}
});
Notification.prototype.toString = function() {
return 'Notification';
}
/**
* IE Notification
* @constructor
*/
function IENotification(title, options) {
var notificationIndex = IENotificationIndex;
Notification.apply(this, arguments);
Object.defineProperties(this, {
close: {
value: function() {
if (notificationIndex === IENotificationIndex) {
win.external.msSiteModeClearIconOverlay();
}
}
}
});
// Clear any previous icon overlay
this.close();
// Set icon
if (this.icon) {
win.external.msSiteModeSetIconOverlay(this.icon, this.description || this.title);
}
// Blink icon
win.external.msSiteModeActivate();
notificationIndex = ++IENotificationIndex;
}
Object.defineProperty(IENotification, 'permission', {
enumerable: true,
get: function() {
var isTabPinned = win.external.msIsSiteMode();
return isTabPinned ? PERMISSION_GRANTED : PERMISSION_DENIED;
}
});
Object.defineProperty(IENotification, 'requestPermission', {
enumerable: true,
writable: true,
value: function(callback) {
return new Promise(function(resolve, reject) {
alert(this.PERMISSION_REQUEST_MESSAGE);
resolve(this.permission);
}.bind(this));
}
});
Object.defineProperty(IENotification, 'PERMISSION_REQUEST_MESSAGE', {
writable: true,
value: 'IE supports notifications in pinned mode only. Pin this page on your taskbar to receive notifications.'
});
IENotification.prototype = Notification.prototype;
/**
* WebKit Notification
* @constructor
*/
function WebKitNotification(title, options) {
Notification.call(this);
var notification = new webkitNotifications(title, options);
}
Object.defineProperty(WebKitNotification, 'permission', {
enumerable: true,
get: function() {
return PERMISSIONS[win.webkitNotifications.checkPermission()];
}
});
Object.defineProperty(WebKitNotification, 'requestPermission', {
enumerable: true,
writable: true,
value: function(callback) {
return new Promise(function(resolve, reject) {
win.webkitNotifications.requestPermission(function(permission) {
resolve(permission);
});
});
}
});
WebKitNotification.prototype = Notification.prototype;
/*
Check Notification support and create Notification
*/
try {
win.Notification = (
// W3C
win.Notification ||
// Opera Mobile/Android Browser
(win.webkitNotifications && WebKitNotification) ||
// IE9+ pinned site
(win.external && win.external.msIsSiteMode() !== undefined && IENotification)
);
} catch(e) {
// IE check may throws an exception in other browsers
} finally {
// Use empty Notification in case no support detected
win.Notification = win.Notification || Notification;
}
/*
Safari6 do not support Notification.permission.
Instead, it support Notification.permissionLevel()
*/
if (!win.Notification.permission) {
Object.defineProperty(win.Notification, 'permission', {
enumerable: true,
get: function() {
return this.permissionLevel();
}
});
}
/*
Notification.requestPermission should return a Promise(by spec).
Keep the original method and replace it with a custom one that
checks if the call of Notification.requestPermission returns a promise
and if not, then return a custom object that simulates the Promise object.
Specification:
Notification.requestPermission().then(callback);
Old Spec:
Notification.requestPermission(callback);
*/
requestPermission = win.Notification.requestPermission.bind(win.Notification);
Object.defineProperty(win.Notification, 'requestPermission', {
enumerable: true,
value: function() {
return new Promise(function(resolve, reject) {
var promise = requestPermission(function(permission) {
resolve(permission);
});
if (!(promise instanceof Promise)) {
return;
}
resolve(promise);
});
}
});
}(window));