This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
angular-page-loader.directive.js
114 lines (86 loc) · 2.91 KB
/
angular-page-loader.directive.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
angular.module('angular-page-loader')
.directive('pageLoader', ['$timeout', '$templateCache', '$location', function pageLoader($timeout, $templateCache, $location) {
var directive = {
restrict: 'EA',
replace: false,
scope: { isLoading: '=?flag' },
transclude: true,
template: '<div class="loader-container" ng-transclude></div>',
link: _link
}
return directive;
function _link(scope, elem, attr) {
var promise, inner = elem.children().eq(0);
// the time to wait before show the loader
var latency = attr.latency || 250;
// the background-color regexps
var hexReg = /^#[0-9A-F]{6}$/i;
var rgbReg = /rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/;
var rgbaReg = /rgba\((\d{1,3}), (\d{1,3}), (\d{1,3}), (\d+(\.\d+))\)/;
// get/set the background color
var color = ( hexReg.test(attr.bgColor) || rgbReg.test(attr.bgColor) || rgbaReg.test(attr.bgColor) )
? attr.bgColor : angular.isString(attr.bgColor) ? attr.bgColor : 'white';
// set element default style
elem.css({
'background-color': color,
'position': 'absolute',
'width': '100%',
'height': '100%',
'z-index': '10000',
'top': '0',
'left': '0',
'right': '0',
'bottom': '0',
});
// add the default loader
// if nothing is transcluded
if( !inner.children().length ) {
var loader = document.createElement('div');
loader.className = attr.loaderClass || 'loader';
inner.append( loader );
}
// if a flag is specified
// use this as loading status
if( attr.flag ) {
scope.$watch('isLoading', function(n) {
return n ? elem.removeClass('ng-hide') : elem.addClass('ng-hide');
});
} else {
// Init promise and show loader
function _start(e) {
// cancel any previous promise
$timeout.cancel(promise);
// check if location change has been prevented
scope.$evalAsync(function() {
if( !e.defaultPrevented ) {
promise = $timeout(function() {
elem.removeClass('ng-hide');
}, latency);
}
});
}
// Clear promise and hide loader
function _stop() {
$timeout.cancel(promise);
elem.addClass('ng-hide');
}
// try if ngRoute module is loaded
// and bind the routechange events callbacks
try {
window.angular.module('ngRoute');
scope.$on( '$locationChangeStart', _start )
scope.$on( '$routeChangeSuccess', _stop );
scope.$on( '$routeChangeError', _stop );
} catch(e) {
// try if ui.router module is loaded
// and bind the state change events
try {
window.angular.module('ui.router');
scope.$on( '$stateChangeStart', _start )
scope.$on( '$stateChangeSuccess', _stop );
scope.$on( '$stateChangeError', _stop );
} catch(e) { }
}
}
}
});