Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 259a572

Browse files
committed
refactor($animateCss): use $animateRunner
1 parent 90098ef commit 259a572

File tree

4 files changed

+80
-73
lines changed

4 files changed

+80
-73
lines changed

src/ngAnimate/animateCss.js

+36-32
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ var ONE_SECOND = 1000;
4242
var BASE_TEN = 10;
4343

4444
function computeCSSStyles($window, element, properties) {
45-
var styles = {};
46-
45+
var styles = Object.create(null);
4746
var detectedStyles = $window.getComputedStyle(element) || {};
4847
forEach(properties, function(style, prop) {
4948
var val = detectedStyles[style];
@@ -61,9 +60,7 @@ function computeCSSStyles($window, element, properties) {
6160

6261
function parseMaxTime(str) {
6362
var maxValue = 0;
64-
var values = isString(str) ?
65-
str.split(/\s*,\s*/) :
66-
[];
63+
var values = str.split(/\s*,\s*/);
6764
forEach(values, function(value) {
6865
maxValue = Math.max(parseFloat(value) || 0, maxValue);
6966
});
@@ -90,15 +87,15 @@ function getCssDelayStyle(delay, isKeyframeAnimation) {
9087
return [prop, delay + 's'];
9188
}
9289

93-
function blockTransitions(node, bool) {
94-
var value = bool ? 'none' : '';
90+
function blockTransitions(node, applyBlock) {
91+
var value = applyBlock ? 'none' : '';
9592
var key = TRANSITION_PROP + PROPERTY_KEY;
9693
applyInlineStyle(node, key, value);
9794
return [key, value];
9895
}
9996

100-
function blockAnimations(node, bool) {
101-
var value = bool ? 'paused' : '';
97+
function blockAnimations(node, applyBlock) {
98+
var value = applyBlock ? 'paused' : '';
10299
var key = ANIMATION_PROP + ANIMATION_PLAYSTATE_KEY;
103100
applyInlineStyle(node, key, value);
104101
return [key, value];
@@ -112,8 +109,8 @@ function applyInlineStyle(node, key, value) {
112109
node.style[key] = value;
113110
}
114111

115-
function LocalCacheLookup() {
116-
var cache = {};
112+
function createLocalCacheLookup() {
113+
var cache = Object.create(null);
117114
return {
118115
flush: function() {
119116
cache = {};
@@ -131,17 +128,17 @@ function LocalCacheLookup() {
131128

132129
put: function(key, value) {
133130
if (!cache[key]) {
134-
cache[key] = { total: 0 };
131+
cache[key] = { total: 1, value: value };
132+
} else {
133+
cache[key].total++;
135134
}
136-
cache[key].total++;
137-
cache[key].value = value;
138135
}
139136
};
140137
}
141138

142139
var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
143-
var gcsLookup = new LocalCacheLookup();
144-
var gcsStaggerLookup = new LocalCacheLookup();
140+
var gcsLookup = createLocalCacheLookup();
141+
var gcsStaggerLookup = createLocalCacheLookup();
145142

146143
this.$get = ['$window', '$$jqLite', '$animateRunner', '$timeout',
147144
'$document', '$$animateOptions', '$sniffer', '$$rAF',
@@ -161,8 +158,8 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
161158

162159
if (!timings) {
163160
timings = computeCSSStyles($window, node, properties);
164-
if (timings.animationIterationCount) {
165-
timings.animationIterationCount = parseInt(timings.animationIterationCount || 1, BASE_TEN);
161+
if(timings.animationIterationCount === 'infinite') {
162+
timings.animationIterationCount = 1;
166163
}
167164
}
168165

@@ -173,12 +170,13 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
173170
}
174171

175172
function computeCachedCSSStaggerStyles(node, className, cacheKey, properties) {
176-
var stagger = {};
177-
var isRepeated = gcsLookup.count(cacheKey) > 0;
173+
var stagger;
178174

179-
if (isRepeated) {
180-
var staggerCacheKey = cacheKey + '-stagger';
181-
stagger = gcsStaggerLookup.get(staggerCacheKey);
175+
// if we have one or more existing matches of matching elements
176+
// containing the same parent + CSS styles (which is how cacheKey works)
177+
// then staggering is possible
178+
if (gcsLookup.count(cacheKey) > 0) {
179+
stagger = gcsStaggerLookup.get(cacheKey);
182180

183181
if (!stagger) {
184182
var staggerClassName = pendClasses(className, '-stagger');
@@ -189,18 +187,21 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
189187

190188
$$jqLite.removeClass(node, staggerClassName);
191189

192-
gcsStaggerLookup.put(staggerCacheKey, stagger);
190+
gcsStaggerLookup.put(cacheKey, stagger);
193191
}
194192
}
195193

196-
return stagger;
194+
return stagger || {};
197195
}
198196

199-
var cancelLastRAFRequest, bod = $document[0].body;
197+
var bod = $document[0].body;
198+
var cancelLastRAFRequest;
199+
var rafWaitQueue = [];
200200
function waitUntilQuiet(callback) {
201201
if (cancelLastRAFRequest) {
202202
cancelLastRAFRequest(); //cancels the request
203203
}
204+
rafWaitQueue.push(callback);
204205
cancelLastRAFRequest = $$rAF(function() {
205206
cancelLastRAFRequest = null;
206207
gcsLookup.flush();
@@ -212,7 +213,10 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
212213
//ensure that the the preparation animation is properly flushed so that
213214
//the active state picks up from there. DO NOT REMOVE THIS LINE.
214215
var a = bod.offsetWidth + 1;
215-
callback();
216+
forEach(rafWaitQueue, function(cb) {
217+
cb();
218+
});
219+
rafWaitQueue.length = 0;
216220
});
217221
}
218222

@@ -262,7 +266,7 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
262266
// class-based animations so that we can avoid applying CSS blocking on
263267
// the element to allow for normal transitions to work (this is how enter,
264268
// leave and move can perform the `ng-EVENT` methods without causing an
265-
// unpected transition animation to occur).
269+
// unexpected transition animation to occur).
266270
var structural = method &&
267271
[' setClass ', ' addClass ',' removeClass '].indexOf(' ' + method + ' ') == -1;
268272

@@ -508,7 +512,7 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
508512

509513
// if the preparation function fails then the promise is not setup
510514
if (runner) {
511-
rejected ? runner.end() : runner.cancel();
515+
runner.complete(!rejected);
512516
}
513517
}
514518

@@ -526,16 +530,16 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
526530
if (animationClosed) return;
527531

528532
var startTime, events = [];
529-
var playPause = function(bool) {
533+
var playPause = function(playAnimation) {
530534
if (!animationCompleted) {
531-
animationPaused = !bool;
535+
animationPaused = !playAnimation;
532536
if (timings.animationDuration) {
533537
var value = blockAnimations(node, animationPaused);
534538
animationPaused
535539
? temporaryStyles.push(value)
536540
: removeFromArray(temporaryStyles, value);
537541
}
538-
} else if (animationPaused && bool) {
542+
} else if (animationPaused && playAnimation) {
539543
animationPaused = false;
540544
close();
541545
}

src/ngAnimate/animateQueue.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
5858
});
5959

6060
this.$get = ['$qRaf', '$$rAF', '$rootScope', '$rootElement', '$document', '$$HashMap',
61-
'$$animation', '$$animateRunner', '$templateRequest', '$$animateOptions',
61+
'$$animation', '$animateRunner', '$templateRequest', '$$animateOptions',
6262
function($qRaf, $$rAF, $rootScope, $rootElement, $document, $$HashMap,
63-
$$animation, $$animateRunner, $templateRequest, $$animateOptions) {
63+
$$animation, $animateRunner, $templateRequest, $$animateOptions) {
6464

6565
var activeAnimationsLookup = new $$HashMap(); // jshint ignore:line
6666
var disabledElementsLookup = new $$HashMap(); // jshint ignore:line

src/ngAnimate/animation.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var $$AnimationProvider = ['$animateProvider', function($animateProvider) {
2020
return element.data(RUNNER_STORAGE_KEY);
2121
}
2222

23-
this.$get = ['$qRaf', '$$jqLite', '$rootScope', '$injector', '$$animateRunner', '$$animateOptions',
24-
function($qRaf, $$jqLite, $rootScope, $injector, $$animateRunner, $$animateOptions) {
23+
this.$get = ['$qRaf', '$$jqLite', '$rootScope', '$injector', '$animateRunner', '$$animateOptions',
24+
function($qRaf, $$jqLite, $rootScope, $injector, $animateRunner, $$animateOptions) {
2525

2626
var animationQueue = [];
2727

0 commit comments

Comments
 (0)