forked from alexgibson/notify.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.js
201 lines (165 loc) · 5.48 KB
/
notify.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
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD environment
define('notify', [], function () {
return factory(root, document);
});
} else {
// Browser environment
root.Notify = factory(root, document);
}
}(this, function (w, d) {
'use strict';
function Notify(title, options) {
this.title = typeof title === 'string' ? title : null;
this.options = {
icon: '',
body: '',
tag: '',
notifyShow: null,
notifyClose: null,
notifyClick: null,
notifyError: null,
permissionDenied: null
};
this.permission = null;
if (!this.isSupported()) {
return;
}
if (!this.title) {
throw new Error('Notify(): first arg (title) must be a string.');
}
//User defined options for notification content
if (typeof options === 'object') {
for (var i in options) {
if (options.hasOwnProperty(i)) {
this.options[i] = options[i];
}
}
//callback when notification is displayed
if (typeof this.options.notifyShow === 'function') {
this.onShowCallback = this.options.notifyShow;
}
//callback when notification is closed
if (typeof this.options.notifyClose === 'function') {
this.onCloseCallback = this.options.notifyClose;
}
//callback when notification is clicked
if (typeof this.options.notifyClick === 'function') {
this.onClickCallback = this.options.notifyClick;
}
//callback when notification throws error
if (typeof this.options.notifyError === 'function') {
this.onErrorCallback = this.options.notifyError;
}
//callback user denies permission for notification
if (typeof this.options.permissionDenied === 'function') {
this.onPermissionDeniedCallback = this.options.permissionDenied;
}
}
}
Notify.prototype.requestPermission = function () {
var that = this;
w.Notification.requestPermission(function (perm) {
that.permission = perm;
switch (that.permission) {
case 'granted':
that.showNotification();
break;
case 'denied':
that.onPermissionDenied();
break;
}
});
};
Notify.prototype.show = function () {
if (!this.isSupported()) { return; }
if (w.Notification) {
if (this.permission === 'granted') {
this.showNotification();
} else {
this.requestPermission();
}
} else {
this.showNotification();
}
};
Notify.prototype.showNotification = function () {
if (w.Notification) {
this.myNotify = new Notification(this.title, {
'body': this.options.body,
'tag' : this.options.tag,
'icon' : this.options.icon
});
this.myNotify.addEventListener('show', this, false);
this.myNotify.addEventListener('error', this, false);
} else {
this.myNotify = navigator.mozNotification.createNotification(
this.title,
this.options.body
);
this.myNotify.show();
}
this.myNotify.addEventListener('close', this, false);
this.myNotify.addEventListener('click', this, false);
};
Notify.prototype.onShowNotification = function () {
if (this.onShowCallback) {
this.onShowCallback();
}
};
Notify.prototype.onCloseNotification = function () {
if (this.onCloseCallback) {
this.onCloseCallback();
}
this.destroy();
};
Notify.prototype.onClickNotification = function () {
if (this.onClickCallback) {
this.onClickCallback();
}
};
Notify.prototype.onErrorNotification = function () {
if (this.onErrorCallback) {
this.onErrorCallback();
}
this.destroy();
};
Notify.prototype.onPermissionDenied = function () {
if (this.onPermissionDeniedCallback) {
this.onPermissionDeniedCallback();
}
};
Notify.prototype.destroy = function () {
if (w.Notification) {
this.myNotify.removeEventListener('show', this, false);
this.myNotify.removeEventListener('error', this, false);
}
this.myNotify.removeEventListener('close', this, false);
this.myNotify.removeEventListener('click', this, false);
};
Notify.prototype.isSupported = function () {
if (w.Notification || 'mozNotification' in navigator) {
return true;
}
return false;
};
Notify.prototype.handleEvent = function (e) {
switch (e.type) {
case 'show':
this.onShowNotification(e);
break;
case 'close':
this.onCloseNotification(e);
break;
case 'click':
this.onClickNotification(e);
break;
case 'error':
this.onErrorNotification(e);
break;
}
};
return Notify;
}));