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
160 lines (124 loc) · 3.69 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
/*! Angular notification v0.2.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', notificationService];
/**
* Create a new Notification service.
*/
function notificationService($window, $rootScope) {
/**
* Check notification support.
* Throws an error if notification are not supported.
*/
function checkSupport() {
if (! $window.Notification)
throw new Error('This browser does not support desktop notification.');
}
/**
* Create a new Notification.
*
* @param {String} title
* @param {Object} options
*/
function NgNotification(title, options) {
options = options || {};
var self = this;
checkSupport();
// Events cache.
this._events = [];
/**
* Create the notification.
*/
function createNotification() {
// Extend options with default provider options.
angular.extend(options, provider.options || {}, {
focusWindowOnClick: true
});
// Create a base notification.
self.baseNotification = new $window.Notification(title, options);
// 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(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 method to request permission.
*
* @param {Function} callback
*/
NgNotification.requestPermission = function (callback) {
checkSupport();
$window.Notification.requestPermission(function (permission) {
// Persist permission.
$window.Notification.permission = $window.Notification.permission || permission;
callback(permission);
});
};
return NgNotification;
}
/**
* Define default options.
*
* @param {Object} options
*/
this.setOptions = function setOptions(options) {
this.options = options;
};
}