-
Notifications
You must be signed in to change notification settings - Fork 16
/
screamer.js
150 lines (131 loc) · 3.84 KB
/
screamer.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
/*
* Authors: Paulo Oliveira and Willian Justen
* https://github.com/willianjsuten/screamer-js
* License: MIT license
*/
/* global define, exports: true, module*/
;(function(root, factory) {
'use strict';
if(typeof define === 'function' && define.amd) {
define('Screamer', factory);
}
else if(typeof exports === 'object') {
exports = module.exports = factory();
}
else {
root.Screamer = factory();
}
})(this, function() {
'use strict';
// Define Global variables
var Notification = window.Notification;
/**
* Our constructor
*
* @param {object}
*/
var Screamer = function(options) {
/**
* Auto initialize the object
* when new operator was forgoten
*/
if (!(this instanceof Screamer)) {
return new Screamer(options);
}
/**
* Start the method requesting permission
* to everything works
*/
Notification.requestPermission();
/**
* Notification API requires title to work properly,
* if title not defined or not string we throw an error.
*/
if (options.title !== undefined && typeof options.title !== 'string') {
throw new Error('Notify(): first arg (title) must be a string.');
}
/**
* Our options passed to Notification
* look at https://developer.mozilla.org/en-US/docs/Web/API/notification
* to more options.
*
* @type {object}
*/
this.options = options;
};
/**
* This method should return if browser supports
* Web Notifications.
*
* @return {boolean}
*/
Screamer.verifySupport = function() {
return (!Notification) ? false : true;
};
/**
* Check if support exists and verify if permission exists,
* if not, requests a permission and verify if granted.
*
* @param {string} - just for test purposes
* @return {boolean}
*/
Screamer.checkPermission = function(perm) {
var permission = (perm === 'granted') ? perm : Notification.permission;
if (!Screamer.verifySupport()) {
return false;
}
if (permission === 'granted') {
return true;
}
else if (permission !== 'denied') {
Notification.requestPermission(function(permission) {
if (permission == 'granted') {
return true;
}
});
}
};
/**
* This fade the notification.
*
* @param {object} - object created by notify
* @param {int} - time in miliseconds
*/
Screamer.prototype.fadeNotification = function(notify, timeout) {
setTimeout(notify.close.bind(notify), timeout);
};
/**
* This handle the 'onclose' event, which came from Notification
*/
Screamer.onClose = function(options) {
if (typeof options.after === 'function') {
options.after();
}
}
Screamer.before = function(options) {
if (typeof options.before === 'function') {
options.before();
}
}
/**
* It should be create a notification if permission is granted.
* If not allowed. should fail sillently and logs that.
*/
Screamer.prototype.notify = function() {
if (Screamer.checkPermission()) {
var notify, options = this.options;
Screamer.before(options);
notify = new Notification(options.title, options);
notify.onclose = function() {
Screamer.onClose(options)
}
if (options.fade) {
this.fadeNotification(notify, options.fade);
}
}
else {
console.log('Permission Denied!');
}
};
return Screamer;
});