Skip to content

Commit d0ed528

Browse files
committed
fix($animate): only cancel class-based animations if the follow-up class contains CSS transition/keyframe animation code
Closes angular#4463 Closes angular#3784
1 parent 280e33d commit d0ed528

File tree

2 files changed

+104
-22
lines changed

2 files changed

+104
-22
lines changed

src/ngAnimate/animate.js

+66-20
Original file line numberDiff line numberDiff line change
@@ -489,35 +489,47 @@ angular.module('ngAnimate', ['ng'])
489489
*/
490490
function performAnimation(event, className, element, parent, after, onComplete) {
491491
var classes = (element.attr('class') || '') + ' ' + className;
492-
var animationLookup = (' ' + classes).replace(/\s+/g,'.'),
493-
animations = [];
494-
forEach(lookup(animationLookup), function(animation, index) {
495-
animations.push({
496-
start : animation[event]
497-
});
498-
});
499-
492+
var animationLookup = (' ' + classes).replace(/\s+/g,'.');
500493
if (!parent) {
501494
parent = after ? after.parent() : element.parent();
502495
}
496+
503497
var disabledAnimation = { running : true };
498+
var matches = lookup(animationLookup);
499+
var isClassBased = event == 'addClass' || event == 'removeClass';
500+
var ngAnimateState = element.data(NG_ANIMATE_STATE) || {};
504501

505-
//skip the animation if animations are disabled, a parent is already being animated
506-
//or the element is not currently attached to the document body.
507-
if ((parent.inheritedData(NG_ANIMATE_STATE) || disabledAnimation).running || animations.length == 0) {
502+
//skip the animation if animations are disabled, a parent is already being animated,
503+
//the element is not currently attached to the document body or then completely close
504+
//the animation if any matching animations are not found at all.
505+
//NOTE: IE8 + IE9 should close properly (run done()) in case a NO animation is not found.
506+
if ((parent.inheritedData(NG_ANIMATE_STATE) || disabledAnimation).running || matches.length == 0) {
508507
done();
509508
return;
510509
}
511510

512-
var ngAnimateState = element.data(NG_ANIMATE_STATE) || {};
511+
var animations = [];
512+
//only add animations if the currently running animation is not structural
513+
//or if there is no animation running at all
514+
if(!ngAnimateState.running || !(isClassBased && ngAnimateState.structural)) {
515+
forEach(matches, function(animation) {
516+
//add the animation to the queue to if it is allowed to be cancelled
517+
if(!animation.allowCancel || animation.allowCancel(element, event, className)) {
518+
animations.push({
519+
start : animation[event]
520+
});
521+
}
522+
});
523+
}
513524

514-
var isClassBased = event == 'addClass' || event == 'removeClass';
515-
if(ngAnimateState.running) {
516-
if(isClassBased && ngAnimateState.structural) {
517-
onComplete && onComplete();
518-
return;
519-
}
525+
//this would mean that an animation was not allowed so let the existing
526+
//animation do it's thing and close this one early
527+
if(animations.length == 0) {
528+
onComplete && onComplete();
529+
return;
530+
}
520531

532+
if(ngAnimateState.running) {
521533
//if an animation is currently running on the element then lets take the steps
522534
//to cancel that animation and fire any required callbacks
523535
$timeout.cancel(ngAnimateState.flagTimer);
@@ -643,6 +655,7 @@ angular.module('ngAnimate', ['ng'])
643655
ELEMENT_NODE = 1;
644656

645657
var NG_ANIMATE_PARENT_KEY = '$ngAnimateKey';
658+
var NG_ANIMATE_CLASS_KEY = '$$ngAnimateClasses';
646659
var lookupCache = {};
647660
var parentCounter = 0;
648661

@@ -661,7 +674,7 @@ angular.module('ngAnimate', ['ng'])
661674
}
662675

663676
function getElementAnimationDetails(element, cacheKey, onlyCheckTransition) {
664-
var data = lookupCache[cacheKey];
677+
var data = cacheKey ? lookupCache[cacheKey] : null;
665678
if(!data) {
666679
var transitionDuration = 0, transitionDelay = 0,
667680
animationDuration = 0, animationDelay = 0;
@@ -694,7 +707,9 @@ angular.module('ngAnimate', ['ng'])
694707
transitionDuration : transitionDuration,
695708
animationDuration : animationDuration
696709
};
697-
lookupCache[cacheKey] = data;
710+
if(cacheKey) {
711+
lookupCache[cacheKey] = data;
712+
}
698713
}
699714
return data;
700715
}
@@ -761,6 +776,7 @@ angular.module('ngAnimate', ['ng'])
761776
element.addClass(activeClassName);
762777
});
763778

779+
element.data(NG_ANIMATE_CLASS_KEY, className + ' ' + activeClassName);
764780
element.on(css3AnimationEvents, onAnimationProgress);
765781

766782
// This will automatically be called by $animate so
@@ -770,6 +786,7 @@ angular.module('ngAnimate', ['ng'])
770786
element.off(css3AnimationEvents, onAnimationProgress);
771787
element.removeClass(className);
772788
element.removeClass(activeClassName);
789+
element.removeData(NG_ANIMATE_CLASS_KEY);
773790

774791
// Only when the animation is cancelled is the done()
775792
// function not called for this animation therefore
@@ -803,6 +820,35 @@ angular.module('ngAnimate', ['ng'])
803820
}
804821

805822
return {
823+
allowCancel : function(element, event, className) {
824+
//always cancel the current animation if it is a
825+
//structural animation
826+
var oldClasses = element.data(NG_ANIMATE_CLASS_KEY);
827+
if(!oldClasses || ['enter','leave','move'].indexOf(event) >= 0) {
828+
return true;
829+
}
830+
831+
var parent = element.parent();
832+
var clone = angular.element(element[0].cloneNode());
833+
834+
//make the element super hidden and override any CSS style values
835+
clone.attr('style','position:absolute; top:-9999px; left:-9999px');
836+
clone.removeAttr('id');
837+
clone.html('');
838+
839+
angular.forEach(oldClasses.split(' '), function(klass) {
840+
clone.removeClass(klass);
841+
});
842+
843+
var suffix = event == 'addClass' ? '-add' : '-remove';
844+
clone.addClass(suffixClasses(className, suffix));
845+
parent.append(clone);
846+
847+
var timings = getElementAnimationDetails(clone);
848+
clone.remove();
849+
850+
return Math.max(timings.transitionDuration, timings.animationDuration) > 0;
851+
},
806852
enter : function(element, done) {
807853
return animate(element, 'ng-enter', done);
808854
},

test/ngAnimate/animateSpec.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,8 @@ describe("ngAnimate", function() {
746746
expect(element.hasClass('ng-enter')).toBe(true);
747747
expect(element.hasClass('ng-enter-active')).toBe(true);
748748
browserTrigger(element,'transitionend', { timeStamp: Date.now() + 22000, elapsedTime: 22000 });
749+
$timeout.flush();
749750
}
750-
$timeout.flush();
751751
expect(element.hasClass('abc')).toBe(true);
752752

753753
$rootScope.klass = 'xyz';
@@ -760,8 +760,8 @@ describe("ngAnimate", function() {
760760
expect(element.hasClass('ng-enter')).toBe(true);
761761
expect(element.hasClass('ng-enter-active')).toBe(true);
762762
browserTrigger(element,'transitionend', { timeStamp: Date.now() + 11000, elapsedTime: 11000 });
763+
$timeout.flush();
763764
}
764-
$timeout.flush();
765765
expect(element.hasClass('xyz')).toBe(true);
766766
}));
767767

@@ -1920,4 +1920,40 @@ describe("ngAnimate", function() {
19201920
expect(count).toBe(40);
19211921
});
19221922
});
1923+
1924+
it("should cancel an ongoing class-based animation only if the new class contains transition/animation CSS code",
1925+
inject(function($compile, $rootScope, $animate, $sniffer) {
1926+
1927+
if (!$sniffer.transitions) return;
1928+
1929+
ss.addRule('.green-add', '-webkit-transition:1s linear all;' +
1930+
'transition:1s linear all;');
1931+
1932+
ss.addRule('.blue-add', 'background:blue;');
1933+
1934+
ss.addRule('.red-add', '-webkit-transition:1s linear all;' +
1935+
'transition:1s linear all;');
1936+
1937+
ss.addRule('.yellow-add', '-webkit-animation: some_animation 4s linear 1s 2 alternate;' +
1938+
'animation: some_animation 4s linear 1s 2 alternate;');
1939+
1940+
var element = $compile('<div></div>')($rootScope);
1941+
$rootElement.append(element);
1942+
jqLite($document[0].body).append($rootElement);
1943+
1944+
$animate.addClass(element, 'green');
1945+
expect(element.hasClass('green-add')).toBe(true);
1946+
1947+
$animate.addClass(element, 'blue');
1948+
expect(element.hasClass('blue')).toBe(true);
1949+
expect(element.hasClass('green-add')).toBe(true); //not cancelled
1950+
1951+
$animate.addClass(element, 'red');
1952+
expect(element.hasClass('green-add')).toBe(false);
1953+
expect(element.hasClass('red-add')).toBe(true);
1954+
1955+
$animate.addClass(element, 'yellow');
1956+
expect(element.hasClass('red-add')).toBe(false);
1957+
expect(element.hasClass('yellow-add')).toBe(true);
1958+
}));
19231959
});

0 commit comments

Comments
 (0)