refactor(compiler): rename AppElement into ViewContainer

This commit is contained in:
Tobias Bosch
2016-11-01 11:12:25 -07:00
committed by vikerman
parent 74ede9aa9b
commit e7c00be19d
18 changed files with 65 additions and 61 deletions

View File

@ -9,7 +9,7 @@
import {ANALYZE_FOR_ENTRY_COMPONENTS, AnimationTransitionEvent, ChangeDetectionStrategy, ChangeDetectorRef, ComponentFactory, ComponentFactoryResolver, ComponentRef, ElementRef, Injector, LOCALE_ID, NgModuleFactory, QueryList, RenderComponentType, Renderer, SecurityContext, SimpleChange, TRANSLATIONS_FORMAT, TemplateRef, ViewContainerRef, ViewEncapsulation} from '@angular/core';
import {CompileIdentifierMetadata, CompileTokenMetadata} from './compile_metadata';
import {AnimationGroupPlayer, AnimationKeyframe, AnimationSequencePlayer, AnimationStyles, AnimationTransition, AppElement, AppView, ChangeDetectorStatus, CodegenComponentFactoryResolver, ComponentRef_, DebugAppView, DebugContext, NgModuleInjector, NoOpAnimationPlayer, StaticNodeDebugInfo, TemplateRef_, UNINITIALIZED, ValueUnwrapper, ViewType, balanceAnimationKeyframes, clearStyles, collectAndResolveStyles, devModeEqual, prepareFinalAnimationStyles, reflector, registerModuleFactory, renderStyles, view_utils} from './private_import_core';
import {AnimationGroupPlayer, AnimationKeyframe, AnimationSequencePlayer, AnimationStyles, AnimationTransition, AppView, ChangeDetectorStatus, CodegenComponentFactoryResolver, ComponentRef_, DebugAppView, DebugContext, NgModuleInjector, NoOpAnimationPlayer, StaticNodeDebugInfo, TemplateRef_, UNINITIALIZED, ValueUnwrapper, ViewContainer, ViewType, balanceAnimationKeyframes, clearStyles, collectAndResolveStyles, devModeEqual, prepareFinalAnimationStyles, reflector, registerModuleFactory, renderStyles, view_utils} from './private_import_core';
var APP_VIEW_MODULE_URL = assetUrl('core', 'linker/view');
var VIEW_UTILS_MODULE_URL = assetUrl('core', 'linker/view_utils');
@ -41,10 +41,10 @@ export class Identifiers {
moduleUrl: APP_VIEW_MODULE_URL,
runtime: DebugAppView
};
static AppElement: IdentifierSpec = {
name: 'AppElement',
moduleUrl: assetUrl('core', 'linker/element'),
runtime: AppElement
static ViewContainer: IdentifierSpec = {
name: 'ViewContainer',
moduleUrl: assetUrl('core', 'linker/view_container'),
runtime: ViewContainer
};
static ElementRef: IdentifierSpec = {
name: 'ElementRef',

View File

@ -17,8 +17,8 @@ export const LifecycleHooks: typeof r.LifecycleHooks = r.LifecycleHooks;
export const LIFECYCLE_HOOKS_VALUES: typeof r.LIFECYCLE_HOOKS_VALUES = r.LIFECYCLE_HOOKS_VALUES;
export type ReflectorReader = typeof r._ReflectorReader;
export const ReflectorReader: typeof r.ReflectorReader = r.ReflectorReader;
export type AppElement = typeof r._AppElement;
export const AppElement: typeof r.AppElement = r.AppElement;
export type ViewContainer = typeof r._ViewContainer;
export const ViewContainer: typeof r.ViewContainer = r.ViewContainer;
export const CodegenComponentFactoryResolver: typeof r.CodegenComponentFactoryResolver =
r.CodegenComponentFactoryResolver;
export const ComponentRef_: typeof r.ComponentRef_ = r.ComponentRef_;

View File

@ -40,7 +40,7 @@ export class CompileElement extends CompileNode {
}
public compViewExpr: o.Expression = null;
public appElement: o.ReadPropExpr;
public viewContainer: o.ReadPropExpr;
public elementRef: o.Expression;
public injector: o.Expression;
public instances = new Map<any, o.Expression>();
@ -74,30 +74,31 @@ export class CompileElement extends CompileNode {
this.instances.set(
resolveIdentifierToken(Identifiers.Renderer).reference, o.THIS_EXPR.prop('renderer'));
if (this.hasViewContainer) {
this._createAppElement();
this._createViewContainer();
}
if (this.component) {
this._createComponentFactoryResolver();
}
}
private _createAppElement() {
var fieldName = `_appEl_${this.nodeIndex}`;
private _createViewContainer() {
var fieldName = `_vc_${this.nodeIndex}`;
var parentNodeIndex = this.isRootElement() ? null : this.parent.nodeIndex;
// private is fine here as no child view will reference an AppElement
// private is fine here as no child view will reference a ViewContainer
this.view.fields.push(new o.ClassField(
fieldName, o.importType(resolveIdentifier(Identifiers.AppElement)),
fieldName, o.importType(resolveIdentifier(Identifiers.ViewContainer)),
[o.StmtModifier.Private]));
var statement =
o.THIS_EXPR.prop(fieldName)
.set(o.importExpr(resolveIdentifier(Identifiers.AppElement)).instantiate([
.set(o.importExpr(resolveIdentifier(Identifiers.ViewContainer)).instantiate([
o.literal(this.nodeIndex), o.literal(parentNodeIndex), o.THIS_EXPR, this.renderNode
]))
.toStmt();
this.view.createMethod.addStmt(statement);
this.appElement = o.THIS_EXPR.prop(fieldName);
this.instances.set(resolveIdentifierToken(Identifiers.AppElement).reference, this.appElement);
this.view.appElements.push(this.appElement);
this.viewContainer = o.THIS_EXPR.prop(fieldName);
this.instances.set(
resolveIdentifierToken(Identifiers.ViewContainer).reference, this.viewContainer);
this.view.viewContainers.push(this.viewContainer);
}
private _createComponentFactoryResolver() {
@ -159,7 +160,7 @@ export class CompileElement extends CompileNode {
if (this.hasViewContainer) {
this.instances.set(
resolveIdentifierToken(Identifiers.ViewContainerRef).reference,
this.appElement.prop('vcRef'));
this.viewContainer.prop('vcRef'));
}
this._resolvedProviders = new Map<any, ProviderAst>();

View File

@ -91,7 +91,7 @@ function createQueryValues(viewValues: ViewQueryValues): o.Expression[] {
return ListWrapper.flatten(viewValues.values.map((entry) => {
if (entry instanceof ViewQueryValues) {
return mapNestedViews(
entry.view.declarationElement.appElement, entry.view, createQueryValues(entry));
entry.view.declarationElement.viewContainer, entry.view, createQueryValues(entry));
} else {
return <o.Expression>entry;
}
@ -99,11 +99,10 @@ function createQueryValues(viewValues: ViewQueryValues): o.Expression[] {
}
function mapNestedViews(
declarationAppElement: o.Expression, view: CompileView,
expressions: o.Expression[]): o.Expression {
viewContainer: o.Expression, view: CompileView, expressions: o.Expression[]): o.Expression {
var adjustedExpressions: o.Expression[] = expressions.map(
(expr) => o.replaceVarInExpression(o.THIS_EXPR.name, o.variable('nestedView'), expr));
return declarationAppElement.callMethod('mapNestedViews', [
return viewContainer.callMethod('mapNestedViews', [
o.variable(view.className),
o.fn(
[new o.FnParam('nestedView', view.classType)],

View File

@ -46,7 +46,7 @@ export class CompileView implements NameResolver {
public rootNodes: CompileViewRootNode[] = [];
public lastRenderNode: o.Expression = o.NULL_EXPR;
public appElements: o.Expression[] = [];
public viewContainers: o.Expression[] = [];
public createMethod: CompileMethod;
public animationBindingsMethod: CompileMethod;

View File

@ -72,21 +72,20 @@ class ViewBuilderVisitor implements TemplateAstVisitor {
var projectedNode = _getOuterContainerOrSelf(node);
var parent = projectedNode.parent;
var ngContentIndex = (<any>projectedNode.sourceAst).ngContentIndex;
var vcAppEl =
(node instanceof CompileElement && node.hasViewContainer) ? node.appElement : null;
var viewContainer =
(node instanceof CompileElement && node.hasViewContainer) ? node.viewContainer : null;
if (this._isRootNode(parent)) {
// store appElement as root node only for ViewContainers
if (this.view.viewType !== ViewType.COMPONENT) {
this.view.rootNodes.push(new CompileViewRootNode(
vcAppEl ? CompileViewRootNodeType.ViewContainer : CompileViewRootNodeType.Node,
vcAppEl || node.renderNode));
viewContainer ? CompileViewRootNodeType.ViewContainer : CompileViewRootNodeType.Node,
viewContainer || node.renderNode));
}
} else if (isPresent(parent.component) && isPresent(ngContentIndex)) {
parent.addContentNode(
ngContentIndex,
new CompileViewRootNode(
vcAppEl ? CompileViewRootNodeType.ViewContainer : CompileViewRootNodeType.Node,
vcAppEl || node.renderNode));
viewContainer ? CompileViewRootNodeType.ViewContainer : CompileViewRootNodeType.Node,
viewContainer || node.renderNode));
}
}
@ -494,8 +493,9 @@ function createViewClass(
function generateDestroyMethod(view: CompileView): o.Statement[] {
const stmts: o.Statement[] = [];
view.appElements.forEach(
(appElement) => { stmts.push(appElement.callMethod('destroyNestedViews', []).toStmt()); });
view.viewContainers.forEach((viewContainer) => {
stmts.push(viewContainer.callMethod('destroyNestedViews', []).toStmt());
});
view.viewChildren.forEach(
(viewChild) => { stmts.push(viewChild.callMethod('destroy', []).toStmt()); });
stmts.push(...view.destroyMethod.finish());
@ -600,9 +600,9 @@ function generateDetectChangesMethod(view: CompileView): o.Statement[] {
}
stmts.push(...view.animationBindingsMethod.finish());
stmts.push(...view.detectChangesInInputsMethod.finish());
view.appElements.forEach((appElement) => {
view.viewContainers.forEach((viewContainer) => {
stmts.push(
appElement.callMethod('detectChangesInNestedViews', [DetectChangesVars.throwOnChange])
viewContainer.callMethod('detectChangesInNestedViews', [DetectChangesVars.throwOnChange])
.toStmt());
});
var afterContentStmts = view.updateContentQueriesMethod.finish().concat(