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];
// TODO(tbosch): we should be caching host protoViews as well!
// -> need a separate cache for this...
if (renderPv.type === renderApi.ProtoViewDto.COMPONENT_VIEW_TYPE &&
isPresent(componentBinding)) {
if (renderPv.type === renderApi.ViewType.COMPONENT && isPresent(componentBinding)) {
// Populate the cache before compiling the nested components,
// so that components can reference themselves in their template.
var component = componentBinding.key.token;

View File

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

View File

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

View File

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

View File

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

View File

@ -23,7 +23,7 @@ export class ProtoViewBuilder {
variableBindings: Map<string, string> = MapWrapper.create();
elements: List<ElementBinderBuilder> = [];
constructor(public rootElement, public type: number) {}
constructor(public rootElement, public type: api.ViewType) {}
bindElement(element, description = null): ElementBinderBuilder {
var builder = new ElementBinderBuilder(this.elements.length, element, description);
@ -187,7 +187,7 @@ export class ElementBinderBuilder {
if (isPresent(this.nestedProtoView)) {
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;
}

View File

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

View File

@ -317,7 +317,7 @@ export function main() {
createRenderProtoView([
createRenderViewportElementBinder(
createRenderProtoView([createRenderComponentElementBinder(0)],
renderApi.ProtoViewDto.EMBEDDED_VIEW_TYPE))
renderApi.ViewType.EMBEDDED))
]),
createRenderProtoView()
],
@ -379,7 +379,7 @@ export function main() {
renderCompiler.spy('compileHost')
.andCallFake((componentId) => {
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>'}));
var rootProtoView =
@ -429,9 +429,9 @@ function createViewportElementBinder(nestedProtoView) {
return elBinder;
}
function createRenderProtoView(elementBinders = null, type: number = null) {
function createRenderProtoView(elementBinders = null, type: renderApi.ViewType = null) {
if (isBlank(type)) {
type = renderApi.ProtoViewDto.COMPONENT_VIEW_TYPE;
type = renderApi.ViewType.COMPONENT;
}
if (isBlank(elementBinders)) {
elementBinders = [];

View File

@ -146,9 +146,9 @@ function directiveBinding({metadata}: {metadata?: any} = {}) {
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)) {
type = renderApi.ProtoViewDto.COMPONENT_VIEW_TYPE;
type = renderApi.ViewType.COMPONENT;
}
if (isBlank(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 {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 {CompileStep} from 'angular2/src/render/dom/compiler/compile_step';
import {CompileStepFactory} from 'angular2/src/render/dom/compiler/compile_step_factory';
@ -140,7 +140,7 @@ export function runCompilerCommonTests() {
compiler.compile(
new ViewDefinition({componentId: 'someId', template: 'inline component'}))
.then((protoView) => {
expect(protoView.type).toEqual(ProtoViewDto.COMPONENT_VIEW_TYPE);
expect(protoView.type).toEqual(ViewType.COMPONENT);
async.done();
});
}));
@ -154,7 +154,7 @@ export function runCompilerCommonTests() {
var compiler = createCompiler(EMPTY_STEP);
compiler.compileHost(someComponent)
.then((protoView) => {
expect(protoView.type).toEqual(ProtoViewDto.HOST_VIEW_TYPE);
expect(protoView.type).toEqual(ViewType.HOST);
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 {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() {
describe('compile_pipeline', () => {
@ -43,7 +43,7 @@ export function main() {
new MockStep((parent, current, control) => {
if (isPresent(DOM.getAttribute(current.element, 'viewroot'))) {
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 {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 {Lexer, Parser} from 'angular2/change_detection';
@ -68,7 +68,7 @@ export function main() {
expect(results[2].inheritedProtoView).not.toBe(null);
expect(results[2].inheritedProtoView)
.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))
.toEqual('<template>a</template>');
});