-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathangular-flashr.js
More file actions
83 lines (72 loc) · 2.55 KB
/
angular-flashr.js
File metadata and controls
83 lines (72 loc) · 2.55 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
'use strict';
angular.module('flashr', [])
.factory('flashr', ['$rootScope',
function ($rootScope) {
var _lastToast = null;
var _toasts = [];
var _toastType = {
error: 'error',
info: 'info',
success: 'success',
warning: 'warning'
};
// when the route changes, flash the "later" messages
$rootScope.$on('$routeChangeSuccess', function () {
for (var i = 0; i < _toasts.length; i++) {
var toast = _toasts[i];
toast(toast.type, toast.msg);
_toasts = _.difference(toast, _toasts);
}
});
return {
now: {
success: nowSuccess,
info: nowInfo,
warning: nowWarning,
error: nowError
},
later: {
success: laterSuccess,
info: laterInfo,
warning: laterWarning,
error: laterError
},
clear: function() {
if (_lastToast != null) {
toastr.clear(_lastToast);
_lastToast = null;
}
}
};
function nowSuccess(message) {
toast(_toastType.success, message);
}
function nowInfo(message) {
toast(_toastType.info, message);
}
function nowWarning(message) {
toast(_toastType.warning, message);
}
function nowError(message) {
toast(_toastType.error, message);
}
function laterSuccess(message) {
_toasts.push({ type: _toastType.success, msg: message });
}
function laterInfo(message) {
_toasts.push({ type: _toastType.info, msg: message });
}
function laterWarning(message) {
_toasts.push({ type: _toastType.warning, msg: message });
}
function laterError(message) {
_toasts.push({ type: _toastType.error, msg: message });
}
function toast(type, message) {
if (_lastToast != null) {
toastr.clear(_lastToast);
_lastToast = null;
}
_lastToast = toastr[type](message);
}
}]);