forked from swimlane/angular-notifications
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotification.js
More file actions
333 lines (289 loc) · 11.9 KB
/
notification.js
File metadata and controls
333 lines (289 loc) · 11.9 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['angular'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
factory(require('angular'));
} else {
// Browser globals
factory(angular);
}
}(function (angular) {
var module = angular.module('notifications', []);
module.provider('$notification', function () {
var settings = {
info: {
duration: 5000,
enabled: true,
class: 'info'
},
warning: {
duration: 5000,
enabled: true,
class: 'warning'
},
error: {
duration: 5000,
enabled: true,
class: 'error'
},
success: {
duration: 5000,
enabled: true,
class: 'success'
},
progress: {
duration: 0,
enabled: true,
class: ''
},
custom: {
duration: 35000,
enabled: true,
class: ''
},
details: true,
localStorage: false,
html5Mode: false,
templateName: 'ng-notification-template'
};
this.setSettings = function (s) {
angular.extend(settings, s);
};
function Notification($timeout, s) {
var settings = s;
var notifications = JSON.parse(localStorage.getItem('$notifications')) || [],
queue = [];
function html5Notify(icon, title, content, ondisplay, onclose) {
if (settings.html5Mode == 'webkit') {
if (window.webkitNotifications.checkPermission() === 0) {
if (!icon) {
icon = 'favicon.ico';
}
var noti = window.webkitNotifications.createNotification(icon, title, content);
if (typeof ondisplay === 'function') {
noti.ondisplay = ondisplay;
}
if (typeof onclose === 'function') {
noti.onclose = onclose;
}
noti.show();
}
else {
settings.html5Mode = false;
}
}
else if (settings.html5Mode == 'moz') {
if (!icon) {
icon = 'favicon.ico';
}
try {
Notification = window.Notification || window.mozNotification;
var noti = new Notification(
title,
{
body: content,
dir: "auto",
lang: "",
tag: 'test',
icon: icon
}
);
}
catch(err) {
settings.html5Mode = false;
}
}
}
return {
/* ========== SETTINGS RELATED METHODS =============*/
disableHtml5Mode: function () {
settings.html5Mode = false;
},
disableType: function (notificationType) {
settings[notificationType].enabled = false;
},
enableHtml5Mode: function () {
// settings.html5Mode = true;
settings.html5Mode = this.requestHtml5ModePermissions();
},
enableType: function (notificationType) {
settings[notificationType].enabled = true;
},
getSettings: function () {
return settings;
},
toggleType: function (notificationType) {
settings[notificationType].enabled = !settings[notificationType].enabled;
},
toggleHtml5Mode: function () {
settings.html5Mode = !settings.html5Mode;
},
requestHtml5ModePermissions: function () {
if (window.webkitNotifications) {
//console.log('notifications are available');
if (window.webkitNotifications.checkPermission() === 0) {
return true;
}
else{
window.webkitNotifications.requestPermission(function() {
if (window.webkitNotifications.checkPermission() === 0) {
settings.html5Mode = 'webkit';
}
else {
settings.html5Mode = false;
}
});
return false;
}
}
else if (window.Notification || window.mozNotification) {
Notification = window.Notification || window.mozNotification;
Notification.requestPermission(function(perm) {
if (perm == 'granted') {
settings.html5Mode = 'moz';
}
else {
settings.html5Mode = false;
}
});
}
else {
//console.log('notifications are not supported');
return false;
}
},
/* ============ QUERYING RELATED METHODS ============*/
getAll: function () {
// Returns all notifications that are currently stored
return notifications;
},
getQueue: function () {
return queue;
},
/* ============== NOTIFICATION METHODS ==============*/
info: function (title, content, userData, duration) {
return this.notify('info', 'info', title, content, userData, duration);
},
error: function (title, content, userData, duration) {
return this.notify('error', 'times', title, content, userData, duration);
},
success: function (title, content, userData, duration) {
return this.notify('success', 'check', title, content, userData, duration);
},
warning: function (title, content, userData, duration) {
return this.notify('warning', 'info', title, content, userData, duration);
},
notify: function (type, icon, title, content, userData, duration) {
return this.makeNotification(type, false, icon, title, content, userData, duration);
},
makeNotification: function (type, image, icon, title, content, userData, duration) {
var notification = {
'type': type,
'image': image,
'icon': icon,
'title': title,
'content': content,
'timestamp': +new Date(),
'userData': userData,
'duration': duration,
'class': settings[type].class
};
notifications.push(notification);
if (duration === undefined) {
duration = settings[type].duration;
}
if (settings.html5Mode) {
html5Notify(image, title, content, function () {}, function () {
});
} else {
queue.push(notification);
if (duration) {
$timeout(function removeFromQueueTimeout() {
queue.splice(queue.indexOf(notification), 1);
}, duration);
}
}
this.save();
return notification;
},
/* ============ PERSISTENCE METHODS ============ */
save: function () {
// Save all the notifications into localStorage
if (settings.localStorage) {
localStorage.setItem('$notifications', JSON.stringify(notifications));
}
},
restore: function () {
// Load all notifications from localStorage
},
clear: function () {
notifications = [];
this.save();
}
}
}
this.$get = ['$timeout', '$templateCache',
function ($timeout, $templateCache) {
if (!$templateCache.get('ng-notification-template')) {
$templateCache.put('ng-notification-template',
'<div class="ng-notification-wrapper" ng-repeat="noti in queue">' +
'<div class="ng-notification-close-btn" ng-click="removeNotification(noti)">' +
'<i class="fa fa-times"></i>' +
'</div>' +
'<div class="ng-notification">' +
'<div class="ng-notification-image ng-notification-type-{{noti.class}}" ng-switch on="noti.image">' +
'<i class="fa fa-{{noti.icon}}" ng-switch-when="false"></i>' +
'<img ng-src="{{noti.image}}" ng-switch-default />' +
'</div>' +
'<div class="ng-notification-content">' +
'<h3 class="ng-notification-title">{{noti.title}}</h3>' +
'<p class="ng-notification-text" ng-bind-html="noti.content"></p>' +
'</div>' +
'</div>' +
'</div>'
);
}
return new Notification($timeout, settings);
}
];
})
module.directive('notifications', ['$notification', '$compile', '$templateCache',
function ($notification, $compile, $templateCache) {
/**
*
* It should also parse the arguments passed to it that specify
* its position on the screen like "bottom right" and apply those
* positions as a class to the container element
*
* Finally, the directive should have its own controller for
* handling all of the notifications from the notification service
*/
function link(scope, element, attrs) {
var position = attrs.notifications;
position = position.split(' ');
element.addClass('ng-notification-container');
for (var i = 0; i < position.length; i++) {
element.addClass(position[i]);
}
}
return {
restrict: 'A',
scope: {},
template: $templateCache.get($notification.getSettings().templateName),
link: link,
controller: ['$scope',
function NotificationsCtrl($scope) {
$scope.queue = $notification.getQueue();
$scope.removeNotification = function (noti) {
$scope.queue.splice($scope.queue.indexOf(noti), 1);
};
}
]
};
}
]);
return module;
}));