-
Notifications
You must be signed in to change notification settings - Fork 55
/
notificationsService.js
110 lines (89 loc) · 3.39 KB
/
notificationsService.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
'use strict';
angular.module('openshiftCommonUI').provider('NotificationsService', function() {
this.dismissDelay = 8000;
this.autoDismissTypes = ['info', 'success'];
this.$get = function($rootScope) {
var notifications = [];
var dismissDelay = this.dismissDelay;
var autoDismissTypes = this.autoDismissTypes;
var notificationHiddenKey = function(notificationID, namespace) {
if (!namespace) {
return 'hide/notification/' + notificationID;
}
return 'hide/notification/' + namespace + '/' + notificationID;
};
var addNotification = function (notification) {
// notifications may already have an id that is not necessarily unique,
// this is an explicitly unique id just for `track by` in templates
notification.trackByID = _.uniqueId('notification-') + Date.now();
notification.skipToast = notification.skipToast || false;
notification.showInDrawer = notification.showInDrawer || false;
notification.timestamp = new Date().toISOString();
if (isNotificationPermanentlyHidden(notification) || isNotificationVisible(notification)) {
return;
}
notifications.push(notification);
$rootScope.$emit('NotificationsService.onNotificationAdded', notification);
};
var hideNotification = function (notificationID) {
if (!notificationID) {
return;
}
_.each(notifications, function(notification) {
if (notification.id === notificationID) {
notification.hidden = true;
}
});
};
var getNotifications = function () {
return notifications;
};
var clearNotifications = function () {
notifications.length = 0;
};
var isNotificationPermanentlyHidden = function (notification) {
if (!notification.id) {
return false;
}
var key = notificationHiddenKey(notification.id, notification.namespace);
return localStorage.getItem(key) === 'true';
};
var permanentlyHideNotification = function (notificationID, namespace) {
var key = notificationHiddenKey(notificationID, namespace);
localStorage.setItem(key, 'true');
};
// Is there a visible toast notification with the same ID right now?
var isNotificationVisible = function (notification) {
if (!notification.id) {
return false;
}
return _.some(notifications, function(next) {
return !next.hidden && notification.id === next.id;
});
};
var isAutoDismiss = function(notification) {
return _.includes(autoDismissTypes, notification.type);
};
// Also handle `addNotification` events on $rootScope, which is used by DataService.
$rootScope.$on('NotificationsService.addNotification', function(event, data) {
addNotification(data);
});
return {
addNotification: addNotification,
hideNotification: hideNotification,
getNotifications: getNotifications,
clearNotifications: clearNotifications,
isNotificationPermanentlyHidden: isNotificationPermanentlyHidden,
permanentlyHideNotification: permanentlyHideNotification,
isAutoDismiss: isAutoDismiss,
dismissDelay: dismissDelay,
autoDismissTypes: autoDismissTypes
};
};
this.setDismissDelay = function(delayInMs) {
this.dismissDelay = delayInMs;
};
this.setAutoDismissTypes = function(arrayOfTypes) {
this.autoDismissTypes = arrayOfTypes;
};
});