forked from e0ipso/message-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message-center.js
177 lines (167 loc) · 5.83 KB
/
message-center.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
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
/*jshint strict:false */
'use strict';
// Create a new angular module.
var MessageCenterModule = angular.module('MessageCenterModule', []);
// Define a service to inject.
MessageCenterModule.
service('messageCenterService', ['$rootScope', '$sce', '$timeout',
function ($rootScope, $sce, $timeout) {
return {
mcMessages: this.mcMessages || [],
offlistener: this.offlistener || undefined,
status: {
unseen: 'unseen',
shown: 'shown',
/** @var Odds are that you will show a message and right after that
* change your route/state. If that happens your message will only be
* seen for a fraction of a second. To avoid that use the "next"
* status, that will make the message available to the next page */
next: 'next',
/** @var Do not delete this message automatically. */
permanent: 'permanent'
},
add: function (type, message, options) {
var availableTypes = ['info', 'warning', 'danger', 'success'],
service = this;
options = options || {};
if (availableTypes.indexOf(type) == -1) {
throw "Invalid message type";
}
var messageObject = {
type: type,
status: options.status || this.status.unseen,
processed: false,
close: function() {
return service.remove(this);
}
};
messageObject.message = options.html ? $sce.trustAsHtml(message) : message;
messageObject.html = !!options.html;
if (angular.isDefined(options.timeout)) {
messageObject.timer = $timeout(function () {
messageObject.close();
}, options.timeout);
}
if (angular.isDefined(options.countdown)) {
messageObject.countdown = options.countdown;
}
if(this.mcMessages.length > 0) {
var a = true
angular.forEach(this.mcMessages, function(value, key) {
if(value.type == type) {
a = false
}
})
if(a) {
this.mcMessages.push(messageObject);
}
}
else {
this.mcMessages.push(messageObject);
}
return messageObject;
},
remove: function (message) {
var index = this.mcMessages.indexOf(message);
this.mcMessages.splice(index, 1);
},
reset: function () {
this.mcMessages = [];
},
removeShown: function () {
for (var index = this.mcMessages.length - 1; index >= 0; index--) {
if (this.mcMessages[index].status == this.status.shown) {
this.remove(this.mcMessages[index]);
}
}
},
markShown: function () {
for (var index = this.mcMessages.length - 1; index >= 0; index--) {
if (!this.mcMessages[index].processed) {
if (this.mcMessages[index].status == this.status.unseen) {
this.mcMessages[index].status = this.status.shown;
this.mcMessages[index].processed = true;
}
else if (this.mcMessages[index].status == this.status.next) {
this.mcMessages[index].status = this.status.unseen;
}
}
}
},
flush: function () {
$rootScope.mcMessages = this.mcMessages;
}
};
}
]);
MessageCenterModule.
directive('mcMessages', ['$rootScope', 'messageCenterService', function ($rootScope, messageCenterService) {
/*jshint multistr: true */
var templateString = '\
<div id="mc-messages-wrapper">\
<div class="alert alert-{{ message.type }}" ng-repeat="message in mcMessages">\
<span ng-switch on="message.html">\
<span ng-switch-when="true">\
<span ng-bind-html="message.message"></span>\
</span>\
<span ng-switch-default>\
{{ message.message }} <span ng-show="message.countdown" countdown="message.countdown">1212</span>\
</span>\
</div>\
</div>\
';
return {
restrict: 'EA',
template: templateString,
link: function(scope, element, attrs) {
// Bind the messages from the service to the root scope.
messageCenterService.flush();
var changeReaction = function (event, to, from) {
// Update 'unseen' messages to be marked as 'shown'.
messageCenterService.markShown();
// Remove the messages that have been shown.
messageCenterService.removeShown();
$rootScope.mcMessages = messageCenterService.mcMessages;
messageCenterService.flush();
};
if (messageCenterService.offlistener === undefined) {
messageCenterService.offlistener = $rootScope.$on('$locationChangeSuccess', changeReaction);
}
}
};
}]);
MessageCenterModule.
directive('countdown', ['$rootScope', '$timeout', function ($rootScope, $timeout) {
/*jshint multistr: true */
var templateString = '\
<span ng-bind="counter"></span>...\
';
return {
restrict: 'EA',
scope: {
countdown: '='
},
template: templateString,
link: function(scope, element, attrs) {
scope.counter = angular.copy(scope.countdown);
var stopped;
scope.stop = function(){
$timeout.cancel(stopped);
}
scope.start = function() {
stopped = $timeout(function() {
console.log(scope.counter);
scope.counter--;
scope.start();
}, 1000);
if(scope.counter < 0) {
scope.counter = angular.copy(scope.countdown);
}
};
scope.start();
scope.$on("$destroy", function( event ) {
scope.stop();
});
}
};
}]);