-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotificationsDirective.js
43 lines (39 loc) · 1.72 KB
/
notificationsDirective.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
/**
* AngularJS Notifications directive
*
* @author Marko Šutija <markosutija@gmail.com>
* @requires notificationsService
*
*/
app.directive('notificationsViewer',
['notificationsService', '$timeout',
function (notificationsService, $timeout) {
return {
template: '<div class="notification-container animation-item-drop-in {{notification.type}}" ng-repeat="notification in notifications track by $index"><div class="notification"><span>{{notification.message}}</span><a ng-click="closeMe(notification);">X</a></div></div>',
link: function (scope, el, attrs) {
scope.notifications = [];
scope.closeMe = function (notification) {
removeNotification(notification);
};
var autoRemove = function (notification) {
$timeout(function () {
removeNotification(notification);
}, 4000);
};
function removeNotification(notification) {
var index = scope.notifications.indexOf(notification);
if (index >= 0) {
scope.notifications.splice(index, 1);
}
}
// Watch notifications changes
notificationsService.watchMe().then(
null,
null,
function (notification) {
scope.notifications.push(notification);
autoRemove(notification);
});
}
}
}]);