Skip to content

Commit

Permalink
refactor(ivy): animations getting whether or not in embedded view fro…
Browse files Browse the repository at this point in the history
…m state
  • Loading branch information
benlesh committed Mar 12, 2019
1 parent 52d19b2 commit f3f6070
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
Expand Up @@ -62,8 +62,9 @@ export class AnimationEngine {
this._transitionEngine.destroy(namespaceId, context);
}

onInsert(namespaceId: string, element: any, parent: any, insertBefore: boolean): void {
this._transitionEngine.insertNode(namespaceId, element, parent, insertBefore);
onInsert(namespaceId: string, element: any, parent: any, shouldCollectEnterElement: boolean):
void {
this._transitionEngine.insertNode(namespaceId, element, parent, shouldCollectEnterElement);
}

onRemove(namespaceId: string, element: any, context: any, isHostElement?: boolean): void {
Expand Down
Expand Up @@ -657,7 +657,8 @@ export class TransitionAnimationEngine {
return false;
}

insertNode(namespaceId: string, element: any, parent: any, insertBefore: boolean): void {
insertNode(namespaceId: string, element: any, parent: any, shouldCollectEnterElement: boolean):
void {
if (!isElementNode(element)) return;

// special case for when an element is removed and reinserted (move operation)
Expand Down Expand Up @@ -688,8 +689,8 @@ export class TransitionAnimationEngine {
}
}

// only *directives and host elements are inserted before
if (insertBefore) {
// For embedded views (*directives)
if (shouldCollectEnterElement) {
this.collectEnterElement(element);
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/render3/state.ts
Expand Up @@ -16,6 +16,24 @@ import {BINDING_INDEX, CONTEXT, DECLARATION_VIEW, FLAGS, InitPhaseState, LView,
import {resetPreOrderHookFlags} from './util/view_utils';


let _inContainer = false;

/**
* Whether or not we're currently updating the contents of a Container (embedded view).
*
* This is used primarily to tell {@link AnimationRenderer} how to handle insertions appropriately.
*/
export function getInContainer() {
return _inContainer;
}
/**
* Set when we enter the process of updating a Container's contained DOM.
*
* This is used primarily to tell {@link AnimationRenderer} how to handle insertions appropriately.
*/
export function setInContainer(value: boolean) {
_inContainer = value;
}

/**
* Store the element depth count. This is used to identify the root elements of the template
Expand Down
65 changes: 36 additions & 29 deletions packages/core/src/render3/view.ts
Expand Up @@ -6,19 +6,18 @@
* found in the LICENSE file at https://angular.io/license
*/

import {assertDomNode, assertEqual, assertNumber} from '../util/assert';
import {assertDomNode, assertEqual} from '../util/assert';

import {assertLContainer, assertLView, assertLViewOrUndefined} from './assert';
import {getLContext} from './context_discovery';
import {assignTViewNodeToLView, createLContainer, createLView, renderEmbeddedTemplate} from './instructions';
import {ACTIVE_INDEX, LContainer, VIEWS} from './interfaces/container';
import {TNode, TNodeType, TProjectionNode} from './interfaces/node';
import {TNode, TNodeType} from './interfaces/node';
import {RComment, RElement, RNode} from './interfaces/renderer';
import {DECLARATION_VIEW, EmbeddedViewFactory, EmbeddedViewFactoryInternal, HOST, LView, LViewFlags, PARENT, QUERIES, RENDERER, TVIEW, View, ViewContainer} from './interfaces/view';
import {addRemoveViewFromContainer, appendChildViewDynamic, appendProjectedNodes, destroyLView, detachView, getRenderParent, insertView, nativeInsertBefore, nativeNextSibling, nativeParentNode, nativeRemoveChild} from './node_manipulation';
import {getIsParent, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from './state';
import {findComponentView} from './util/view_traversal_utils';
import {getLastRootElementFromView, getRNode, isComponent, lContainerToViewContainer, unwrapLContainer, unwrapRNode, viewContainerToLContainer, viewToLView} from './util/view_utils';
import {DECLARATION_VIEW, EmbeddedViewFactory, EmbeddedViewFactoryInternal, HOST, LView, LViewFlags, QUERIES, RENDERER, TVIEW, View, ViewContainer} from './interfaces/view';
import {addRemoveViewFromContainer, appendChildViewDynamic, destroyLView, detachView, insertView, nativeNextSibling} from './node_manipulation';
import {getInContainer, getIsParent, getPreviousOrParentTNode, setInContainer, setIsParent, setPreviousOrParentTNode} from './state';
import {getLastRootElementFromView, lContainerToViewContainer, unwrapLContainer, unwrapRNode, viewContainerToLContainer, viewToLView} from './util/view_utils';



Expand Down Expand Up @@ -124,10 +123,11 @@ export function getEmbeddedViewFactoryInternal<T extends{}>(
return function embeddedViewFactory(context: T) {
const _isParent = getIsParent();
const _previousOrParentTNode = getPreviousOrParentTNode();
const _inContainer = getInContainer();
try {
setIsParent(true);
setPreviousOrParentTNode(null !);

setInContainer(true);
const lView = createLView(
declarationLView, templateTView, context, LViewFlags.CheckAlways, null, null);
lView[DECLARATION_VIEW] = declarationLView;
Expand All @@ -153,6 +153,7 @@ export function getEmbeddedViewFactoryInternal<T extends{}>(

return lView;
} finally {
setInContainer(_inContainer);
setIsParent(_isParent);
setPreviousOrParentTNode(_previousOrParentTNode);
}
Expand Down Expand Up @@ -221,27 +222,33 @@ export function viewContainerInsertAfter(
*/
function viewContainerInsertAfterInternal(
lContainer: LContainer, lView: LView, insertAfterLView: LView | null) {
const containerNode = unwrapRNode(lContainer[HOST]);

// Because we're dynamically adding a view to the container, we reset the ACTIVE_INDEX to ensure
// the container is updated.
lContainer[ACTIVE_INDEX] = -1;

const insertAfterNode =
insertAfterLView ? getLastRootElementFromView(insertAfterLView) : containerNode;
ngDevMode && assertDomNode(insertAfterNode);
const tView = lView[TVIEW];
let tNode = tView.firstChild;

const index =
insertAfterLView ? viewContainerIndexOfInternal(lContainer, insertAfterLView) + 1 : 0;
insertView(lView, lContainer, index);

const renderer = lView[RENDERER];
const beforeNode = nativeNextSibling(renderer, insertAfterNode);
ngDevMode && assertEqual(tNode && tNode.parent, null, 'tNode parent should be null');

addRemoveViewFromContainer(lView, true, beforeNode);
const _inContainer = getInContainer();
try {
setInContainer(true);
const containerNode = unwrapRNode(lContainer[HOST]);

// Because we're dynamically adding a view to the container, we reset the ACTIVE_INDEX to ensure
// the container is updated.
lContainer[ACTIVE_INDEX] = -1;

const insertAfterNode =
insertAfterLView ? getLastRootElementFromView(insertAfterLView) : containerNode;
ngDevMode && assertDomNode(insertAfterNode);
const tView = lView[TVIEW];
let tNode = tView.firstChild;

const index =
insertAfterLView ? viewContainerIndexOfInternal(lContainer, insertAfterLView) + 1 : 0;
insertView(lView, lContainer, index);

const renderer = lView[RENDERER];
const beforeNode = nativeNextSibling(renderer, insertAfterNode);
ngDevMode && assertEqual(tNode && tNode.parent, null, 'tNode parent should be null');

addRemoveViewFromContainer(lView, true, beforeNode);
} finally {
setInContainer(_inContainer);
}
}

/**
Expand Down
Expand Up @@ -8,6 +8,7 @@
import {AnimationTriggerMetadata} from '@angular/animations';
import {ɵAnimationEngine as AnimationEngine} from '@angular/animations/browser';
import {Injectable, NgZone, Renderer2, RendererFactory2, RendererStyleFlags2, RendererType2} from '@angular/core';
import {getInContainer} from '@angular/core/src/render3/state';

const ANIMATION_PREFIX = '@';
const DISABLE_ANIMATIONS_FLAG = '@.disabled';
Expand Down Expand Up @@ -140,12 +141,14 @@ export class BaseAnimationRenderer implements Renderer2 {

appendChild(parent: any, newChild: any): void {
this.delegate.appendChild(parent, newChild);
this.engine.onInsert(this.namespaceId, newChild, parent, false);
const shouldCollectEnterElement = getInContainer();
this.engine.onInsert(this.namespaceId, newChild, parent, shouldCollectEnterElement);
}

insertBefore(parent: any, newChild: any, refChild: any): void {
this.delegate.insertBefore(parent, newChild, refChild);
this.engine.onInsert(this.namespaceId, newChild, parent, true);
const shouldCollectEnterElement = getInContainer();
this.engine.onInsert(this.namespaceId, newChild, parent, shouldCollectEnterElement);
}

removeChild(parent: any, oldChild: any, isHostElement: boolean): void {
Expand Down

0 comments on commit f3f6070

Please sign in to comment.