forked from jmsanpascual/angular-desktop-notification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-desktop-notification.js
More file actions
132 lines (107 loc) · 4.87 KB
/
angular-desktop-notification.js
File metadata and controls
132 lines (107 loc) · 4.87 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
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
(function(window) {
'use strict';
window.angular
.module('ngDesktopNotification', [])
.provider('desktopNotification', desktopNotification)
.constant('PERMISSIONS', {
DEFAULT: 'default',
GRANTED: 'granted',
DENIED: 'denied'
});
/* @ngInject */
function desktopNotification() {
var settings = {
autoClose: true,
duration: 5,
showOnPageHidden: false
};
return {
config: config,
$get: ['$q', '$timeout', 'PERMISSIONS', factory]
};
function config(options) {
for (var key in options) {
if (settings.hasOwnProperty(key) && options[key] != undefined) {
settings[key] = options[key];
}
}
}
function factory($q, $timeout, PERMISSIONS) {
var Notification = window.Notification || window.mozNotification || window.webkitNotification,
service = {
isSupported: isSupported,
currentPermission: currentPermission,
requestPermission: requestPermission,
show: showNotification,
permissions: {
'default': PERMISSIONS.DEFAULT,
'granted': PERMISSIONS.GRANTED,
'denied': PERMISSIONS.DENIED
}
};
return service;
// Public API
function isSupported() {
return ! (typeof Notification === 'undefined');
}
function currentPermission() {
// Wrap with a function, so that we can extend this easily to
// support getting permissions using older API versions
return (Notification || {}).permission;
}
function requestPermission() {
if (! isSupported()) return $q.reject('Notification API not supported');
var deferred = $q.defer();
Notification.requestPermission().then(requestPermission);
return deferred.promise;
// Convert the ES6 promise to angular's $q promise
function requestPermission(permission) {
if (PERMISSIONS.GRANTED === permission) {
deferred.resolve(permission);
} else {
deferred.reject(permission);
}
}
}
function showNotification(title, options) {
// Ensures that options is always an object
options = options || {};
// Check first if supported, validate arguments, then check if
// showOnPageHidden property is set to true, if yes then proceed
// on checking if page is visible, lastly check if
// notification is disabled by the client
if (! isSupported() || ! _isArgsValid(title, options) ||
_isPageVisible(options.showOnPageHidden) ||
currentPermission() !== PERMISSIONS.GRANTED) return;
var notification = new Notification(title, options),
autoClose = (options.autoClose === undefined) ? settings.autoClose : options.autoClose,
duration = options.duration || settings.duration;
notification.onclick = options.onClick;
// If autoClose is set to true, close the notification using the duration
if (autoClose) _autoCloseAfter(notification, duration);
return notification;
}
// Private functions
function _isArgsValid(title, options) {
var isTitleString = angular.isString(title),
isOnClickFunction = (! options.onClick || angular.isFunction(options.onClick));
return (isTitleString && isOnClickFunction);
}
function _isPageVisible(showOnPageHidden) {
// Check both showOnPageHidden parameter and default
if (! showOnPageHidden && ! settings.showOnPageHidden) return;
return ! (
window.document.hidden ||
// Uncomment when MS support is added
// window.document.msHidden ||
window.document.mozHidden ||
window.document.webkitHidden
);
}
function _autoCloseAfter(notification, duration) {
var durationInMs = duration * 1000;
$timeout(notification.close.bind(notification), durationInMs, false);
}
}
}
})(window);