-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifier.js
More file actions
112 lines (87 loc) · 3.01 KB
/
Copy pathNotifier.js
File metadata and controls
112 lines (87 loc) · 3.01 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
(function(){
angular.module('Notifier', [])
.directive('notifier', function(Notifier){
return{
transclude: false, // this is left this way to work in IE8... IE8....
replace: true,
template: '<ul class="Notifier">'+
'<li ng-repeat="notification in notifications" class="Notifier-notification">'+
'<i class="Icon-notification-{{ notification.type }}"></i>'+
'<span class="Notifier-message">{{ notification.text }}</span>'+
'<div class="Notifier-actions">'+
'<a ng-show="notification.data" ng-click="viewData(notification.data)" class="view-data">View</a>'+
'<a ng-show="notification.xhrToken" ng-click="retry(notification.xhrToken)" class="retry">Retry</a>'+
'<a ng-click="remove(notification)" class="remove">Close</a>'+
'</div>'+
'</li>'+
'</ul>',
link: function( $scope, element, attr ){
// seconds to show non-indefinite notifications
// default to 3 sec
$scope.notifications = Notifier.notifications;
$scope.remove = function( notification ){
var index = Notifier._getNotificationIndexByID(notification.id);
Notifier._removeNotification(index);
},
// a notification can contain information set
// at the time of creation. This is passed
// up through an event
$scope.viewData = Notifier.sendNotificationData;
$scope.retry = function(token){
Notifier.retry(token);
};
}
}
})
.service('Notifier', function($timeout, $http, $rootScope){
var self = this;
self.notifications = [];
self.timing = 3000;
self.notify = function( id, text, type ){
var index,
n;
n = {id: id, text: text, type: type};
// remove any event with this id
// all progress events should be captured and removed
// here
index = self._getNotificationIndexByID(n.id);
if( index !== undefined )
self._removeNotification(index);
// for timed out notifications we set the timeout here
if( n.type == 'progress' || n.type == 'indefinite' ){
self._addIndefiniteNotification(n);
}else{
self._addTimedNotification(n);
}
}
self.retry = function( xhrToken ){
$http(xhrToken);
}
self._getNotificationIndexByID = function( id ){
for (var i = 0; i < self.notifications.length; i++) {
notificationInArray = self.notifications[i];
if( id == notificationInArray.id ){
return i;
}
};
}
self.sendNotificationData = function(data){
$rootScope.$emit('Notifier.VIEW_DATA', data);
}
self._removeNotification = function( index ){
if( index === undefined )return;
self.notifications.splice(index,1);
}
self._addTimedNotification = function( notification ){
self.notifications.push( notification );
$timeout(function(){
// remove the notification from the array
var index = self._getNotificationIndexByID(notification.id);
self._removeNotification(index);
}, self.timing);
}
self._addIndefiniteNotification = function( notification ){
self.notifications.push(notification);
}
});
})();