forked from cgross/angular-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-notify.js
executable file
·117 lines (93 loc) · 3.39 KB
/
angular-notify.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
angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile','$templateCache','$rootScope',
function($timeout,$http,$compile,$templateCache,$rootScope){
var startTop = 10;
var verticalSpacing = 5;
var duration = 4000;
var defaultTemplate = 'angular-notify.html';
var position = 'right';
var container = document.body;
var messageElements = [];
var notify = function(args){
if (typeof args !== 'object'){
args = {message:args};
}
args.template = args.template ? args.template : defaultTemplate;
args.position = args.position ? args.position : position;
args.container = args.container ? args.container : container;
var scope = args.scope ? args.scope.$new() : $rootScope.$new();
scope.$message = args.message;
$http.get(args.template,{cache: $templateCache}).success(function(template){
var templateElement = $compile(template)(scope);
templateElement.bind('webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd', function(e){
if (e.propertyName === 'opacity' ||
(e.originalEvent && e.originalEvent.propertyName === 'opacity')){
templateElement.remove();
messageElements.splice(messageElements.indexOf(templateElement),1);
layoutMessages();
}
});
angular.element(args.container).append(templateElement);
messageElements.push(templateElement);
if (args.position === 'center'){
$timeout(function(){
templateElement.css('margin-left','-' + (templateElement[0].offsetWidth /2) + 'px');
});
}
scope.$close = function(){
templateElement.css('opacity',0);
};
var layoutMessages = function(){
var j = 0;
var currentY = startTop;
for(var i = messageElements.length - 1; i >= 0; i --){
var element = messageElements[i];
var top = currentY;
currentY += messageElements[i][0].offsetHeight + verticalSpacing;
element.css('top',top + 'px');
j ++;
}
};
$timeout(function(){
layoutMessages();
});
if (duration > 0){
$timeout(function(){
scope.$close();
},duration);
}
}).error(function(data){
throw new Error('Template specified for cgNotify ('+args.template+') could not be loaded. ' + data);
});
var retVal = {};
retVal.close = function(){
if (scope.$close){
scope.$close();
}
};
Object.defineProperty(retVal,'message',{
get: function(){
return scope.$message;
},
set: function(val){
scope.$message = val;
}
});
return retVal;
};
notify.config = function(args){
startTop = !angular.isUndefined(args.top) ? args.top : startTop;
verticalSpacing = !angular.isUndefined(args.verticalSpacing) ? args.verticalSpacing : verticalSpacing;
duration = !angular.isUndefined(args.duration) ? args.duration : duration;
defaultTemplate = args.template ? args.template : defaultTemplate;
position = !angular.isUndefined(args.position) ? args.position : position;
container = args.container ? args.container : container;
};
notify.closeAll = function(){
for(var i = messageElements.length - 1; i >= 0; i --){
var element = messageElements[i];
element.css('opacity',0);
}
};
return notify;
}
]);