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

Commit d5ea497

Browse files
committed
refactor(ngAnimate): simplify areAnimationsAllowed() and remove redundant calls
1 parent 3db2092 commit d5ea497

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

src/ngAnimate/animateQueue.js

+28-32
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
381381
// there is no point in traversing the same collection of parent ancestors if a followup
382382
// animation will be run on the same element that already did all that checking work
383383
if (!skipAnimations && (!hasExistingAnimation || existingAnimation.state !== PRE_DIGEST_STATE)) {
384-
skipAnimations = !areAnimationsAllowed(element, parent, event);
384+
skipAnimations = !areAnimationsAllowed(node, getDomNode(parent), event);
385385
}
386386

387387
if (skipAnimations) {
@@ -610,65 +610,61 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
610610
activeAnimationsLookup.remove(node);
611611
}
612612

613-
function isMatchingElement(nodeOrElmA, nodeOrElmB) {
614-
return getDomNode(nodeOrElmA) === getDomNode(nodeOrElmB);
615-
}
616-
617613
/**
618614
* This fn returns false if any of the following is true:
619615
* a) animations on any parent element are disabled, and animations on the element aren't explicitly allowed
620616
* b) a parent element has an ongoing structural animation, and animateChildren is false
621617
* c) the element is not a child of the body
622618
* d) the element is not a child of the $rootElement
623619
*/
624-
function areAnimationsAllowed(element, parentElement, event) {
625-
var bodyElement = jqLite($document[0].body);
626-
var bodyElementDetected = isMatchingElement(element, bodyElement) || element[0].nodeName === 'HTML';
627-
var rootElementDetected = isMatchingElement(element, $rootElement);
620+
function areAnimationsAllowed(node, parentNode, event) {
621+
var bodyNode = $document[0].body;
622+
var rootNode = getDomNode($rootElement);
623+
624+
var bodyNodeDetected = (node === bodyNode) || node.nodeName === 'HTML';
625+
var rootNodeDetected = (node === rootNode);
628626
var parentAnimationDetected = false;
627+
var elementDisabled = disabledElementsLookup.get(node);
629628
var animateChildren;
630-
var elementDisabled = disabledElementsLookup.get(getDomNode(element));
631629

632-
var parentHost = jqLite.data(element[0], NG_ANIMATE_PIN_DATA);
630+
var parentHost = jqLite.data(node, NG_ANIMATE_PIN_DATA);
633631
if (parentHost) {
634-
parentElement = parentHost;
632+
parentNode = getDomNode(parentHost);
635633
}
636634

637-
parentElement = getDomNode(parentElement);
638-
639-
while (parentElement) {
640-
if (!rootElementDetected) {
635+
while (parentNode) {
636+
if (!rootNodeDetected) {
641637
// angular doesn't want to attempt to animate elements outside of the application
642638
// therefore we need to ensure that the rootElement is an ancestor of the current element
643-
rootElementDetected = isMatchingElement(parentElement, $rootElement);
639+
rootNodeDetected = (parentNode === rootNode);
644640
}
645641

646-
if (parentElement.nodeType !== ELEMENT_NODE) {
642+
if (parentNode.nodeType !== ELEMENT_NODE) {
647643
// no point in inspecting the #document element
648644
break;
649645
}
650646

651-
var details = activeAnimationsLookup.get(parentElement) || {};
647+
var details = activeAnimationsLookup.get(parentNode) || {};
652648
// either an enter, leave or move animation will commence
653649
// therefore we can't allow any animations to take place
654650
// but if a parent animation is class-based then that's ok
655651
if (!parentAnimationDetected) {
656-
var parentElementDisabled = disabledElementsLookup.get(parentElement);
652+
var parentNodeDisabled = disabledElementsLookup.get(parentNode);
657653

658-
if (parentElementDisabled === true && elementDisabled !== false) {
654+
if (parentNodeDisabled === true && elementDisabled !== false) {
659655
// disable animations if the user hasn't explicitly enabled animations on the
660656
// current element
661657
elementDisabled = true;
662658
// element is disabled via parent element, no need to check anything else
663659
break;
664-
} else if (parentElementDisabled === false) {
660+
} else if (parentNodeDisabled === false) {
665661
elementDisabled = false;
666662
}
667663
parentAnimationDetected = details.structural;
668664
}
669665

670666
if (isUndefined(animateChildren) || animateChildren === true) {
671-
var value = jqLite.data(parentElement, NG_ANIMATE_CHILDREN_DATA);
667+
var value = jqLite.data(parentNode, NG_ANIMATE_CHILDREN_DATA);
672668
if (isDefined(value)) {
673669
animateChildren = value;
674670
}
@@ -677,33 +673,33 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
677673
// there is no need to continue traversing at this point
678674
if (parentAnimationDetected && animateChildren === false) break;
679675

680-
if (!bodyElementDetected) {
676+
if (!bodyNodeDetected) {
681677
// we also need to ensure that the element is or will be a part of the body element
682678
// otherwise it is pointless to even issue an animation to be rendered
683-
bodyElementDetected = isMatchingElement(parentElement, bodyElement);
679+
bodyNodeDetected = (parentNode === bodyNode);
684680
}
685681

686-
if (bodyElementDetected && rootElementDetected) {
682+
if (bodyNodeDetected && rootNodeDetected) {
687683
// If both body and root have been found, any other checks are pointless,
688684
// as no animation data should live outside the application
689685
break;
690686
}
691687

692-
if (!rootElementDetected) {
693-
// If no rootElement is detected, check if the parentElement is pinned to another element
694-
parentHost = jqLite.data(parentElement, NG_ANIMATE_PIN_DATA);
688+
if (!rootNodeDetected) {
689+
// If `rootNode` is not detected, check if `parentNode` is pinned to another element
690+
parentHost = jqLite.data(parentNode, NG_ANIMATE_PIN_DATA);
695691
if (parentHost) {
696692
// The pin target element becomes the next parent element
697-
parentElement = getDomNode(parentHost);
693+
parentNode = getDomNode(parentHost);
698694
continue;
699695
}
700696
}
701697

702-
parentElement = parentElement.parentNode;
698+
parentNode = parentNode.parentNode;
703699
}
704700

705701
var allowAnimation = (!parentAnimationDetected || animateChildren) && elementDisabled !== true;
706-
return allowAnimation && rootElementDetected && bodyElementDetected;
702+
return allowAnimation && rootNodeDetected && bodyNodeDetected;
707703
}
708704

709705
function markElementAnimationState(element, state, details) {

0 commit comments

Comments
 (0)