chore(rename): rename View and Template concepts for #1244

This commit is contained in:
Pawel Kozlowski
2015-04-09 21:20:11 +02:00
committed by Jeremy Elbourn
parent 564477b8a0
commit bf7933714a
103 changed files with 767 additions and 765 deletions

View File

@ -5,10 +5,10 @@ import {List, ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection
import {DirectiveMetadataReader} from './directive_metadata_reader';
import {Component, Viewport, DynamicComponent, Decorator} from '../annotations/annotations';
import {ProtoView} from './view';
import {AppProtoView} from './view';
import {DirectiveBinding} from './element_injector';
import {TemplateResolver} from './template_resolver';
import {Template} from '../annotations/template';
import {View} from '../annotations/view';
import {ComponentUrlMapper} from './component_url_mapper';
import {ProtoViewFactory} from './proto_view_factory';
import {UrlResolver} from 'angular2/src/services/url_resolver';
@ -16,7 +16,7 @@ import {UrlResolver} from 'angular2/src/services/url_resolver';
import * as renderApi from 'angular2/src/render/api';
/**
* Cache that stores the ProtoView of the template of a component.
* Cache that stores the AppProtoView of the template of a component.
* Used to prevent duplicate work and resolve cyclic dependencies.
*/
@Injectable()
@ -26,11 +26,11 @@ export class CompilerCache {
this._cache = MapWrapper.create();
}
set(component:Type, protoView:ProtoView) {
set(component:Type, protoView:AppProtoView) {
MapWrapper.set(this._cache, component, protoView);
}
get(component:Type):ProtoView {
get(component:Type):AppProtoView {
var result = MapWrapper.get(this._cache, component);
return normalizeBlank(result);
}
@ -80,24 +80,24 @@ export class Compiler {
// Create a rootView as if the compiler encountered <rootcmp></rootcmp>.
// Used for bootstrapping.
compileRoot(elementOrSelector, componentTypeOrBinding:any):Promise<ProtoView> {
compileRoot(elementOrSelector, componentTypeOrBinding:any):Promise<AppProtoView> {
return this._renderer.createRootProtoView(elementOrSelector, 'root').then( (rootRenderPv) => {
return this._compileNestedProtoViews(null, rootRenderPv, [this._bindDirective(componentTypeOrBinding)], true);
});
}
compile(component: Type):Promise<ProtoView> {
compile(component: Type):Promise<AppProtoView> {
var protoView = this._compile(this._bindDirective(component));
return PromiseWrapper.isPromise(protoView) ? protoView : PromiseWrapper.resolve(protoView);
}
// TODO(vicb): union type return ProtoView or Promise<ProtoView>
// TODO(vicb): union type return AppProtoView or Promise<AppProtoView>
_compile(componentBinding: DirectiveBinding) {
var component = componentBinding.key.token;
var protoView = this._compilerCache.get(component);
if (isPresent(protoView)) {
// The component has already been compiled into a ProtoView,
// returns a plain ProtoView, not wrapped inside of a Promise.
// The component has already been compiled into an AppProtoView,
// returns a plain AppProtoView, not wrapped inside of a Promise.
// Needed for recursive components.
return protoView;
}
@ -124,7 +124,7 @@ export class Compiler {
return pvPromise;
}
// TODO(tbosch): union type return ProtoView or Promise<ProtoView>
// TODO(tbosch): union type return AppProtoView or Promise<AppProtoView>
_compileNestedProtoViews(componentBinding, renderPv, directives, isComponentRootView) {
var nestedPVPromises = [];
var protoView = this._protoViewFactory.createProtoView(componentBinding, renderPv, directives);
@ -143,7 +143,7 @@ export class Compiler {
var elementBinderDone = (nestedPv) => {
elementBinder.nestedProtoView = nestedPv;
// Can't set the parentProtoView for components,
// as their ProtoView might be used in multiple other components.
// as their AppProtoView might be used in multiple other components.
nestedPv.parentProtoView = isPresent(nestedComponent) ? null : protoView;
};
var nestedCall = null;
@ -180,23 +180,23 @@ export class Compiler {
}
}
_buildRenderTemplate(component, template, directives) {
_buildRenderTemplate(component, view, directives) {
var componentUrl = this._urlResolver.resolve(
this._appUrl, this._componentUrlMapper.getUrl(component)
);
var templateAbsUrl = null;
if (isPresent(template.url)) {
templateAbsUrl = this._urlResolver.resolve(componentUrl, template.url);
if (isPresent(view.templateUrl)) {
templateAbsUrl = this._urlResolver.resolve(componentUrl, view.templateUrl);
} else {
// Note: If we have an inline template, we also need to send
// the url for the component to the renderer so that it
// is able to resolve urls in stylesheets.
templateAbsUrl = componentUrl;
}
return new renderApi.Template({
return new renderApi.ViewDefinition({
componentId: stringify(component),
absUrl: templateAbsUrl,
inline: template.inline,
template: view.template,
directives: ListWrapper.map(directives, this._buildRenderDirective)
});
}
@ -228,14 +228,14 @@ export class Compiler {
type: renderType,
selector: ann.selector,
compileChildren: compileChildren,
events: isPresent(ann.events) ? MapWrapper.createFromStringMap(ann.events) : null,
bind: isPresent(ann.bind) ? MapWrapper.createFromStringMap(ann.bind) : null,
hostListeners: isPresent(ann.hostListeners) ? MapWrapper.createFromStringMap(ann.hostListeners) : null,
properties: isPresent(ann.properties) ? MapWrapper.createFromStringMap(ann.properties) : null,
setters: setters,
readAttributes: readAttributes
});
}
_flattenDirectives(template: Template):List<Type> {
_flattenDirectives(template: View):List<Type> {
if (isBlank(template.directives)) return [];
var directives = [];

View File

@ -36,7 +36,7 @@ export class DynamicComponentLoader {
var annotation = this._directiveMetadataReader.read(type).annotation;
var inj = this._componentAppInjector(location, injector, annotation.services);
var inj = this._componentAppInjector(location, injector, annotation.injectables);
var hostEi = location.elementInjector;
var hostView = location.hostView;
@ -96,4 +96,4 @@ export class DynamicComponentLoader {
throw new BaseException(`Could not load '${stringify(type)}' because it is not a component.`);
}
}
}
}

View File

@ -8,8 +8,8 @@ export class ElementBinder {
protoElementInjector:eiModule.ProtoElementInjector;
componentDirective:DirectiveBinding;
viewportDirective:DirectiveBinding;
nestedProtoView: viewModule.ProtoView;
events:StringMap;
nestedProtoView: viewModule.AppProtoView;
hostListeners:StringMap;
parent:ElementBinder;
index:int;
distanceToParent:int;
@ -28,7 +28,7 @@ export class ElementBinder {
this.index = index;
this.distanceToParent = distanceToParent;
// updated later when events are bound
this.events = null;
this.hostListeners = null;
// updated later, so we are able to resolve cycles
this.nestedProtoView = null;
}

View File

@ -49,9 +49,9 @@ export class DirectiveRef extends ElementRef {
}
export class ComponentRef extends DirectiveRef {
componentView:viewModule.View;
componentView:viewModule.AppView;
constructor(key:Key, elementInjector:ElementInjector, componentView:viewModule.View){
constructor(key:Key, elementInjector:ElementInjector, componentView:viewModule.AppView){
super(key, elementInjector);
this.componentView = componentView;
}
@ -65,8 +65,8 @@ class StaticKeys {
directiveRefId:number;
constructor() {
//TODO: vsavkin Key.annotate(Key.get(View), 'static')
this.viewId = Key.get(viewModule.View).id;
//TODO: vsavkin Key.annotate(Key.get(AppView), 'static')
this.viewId = Key.get(viewModule.AppView).id;
this.ngElementId = Key.get(NgElement).id;
this.viewContainerId = Key.get(ViewContainer).id;
this.bindingPropagationConfigId = Key.get(BindingPropagationConfig).id;
@ -267,7 +267,7 @@ export class DirectiveDependency extends Dependency {
var p = ListWrapper.find(properties, (p) => p instanceof Attribute);
return isPresent(p) ? p.attributeName : null;
}
static _query(properties) {
var p = ListWrapper.find(properties, (p) => p instanceof Query);
return isPresent(p) ? p.directive : null;
@ -305,7 +305,7 @@ export class DirectiveBinding extends Binding {
// TODO(rado): benchmark and consider rolling in as ElementInjector fields.
export class PreBuiltObjects {
view:viewModule.View;
view:viewModule.AppView;
element:NgElement;
viewContainer:ViewContainer;
bindingPropagationConfig:BindingPropagationConfig;
@ -362,7 +362,7 @@ export class ProtoElementInjector {
_keyId9:int;
parent:ProtoElementInjector;
index:int;
view:viewModule.View;
view:viewModule.AppView;
distanceToParent:number;
attributes:Map;
@ -648,7 +648,7 @@ export class ElementInjector extends TreeNode {
}
_isDynamicallyLoadedComponentKey(key:Key) {
return isPresent(this._dynamicallyCreatedComponentBinding) && key.id ===
return isPresent(this._dynamicallyCreatedComponentBinding) && key.id ===
this._dynamicallyCreatedComponentBinding.key.id;
}
@ -910,7 +910,7 @@ export class ElementInjector extends TreeNode {
_getPreBuiltObjectByKeyId(keyId:int) {
var staticKeys = StaticKeys.instance();
// TODO: View should not be injectable. Remove it.
// TODO: AppView should not be injectable. Remove it.
if (keyId === staticKeys.viewId) return this._preBuiltObjects.view;
if (keyId === staticKeys.ngElementId) return this._preBuiltObjects.element;
if (keyId === staticKeys.viewContainerId) return this._preBuiltObjects.viewContainer;

View File

@ -12,7 +12,7 @@ import {DirectDomViewRef} from 'angular2/src/render/dom/direct_dom_renderer';
* @publicModule angular2/angular2
*/
export class NgElement {
_view:viewModule.View;
_view:viewModule.AppView;
_boundElementIndex:number;
constructor(view, boundElementIndex) {
@ -31,4 +31,4 @@ export class NgElement {
getAttribute(name:string) {
return normalizeBlank(DOM.getAttribute(this.domElement, name));
}
}
}

View File

@ -7,7 +7,7 @@ import {ChangeDetection} from 'angular2/change_detection';
import {Component, Viewport, DynamicComponent} from '../annotations/annotations';
import * as renderApi from 'angular2/src/render/api';
import {ProtoView} from './view';
import {AppProtoView} from './view';
import {ProtoElementInjector, DirectiveBinding} from './element_injector';
@Injectable()
@ -20,7 +20,7 @@ export class ProtoViewFactory {
this._renderer = renderer;
}
createProtoView(componentBinding:DirectiveBinding, renderProtoView: renderApi.ProtoView, directives:List<DirectiveBinding>):ProtoView {
createProtoView(componentBinding:DirectiveBinding, renderProtoView: renderApi.ProtoViewDto, directives:List<DirectiveBinding>):AppProtoView {
var protoChangeDetector;
if (isBlank(componentBinding)) {
protoChangeDetector = this._changeDetection.createProtoChangeDetector('root', null);
@ -30,7 +30,7 @@ export class ProtoViewFactory {
'dummy', componentAnnotation.changeDetection
);
}
var protoView = new ProtoView(this._renderer, renderProtoView.render, protoChangeDetector);
var protoView = new AppProtoView(this._renderer, renderProtoView.render, protoChangeDetector);
for (var i=0; i<renderProtoView.elementBinders.length; i++) {
var renderElementBinder = renderProtoView.elementBinders[i];

View File

@ -1,5 +1,5 @@
import {Injectable} from 'angular2/di';
import {Template} from 'angular2/src/core/annotations/template';
import {View} from 'angular2/src/core/annotations/view';
import {Type, stringify, isBlank, BaseException} from 'angular2/src/facade/lang';
import {Map, MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
@ -15,22 +15,22 @@ export class TemplateResolver {
this._cache = MapWrapper.create();
}
resolve(component: Type): Template {
var template = MapWrapper.get(this._cache, component);
resolve(component: Type): View {
var view = MapWrapper.get(this._cache, component);
if (isBlank(template)) {
template = this._resolve(component);
MapWrapper.set(this._cache, component, template);
if (isBlank(view)) {
view = this._resolve(component);
MapWrapper.set(this._cache, component, view);
}
return template;
return view;
}
_resolve(component: Type) {
var annotations = reflector.annotations(component);
for (var i = 0; i < annotations.length; i++) {
var annotation = annotations[i];
if (annotation instanceof Template) {
if (annotation instanceof View) {
return annotation;
}
}

View File

@ -18,21 +18,22 @@ import * as renderApi from 'angular2/src/render/api';
@IMPLEMENTS(ChangeDispatcher)
// TODO(tbosch): this is not supported in dart2js (no '.' is allowed)
// @IMPLEMENTS(renderApi.EventDispatcher)
export class View {
export class AppView {
render:renderApi.ViewRef;
/// This list matches the _nodes list. It is sparse, since only Elements have ElementInjector
rootElementInjectors:List<ElementInjector>;
elementInjectors:List<ElementInjector>;
changeDetector:ChangeDetector;
componentChildViews: List<View>;
componentChildViews: List<AppView>;
viewContainers: List<ViewContainer>;
preBuiltObjects: List<PreBuiltObjects>;
proto: ProtoView;
proto: AppProtoView;
/**
* The context against which data-binding expressions in this view are evaluated against.
* This is always a component instance.
*/
context: any;
/**
@ -42,7 +43,7 @@ export class View {
*/
locals:Locals;
constructor(proto:ProtoView, protoLocals:Map) {
constructor(proto:AppProtoView, protoLocals:Map) {
this.render = null;
this.proto = proto;
this.changeDetector = null;
@ -150,9 +151,9 @@ export class View {
// shadowDomAppInjector
if (isPresent(componentDirective)) {
var services = componentDirective.annotation.services;
if (isPresent(services))
shadowDomAppInjector = appInjector.createChild(services);
var injectables = componentDirective.annotation.injectables;
if (isPresent(injectables))
shadowDomAppInjector = appInjector.createChild(injectables);
else {
shadowDomAppInjector = appInjector;
}
@ -249,13 +250,13 @@ export class View {
this.proto.renderer.setText(this.render, b.elementIndex, currentValue);
}
}
directive(directive:DirectiveRecord) {
var elementInjector:ElementInjector = this.elementInjectors[directive.elementIndex];
return elementInjector.getDirectiveAtIndex(directive.directiveIndex);
}
addComponentChildView(view:View) {
addComponentChildView(view:AppView) {
ListWrapper.push(this.componentChildViews, view);
this.changeDetector.addShadowDomChild(view.changeDetector);
}
@ -269,8 +270,8 @@ export class View {
// queuing up and firing.
if (this.hydrated()) {
var elBinder = this.proto.elementBinders[elementIndex];
if (isBlank(elBinder.events)) return;
var eventMap = elBinder.events[eventName];
if (isBlank(elBinder.hostListeners)) return;
var eventMap = elBinder.hostListeners[eventName];
if (isBlank(eventName)) return;
MapWrapper.forEach(eventMap, (expr, directiveIndex) => {
var context;
@ -289,14 +290,14 @@ export class View {
*
* @publicModule angular2/template
*/
export class ProtoView {
export class AppProtoView {
elementBinders:List<ElementBinder>;
protoChangeDetector:ProtoChangeDetector;
variableBindings: Map;
protoLocals:Map;
textNodesWithBindingCount:int;
bindings:List;
parentProtoView:ProtoView;
parentProtoView:AppProtoView;
_variableBindings:List;
_directiveRecordsMap:Map;
@ -323,8 +324,8 @@ export class ProtoView {
}
//TODO: Tobias or Victor. Moving it into the constructor.
// this work should be done the constructor of ProtoView once we separate
// ProtoView and ProtoViewBuilder
// this work should be done the constructor of AppProtoView once we separate
// AppProtoView and ProtoViewBuilder
getVariableBindings() {
if (isPresent(this._variableBindings)) {
return this._variableBindings;
@ -342,7 +343,7 @@ export class ProtoView {
//TODO: Tobias or Victor. Moving it into the constructor.
// this work should be done the constructor of ProtoView once we separate
// ProtoView and ProtoViewBuilder
// AppProtoView and ProtoViewBuilder
getdirectiveRecords() {
if (isPresent(this._directiveRecords)) {
return this._directiveRecords;
@ -408,10 +409,10 @@ export class ProtoView {
*/
bindEvent(eventName:string, expression:AST, directiveIndex: int = -1) {
var elBinder = this.elementBinders[this.elementBinders.length - 1];
var events = elBinder.events;
var events = elBinder.hostListeners;
if (isBlank(events)) {
events = StringMapWrapper.create();
elBinder.events = events;
elBinder.hostListeners = events;
}
var event = StringMapWrapper.get(events, eventName);
if (isBlank(event)) {
@ -449,4 +450,4 @@ export class ProtoView {
return MapWrapper.get(this._directiveRecordsMap, id);
}
}
}

View File

@ -14,16 +14,16 @@ import * as vfModule from './view_factory';
export class ViewContainer {
render:renderApi.ViewContainerRef;
viewFactory: vfModule.ViewFactory;
parentView: viewModule.View;
defaultProtoView: viewModule.ProtoView;
_views: List<viewModule.View>;
parentView: viewModule.AppView;
defaultProtoView: viewModule.AppProtoView;
_views: List<viewModule.AppView>;
elementInjector: eiModule.ElementInjector;
appInjector: Injector;
hostElementInjector: eiModule.ElementInjector;
constructor(viewFactory:vfModule.ViewFactory,
parentView: viewModule.View,
defaultProtoView: viewModule.ProtoView,
parentView: viewModule.AppView,
defaultProtoView: viewModule.AppProtoView,
elementInjector: eiModule.ElementInjector) {
this.viewFactory = viewFactory;
this.render = null;
@ -67,7 +67,7 @@ export class ViewContainer {
}
}
get(index: number): viewModule.View {
get(index: number): viewModule.AppView {
return this._views[index];
}
@ -86,7 +86,7 @@ export class ViewContainer {
// TODO(rado): profile and decide whether bounds checks should be added
// to the methods below.
create(atIndex=-1): viewModule.View {
create(atIndex=-1): viewModule.AppView {
if (!this.hydrated()) throw new BaseException(
'Cannot create views on a dehydrated ViewContainer');
var newView = this.viewFactory.getView(this.defaultProtoView);
@ -101,13 +101,13 @@ export class ViewContainer {
return newView;
}
insert(view, atIndex=-1): viewModule.View {
insert(view, atIndex=-1): viewModule.AppView {
this._insertWithoutRender(view, atIndex);
this.defaultProtoView.renderer.insertViewIntoContainer(this.render, view.render, atIndex);
return view;
}
_insertWithoutRender(view, atIndex=-1): viewModule.View {
_insertWithoutRender(view, atIndex=-1): viewModule.AppView {
if (atIndex == -1) atIndex = this._views.length;
ListWrapper.insert(this._views, atIndex, view);
this.parentView.changeDetector.addChild(view.changeDetector);
@ -128,7 +128,7 @@ export class ViewContainer {
* The method can be used together with insert to implement a view move, i.e.
* moving the dom nodes while the directives in the view stay intact.
*/
detach(atIndex=-1): viewModule.View {
detach(atIndex=-1): viewModule.AppView {
if (atIndex == -1) atIndex = this._views.length - 1;
var detachedView = this.get(atIndex);
ListWrapper.removeAt(this._views, atIndex);

View File

@ -13,14 +13,14 @@ export const VIEW_POOL_CAPACITY = 'ViewFactory.viewPoolCapacity';
@Injectable()
export class ViewFactory {
_poolCapacity:number;
_pooledViews:List<viewModule.View>;
_pooledViews:List<viewModule.AppView>;
constructor(@Inject(VIEW_POOL_CAPACITY) capacity) {
this._poolCapacity = capacity;
this._pooledViews = ListWrapper.create();
}
getView(protoView:viewModule.ProtoView):viewModule.View {
getView(protoView:viewModule.AppProtoView):viewModule.AppView {
// TODO(tbosch): benchmark this scanning of views and maybe
// replace it with a fancy LRU Map/List combination...
var view;
@ -36,7 +36,7 @@ export class ViewFactory {
return view;
}
returnView(view:viewModule.View) {
returnView(view:viewModule.AppView) {
if (view.hydrated()) {
throw new BaseException('Only dehydrated Views can be put back into the pool!');
}
@ -46,8 +46,8 @@ export class ViewFactory {
}
}
_createView(protoView:viewModule.ProtoView): viewModule.View {
var view = new viewModule.View(protoView, protoView.protoLocals);
_createView(protoView:viewModule.AppProtoView): viewModule.AppView {
var view = new viewModule.AppView(protoView, protoView.protoLocals);
var changeDetector = protoView.protoChangeDetector.instantiate(view, protoView.bindings,
protoView.getVariableBindings(), protoView.getdirectiveRecords());
@ -106,4 +106,4 @@ export class ViewFactory {
return view;
}
}
}