forked from IanShoe/angular-message-center
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage-center.js
More file actions
185 lines (165 loc) · 4.98 KB
/
message-center.js
File metadata and controls
185 lines (165 loc) · 4.98 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
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
178
179
180
181
182
183
184
185
angular.module('MessageCenter', ['ngAnimate','template/message-center/message.html', 'template/message-center/message-center.html']).
run(['$document', '$compile', '$rootScope', function($document, $compile, $rootScope) {
// keep this check for backwards compatibility for now
if (!$document.find('message-center').length) {
// Compile message-center element
var messageCenterElem = $compile('<message-center></message-center>')($rootScope);
// Add element to body
$document.find('body').append(messageCenterElem);
}
}]).
factory('MessageService', ['$rootScope', function ($rootScope) {
var MessageService = {};
MessageService.config = {
disabled: false,
max: 3,
timeout: 3000
};
var history = [];
var disabledHistory = [];
var counter = 0;
MessageService.configure = function(config) {
//need to make sure that omitted values don't override current values
this.config.disabled = angular.isDefined(config.disabled) ? config.disabled : this.config.disabled;
this.config.max = angular.isDefined(config.max) ? config.max : this.config.max;
this.config.timeout = angular.isDefined(config.timeout) ? config.timeout : this.config.timeout;
};
MessageService.broadcast = function (msg, opts) {
if (this.config.disabled) {
disabledHistory.push({message:msg, opts:opts});
return;
}
counter++;
var message = {
classes: [],
message: msg,
id: counter,
timeout: MessageService.config.timeout
};
if (opts) {
if (opts.important) {
message.type = 'important';
}
if (opts.color) {
message.classes.push(opts.color);
}
if (angular.isDefined(opts.timeout) && angular.isNumber(opts.timeout)) {
message.timeout = opts.timeout;
}
}
history.push(message);
$rootScope.$broadcast('MessageService.broadcast', message);
};
MessageService.info = function(msg, opts) {
opts = opts || {};
opts.color = 'info';
this.broadcast(msg, opts);
};
MessageService.danger = function(msg, opts) {
opts = opts || {};
opts.color = 'danger';
this.broadcast(msg, opts);
};
MessageService.error = function(msg, opts) {
opts = opts || {};
opts.color = 'danger';
this.broadcast(msg, opts);
};
MessageService.success = function(msg, opts) {
opts = opts || {};
opts.color = 'success';
this.broadcast(msg, opts);
};
MessageService.warn = function(msg, opts) {
opts = opts || {};
opts.color = 'warning';
this.broadcast(msg, opts);
};
MessageService.warning = function(msg, opts) {
opts = opts || {};
opts.color = 'warning';
this.broadcast(msg, opts);
};
MessageService.getHistory = function() {
return history;
};
MessageService.getDisabledHistory = function() {
return disabledHistory;
};
MessageService.clearHistory = function() {
history = [];
counter = 0;
};
return MessageService;
}]).
directive('messageCenter', ['$timeout', 'MessageService', '$rootScope', function ($timeout, MessageService, $rootScope) {
return {
restrict: 'E',
scope: {},
templateUrl: 'template/message-center/message-center.html',
controller: ['$scope', function($scope) {
$scope.removeItem = function(message) {
// Maybe have a reference to the timeout on message for easier cancelling
message.type ? remove($scope.impMessages, message) : remove($scope.messages, message);
};
}],
link: function (scope) {
scope.messages = [];
scope.impMessages = [];
var queue = [];
var impQueue = [];
scope.$on('MessageService.broadcast', function (event, message) {
var q, list;
if (message.type) {
q = impQueue;
list = scope.impMessages;
}
else {
q = queue;
list = scope.messages;
}
q.push(message);
if (list.length < MessageService.config.max && q.length === 1) {
// if it's the first item in queue and the max hasn't been hit yet, then start processing
processQueue(q, list);
}
});
function processQueue(q, list) {
if (q.length === 0) {
return;
}
var nextMsg = q.shift();
//Force the ng-repeat to update the bindings whatever the situation
if (!$rootScope.$$phase){
scope.$apply(function (){
list.push(nextMsg);
});
} else{
list.push(nextMsg);
}
$timeout(function() {
remove(list, nextMsg);
if (q.length > 0) {
$timeout(function() {
processQueue(q, list);
}, nextMsg.timeout);
}
}, nextMsg.timeout);
}
}
};
function remove(array, item) {
var index = array.indexOf(item);
if (index !== -1) {
array.splice(index, 1);
}
return array;
}
}]).
directive('message', [function () {
return {
replace: true,
restrict: 'E',
templateUrl: 'template/message-center/message.html'
};
}]);