-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-notification.js
78 lines (68 loc) · 2.63 KB
/
angular-notification.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
/**
* angular-notification v1.0
*
* Copyright (c) 2014 Yuthasak Tanruengsri [email protected]
* https://github.com/ytanruengsri/angular-notification
*
* License: MIT
*/
'use strict';
angular.module('tellMeModule', ['ngAnimate', 'tellMeTemplate'])
.constant('$tellMeConstant', {
'defaultZIndex': 9999,
'defaultPosition': 'top-right',
'defaultSelfDestroyDuration': 3500
})
.factory('$tellMe', ['$timeout', '$tellMeConstant', function ($timeout, $tellMeConstant) {
var messages = [];
return {
/* ============ GETTER FN ============*/
getMessages: function() {
return messages;
},
/* ============== NOTIFICATION FN ==============*/
now: function(obj) {
return this.createNotification(obj.level, obj.title, obj.message);
},
createNotification: function(level, title, message) {
var tellMeNow = {
'level': level,
'title': title,
'message': message,
'created': Date.now()
};
var timeout = $timeout(function() {
messages.splice(messages.indexOf(tellMeNow), 1);
}, $tellMeConstant.defaultSelfDestroyDuration);
angular.extend(tellMeNow, {timer: timeout});
messages.push(tellMeNow);
return tellMeNow;
}
};
}])
.directive('tellMe', ['$timeout', '$tellMeConstant', '$tellMe',
function($timeout, $tellMeConstant, $tellMe) {
return {
restrict: 'E',
replace: true,
scope: {
zIndex: '=',
position: '='
},
templateUrl: '../template/angular-notification-template.html',
link: function(scope, elem) {
/* ============== Config ==============*/
var zIndex = angular.isDefined(scope.zIndex) ? scope.zIndex : $tellMeConstant.defaultZIndex;
elem.css('zIndex', zIndex);
},
controller: function($scope) {
$scope.messages = $tellMe.getMessages();
$scope.$on('clearTellMe', function() {
$scope.messages.splice(0, $scope.messages.length)
});
$scope.hideMessage = function(msg) {
$scope.messages.splice($scope.messages.indexOf(msg), 1);
};
}
};
}]);