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';
|
2015-05-11 12:31:16 -07:00
|
|
|
import {Locals} from 'angular2/change_detection';
|
2015-03-23 14:10:55 -07:00
|
|
|
import {int, isPresent, isBlank, BaseException} from 'angular2/src/facade/lang';
|
|
|
|
|
2015-05-06 10:49:42 -07:00
|
|
|
import {DomViewContainer} from './view_container';
|
2015-05-06 11:22:28 -07:00
|
|
|
import {DomProtoView} 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-05-06 10:49:42 -07:00
|
|
|
import {RenderViewRef} from '../../api';
|
|
|
|
|
|
|
|
// TODO(tbosch): enable this again!
|
2015-04-07 20:54:20 -07:00
|
|
|
// import {EventDispatcher} from '../../api';
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-05-06 10:49:42 -07:00
|
|
|
export function resolveInternalDomView(viewRef:RenderViewRef) {
|
|
|
|
var domViewRef:DomViewRef = viewRef;
|
|
|
|
return domViewRef._view;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DomViewRef extends RenderViewRef {
|
|
|
|
_view:DomView;
|
|
|
|
constructor(view:DomView) {
|
|
|
|
super();
|
|
|
|
this._view = view;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-05-06 11:22:28 -07:00
|
|
|
export class DomView {
|
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-05-06 10:49:42 -07:00
|
|
|
viewContainers: List<DomViewContainer>;
|
2015-03-23 14:10:55 -07:00
|
|
|
contentTags: List<Content>;
|
|
|
|
lightDoms: List<LightDom>;
|
2015-04-16 15:38:28 -07:00
|
|
|
hostLightDom: LightDom;
|
2015-05-06 10:49:42 -07:00
|
|
|
shadowRoot;
|
2015-05-06 11:22:28 -07:00
|
|
|
proto: DomProtoView;
|
2015-04-15 21:51:30 -07:00
|
|
|
hydrated: boolean;
|
2015-05-06 10:49:42 -07:00
|
|
|
eventDispatcher: any/*EventDispatcher*/;
|
2015-04-15 21:51:30 -07:00
|
|
|
eventHandlerRemovers: List<Function>;
|
2015-03-23 14:10:55 -07:00
|
|
|
|
|
|
|
constructor(
|
2015-05-06 11:22:28 -07:00
|
|
|
proto:DomProtoView, 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-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 = [];
|
2015-05-06 10:49:42 -07:00
|
|
|
this.eventDispatcher = null;
|
|
|
|
this.shadowRoot = null;
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-05-11 12:31:16 -07:00
|
|
|
callAction(elementIndex:number, actionExpression:string, actionArgs:any) {
|
|
|
|
var binder = this.proto.elementBinders[elementIndex];
|
|
|
|
var hostAction = MapWrapper.get(binder.hostActions, actionExpression);
|
|
|
|
hostAction.eval(this.boundElements[elementIndex], this._localsWithAction(actionArgs));
|
|
|
|
}
|
|
|
|
|
|
|
|
_localsWithAction(action:Object):Locals {
|
|
|
|
var map = MapWrapper.create();
|
|
|
|
MapWrapper.set(map, '$action', action);
|
|
|
|
return new Locals(null, map);
|
|
|
|
}
|
|
|
|
|
2015-03-23 14:10:55 -07:00
|
|
|
setText(textIndex:number, value:string) {
|
|
|
|
DOM.setText(this.boundTextNodes[textIndex], value);
|
|
|
|
}
|
|
|
|
|
2015-04-16 18:03:15 +02:00
|
|
|
dispatchEvent(elementIndex, eventName, event): boolean {
|
|
|
|
var allowDefaultBehavior = true;
|
2015-05-06 10:49:42 -07:00
|
|
|
if (isPresent(this.eventDispatcher)) {
|
2015-03-23 14:10:55 -07:00
|
|
|
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));
|
2015-05-06 10:49:42 -07:00
|
|
|
// this.eventDispatcher.dispatchEvent(elementIndex, eventName, localValues);
|
|
|
|
allowDefaultBehavior = this.eventDispatcher.dispatchEvent(elementIndex, eventName, evalLocals);
|
2015-04-16 18:03:15 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|