2015-03-23 14:10:55 -07:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
|
|
|
import {ListWrapper, MapWrapper, Map, StringMapWrapper, List} from 'angular2/src/facade/collection';
|
|
|
|
import {int, isPresent, isBlank, BaseException} from 'angular2/src/facade/lang';
|
|
|
|
|
|
|
|
import {ViewContainer} from './view_container';
|
2015-04-09 21:20:11 +02:00
|
|
|
import {RenderProtoView} from './proto_view';
|
2015-03-23 14:10:55 -07:00
|
|
|
import {LightDom} from '../shadow_dom/light_dom';
|
|
|
|
import {Content} from '../shadow_dom/content_tag';
|
|
|
|
|
2015-04-07 20:54:20 -07:00
|
|
|
// import {EventDispatcher} from '../../api';
|
2015-03-23 14:10:55 -07:00
|
|
|
|
|
|
|
const NG_BINDING_CLASS = 'ng-binding';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Const of making objects: http://jsperf.com/instantiate-size-of-object
|
|
|
|
*/
|
2015-04-09 21:20:11 +02:00
|
|
|
export class RenderView {
|
2015-03-23 14:10:55 -07:00
|
|
|
boundElements:List;
|
|
|
|
boundTextNodes:List;
|
|
|
|
/// When the view is part of render tree, the DocumentFragment is empty, which is why we need
|
|
|
|
/// to keep track of the nodes.
|
|
|
|
rootNodes:List;
|
|
|
|
// TODO(tbosch): move componentChildViews, viewContainers, contentTags, lightDoms into
|
|
|
|
// a single array with records inside
|
2015-04-09 21:20:11 +02:00
|
|
|
componentChildViews: List<RenderView>;
|
2015-03-23 14:10:55 -07:00
|
|
|
viewContainers: List<ViewContainer>;
|
|
|
|
contentTags: List<Content>;
|
|
|
|
lightDoms: List<LightDom>;
|
2015-04-16 15:38:28 -07:00
|
|
|
hostLightDom: LightDom;
|
2015-04-09 21:20:11 +02:00
|
|
|
proto: RenderProtoView;
|
2015-04-15 21:51:30 -07:00
|
|
|
hydrated: boolean;
|
2015-04-07 20:54:20 -07:00
|
|
|
_eventDispatcher: any/*EventDispatcher*/;
|
2015-04-15 21:51:30 -07:00
|
|
|
eventHandlerRemovers: List<Function>;
|
2015-04-20 11:34:53 -07:00
|
|
|
/// Host views that were added by an imperative view.
|
|
|
|
/// This is a dynamically growing / shrinking array.
|
|
|
|
imperativeHostViews: List<RenderView>;
|
2015-03-23 14:10:55 -07:00
|
|
|
|
|
|
|
constructor(
|
2015-04-09 21:20:11 +02:00
|
|
|
proto:RenderProtoView, rootNodes:List,
|
2015-04-16 15:38:28 -07:00
|
|
|
boundTextNodes: List, boundElements:List, contentTags:List) {
|
2015-03-23 14:10:55 -07:00
|
|
|
this.proto = proto;
|
|
|
|
this.rootNodes = rootNodes;
|
|
|
|
this.boundTextNodes = boundTextNodes;
|
|
|
|
this.boundElements = boundElements;
|
2015-04-16 15:38:28 -07:00
|
|
|
this.viewContainers = ListWrapper.createFixedSize(boundElements.length);
|
2015-03-23 14:10:55 -07:00
|
|
|
this.contentTags = contentTags;
|
|
|
|
this.lightDoms = ListWrapper.createFixedSize(boundElements.length);
|
2015-04-07 20:54:20 -07:00
|
|
|
ListWrapper.fill(this.lightDoms, null);
|
2015-03-23 14:10:55 -07:00
|
|
|
this.componentChildViews = ListWrapper.createFixedSize(boundElements.length);
|
2015-04-16 15:38:28 -07:00
|
|
|
this.hostLightDom = null;
|
2015-04-15 21:51:30 -07:00
|
|
|
this.hydrated = false;
|
2015-04-20 11:34:53 -07:00
|
|
|
this.eventHandlerRemovers = [];
|
|
|
|
this.imperativeHostViews = [];
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
|
|
|
|
2015-04-16 15:38:28 -07:00
|
|
|
getDirectParentLightDom(boundElementIndex:number) {
|
|
|
|
var binder = this.proto.elementBinders[boundElementIndex];
|
|
|
|
var destLightDom = null;
|
|
|
|
if (binder.parentIndex !== -1 && binder.distanceToParent === 1) {
|
|
|
|
destLightDom = this.lightDoms[binder.parentIndex];
|
|
|
|
}
|
|
|
|
return destLightDom;
|
|
|
|
}
|
|
|
|
|
|
|
|
getOrCreateViewContainer(binderIndex) {
|
|
|
|
var vc = this.viewContainers[binderIndex];
|
|
|
|
if (isBlank(vc)) {
|
|
|
|
vc = new ViewContainer(this, binderIndex);
|
|
|
|
this.viewContainers[binderIndex] = vc;
|
|
|
|
}
|
|
|
|
return vc;
|
|
|
|
}
|
|
|
|
|
2015-03-23 14:10:55 -07:00
|
|
|
setElementProperty(elementIndex:number, propertyName:string, value:any) {
|
2015-04-02 14:40:49 -07:00
|
|
|
var setter = MapWrapper.get(this.proto.elementBinders[elementIndex].propertySetters, propertyName);
|
2015-03-23 14:10:55 -07:00
|
|
|
setter(this.boundElements[elementIndex], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
setText(textIndex:number, value:string) {
|
|
|
|
DOM.setText(this.boundTextNodes[textIndex], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
getViewContainer(index:number):ViewContainer {
|
|
|
|
return this.viewContainers[index];
|
|
|
|
}
|
|
|
|
|
2015-04-07 20:54:20 -07:00
|
|
|
setEventDispatcher(dispatcher:any/*EventDispatcher*/) {
|
2015-03-23 14:10:55 -07:00
|
|
|
this._eventDispatcher = dispatcher;
|
|
|
|
}
|
|
|
|
|
2015-04-16 18:03:15 +02:00
|
|
|
dispatchEvent(elementIndex, eventName, event): boolean {
|
|
|
|
var allowDefaultBehavior = true;
|
2015-03-23 14:10:55 -07:00
|
|
|
if (isPresent(this._eventDispatcher)) {
|
|
|
|
var evalLocals = MapWrapper.create();
|
|
|
|
MapWrapper.set(evalLocals, '$event', event);
|
2015-04-07 20:54:20 -07:00
|
|
|
// TODO(tbosch): reenable this when we are parsing element properties
|
|
|
|
// out of action expressions
|
|
|
|
// var localValues = this.proto.elementBinders[elementIndex].eventLocals.eval(null, new Locals(null, evalLocals));
|
|
|
|
// this._eventDispatcher.dispatchEvent(elementIndex, eventName, localValues);
|
2015-04-16 18:03:15 +02:00
|
|
|
allowDefaultBehavior = this._eventDispatcher.dispatchEvent(elementIndex, eventName, evalLocals);
|
|
|
|
if (!allowDefaultBehavior) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
2015-04-16 18:03:15 +02:00
|
|
|
return allowDefaultBehavior;
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
|
|
|
}
|