diff --git a/.gitignore b/.gitignore index 1690f49..e5202bf 100755 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,8 @@ npm-debug.log node_modules .DS_Store -temp \ No newline at end of file +temp + +_SpecRunner.html + +.grunt \ No newline at end of file diff --git a/README.md b/README.md index 2be6fb4..8ba7aab 100755 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ Only the values you'd like overriden need to be specified. ## Release History + * v4.0.2 - Fix for min duration only being used when delay also being set. * v4.0.0 - Big update * Dependency on angular-promise-tracker has been removed. We now track promises directly. * Message is now configurable. diff --git a/angular-busy.js b/angular-busy.js index e249921..f21ff55 100755 --- a/angular-busy.js +++ b/angular-busy.js @@ -26,16 +26,16 @@ angular.module('cgBusy').factory('_cgBusyTrackerFactory',['$timeout',function($t return; } - if (options.delay > 0) { + if (options.delay) { tracker.delayPromise = $timeout(function(){ tracker.delayPromise = null; - if (options.minDuration) { - tracker.durationPromise = $timeout(function(){ - tracker.durationPromise = null; - },options.minDuration); - } },options.delay); } + if (options.minDuration) { + tracker.durationPromise = $timeout(function(){ + tracker.durationPromise = null; + },options.minDuration); + } }; tracker.getThen = function(promise){ diff --git a/bower.json b/bower.json index 95748ea..1a025c9 100755 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-busy", - "version": "4.0.1", + "version": "4.0.2", "main": [ "dist/angular-busy.js", "dist/angular-busy.css" diff --git a/dist/angular-busy.js b/dist/angular-busy.js index 646aecf..6f059e6 100644 --- a/dist/angular-busy.js +++ b/dist/angular-busy.js @@ -26,16 +26,16 @@ angular.module('cgBusy').factory('_cgBusyTrackerFactory',['$timeout',function($t return; } - if (options.delay > 0) { + if (options.delay) { tracker.delayPromise = $timeout(function(){ tracker.delayPromise = null; - if (options.minDuration) { - tracker.durationPromise = $timeout(function(){ - tracker.durationPromise = null; - },options.minDuration); - } },options.delay); } + if (options.minDuration) { + tracker.durationPromise = $timeout(function(){ + tracker.durationPromise = null; + },options.minDuration); + } }; tracker.getThen = function(promise){ diff --git a/dist/angular-busy.min.js b/dist/angular-busy.min.js index 344f6c4..063ef38 100644 --- a/dist/angular-busy.min.js +++ b/dist/angular-busy.min.js @@ -1 +1 @@ -angular.module("cgBusy",[]),angular.module("cgBusy").factory("_cgBusyTrackerFactory",["$timeout",function(a){return function(){var b={};b.promises=[],b.delayPromise=null,b.durationPromise=null,b.reset=function(d){b.minDuration=d.minDuration,b.promises=[],angular.forEach(d.promises,function(a){a&&!a.$cgBusyFulfilled&&c(a)}),0!==b.promises.length&&d.delay>0&&(b.delayPromise=a(function(){b.delayPromise=null,d.minDuration&&(b.durationPromise=a(function(){b.durationPromise=null},d.minDuration))},d.delay))},b.getThen=function(a){var b=a&&(a.then||a.$then||a.$promise&&a.$promise.then);return b};var c=function(a){var c=b.getThen(a);if(!c)throw new Error("cgBusy expects a promise (or something that has a .promise or .$promise");-1===b.promises.indexOf(a)&&(b.promises.push(a),c(function(){a.$cgBusyFulfilled=!0,-1!==b.promises.indexOf(a)&&b.promises.splice(b.promises.indexOf(a),1)},function(){a.$cgBusyFulfilled=!0,-1!==b.promises.indexOf(a)&&b.promises.splice(b.promises.indexOf(a),1)}))};return b.active=function(){return b.delayPromise?!1:b.durationPromise?!0:b.promises.length>0},b}}]),angular.module("cgBusy").value("cgBusyDefaults",{}),angular.module("cgBusy").directive("cgBusy",["$compile","$templateCache","cgBusyDefaults","$http","_cgBusyTrackerFactory",function(a,b,c,d,e){return{restrict:"A",link:function(f,g,h){var i=g.css("position");("static"===i||""===i||"undefined"==typeof i)&&g.css("position","relative");var j,k,l,m,n=e(),o={templateUrl:"angular-busy.html",delay:0,minDuration:0,backdrop:!0,message:"Please Wait..."};angular.extend(o,c),f.$watchCollection(h.cgBusy,function(c){if(c||(c={promise:null}),angular.isString(c))throw new Error("Invalid value for cg-busy. cgBusy no longer accepts string ids to represent promises/trackers.");(angular.isArray(c)||n.getThen(c))&&(c={promise:c}),c=angular.extend(angular.copy(o),c),c.templateUrl||(c.templateUrl=o.templateUrl),angular.isArray(c.promise)||(c.promise=[c.promise]),l||(l=f.$new()),l.$message=c.message,angular.equals(n.promises,c.promise)||n.reset({promises:c.promise,delay:c.delay,minDuration:c.minDuration}),l.$cgBusyIsActive=function(){return n.active()},j&&k===c.templateUrl&&m===c.backdrop||(j&&j.remove(),k=c.templateUrl,m=c.backdrop,d.get(k,{cache:b}).success(function(b){c.backdrop="undefined"==typeof c.backdrop?!0:c.backdrop;var d=c.backdrop?'
':"",e='
'+d+b+"
";j=a(e)(l),angular.element(j.children()[c.backdrop?1:0]).css("position","absolute").css("top",0).css("left",0).css("right",0).css("bottom",0),g.append(j)}).error(function(a){throw new Error("Template specified for cgBusy ("+c.templateUrl+") could not be loaded. "+a)}))},!0)}}}]),angular.module("cgBusy").run(["$templateCache",function(a){"use strict";a.put("angular-busy.html",'
\n\n
\n\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n\n
{{$message}}
\n\n
\n\n
')}]); \ No newline at end of file +angular.module("cgBusy",[]),angular.module("cgBusy").factory("_cgBusyTrackerFactory",["$timeout",function(a){return function(){var b={};b.promises=[],b.delayPromise=null,b.durationPromise=null,b.reset=function(d){b.minDuration=d.minDuration,b.promises=[],angular.forEach(d.promises,function(a){a&&!a.$cgBusyFulfilled&&c(a)}),0!==b.promises.length&&(d.delay&&(b.delayPromise=a(function(){b.delayPromise=null},d.delay)),d.minDuration&&(b.durationPromise=a(function(){b.durationPromise=null},d.minDuration)))},b.getThen=function(a){var b=a&&(a.then||a.$then||a.$promise&&a.$promise.then);return b};var c=function(a){var c=b.getThen(a);if(!c)throw new Error("cgBusy expects a promise (or something that has a .promise or .$promise");-1===b.promises.indexOf(a)&&(b.promises.push(a),c(function(){a.$cgBusyFulfilled=!0,-1!==b.promises.indexOf(a)&&b.promises.splice(b.promises.indexOf(a),1)},function(){a.$cgBusyFulfilled=!0,-1!==b.promises.indexOf(a)&&b.promises.splice(b.promises.indexOf(a),1)}))};return b.active=function(){return b.delayPromise?!1:b.durationPromise?!0:b.promises.length>0},b}}]),angular.module("cgBusy").value("cgBusyDefaults",{}),angular.module("cgBusy").directive("cgBusy",["$compile","$templateCache","cgBusyDefaults","$http","_cgBusyTrackerFactory",function(a,b,c,d,e){return{restrict:"A",link:function(f,g,h){var i=g.css("position");("static"===i||""===i||"undefined"==typeof i)&&g.css("position","relative");var j,k,l,m,n=e(),o={templateUrl:"angular-busy.html",delay:0,minDuration:0,backdrop:!0,message:"Please Wait..."};angular.extend(o,c),f.$watchCollection(h.cgBusy,function(c){if(c||(c={promise:null}),angular.isString(c))throw new Error("Invalid value for cg-busy. cgBusy no longer accepts string ids to represent promises/trackers.");(angular.isArray(c)||n.getThen(c))&&(c={promise:c}),c=angular.extend(angular.copy(o),c),c.templateUrl||(c.templateUrl=o.templateUrl),angular.isArray(c.promise)||(c.promise=[c.promise]),l||(l=f.$new()),l.$message=c.message,angular.equals(n.promises,c.promise)||n.reset({promises:c.promise,delay:c.delay,minDuration:c.minDuration}),l.$cgBusyIsActive=function(){return n.active()},j&&k===c.templateUrl&&m===c.backdrop||(j&&j.remove(),k=c.templateUrl,m=c.backdrop,d.get(k,{cache:b}).success(function(b){c.backdrop="undefined"==typeof c.backdrop?!0:c.backdrop;var d=c.backdrop?'
':"",e='
'+d+b+"
";j=a(e)(l),angular.element(j.children()[c.backdrop?1:0]).css("position","absolute").css("top",0).css("left",0).css("right",0).css("bottom",0),g.append(j)}).error(function(a){throw new Error("Template specified for cgBusy ("+c.templateUrl+") could not be loaded. "+a)}))},!0)}}}]),angular.module("cgBusy").run(["$templateCache",function(a){"use strict";a.put("angular-busy.html",'
\n\n
\n\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n\n
{{$message}}
\n\n
\n\n
')}]); \ No newline at end of file diff --git a/package.json b/package.json index 94efc89..f11b0c5 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-busy", - "version": "4.0.1", + "version": "4.0.2", "description": "", "repository": { "type": "git", diff --git a/test/spec.js b/test/spec.js index 50b29ea..e8a37ae 100755 --- a/test/spec.js +++ b/test/spec.js @@ -1,81 +1,137 @@ describe('cgBusy', function() { - beforeEach(module('app')); + beforeEach(module('app')); - var scope,compile,q,httpBackend; + var scope,compile,q,httpBackend,timeout; - beforeEach(inject(function($rootScope,$compile,$q,$httpBackend,$templateCache) { - scope = $rootScope.$new(); - compile = $compile; - q = $q; - httpBackend = $httpBackend; - httpBackend.whenGET('test-custom-template.html').respond(function(method, url, data, headers){ + beforeEach(inject(function($rootScope,$compile,$q,$httpBackend,$templateCache,$timeout) { + scope = $rootScope.$new(); + compile = $compile; + q = $q; + httpBackend = $httpBackend; + timeout = $timeout; + httpBackend.whenGET('test-custom-template.html').respond(function(method, url, data, headers){ - return [[200],'
test-custom-template-contents
']; - }); - })); + return [[200],'
test-custom-template-contents
']; + }); + })); - it('should show the overlay during promise', function() { + it('should show the overlay during promise', function() { - this.element = compile('
')(scope); - angular.element('body').append(this.element); + this.element = compile('
')(scope); + angular.element('body').append(this.element); - this.testPromise = q.defer(); - scope.my_promise = this.testPromise.promise; + this.testPromise = q.defer(); + scope.my_promise = this.testPromise.promise; - //httpBackend.flush(); + //httpBackend.flush(); - scope.$apply(); + scope.$apply(); - expect(this.element.children().length).toBe(1); //ensure element is added + expect(this.element.children().length).toBe(1); //ensure element is added - expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing) + expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing) - this.testPromise.resolve(); - scope.$apply(); + this.testPromise.resolve(); + scope.$apply(); - expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved - }); + expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved + }); - it('should show the overlay during multiple promises', function() { + it('should show the overlay during multiple promises', function() { - this.element = compile('
')(scope); - angular.element('body').append(this.element); + this.element = compile('
')(scope); + angular.element('body').append(this.element); - this.testPromise = q.defer(); - scope.my_promise = this.testPromise.promise; + this.testPromise = q.defer(); + scope.my_promise = this.testPromise.promise; - this.testPromise2 = q.defer(); - scope.my_promise2 = this.testPromise2.promise; + this.testPromise2 = q.defer(); + scope.my_promise2 = this.testPromise2.promise; - //httpBackend.flush(); + //httpBackend.flush(); - scope.$apply(); + scope.$apply(); - expect(this.element.children().length).toBe(1); //ensure element is added + expect(this.element.children().length).toBe(1); //ensure element is added - expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing) + expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing) - this.testPromise.resolve(); - scope.$apply(); + this.testPromise.resolve(); + scope.$apply(); - expect(this.element.children().css('display')).toBe('block'); //ensure its still visible (promise is ongoing) + expect(this.element.children().css('display')).toBe('block'); //ensure its still visible (promise is ongoing) - this.testPromise2.resolve(); - scope.$apply(); - expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved - }); + this.testPromise2.resolve(); + scope.$apply(); + expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved + }); - it('should load custom templates', function(){ + it('should load custom templates', function(){ - this.element = compile('
')(scope); - angular.element('body').append(this.element); + this.element = compile('
')(scope); + angular.element('body').append(this.element); - httpBackend.flush(); + httpBackend.flush(); - scope.$apply(); + scope.$apply(); - expect(angular.element('#custom').html()).toBe('test-custom-template-contents'); + expect(angular.element('#custom').html()).toBe('test-custom-template-contents'); + + }); + + it('should delay when delay provided.', function() { + + this.element = compile('
')(scope); + angular.element('body').append(this.element); + + this.testPromise = q.defer(); + scope.my_promise = this.testPromise.promise; + + scope.$apply(); + + expect(this.element.children().length).toBe(1); //ensure element is added + + expect(this.element.children().css('display')).toBe('none'); + + timeout.flush(200); + expect(this.element.children().css('display')).toBe('none'); + + timeout.flush(301); + expect(this.element.children().css('display')).toBe('block'); + this.testPromise.resolve(); + scope.$apply(); + expect(this.element.children().css('display')).toBe('none'); + + }); + + it('should use minDuration correctly.', function() { + + this.element = compile('
')(scope); + angular.element('body').append(this.element); + + this.testPromise = q.defer(); + scope.my_promise = this.testPromise.promise; + + scope.$apply(); + + expect(this.element.children().length).toBe(1); //ensure element is added + + expect(this.element.children().css('display')).toBe('block'); + + timeout.flush(200); + expect(this.element.children().css('display')).toBe('block'); + + this.testPromise.resolve(); + timeout.flush(400); + expect(this.element.children().css('display')).toBe('block'); + + timeout.flush(300); //900ms total + expect(this.element.children().css('display')).toBe('block'); + + timeout.flush(101); //1001ms total + expect(this.element.children().css('display')).toBe('none'); + + }); - }) }); \ No newline at end of file