forked from gravmatt/js-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify.js
More file actions
45 lines (44 loc) · 1.52 KB
/
notify.js
File metadata and controls
45 lines (44 loc) · 1.52 KB
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
/*! Copyright (c) 2016-2017 Rene Tanczos <gravmatt@gmail.com> - The MIT License (MIT) */
(function(window, document, undefined) {
var notify = function (title, options) {
var guid = function() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
};
if (!window.Notification) {
return;
}
if (Notification.permission === 'default') {
Notification.requestPermission(function () {
title && notify(title, options);
});
}
else if (Notification.permission === 'granted') {
if(!title) return undefined;
var opt = options || {}
opt.tag = guid()
var n = new Notification(title, opt);
n.onclick = function () {
opt.onclick && opt.onclick(opt);
this.close();
};
n.onclose = function () {
opt.onclose && opt.onclose(opt);
};
return n;
}
else if (Notification.permission === 'denied') {
(options && options.ondenied) && options.ondenied(options);
}
};
if ( typeof module === "object" && module && typeof module.exports === "object" ) {
module.exports = notify;
} else {
window.notify = notify;
if ( typeof define === "function" && define.amd ) {
define( "notify", [], function () { return notify; } );
}
}
})(window, document);