This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathangular-notification.js
188 lines (148 loc) · 4.53 KB
/
angular-notification.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
/*! Angular notification v1.1.1 | (c) 2013 Greg Bergé | License MIT */
angular
.module('notification', [])
.provider('$notification', $notificationProvider);
/**
* Notification provider.
* Configure the default notification options.
*/
function $notificationProvider() {
var provider = this;
/**
* Expose Notification service.
*/
this.$get = ['$window', '$rootScope', '$q', $notificationFactory];
/**
* Create a new Notification service.
*/
function $notificationFactory($window, $rootScope, $q) {
/**
* Create a new Notification.
*
* @param {String} title
* @param {Object} options
*/
function NgNotification(title, options) {
if (!$window.Notification) return false;
options = options || {};
var self = this;
// Events cache.
this._events = [];
/**
* Create the notification.
*/
function createNotification() {
// Extend options with default provider options.
options = angular.extend({
focusWindowOnClick: true
}, provider.options || {}, options);
// Create a base notification.
try {
self.baseNotification = new $window.Notification(title, options);
} catch (error) {
return;
}
// Close notification after specified delay.
if (options.delay) setTimeout(angular.bind(self, self.close), options.delay);
// Focus window on click.
if (options.focusWindowOnClick)
self.$on('click', function () {
$window.focus();
});
// Re-bind events.
self._events.forEach(function (args) {
self.$on.apply(self, args);
});
// Reset events.
self._events = [];
}
if ($window.Notification.permission === 'granted') {
return createNotification();
} else if ($window.Notification.permission !== 'denied') {
NgNotification.requestPermission().then(createNotification);
}
}
/**
* Listen on event of a given type.
* Supported events are:
* - error
* - show
* - click
* - close
*
* @param {String} name
* @param {Function} listener
*/
NgNotification.prototype.$on = function $on(name, listener) {
var self = this;
// If the notification is not ready, we cache the event.
if (!this.baseNotification) return this._events.push([name, listener]);
this.baseNotification.addEventListener(name, applyListener);
function applyListener() {
var args = arguments;
$rootScope.$apply(function () {
listener.apply(self, args);
});
}
// Return the deregistration function.
return function $off() {
this.baseNotification.removeListener(event, applyListener);
};
};
/**
* Close the notification.
*/
NgNotification.prototype.close = function close() {
if (this.baseNotification) this.baseNotification.close();
};
/**
* Static property to check whether the browser supports Notification or not.
* It returns a bool.
*/
NgNotification.isSupported = !!$window.Notification;
/**
* Static method to get current permission.
* It returns a string.
*/
NgNotification.getPermission = function () {
return !$window.Notification ? undefined : $window.Notification.permission;
};
/**
* Static method to request permission.
* It returns a promise
*/
NgNotification.requestPermission = function () {
return $q(function (resolve, reject) {
if (!$window.Notification)
return reject();
$window.Notification.requestPermission(function (permission) {
// Persist permission.
$window.Notification.permission = $window.Notification.permission || permission;
resolve($window.Notification.permission);
});
});
};
/**
* Create a new notification.
*
* @param {string} title
* @param {options} options
*/
function $notification(title, options) {
return new NgNotification(title, options);
}
// Expose static members on $notification.
$notification.isSupported = NgNotification.isSupported;
$notification.getPermission = NgNotification.getPermission;
$notification.requestPermission = NgNotification.requestPermission;
return $notification;
}
/**
* Define default options.
*
* @param {Object} options
*/
this.setOptions = function setOptions(options) {
this.options = options;
};
}