refactor(ProtoViewDto): switch to enum

This commit is contained in:
Victor Berchet 2015-06-09 15:20:33 +02:00
parent af35ab56a3
commit 6ca81fb98c
12 changed files with 38 additions and 38 deletions

View File

@ -161,8 +161,7 @@ export class Compiler {
var protoView = protoViews[0]; var protoView = protoViews[0];
// TODO(tbosch): we should be caching host protoViews as well! // TODO(tbosch): we should be caching host protoViews as well!
// -> need a separate cache for this... // -> need a separate cache for this...
if (renderPv.type === renderApi.ProtoViewDto.COMPONENT_VIEW_TYPE && if (renderPv.type === renderApi.ViewType.COMPONENT && isPresent(componentBinding)) {
isPresent(componentBinding)) {
// Populate the cache before compiling the nested components, // Populate the cache before compiling the nested components,
// so that components can reference themselves in their template. // so that components can reference themselves in their template.
var component = componentBinding.key.token; var component = componentBinding.key.token;

View File

@ -222,10 +222,10 @@ function _getChangeDetectorDefinitions(
bindingRecordsCreator.getDirectiveRecords(elementBinders, allRenderDirectiveMetadata); bindingRecordsCreator.getDirectiveRecords(elementBinders, allRenderDirectiveMetadata);
var strategyName = DEFAULT; var strategyName = DEFAULT;
var typeString; var typeString;
if (pvWithIndex.renderProtoView.type === renderApi.ProtoViewDto.COMPONENT_VIEW_TYPE) { if (pvWithIndex.renderProtoView.type === renderApi.ViewType.COMPONENT) {
strategyName = hostComponentMetadata.changeDetection; strategyName = hostComponentMetadata.changeDetection;
typeString = 'comp'; typeString = 'comp';
} else if (pvWithIndex.renderProtoView.type === renderApi.ProtoViewDto.HOST_VIEW_TYPE) { } else if (pvWithIndex.renderProtoView.type === renderApi.ViewType.HOST) {
typeString = 'host'; typeString = 'host';
} else { } else {
typeString = 'embedded'; typeString = 'embedded';

View File

@ -92,28 +92,29 @@ export class DirectiveBinder {
} }
} }
export class ProtoViewDto { export enum ViewType {
// A view that contains the host element with bound // A view that contains the host element with bound component directive.
// component directive. // Contains a COMPONENT view
// Contains a view of type #COMPONENT_VIEW_TYPE. HOST,
static get HOST_VIEW_TYPE() { return 0; }
// The view of the component // The view of the component
// Can contain 0 to n views of type #EMBEDDED_VIEW_TYPE // Can contain 0 to n EMBEDDED views
static get COMPONENT_VIEW_TYPE() { return 1; } COMPONENT,
// A view that is embedded into another View via a <template> element // A view that is embedded into another View via a <template> element
// inside of a component view // inside of a COMPONENT view
static get EMBEDDED_VIEW_TYPE() { return 2; } EMBEDDED
}
export class ProtoViewDto {
render: RenderProtoViewRef; render: RenderProtoViewRef;
elementBinders: List<ElementBinder>; elementBinders: List<ElementBinder>;
variableBindings: Map<string, string>; variableBindings: Map<string, string>;
type: number; type: ViewType;
constructor({render, elementBinders, variableBindings, type}: { constructor({render, elementBinders, variableBindings, type}: {
render?: RenderProtoViewRef, render?: RenderProtoViewRef,
elementBinders?: List<ElementBinder>, elementBinders?: List<ElementBinder>,
variableBindings?: Map<string, string>, variableBindings?: Map<string, string>,
type?: number type?: ViewType
}) { }) {
this.render = render; this.render = render;
this.elementBinders = elementBinders; this.elementBinders = elementBinders;

View File

@ -5,7 +5,7 @@ import {CompileElement} from './compile_element';
import {CompileControl} from './compile_control'; import {CompileControl} from './compile_control';
import {CompileStep} from './compile_step'; import {CompileStep} from './compile_step';
import {ProtoViewBuilder} from '../view/proto_view_builder'; import {ProtoViewBuilder} from '../view/proto_view_builder';
import {ProtoViewDto} from '../../api'; import {ProtoViewDto, ViewType} from '../../api';
/** /**
* CompilePipeline for executing CompileSteps recursively for * CompilePipeline for executing CompileSteps recursively for
@ -15,10 +15,10 @@ export class CompilePipeline {
_control: CompileControl; _control: CompileControl;
constructor(steps: List<CompileStep>) { this._control = new CompileControl(steps); } constructor(steps: List<CompileStep>) { this._control = new CompileControl(steps); }
process(rootElement, protoViewType: number = null, process(rootElement, protoViewType: ViewType = null,
compilationCtxtDescription: string = ''): List<CompileElement> { compilationCtxtDescription: string = ''): List<CompileElement> {
if (isBlank(protoViewType)) { if (isBlank(protoViewType)) {
protoViewType = ProtoViewDto.COMPONENT_VIEW_TYPE; protoViewType = ViewType.COMPONENT;
} }
var results = ListWrapper.create(); var results = ListWrapper.create();
var rootCompileElement = new CompileElement(rootElement, compilationCtxtDescription); var rootCompileElement = new CompileElement(rootElement, compilationCtxtDescription);

View File

@ -7,6 +7,7 @@ import {DOM} from 'angular2/src/dom/dom_adapter';
import { import {
ViewDefinition, ViewDefinition,
ProtoViewDto, ProtoViewDto,
ViewType,
DirectiveMetadata, DirectiveMetadata,
RenderCompiler, RenderCompiler,
RenderProtoViewRef RenderProtoViewRef
@ -38,8 +39,7 @@ export class DomCompiler extends RenderCompiler {
compile(template: ViewDefinition): Promise<ProtoViewDto> { compile(template: ViewDefinition): Promise<ProtoViewDto> {
var tplPromise = this._templateLoader.load(template); var tplPromise = this._templateLoader.load(template);
return PromiseWrapper.then( return PromiseWrapper.then(
tplPromise, (el) => this._compileTemplate(template, el, ProtoViewDto.COMPONENT_VIEW_TYPE), tplPromise, (el) => this._compileTemplate(template, el, ViewType.COMPONENT), (e) => {
(e) => {
throw new BaseException( throw new BaseException(
`Failed to load the template for "${template.componentId}" : ${e}`); `Failed to load the template for "${template.componentId}" : ${e}`);
}); });
@ -52,11 +52,11 @@ export class DomCompiler extends RenderCompiler {
directives: [directiveMetadata] directives: [directiveMetadata]
}); });
var element = DOM.createElement(directiveMetadata.selector); var element = DOM.createElement(directiveMetadata.selector);
return this._compileTemplate(hostViewDef, element, ProtoViewDto.HOST_VIEW_TYPE); return this._compileTemplate(hostViewDef, element, ViewType.HOST);
} }
_compileTemplate(viewDef: ViewDefinition, tplElement, _compileTemplate(viewDef: ViewDefinition, tplElement,
protoViewType: number): Promise<ProtoViewDto> { protoViewType: ViewType): Promise<ProtoViewDto> {
var subTaskPromises = []; var subTaskPromises = [];
var pipeline = new CompilePipeline(this._stepFactory.createSteps(viewDef, subTaskPromises)); var pipeline = new CompilePipeline(this._stepFactory.createSteps(viewDef, subTaskPromises));
var compileElements = pipeline.process(tplElement, protoViewType, viewDef.componentId); var compileElements = pipeline.process(tplElement, protoViewType, viewDef.componentId);

View File

@ -23,7 +23,7 @@ export class ProtoViewBuilder {
variableBindings: Map<string, string> = MapWrapper.create(); variableBindings: Map<string, string> = MapWrapper.create();
elements: List<ElementBinderBuilder> = []; elements: List<ElementBinderBuilder> = [];
constructor(public rootElement, public type: number) {} constructor(public rootElement, public type: api.ViewType) {}
bindElement(element, description = null): ElementBinderBuilder { bindElement(element, description = null): ElementBinderBuilder {
var builder = new ElementBinderBuilder(this.elements.length, element, description); var builder = new ElementBinderBuilder(this.elements.length, element, description);
@ -187,7 +187,7 @@ export class ElementBinderBuilder {
if (isPresent(this.nestedProtoView)) { if (isPresent(this.nestedProtoView)) {
throw new BaseException('Only one nested view per element is allowed'); throw new BaseException('Only one nested view per element is allowed');
} }
this.nestedProtoView = new ProtoViewBuilder(rootElement, api.ProtoViewDto.EMBEDDED_VIEW_TYPE); this.nestedProtoView = new ProtoViewBuilder(rootElement, api.ViewType.EMBEDDED);
return this.nestedProtoView; return this.nestedProtoView;
} }

View File

@ -101,7 +101,7 @@ class _TemplateExtractor {
new CompilePipeline(_factory.createSteps(viewDef, subtaskPromises)); new CompilePipeline(_factory.createSteps(viewDef, subtaskPromises));
var compileElements = pipeline.process( var compileElements = pipeline.process(
templateEl, ProtoViewDto.COMPONENT_VIEW_TYPE, viewDef.componentId); templateEl, ViewType.COMPONENT, viewDef.componentId);
var protoViewDto = compileElements[0].inheritedProtoView var protoViewDto = compileElements[0].inheritedProtoView
.build(new PropertySetterFactory()); .build(new PropertySetterFactory());

View File

@ -317,7 +317,7 @@ export function main() {
createRenderProtoView([ createRenderProtoView([
createRenderViewportElementBinder( createRenderViewportElementBinder(
createRenderProtoView([createRenderComponentElementBinder(0)], createRenderProtoView([createRenderComponentElementBinder(0)],
renderApi.ProtoViewDto.EMBEDDED_VIEW_TYPE)) renderApi.ViewType.EMBEDDED))
]), ]),
createRenderProtoView() createRenderProtoView()
], ],
@ -379,7 +379,7 @@ export function main() {
renderCompiler.spy('compileHost') renderCompiler.spy('compileHost')
.andCallFake((componentId) => { .andCallFake((componentId) => {
return PromiseWrapper.resolve(createRenderProtoView( return PromiseWrapper.resolve(createRenderProtoView(
[createRenderComponentElementBinder(0)], renderApi.ProtoViewDto.HOST_VIEW_TYPE)); [createRenderComponentElementBinder(0)], renderApi.ViewType.HOST));
}); });
tplResolver.setView(MainComponent, new viewAnn.View({template: '<div></div>'})); tplResolver.setView(MainComponent, new viewAnn.View({template: '<div></div>'}));
var rootProtoView = var rootProtoView =
@ -429,9 +429,9 @@ function createViewportElementBinder(nestedProtoView) {
return elBinder; return elBinder;
} }
function createRenderProtoView(elementBinders = null, type: number = null) { function createRenderProtoView(elementBinders = null, type: renderApi.ViewType = null) {
if (isBlank(type)) { if (isBlank(type)) {
type = renderApi.ProtoViewDto.COMPONENT_VIEW_TYPE; type = renderApi.ViewType.COMPONENT;
} }
if (isBlank(elementBinders)) { if (isBlank(elementBinders)) {
elementBinders = []; elementBinders = [];

View File

@ -146,9 +146,9 @@ function directiveBinding({metadata}: {metadata?: any} = {}) {
return new DirectiveBinding(Key.get("dummy"), null, [], false, [], [], [], metadata); return new DirectiveBinding(Key.get("dummy"), null, [], false, [], [], [], metadata);
} }
function createRenderProtoView(elementBinders = null, type: number = null) { function createRenderProtoView(elementBinders = null, type: renderApi.ViewType = null) {
if (isBlank(type)) { if (isBlank(type)) {
type = renderApi.ProtoViewDto.COMPONENT_VIEW_TYPE; type = renderApi.ViewType.COMPONENT;
} }
if (isBlank(elementBinders)) { if (isBlank(elementBinders)) {
elementBinders = []; elementBinders = [];

View File

@ -17,7 +17,7 @@ import {Type, isBlank, stringify, isPresent} from 'angular2/src/facade/lang';
import {PromiseWrapper, Promise} from 'angular2/src/facade/async'; import {PromiseWrapper, Promise} from 'angular2/src/facade/async';
import {DomCompiler} from 'angular2/src/render/dom/compiler/compiler'; import {DomCompiler} from 'angular2/src/render/dom/compiler/compiler';
import {ProtoViewDto, ViewDefinition, DirectiveMetadata} from 'angular2/src/render/api'; import {ProtoViewDto, ViewDefinition, DirectiveMetadata, ViewType} from 'angular2/src/render/api';
import {CompileElement} from 'angular2/src/render/dom/compiler/compile_element'; import {CompileElement} from 'angular2/src/render/dom/compiler/compile_element';
import {CompileStep} from 'angular2/src/render/dom/compiler/compile_step'; import {CompileStep} from 'angular2/src/render/dom/compiler/compile_step';
import {CompileStepFactory} from 'angular2/src/render/dom/compiler/compile_step_factory'; import {CompileStepFactory} from 'angular2/src/render/dom/compiler/compile_step_factory';
@ -140,7 +140,7 @@ export function runCompilerCommonTests() {
compiler.compile( compiler.compile(
new ViewDefinition({componentId: 'someId', template: 'inline component'})) new ViewDefinition({componentId: 'someId', template: 'inline component'}))
.then((protoView) => { .then((protoView) => {
expect(protoView.type).toEqual(ProtoViewDto.COMPONENT_VIEW_TYPE); expect(protoView.type).toEqual(ViewType.COMPONENT);
async.done(); async.done();
}); });
})); }));
@ -154,7 +154,7 @@ export function runCompilerCommonTests() {
var compiler = createCompiler(EMPTY_STEP); var compiler = createCompiler(EMPTY_STEP);
compiler.compileHost(someComponent) compiler.compileHost(someComponent)
.then((protoView) => { .then((protoView) => {
expect(protoView.type).toEqual(ProtoViewDto.HOST_VIEW_TYPE); expect(protoView.type).toEqual(ViewType.HOST);
async.done(); async.done();
}); });
})); }));

View File

@ -9,7 +9,7 @@ import {CompileStep} from 'angular2/src/render/dom/compiler/compile_step';
import {CompileControl} from 'angular2/src/render/dom/compiler/compile_control'; import {CompileControl} from 'angular2/src/render/dom/compiler/compile_control';
import {ProtoViewBuilder} from 'angular2/src/render/dom/view/proto_view_builder'; import {ProtoViewBuilder} from 'angular2/src/render/dom/view/proto_view_builder';
import {ProtoViewDto} from 'angular2/src/render/api'; import {ProtoViewDto, ViewType} from 'angular2/src/render/api';
export function main() { export function main() {
describe('compile_pipeline', () => { describe('compile_pipeline', () => {
@ -43,7 +43,7 @@ export function main() {
new MockStep((parent, current, control) => { new MockStep((parent, current, control) => {
if (isPresent(DOM.getAttribute(current.element, 'viewroot'))) { if (isPresent(DOM.getAttribute(current.element, 'viewroot'))) {
current.inheritedProtoView = current.inheritedProtoView =
new ProtoViewBuilder(current.element, ProtoViewDto.EMBEDDED_VIEW_TYPE); new ProtoViewBuilder(current.element, ViewType.EMBEDDED);
} }
}) })
]); ]);

View File

@ -12,7 +12,7 @@ import {MapWrapper} from 'angular2/src/facade/collection';
import {ViewSplitter} from 'angular2/src/render/dom/compiler/view_splitter'; import {ViewSplitter} from 'angular2/src/render/dom/compiler/view_splitter';
import {CompilePipeline} from 'angular2/src/render/dom/compiler/compile_pipeline'; import {CompilePipeline} from 'angular2/src/render/dom/compiler/compile_pipeline';
import {ProtoViewDto} from 'angular2/src/render/api'; import {ProtoViewDto, ViewType} from 'angular2/src/render/api';
import {DOM} from 'angular2/src/dom/dom_adapter'; import {DOM} from 'angular2/src/dom/dom_adapter';
import {Lexer, Parser} from 'angular2/change_detection'; import {Lexer, Parser} from 'angular2/change_detection';
@ -68,7 +68,7 @@ export function main() {
expect(results[2].inheritedProtoView).not.toBe(null); expect(results[2].inheritedProtoView).not.toBe(null);
expect(results[2].inheritedProtoView) expect(results[2].inheritedProtoView)
.toBe(results[1].inheritedElementBinder.nestedProtoView); .toBe(results[1].inheritedElementBinder.nestedProtoView);
expect(results[2].inheritedProtoView.type).toBe(ProtoViewDto.EMBEDDED_VIEW_TYPE); expect(results[2].inheritedProtoView.type).toBe(ViewType.EMBEDDED);
expect(stringifyElement(results[2].inheritedProtoView.rootElement)) expect(stringifyElement(results[2].inheritedProtoView.rootElement))
.toEqual('<template>a</template>'); .toEqual('<template>a</template>');
}); });