diff --git a/README.md b/README.md index cb409bd..9eeeb20 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,10 @@ The loading bar broadcasts the following events over $rootScope allowing further **`cfpLoadingBar:completed`** triggered once when the all XHR requests have returned (either successfully or not) +The loading bar can also be manually closed by firing an event + +**`cfpLoadingBar:manual-loaded`** Trigger this if you want to leave connection open, but cancel loading bar + ## Credits: Credit goes to [rstacruz](https://github.com/rstacruz) for his excellent [nProgress](https://github.com/rstacruz/nprogress). diff --git a/src/loading-bar.js b/src/loading-bar.js index 44e092a..be2d2e3 100644 --- a/src/loading-bar.js +++ b/src/loading-bar.js @@ -88,6 +88,19 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar']) return cached; } + $rootScope.$on('cfpLoadingBar:manual-loaded', function(event, config) { + + if (!config.result.config.ignoreLoadingBar && !isCached(config.result.config)) { + reqsCompleted++; + $rootScope.$broadcast('cfpLoadingBar:loaded',config); + if (reqsCompleted >= reqsTotal) { + setComplete(); + } else { + cfpLoadingBar.set(reqsCompleted / reqsTotal); + } + } + }); + return { 'request': function(config) { diff --git a/test/loading-bar-interceptor.coffee b/test/loading-bar-interceptor.coffee index 241967f..d6a4302 100644 --- a/test/loading-bar-interceptor.coffee +++ b/test/loading-bar-interceptor.coffee @@ -200,6 +200,24 @@ describe 'loadingBarInterceptor Service', -> expect(cfpLoadingBar.status()).toBe 1 $timeout.flush() + + it 'should count http manual loaded event as responses so the loading bar can complete', inject (cfpLoadingBar, $rootScope) -> + # $httpBackend.expectGET(endpoint).respond response + $httpBackend.expectGET(endpoint).respond 401 + $httpBackend.expectGET(endpoint).respond 401 + $http.get(endpoint) + $http.get(endpoint) + + expect(cfpLoadingBar.status()).toBe 0 + $timeout.flush() + $timeout.flush() + $httpBackend.flush(1) + expect(cfpLoadingBar.status()).toBe 0.5 + + $rootScope.$emit('cfpLoadingBar:manual-loaded', {url: endpoint, result: { config: { cache : false}}}) + expect(cfpLoadingBar.status()).toBe 1 + + $timeout.flush() it 'should insert the loadingbar into the DOM when a request is sent', inject (cfpLoadingBar) -> $httpBackend.expectGET(endpoint).respond response