This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathangular-ra-pageload.js
240 lines (204 loc) · 6.06 KB
/
angular-ra-pageload.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*!
* angular-ra-pageload.js v0.1.1
*
* Copyright 2015
* MIT License
*/
(function() {
'use strict';
/**
* @ngdoc overview
* @name ra.pageload
*
* @description
* A module for monitoring initial http GET requests per route change
*
* @example
* To use, include `ra.pageload` as a dependency.
* <pre>
* angular.module('application', ['ra.pageload'])
* </pre>
*/
angular.module('ra.pageload', [])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('raLoadingInterceptor');
}])
/**
* @ngdoc object
* @name ra.pageload.raLoadingInterceptor
*
* @requires $q
* @requires raLoadingProgress
*
* @description
* A `httpInterceptor` that adds GET requests to a queue and removes them once they are done.
*/
.factory('raLoadingInterceptor', ['$q', 'raLoadingProgress', function($q, raLoadingProgress) {
var raLoadingInterceptor = {
/**
* @ngdoc method
* @name request
* @methodOf ra.pageload.raLoadingInterceptor
*
* @description
* Adds a new http request to the loading queue.
*
* @param {Object} request http config object
* @return {Object} http config object
*/
request: function(request) {
if (!raLoadingProgress.initialized) {
raLoadingProgress.init();
}
raLoadingProgress.queue(request);
return request;
},
/**
* @ngdoc method
* @name response
* @methodOf ra.pageload.raLoadingInterceptor
*
* @description
* Removes a completed http request from the loading queue
*
* @param {Object} request http response object
* @return {Object} http response object
*/
response: function(response) {
raLoadingProgress.dequeue(response.config);
return response;
},
/**
* @ngdoc method
* @name requestError
* @methodOf ra.pageload.raLoadingInterceptor
*
* @description
* Removes an unsuccessful http request from the loading queue
*
* @param {Object} request http response object
* @return {Object} rejected http promise
*/
requestError: function(response) {
return raLoadingInterceptor.responseError(response);
},
/**
* @ngdoc method
* @name responseError
* @methodOf ra.pageload.raLoadingInterceptor
*
* @description
* Removes an unsuccessful http response from the loading queue
*
* @param {Object} request http response object
* @return {Object} rejected http promise
*/
responseError: function(response) {
raLoadingProgress.dequeue(response.config);
return $q.reject(response);
}
};
return raLoadingInterceptor;
}])
/**
* @ngdoc object
* @name ra.pageload.raLoadingProgress
*
* @requires $rootScope
* @requires $timeout
*
* @description
* Monitors the loading progress of http GET requests
*/
.factory('raLoadingProgress', ['$rootScope', '$timeout', function($rootScope, $timeout) {
var raLoadingProgress = {
/**
* @ngdoc method
* @name init
* @methodOf ra.pageload.raLoadingProgress
* @description
* Initialises the service
* @return {Object} self
*/
init: function() {
this.pendingRequests = [];
this.ready = false;
this.initialized = true;
// Reset loading queue on route change
$rootScope.$on('$routeChangeSuccess', this.reset.bind(this));
$rootScope.$on('$routeUpdate', this.reset.bind(this));
return this;
},
/**
* @ngdoc method
* @name reset
* @methodOf ra.pageload.raLoadingProgress
* @description
* Resets the queue of pending http requests
* @return {Object} self
*/
reset: function() {
this.pendingRequests.length = 0;
this.ready = false;
return this;
},
/**
* @ngdoc method
* @name queue
* @methodOf ra.pageload.raLoadingProgress
* @description
* Adds a http request to the queue of pending requests
* @param {Object} request The http request to be added
* @return {Object} self
*/
queue: function(request) {
// Add to queue if it is a GET request
if (request && request.method === 'GET') {
this.pendingRequests.push(request.url);
this.ready = false;
}
return this;
},
/**
* @ngdoc method
* @name dequeue
* @methodOf ra.pageload.raLoadingProgress
* @description
* Removes a http request from the queue.
* If the queue is empty, it fires `pageload:ready` event to notify child scopes that all pending requests are completed.
* @param {Object} request The http request to be removed
* @return {Object} self
*/
dequeue: function(request) {
var index = this.pendingRequests.indexOf(request && request.url);
// Remove from queue
if (index !== -1) {
this.pendingRequests.splice(index, 1);
}
// Wait for the next digest cycle
$timeout(function() {
// If there are no pending requests, notify child scopes
if (this.pendingRequests.length === 0 && !this.ready) {
this.notify('pageload:ready');
this.ready = true;
}
}.bind(this));
return this;
},
/**
* @ngdoc method
* @name notify
* @methodOf ra.pageload.raLoadingProgress
* @description
* Broadcasts an event to child scopes
* @param {string} name Event name to broadcast
* @param {...*} args Optional one or more arguments which will be passed onto the event listeners.
* @return {Object} Event object
*/
notify: function() {
return $rootScope.$broadcast.apply($rootScope, arguments);
}
};
return raLoadingProgress;
}]);
})();