From 31927eff309e58a7ff051794de00e6516e91a1f9 Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Mon, 30 Nov 2015 12:23:05 +0100 Subject: [PATCH] fix(ngAnimate): don't normalize classes on follow-up animations to joined animations This allows follow-up animations to remove a class that is currently being added. Fixes #13339 Fixes #13380 Closes #13414 --- src/ngAnimate/animateQueue.js | 61 +++++++++++++++++++++++-------- test/ngAnimate/integrationSpec.js | 39 ++++++++++++++++++++ 2 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/ngAnimate/animateQueue.js b/src/ngAnimate/animateQueue.js index 04f837c8aaa9..62bf8387dfdf 100644 --- a/src/ngAnimate/animateQueue.js +++ b/src/ngAnimate/animateQueue.js @@ -12,6 +12,14 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) { join: [] }; + function makeTruthyMapFromKeys(keys) { + var map = Object.create(null); + forEach(keys, function(key) { + map[key] = true; + }); + return map; + } + function isAllowed(ruleType, element, currentAnimation, previousAnimation) { return rules[ruleType].some(function(fn) { return fn(element, currentAnimation, previousAnimation); @@ -59,11 +67,38 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) { }); rules.cancel.push(function(element, newAnimation, currentAnimation) { - var nO = newAnimation.options; - var cO = currentAnimation.options; + var ONE_SPACE = ' '; + + var nA = newAnimation.options.addClass; + var nR = newAnimation.options.removeClass; + var cA = currentAnimation.options.addClass; + var cR = currentAnimation.options.removeClass; + + // early detection to save the global CPU shortage :) + if ((!isDefined(nA) && !isDefined(nR)) || (!isDefined(cA) && !isDefined(cR))) { + return false; + } + + var cancelSomething = false; + + cA = cA ? makeTruthyMapFromKeys(cA.split(ONE_SPACE)) : null; + cR = cR ? makeTruthyMapFromKeys(cR.split(ONE_SPACE)) : null; + + if (cR && nA) { + cancelSomething = nA.split(ONE_SPACE).some(function(className) { + return cR[className]; + }); + + if (cancelSomething) return true; + } - // if the exact same CSS class is added/removed then it's safe to cancel it - return (nO.addClass && nO.addClass === cO.removeClass) || (nO.removeClass && nO.removeClass === cO.addClass); + if (cA && nR) { + cancelSomething = nR.split(ONE_SPACE).some(function(className) { + return cA[className]; + }); + } + + return cancelSomething; }); this.$get = ['$$rAF', '$rootScope', '$rootElement', '$document', '$$HashMap', @@ -360,19 +395,15 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) { // so an example would involve a leave animation taking over an enter. Then when // the postDigest kicks in the enter will be ignored. var joinAnimationFlag = isAllowed('join', element, newAnimation, existingAnimation); - if (joinAnimationFlag) { - if (existingAnimation.state === RUNNING_STATE) { - normalizeAnimationOptions(element, options); - } else { - applyGeneratedPreparationClasses(element, isStructural ? event : null, options); + if (joinAnimationFlag && existingAnimation.state < RUNNING_STATE) { + applyGeneratedPreparationClasses(element, isStructural ? event : null, options); - event = newAnimation.event = existingAnimation.event; - options = mergeAnimationOptions(element, existingAnimation.options, newAnimation.options); + event = newAnimation.event = existingAnimation.event; + options = mergeAnimationOptions(element, existingAnimation.options, newAnimation.options); - //we return the same runner since only the option values of this animation will - //be fed into the `existingAnimation`. - return existingAnimation.runner; - } + //we return the same runner since only the option values of this animation will + //be fed into the `existingAnimation`. + return existingAnimation.runner; } } } else { diff --git a/test/ngAnimate/integrationSpec.js b/test/ngAnimate/integrationSpec.js index 8f6cde838b55..be32a1fe37ac 100644 --- a/test/ngAnimate/integrationSpec.js +++ b/test/ngAnimate/integrationSpec.js @@ -351,6 +351,45 @@ describe('ngAnimate integration tests', function() { dealoc(element); })); + + + it("should not normalize classes in a follow-up animation to a joined class-based animation", + inject(function($animate, $animateCss, $rootScope, $document, $rootElement, $$rAF) { + + ss.addRule('.hide', 'opacity: 0'); + ss.addRule('.hide-add, .hide-remove', 'transition: 1s linear all'); + + jqLite($document[0].body).append($rootElement); + element = jqLite('
'); + $rootElement.append(element); + + // These animations will be joined together + $animate.addClass(element, 'red'); + $animate.addClass(element, 'hide'); + $rootScope.$digest(); + + expect(element).toHaveClass('red-add'); + expect(element).toHaveClass('hide-add'); + + // When a digest has passed, but no $rAF has been issued yet, .hide hasn't been added to + // the element, which means class normalization does not allow .hide to get removed + $animate.removeClass(element, 'hide'); + $rootScope.$digest(); + $$rAF.flush(); + + expect(element).not.toHaveClass('hide-add hide-add-active'); + expect(element).toHaveClass('hide-remove hide-remove-active'); + + //End the animation process + browserTrigger(element, 'transitionend', + { timeStamp: Date.now() + 1000, elapsedTime: 2 }); + $animate.flush(); + + expect(element).not.toHaveClass('hide-add-active red-add-active'); + expect(element).toHaveClass('red'); + expect(element).not.toHaveClass('hide'); + })); + }); describe('JS animations', function() {