diff options
| author | Matias Niemelä | 2014-01-12 15:49:52 -0500 |
|---|---|---|
| committer | Matias Niemelä | 2014-01-14 13:21:19 -0500 |
| commit | 4ae3184c5915aac9aa00889aa2153c8e84c14966 (patch) | |
| tree | 3eab74b2750934456f8fb29beae748bbe688eac1 /src | |
| parent | ed53100a0dbc9119d5dfc8b7248845d4f6989df2 (diff) | |
| download | angular.js-4ae3184c5915aac9aa00889aa2153c8e84c14966.tar.bz2 | |
feat($animate): use requestAnimationFrame instead of a timeout to issue a reflow
Closes #4278
Closes #4225
Diffstat (limited to 'src')
| -rw-r--r-- | src/ngAnimate/animate.js | 37 | ||||
| -rw-r--r-- | src/ngMock/angular-mocks.js | 21 |
2 files changed, 51 insertions, 7 deletions
diff --git a/src/ngAnimate/animate.js b/src/ngAnimate/animate.js index 22a9dde7..0417f18e 100644 --- a/src/ngAnimate/animate.js +++ b/src/ngAnimate/animate.js @@ -248,6 +248,28 @@ angular.module('ngAnimate', ['ng']) * Please visit the {@link ngAnimate `ngAnimate`} module overview page learn more about how to use animations in your application. * */ + .factory('$$animateReflow', ['$window', '$timeout', function($window, $timeout) { + var requestAnimationFrame = $window.requestAnimationFrame || + $window.mozRequestAnimationFrame || + $window.webkitRequestAnimationFrame || + function(fn) { + return $timeout(fn, 10, false); + }; + + var cancelAnimationFrame = $window.cancelAnimationFrame || + $window.mozCancelAnimationFrame || + $window.webkitCancelAnimationFrame || + function(timer) { + return $timeout.cancel(timer); + }; + return function(fn) { + var id = requestAnimationFrame(fn); + return function() { + cancelAnimationFrame(id); + }; + }; + }]) + .config(['$provide', '$animateProvider', function($provide, $animateProvider) { var noop = angular.noop; var forEach = angular.forEach; @@ -872,7 +894,8 @@ angular.module('ngAnimate', ['ng']) } }]); - $animateProvider.register('', ['$window', '$sniffer', '$timeout', function($window, $sniffer, $timeout) { + $animateProvider.register('', ['$window', '$sniffer', '$timeout', '$$animateReflow', + function($window, $sniffer, $timeout, $$animateReflow) { // Detect proper transitionend/animationend event names. var CSS_PREFIX = '', TRANSITION_PROP, TRANSITIONEND_EVENT, ANIMATION_PROP, ANIMATIONEND_EVENT; @@ -917,11 +940,13 @@ angular.module('ngAnimate', ['ng']) var parentCounter = 0; var animationReflowQueue = []; var animationElementQueue = []; - var animationTimer; + var cancelAnimationReflow; var closingAnimationTime = 0; var timeOut = false; function afterReflow(element, callback) { - $timeout.cancel(animationTimer); + if(cancelAnimationReflow) { + cancelAnimationReflow(); + } animationReflowQueue.push(callback); @@ -942,7 +967,7 @@ angular.module('ngAnimate', ['ng']) //a follow-up animation is midway in its animation elementData.animationCount = animationCounter; - animationTimer = $timeout(function() { + cancelAnimationReflow = $$animateReflow(function() { forEach(animationReflowQueue, function(fn) { fn(); }); @@ -963,11 +988,11 @@ angular.module('ngAnimate', ['ng']) animationReflowQueue = []; animationElementQueue = []; - animationTimer = null; + cancelAnimationReflow = null; lookupCache = {}; closingAnimationTime = 0; animationCounter++; - }, 10, false); + }); } function closeAllAnimations(elements, count) { diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index b53b4e02..803cedbd 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -756,6 +756,26 @@ angular.mock.TzDate = function (offset, timestamp) { angular.mock.TzDate.prototype = Date.prototype; /* jshint +W101 */ +angular.module('ngAnimate').config(['$provide', function($provide) { + var reflowQueue = []; + $provide.value('$$animateReflow', function(fn) { + reflowQueue.push(fn); + return angular.noop; + }); + $provide.decorator('$animate', function($delegate) { + $delegate.triggerReflow = function() { + if(reflowQueue.length === 0) { + throw new Error('No animation reflows present'); + } + angular.forEach(reflowQueue, function(fn) { + fn(); + }); + reflowQueue = []; + }; + return $delegate; + }); +}]); + angular.mock.animate = angular.module('mock.animate', ['ng']) .config(['$provide', function($provide) { @@ -1913,7 +1933,6 @@ angular.mock.clearDataCache = function() { }; - if(window.jasmine || window.mocha) { var currentSpec = null, |
