1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | if (window.Notification && window.Notification.permission !== 'granted') {
|
13 | window.Notification.requestPermission();
|
14 | }
|
15 |
|
16 | var iconURL = '';
|
17 | var repeatableEffects = ['flash', 'scroll'];
|
18 | var defaultNotification = {
|
19 | title: 'iNotify !',
|
20 | body: 'You have a new message.',
|
21 | openurl: ''
|
22 | };
|
23 |
|
24 | function jsonArguments(news, olds) {
|
25 | for (var a in olds) {
|
26 | if (news[a]) {
|
27 | olds[a] = news[a];
|
28 | }
|
29 | }
|
30 |
|
31 | return olds;
|
32 | }
|
33 |
|
34 | function isArray(value) {
|
35 | return Object.prototype.toString.call(value) === '[object Array]';
|
36 | }
|
37 |
|
38 | function createAudio(url) {
|
39 | var audioElm = document.createElement('audio');
|
40 | var source;
|
41 |
|
42 | if (isArray(url) && url.length > 0) {
|
43 | for (var i = 0; i < url.length; i++) {
|
44 | source = document.createElement('source');
|
45 | source.src = url[i];
|
46 | source.type = "audio/".concat(getExtension(url[i]));
|
47 | audioElm.appendChild(source);
|
48 | }
|
49 | } else {
|
50 | audioElm.src = url;
|
51 | }
|
52 |
|
53 | return audioElm;
|
54 | }
|
55 |
|
56 | function getFavicon(setting) {
|
57 | var ic = document.querySelectorAll('link[rel~=shortcut]')[0];
|
58 |
|
59 | if (!ic) {
|
60 | ic = changeFavicon('O', setting);
|
61 | }
|
62 |
|
63 | return ic;
|
64 | }
|
65 |
|
66 | function getExtension(fileName) {
|
67 | return fileName.match(/\.([^\\.]+)$/)[1];
|
68 | }
|
69 |
|
70 | function changeFavicon(num, settings) {
|
71 | var canvas = document.createElement('canvas');
|
72 | var head = document.getElementsByTagName('head')[0];
|
73 | var linkTag = document.createElement('link');
|
74 | var ctx = null;
|
75 | canvas.height = 32;
|
76 | canvas.width = 32;
|
77 | ctx = canvas.getContext('2d');
|
78 | ctx.fillStyle = settings.backgroundColor;
|
79 | ctx.fillRect(0, 0, 32, 32);
|
80 | ctx.textAlign = 'center';
|
81 | ctx.font = '22px "helvetica", sans-serif';
|
82 | ctx.fillStyle = settings.textColor;
|
83 | num && ctx.fillText(num, 16, 24);
|
84 |
|
85 | linkTag.setAttribute('rel', 'shortcut icon');
|
86 | linkTag.setAttribute('type', 'image/x-icon');
|
87 | linkTag.setAttribute('id', "new".concat(settings.id));
|
88 | linkTag.setAttribute('href', canvas.toDataURL('image/png'));
|
89 | iconURL = canvas.toDataURL('image/png');
|
90 | return head.appendChild(linkTag);
|
91 | }
|
92 |
|
93 | function Notify(config) {
|
94 | if (config) {
|
95 | this.init(config);
|
96 | }
|
97 | }
|
98 |
|
99 | Notify.prototype = {
|
100 | init: function init(config) {
|
101 | if (!config) {
|
102 | config = {};
|
103 | }
|
104 |
|
105 | this.interval = config.interval || 100;
|
106 |
|
107 | this.effect = config.effect || 'flash';
|
108 |
|
109 | this.title = config.title || document.title;
|
110 |
|
111 | this.message = config.message || this.title;
|
112 |
|
113 | this.onclick = config.onclick || this.onclick;
|
114 |
|
115 | this.openurl = config.openurl || this.openurl;
|
116 |
|
117 | this.updateFavicon = config.updateFavicon || {
|
118 | id: 'favicon',
|
119 | textColor: '#fff',
|
120 | backgroundColor: '#2F9A00'
|
121 | };
|
122 | this.audio = config.audio || '';
|
123 | this.favicon = getFavicon(this.updateFavicon);
|
124 | this.cloneFavicon = this.favicon.cloneNode(true);
|
125 | iconURL = config.notification && config.notification.icon ? config.notification.icon : config.icon ? config.icon : this.favicon.href;
|
126 | defaultNotification.icon = iconURL;
|
127 | this.notification = config.notification || defaultNotification;
|
128 |
|
129 | if (this.audio && this.audio.file) {
|
130 | this.setURL(this.audio.file);
|
131 | }
|
132 |
|
133 | return this;
|
134 | },
|
135 | render: function render() {
|
136 | if (this.effect === 'flash') {
|
137 | document.title = this.title === document.title ? this.message : this.title;
|
138 | } else if (this.effect === 'scroll') {
|
139 | var title = this.message || document.title;
|
140 |
|
141 | if (!this.scrollTitle || !this.scrollTitle.slice(1)) {
|
142 | document.title = title;
|
143 | this.scrollTitle = title;
|
144 | } else {
|
145 | this.scrollTitle = this.scrollTitle.slice(1);
|
146 | document.title = this.scrollTitle;
|
147 | }
|
148 | }
|
149 |
|
150 | return this;
|
151 | },
|
152 |
|
153 | setTitle: function setTitle(str) {
|
154 | if (str === true) {
|
155 | if (repeatableEffects.indexOf(this.effect) >= 0) {
|
156 | return this.addTimer();
|
157 | }
|
158 | } else if (str) {
|
159 | this.message = str;
|
160 | this.scrollTitle = '';
|
161 | this.addTimer();
|
162 | } else {
|
163 | this.clearTimer();
|
164 | }
|
165 |
|
166 | return this;
|
167 | },
|
168 | setURL: function setURL(url) {
|
169 | if (url) {
|
170 | if (this.audioElm) {
|
171 | this.audioElm.remove();
|
172 | }
|
173 |
|
174 | this.audioElm = createAudio(url);
|
175 | document.body.appendChild(this.audioElm);
|
176 | }
|
177 |
|
178 | return this;
|
179 | },
|
180 | loopPlay: function loopPlay() {
|
181 | this.setURL();
|
182 | this.audioElm.loop = true;
|
183 | this.player();
|
184 | return this;
|
185 | },
|
186 | stopPlay: function stopPlay() {
|
187 | this.audioElm && (this.audioElm.loop = false, this.audioElm.pause());
|
188 | return this;
|
189 | },
|
190 |
|
191 | player: function player() {
|
192 | if (!this.audio || !this.audio.file) {
|
193 | return;
|
194 | }
|
195 |
|
196 | if (!this.audioElm) {
|
197 | this.audioElm = createAudio(this.audio.file);
|
198 | document.body.appendChild(this.audioElm);
|
199 | }
|
200 |
|
201 | this.audioElm.play();
|
202 | return this;
|
203 | },
|
204 | notify: function notify(json) {
|
205 | var nt = this.notification;
|
206 | var url = json.openurl ? json.openurl : this.openurl;
|
207 | var onclick = json.onclick ? json.onclick : this.onclick;
|
208 |
|
209 | if (window.Notification) {
|
210 | if (json) {
|
211 | nt = jsonArguments(json, nt);
|
212 | } else {
|
213 | nt = defaultNotification;
|
214 | }
|
215 |
|
216 | var option = {};
|
217 | option.icon = json.icon ? json.icon : iconURL;
|
218 | option.body = nt.body;
|
219 | if (json.dir) option.dir = json.dir;
|
220 | var n = new Notification(nt.title, option);
|
221 |
|
222 | n.onclick = function () {
|
223 | onclick && typeof onclick === 'function' && onclick(n);
|
224 | url && window.open(url);
|
225 | };
|
226 |
|
227 | n.onshow = function () {
|
228 | json.onshow && typeof json.onshow === 'function' && json.onshow(n);
|
229 | };
|
230 |
|
231 | n.onclose = function () {
|
232 | json.onclose && typeof json.onclose === 'function' && json.onclose(n);
|
233 | };
|
234 |
|
235 | n.onerror = function () {
|
236 | json.onerror && typeof json.onerror === 'function' && json.onerror(n);
|
237 | };
|
238 |
|
239 | this.Notifiy = n;
|
240 | }
|
241 |
|
242 | return this;
|
243 | },
|
244 |
|
245 | isPermission: function isPermission() {
|
246 | return window.Notification && Notification.permission === 'granted';
|
247 | },
|
248 |
|
249 | setInterval: function setInterval(num) {
|
250 | if (num) {
|
251 | this.interval = num;
|
252 | this.addTimer();
|
253 | }
|
254 |
|
255 | return this;
|
256 | },
|
257 |
|
258 | setFavicon: function setFavicon(num) {
|
259 | if (!num && num !== 0) {
|
260 | return this.faviconClear();
|
261 | }
|
262 |
|
263 | var oldicon = document.getElementById("new".concat(this.updateFavicon.id));
|
264 |
|
265 | if (this.favicon) {
|
266 | this.favicon.remove();
|
267 | }
|
268 |
|
269 | if (oldicon) {
|
270 | oldicon.remove();
|
271 | }
|
272 |
|
273 | this.updateFavicon.num = num;
|
274 | changeFavicon(num, this.updateFavicon);
|
275 | return this;
|
276 | },
|
277 |
|
278 | setFaviconColor: function setFaviconColor(color) {
|
279 | if (color) {
|
280 | this.faviconRemove();
|
281 | this.updateFavicon.textColor = color;
|
282 | changeFavicon(this.updateFavicon.num, this.updateFavicon);
|
283 | }
|
284 |
|
285 | return this;
|
286 | },
|
287 |
|
288 | setFaviconBackgroundColor: function setFaviconBackgroundColor(color) {
|
289 | if (color) {
|
290 | this.faviconRemove();
|
291 | this.updateFavicon.backgroundColor = color;
|
292 | changeFavicon(this.updateFavicon.num, this.updateFavicon);
|
293 | }
|
294 |
|
295 | return this;
|
296 | },
|
297 | faviconRemove: function faviconRemove() {
|
298 | this.faviconClear();
|
299 | var oldicon = document.getElementById("new".concat(this.updateFavicon.id));
|
300 |
|
301 | if (this.favicon) {
|
302 | this.favicon.remove();
|
303 | }
|
304 |
|
305 | if (oldicon) {
|
306 | oldicon.remove();
|
307 | }
|
308 | },
|
309 |
|
310 | addTimer: function addTimer() {
|
311 | this.clearTimer();
|
312 |
|
313 | if (repeatableEffects.indexOf(this.effect) >= 0) {
|
314 | this.timer = setInterval(this.render.bind(this), this.interval);
|
315 | }
|
316 |
|
317 | return this;
|
318 | },
|
319 | close: function close() {
|
320 | if (this.Notifiy) this.Notifiy.close();
|
321 | },
|
322 |
|
323 | faviconClear: function faviconClear() {
|
324 | var newicon = document.getElementById("new".concat(this.updateFavicon.id));
|
325 | var head = document.getElementsByTagName('head')[0];
|
326 | var ficon = document.querySelectorAll('link[rel~=shortcut]');
|
327 | newicon && newicon.remove();
|
328 |
|
329 | if (ficon.length > 0) {
|
330 | for (var i = 0; i < ficon.length; i++) {
|
331 | ficon[i].remove();
|
332 | }
|
333 | }
|
334 |
|
335 | head.appendChild(this.cloneFavicon);
|
336 | iconURL = this.cloneFavicon.href;
|
337 | this.favicon = this.cloneFavicon;
|
338 | return this;
|
339 | },
|
340 |
|
341 | clearTimer: function clearTimer() {
|
342 | this.timer && clearInterval(this.timer);
|
343 | document.title = this.title;
|
344 | return this;
|
345 | }
|
346 | };
|
347 |
|
348 | export default Notify;
|