-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathne-loading.js
More file actions
executable file
·158 lines (143 loc) · 5.21 KB
/
ne-loading.js
File metadata and controls
executable file
·158 lines (143 loc) · 5.21 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* NE LOADING
* ***************************************************************************************************************************
*/
angular.module('neLoading', [])
.constant('neLoadingDebounce', 350) // delay of changes apply, if response will be received in lower interval than debounce, loading will not emit changes
.constant('neLoadingEndDelay', 300) // delay of loading end, loading message hide will be delayed
.factory('neLoading',['$timeout','neLoadingDebounce','neLoadingEndDelay', function($timeout, debounce, endDelay) {
var service = {
requestCount: 0,
isLoading: function() {
return service.requestCount > 0;
},
statusTimeout:null,
status:0,
prevStatus:0,
lastStart: new Date().getTime(),
statusListeners:[],
fireStatusListeners: function(status){
for(var i=0;i<service.statusListeners.length;i++){
(function(i){
$timeout(function(){
service.statusListeners[i](status!==undefined ? status : service.status);
},0,false);
})(i);
}
},
setStatus: function(percent) {
if(service.statusTimeout) $timeout.cancel(service.statusTimeout);
if(percent < 0) return;
service.prevStatus = service.status+0;
service.status = percent;
var now = new Date().getTime();
if(service.prevStatus === 0 && percent > 0) service.lastStart = now;
if((now - service.lastStart) > debounce) service.fireStatusListeners(service.status);
if(service.status > 0 && service.status < 99){
service.statusTimeout = $timeout(function(){
service.setStatus(randomIncrement(service.status));
}, debounce, false);
}
else if(service.status >= 100){
service.status = 0;
service.prevStatus = 0;
service.statusTimeout = $timeout(function(){
service.setStatus(0);
service.fireStatusListeners(0);
}, endDelay, false);
}
},
reqStarted: function(debugNotes){
// if(service.statusTimeout) $timeout.cancel(service.statusTimeout);
if(service.status===0) service.setStatus(1);
//$timeout(function(){
service.requestCount++;
if(debugNotes) console.log(debugNotes, service.requestCount, service.status);
//}, 0, false);
},
reqEnded: function(debugNotes){
//$timeout(function(){
if(service.requestCount>0) service.requestCount--;
if(debugNotes) console.log(debugNotes, service.requestCount, service.status);
if(service.requestCount === 0) service.setStatus(100);
//}, 0, false);
}
};
function randomIncrement(status){
var rnd = 0;
var stat = status / 100;
if (stat >= 0 && stat < 0.25) {
// Start out between 3 - 6% increments
rnd = (Math.random() * (5 - 3 + 1) + 3) / 100;
} else if (stat >= 0.25 && stat < 0.65) {
// increment between 0 - 3%
rnd = (Math.random() * 3) / 100;
} else if (stat >= 0.65 && stat < 0.9) {
// increment between 0 - 2%
rnd = (Math.random() * 2) / 100;
} else if (stat >= 0.9 && stat < 0.99) {
// finally, increment it .5 %
rnd = 0.005;
} else {
// after 99%, don't increment:
rnd = 0;
}
return status + Math.ceil(100*rnd);
}
return service;
}])
.factory('neLoadingInterceptor',['$q', '$cacheFactory', 'neLoading', function($q, $cacheFactory, loading){
function isCached(config) {
if(!config) return false;
var cache;
if (config.method !== 'GET' || config.cache === false) {
config.cached = false;
return false;
}
if (config.cache === true){ //&& defaults.cache === undefined) {
cache = $cacheFactory.get('$http');
}
else {
cache = config.cache;
}
var cached = cache !== undefined ?
cache.get(config.url) !== undefined : false;
if (config.cached !== undefined && cached !== config.cached) {
return config.cached;
}
config.cached = cached;
return cached;
}
return {
request: function(config) {
// Check to make sure this request hasn't already been cached and that
// the requester didn't explicitly ask us to ignore this request:
if (!config.ignoreLoading && !isCached(config)) {
loading.reqStarted();
}
return config;
},
response: function(response) {
if(!response.config.ignoreLoading && !isCached(response.config)) {
loading.reqEnded();
}
return response;
},
responseError: function(rejection) {
if (!rejection.config.ignoreLoading && !isCached(rejection.config)) {
loading.reqEnded();
}
return $q.reject(rejection);
}
};
}])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('neLoadingInterceptor');
}])
.controller('NeLoadingCtrl',['$scope', 'neLoading', function($scope, loading) {
loading.statusListeners.push(function(status){
$scope.status = status;
$scope.loading = status > 0;
$scope.$digest();
});
}]);