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-05-20 17:19:46 -07:00
|
|
|
import {isPresent, isBlank, BaseException} from 'angular2/src/facade/lang';
|
2015-03-23 14:10:55 -07:00
|
|
|
|
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';
|
2015-06-02 10:15:16 -07:00
|
|
|
import {DomElement} from './element';
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
import {RenderViewRef, EventDispatcher} from '../../api';
|
2015-05-06 10:49:42 -07:00
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
export function resolveInternalDomView(viewRef: RenderViewRef) {
|
|
|
|
return (<DomViewRef>viewRef)._view;
|
2015-05-06 10:49:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DomViewRef extends RenderViewRef {
|
2015-05-18 11:57:20 -07:00
|
|
|
_view: DomView;
|
|
|
|
constructor(view: DomView) {
|
2015-05-06 10:49:42 -07:00
|
|
|
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-04-16 15:38:28 -07:00
|
|
|
hostLightDom: LightDom;
|
2015-05-06 10:49:42 -07:00
|
|
|
shadowRoot;
|
2015-04-15 21:51:30 -07:00
|
|
|
hydrated: boolean;
|
2015-05-18 11:57:20 -07:00
|
|
|
eventDispatcher: EventDispatcher;
|
2015-04-15 21:51:30 -07:00
|
|
|
eventHandlerRemovers: List<Function>;
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
constructor(public proto: DomProtoView, public rootNodes: List</*node*/ any>,
|
2015-06-02 10:15:16 -07:00
|
|
|
public boundTextNodes: List</*node*/ any>, public boundElements: List<DomElement>) {
|
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-06-02 10:15:16 -07:00
|
|
|
getDirectParentElement(boundElementIndex: number): DomElement {
|
2015-04-16 15:38:28 -07:00
|
|
|
var binder = this.proto.elementBinders[boundElementIndex];
|
2015-06-02 10:15:16 -07:00
|
|
|
var parent = null;
|
2015-04-16 15:38:28 -07:00
|
|
|
if (binder.parentIndex !== -1 && binder.distanceToParent === 1) {
|
2015-06-02 10:15:16 -07:00
|
|
|
parent = this.boundElements[binder.parentIndex];
|
2015-04-16 15:38:28 -07:00
|
|
|
}
|
2015-06-02 10:15:16 -07:00
|
|
|
return parent;
|
2015-04-16 15:38:28 -07:00
|
|
|
}
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
setElementProperty(elementIndex: number, propertyName: string, value: any) {
|
|
|
|
var setter =
|
|
|
|
MapWrapper.get(this.proto.elementBinders[elementIndex].propertySetters, propertyName);
|
2015-06-02 10:15:16 -07:00
|
|
|
setter(this.boundElements[elementIndex].element, value);
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
callAction(elementIndex: number, actionExpression: string, actionArgs: any) {
|
2015-05-11 12:31:16 -07:00
|
|
|
var binder = this.proto.elementBinders[elementIndex];
|
|
|
|
var hostAction = MapWrapper.get(binder.hostActions, actionExpression);
|
2015-06-02 10:15:16 -07:00
|
|
|
hostAction.eval(this.boundElements[elementIndex].element, this._localsWithAction(actionArgs));
|
2015-05-11 12:31:16 -07:00
|
|
|
}
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
_localsWithAction(action: Object): Locals {
|
2015-05-11 12:31:16 -07:00
|
|
|
var map = MapWrapper.create();
|
|
|
|
MapWrapper.set(map, '$action', action);
|
|
|
|
return new Locals(null, map);
|
|
|
|
}
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
setText(textIndex: number, value: string) { DOM.setText(this.boundTextNodes[textIndex], value); }
|
2015-03-23 14:10:55 -07:00
|
|
|
|
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
|
2015-05-18 11:57:20 -07:00
|
|
|
// 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);
|
2015-05-18 11:57:20 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|