This repository has been archived by the owner on Feb 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.js
74 lines (63 loc) · 1.81 KB
/
flash.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
angular.module("flash", [])
.provider("flash", function() {
var types = {};
var clearOnRootScopeEvents = [];
return {
addType: function(type, value) {
types[type] = value;
},
clearOnEvent: function(name) {
clearOnRootScopeEvents.push(name);
},
$get: function() {
var service = {};
var _this = this;
_this.messages = [];
return {
addMessage: function(type, message) {
_this.messages.push({ cssClass: types[type], text: message })
},
getMessages: function() {
return _this.messages;
},
clearOnRootScopeEvents: clearOnRootScopeEvents,
clearMessages: function() {
_this.messages.length = 0;
}
}
}
};
})
// TODO: Seriously reconsider removing the listning on the root scope from
// this directive and finding another place to put it.
.directive("flashMessages", function($rootScope, flash) {
return {
restrict: "E",
scope: {},
template: [
'<div ng-repeat="message in flash.messages" class="{{ message.cssClass }}">',
'{{ message.text }}',
'</div>'
].join(""),
link: function(scope) {
scope.flash = { messages: flash.getMessages() };
var clearFunc = function() { flash.clearMessages(); };
angular.forEach(flash.clearOnRootScopeEvents, function(eventName) {
$rootScope.$on(eventName, clearFunc);
});
}
}
})
.directive("flashClear", function(flash) {
return {
restrict: "A",
scope: {},
link: function(scope, element) {
element.on("click", function() {
scope.$apply(function() {
flash.clearMessages();
});
});
}
}
});