-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-lightify.js
98 lines (85 loc) · 3.45 KB
/
angular-lightify.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
/**
* Yet another simple and lightweight notification factory for Angularjs which depends to
* the bootstrap-notify (https://github.com/nerezo/angular-lightify).
*
* @versionv1.0 - 2014-12-02 * @link https://github.com/nerezo/angular-lightify
* @author Okan Özeren <okanozeren[at]gmail.com>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
angular.module('nrzLightify', [])
.factory('nrzLightify', ['$rootScope', '$timeout', function ($rootScope, $timeout) {
var messages = [];
var reset;
// Cleans all available shown messages
var cleanup = function () {
$timeout.cancel(reset);
reset = $timeout(function () {
messages = [];
});
};
// Trigger the the flash:message event
var emit = function () {
$rootScope.$emit('flash:message', messages, nrzLightify, cleanup);
};
// Below events trigger on $stateChangeSuccess event with uiRouter and $routeChangeSuccess event with ngRoute
$rootScope.$on('$routeChangeSuccess', emit);
$rootScope.$on('$stateChangeSuccess', emit);
var nrzLightify = function (message, timeout) {
switch (message.type) {
case 'info':
message.text = '<strong>Info:</strong> ' + message.text;
message.fadeOut = { enabled: true, delay: (timeout === undefined ? 7000 : timeout) };
break;
case 'success':
message.text = '<strong>Success:</strong> ' + message.text;
message.fadeOut = { enabled: true, delay: (timeout === undefined ? 7000 : timeout) };
break;
case 'warning':
message.text = '<strong>Warning:</strong> ' + message.text;
message.fadeOut = { enabled: true, delay: (timeout === undefined ? 10000 : timeout) };
break;
case 'danger':
message.text = '<strong>Important:</strong> ' + message.text;
message.fadeOut = { enabled: true, delay: (timeout === undefined ? 10000 : timeout) };
break;
default:
message.text = message.text;
message.fadeOut = { enabled: true, delay: (timeout === undefined ? 10000 : timeout) };
break;
}
nrzLightify.nrzLightifyContainer.notify({
message: { html: '<span>' + message.text + '</span>', text: false },
type: message.type,
fadeOut: message.fadeOut
}).show();
};
// The container variable initializes.
nrzLightify.nrzLightifyContainer = {};
// The flash stack updater method.
nrzLightify.flash = function (message, timeout) {
messages.push({ message: message, timeout: timeout });
};
return nrzLightify;
}])
.directive('nrzLightifyMessages', function (nrzLightify) {
return {
restrict: 'EA',
replace: true,
scope: false,
controller: function ($scope, $rootScope) {
$rootScope.$on('flash:message', function (_, messages, nrzLightify, done) {
// Uses all stacked messages for showing the notification bands.
angular.forEach(messages, function (message, index) {
nrzLightify(message.message, message.timeout);
});
done();
});
},
link: function (scope, element, attrs) {
// Default and user selected css class are adding to the element.
element.addClass('notifications').addClass(attrs.position);
// Keeping the element to be using within the methods of the factory.
nrzLightify.nrzLightifyContainer = element;
}
};
});