@@ -16,15 +16,36 @@ zsd.config(['$routeProvider', function($routeProvider){
1616
1717zsd . config ( [ '$httpProvider' , function ( $httpProvider ) {
1818 $httpProvider . interceptors . push ( 'HTTPErrorInterceptor' ) ;
19+ $httpProvider . interceptors . push ( 'HTTPActivityInterceptor' ) ;
1920} ] ) ;
2021
21- zsd . factory ( 'HTTPErrorInterceptor' , [ '$q' , '$rootScope' , function ( $q , $rootScope ) {
22+ zsd . factory ( 'HTTPActivityInterceptor' , [ '$q' , '$rootScope' , '$timeout' , function ( $q , $rootScope , $timeout ) {
23+ var activityCounter = 0 ;
24+ var timeoutHandlers = [ ] ;
2225 return {
23- 'requestError' : function ( rejection ) {
24- console . log ( "requestError" ) ;
25- console . log ( rejection ) ;
26- return $q . reject ( rejection ) ;
26+ 'request' : function ( config ) {
27+ activityCounter ++ ;
28+ $rootScope . $broadcast ( 'http-activity' , activityCounter ) ;
29+
30+ return config
31+ } ,
32+ 'response' : function ( response ) {
33+ activityCounter -- ;
34+ $rootScope . $broadcast ( 'http-activity' , activityCounter ) ;
35+
36+ return response ;
2737 } ,
38+ 'responseError' : function ( rejection ) {
39+ activityCounter -- ;
40+ $rootScope . $broadcast ( 'http-activity' , activityCounter ) ;
41+
42+ return $q . reject ( rejection ) ;
43+ }
44+ } ;
45+ } ] ) ;
46+
47+ zsd . factory ( 'HTTPErrorInterceptor' , [ '$q' , '$rootScope' , function ( $q , $rootScope ) {
48+ return {
2849 'responseError' : function ( rejection ) {
2950 // 406 are handled in the controller (file size limit)
3051 if ( rejection . status !== 406 ) {
@@ -135,7 +156,7 @@ zsd.directive('snapshots', [function(){
135156
136157// https://github.com/angular/angular.js/issues/339
137158zsd . directive ( 'embedSrc' , function ( ) {
138- return {
159+ return {
139160 restrict : 'A' ,
140161 link : function ( scope , element , attrs ) {
141162 var current = element ;
@@ -211,10 +232,8 @@ zsd.directive('dirBrowser', ['Backend', function(Backend){
211232 } ;
212233} ] ) ;
213234
214- zsd . controller ( 'MainCtrl' , [ '$location' , '$rootScope' , 'Config' , function ( $location , $rootScope , Config ) {
235+ zsd . controller ( 'MainCtrl' , [ '$location' , '$rootScope' , '$timeout' , ' Config', function ( $location , $rootScope , $timeout , Config ) {
215236 var self = this ;
216-
217-
218237
219238 Config . promise . then ( function ( ) {
220239 self . config = Config . config ( ) ;
@@ -223,6 +242,25 @@ zsd.controller('MainCtrl', ['$location', '$rootScope', 'Config', function($locat
223242 $rootScope . $on ( 'response-error' , function ( event , args ) {
224243 self . error = args . data ;
225244 } ) ;
245+
246+
247+ $rootScope . $on ( 'http-activity' , function ( event , args ) {
248+ // first http request pending
249+ if ( typeof self . timeoutHndl === 'undefined' ) {
250+ // delayed - only show spinner when duration > 'delay'
251+ var delay = 1000 ;
252+ self . timeoutHndl = $timeout ( function ( ) {
253+ self . loading = true ;
254+ } , delay ) ;
255+ }
256+
257+ // no more http request pending
258+ if ( args === 0 ) {
259+ $timeout . cancel ( self . timeoutHndl ) ;
260+ delete self . timeoutHndl ;
261+ self . loading = false ;
262+ }
263+ } ) ;
226264
227265
228266 self . activeClassIfAt = function ( path ) {
0 commit comments