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

fix(ngAnimate): close follow-up class-based animations when the same class is added/removed when removed/added #11947

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ngAnimate/animateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
return currentAnimation.state === RUNNING_STATE && newAnimation.structural;
});

rules.cancel.push(function(element, newAnimation, currentAnimation) {
var nO = newAnimation.options;
var cO = currentAnimation.options;

// 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);
});

this.$get = ['$$rAF', '$rootScope', '$rootElement', '$document', '$$HashMap',
'$$animation', '$$AnimateRunner', '$templateRequest', '$$jqLite',
function($$rAF, $rootScope, $rootElement, $document, $$HashMap,
Expand Down Expand Up @@ -358,6 +366,7 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {

if (!isValidAnimation) {
close();
clearElementAnimationState(element);
return runner;
}

Expand Down
41 changes: 41 additions & 0 deletions test/ngAnimate/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,47 @@ describe("animations", function() {
expect(enterDone).toHaveBeenCalled();
}));

it('should cancel the previously running addClass animation if a follow-up removeClass animation is using the same class value',
inject(function($animate, $rootScope, $$rAF) {

parent.append(element);
var runner = $animate.addClass(element, 'active-class');
$rootScope.$digest();

var doneHandler = jasmine.createSpy('addClass done');
runner.done(doneHandler);

$$rAF.flush();

expect(doneHandler).not.toHaveBeenCalled();

$animate.removeClass(element, 'active-class');
$rootScope.$digest();

expect(doneHandler).toHaveBeenCalled();
}));

it('should cancel the previously running removeClass animation if a follow-up addClass animation is using the same class value',
inject(function($animate, $rootScope, $$rAF) {

element.addClass('active-class');
parent.append(element);
var runner = $animate.removeClass(element, 'active-class');
$rootScope.$digest();

var doneHandler = jasmine.createSpy('addClass done');
runner.done(doneHandler);

$$rAF.flush();

expect(doneHandler).not.toHaveBeenCalled();

$animate.addClass(element, 'active-class');
$rootScope.$digest();

expect(doneHandler).toHaveBeenCalled();
}));

it('should skip the class-based animation entirely if there is an active structural animation',
inject(function($animate, $rootScope) {

Expand Down