240 lines
8.9 KiB
TypeScript
Raw Normal View History

import {ListWrapper, MapWrapper, Map, StringMapWrapper, List} from 'angular2/src/facade/collection';
2015-05-20 09:48:15 -07:00
import {
AST,
Locals,
ChangeDispatcher,
ProtoChangeDetector,
ChangeDetector,
BindingRecord,
DirectiveRecord,
DirectiveIndex,
ChangeDetectorRef
} from 'angular2/change_detection';
import {
ProtoElementInjector,
ElementInjector,
PreBuiltObjects,
DirectiveBinding
} from './element_injector';
import {ElementBinder} from './element_binder';
import {isPresent, isBlank, BaseException} from 'angular2/src/facade/lang';
import * as renderApi from 'angular2/src/render/api';
2015-05-18 11:57:20 -07:00
import {EventDispatcher} from 'angular2/src/render/api';
import {ViewRef} from './view_ref';
import {ElementRef} from './element_ref';
export class AppViewContainer {
// The order in this list matches the DOM order.
views: List<AppView> = [];
}
2014-09-28 16:29:11 -07:00
/**
2014-10-10 20:44:55 -07:00
* Const of making objects: http://jsperf.com/instantiate-size-of-object
*
2014-10-10 20:44:55 -07:00
*/
2015-05-20 09:48:15 -07:00
export class AppView implements ChangeDispatcher, EventDispatcher {
2015-06-12 22:38:44 +02:00
render: renderApi.RenderViewRef = null;
2014-09-28 16:29:11 -07:00
/// This list matches the _nodes list. It is sparse, since only Elements have ElementInjector
2015-05-20 09:48:15 -07:00
rootElementInjectors: List<ElementInjector>;
2015-06-12 22:38:44 +02:00
elementInjectors: List<ElementInjector> = null;
changeDetector: ChangeDetector = null;
componentChildViews: List<AppView> = null;
viewContainers: List<AppViewContainer>;
2015-06-12 22:38:44 +02:00
preBuiltObjects: List<PreBuiltObjects> = null;
elementRefs: List<ElementRef>;
ref: ViewRef;
/**
* The context against which data-binding expressions in this view are evaluated against.
* This is always a component instance.
*/
2015-06-12 22:38:44 +02:00
context: any = null;
/**
* Variables, local to this view, that can be used in binding expressions (in addition to the
* context). This is used for thing like `<video #player>` or
* `<li template="for #item of items">`, where "player" and "item" are locals, respectively.
*/
2015-05-20 09:48:15 -07:00
locals: Locals;
2015-05-20 09:48:15 -07:00
constructor(public renderer: renderApi.Renderer, public proto: AppProtoView,
protoLocals: Map<string, any>) {
this.viewContainers = ListWrapper.createFixedSize(this.proto.elementBinders.length);
this.elementRefs = ListWrapper.createFixedSize(this.proto.elementBinders.length);
this.ref = new ViewRef(this);
for (var i = 0; i < this.elementRefs.length; i++) {
this.elementRefs[i] = new ElementRef(this.ref, i, renderer);
}
2015-05-20 09:48:15 -07:00
this.locals = new Locals(null, MapWrapper.clone(protoLocals)); // TODO optimize this
}
2015-05-20 09:48:15 -07:00
init(changeDetector: ChangeDetector, elementInjectors: List<ElementInjector>,
rootElementInjectors: List<ElementInjector>, preBuiltObjects: List<PreBuiltObjects>,
componentChildViews: List<AppView>) {
this.changeDetector = changeDetector;
this.elementInjectors = elementInjectors;
this.rootElementInjectors = rootElementInjectors;
this.preBuiltObjects = preBuiltObjects;
this.componentChildViews = componentChildViews;
}
2015-05-20 09:48:15 -07:00
setLocal(contextName: string, value): void {
if (!this.hydrated()) throw new BaseException('Cannot set locals on dehydrated view.');
2015-06-17 21:42:56 -07:00
if (!this.proto.variableBindings.has(contextName)) {
2015-01-28 00:42:08 +01:00
return;
}
var templateName = this.proto.variableBindings.get(contextName);
this.locals.set(templateName, value);
}
2015-05-20 09:48:15 -07:00
hydrated(): boolean { return isPresent(this.context); }
/**
* Triggers the event handlers for the element and the directives.
*
* This method is intended to be called from directive EventEmitters.
*
* @param {string} eventName
* @param {*} eventObj
* @param {int} binderIndex
*/
2015-04-19 14:47:02 -07:00
triggerEventHandlers(eventName: string, eventObj, binderIndex: int): void {
var locals = new Map();
locals.set('$event', eventObj);
this.dispatchEvent(binderIndex, eventName, locals);
}
// dispatch to element injector or text nodes based on context
2015-05-20 09:48:15 -07:00
notifyOnBinding(b: BindingRecord, currentValue: any): void {
if (b.isElementProperty()) {
this.renderer.setElementProperty(this.elementRefs[b.elementIndex], b.propertyName,
currentValue);
} else if (b.isElementAttribute()) {
this.renderer.setElementAttribute(this.elementRefs[b.elementIndex], b.propertyName,
currentValue);
} else if (b.isElementClass()) {
this.renderer.setElementClass(this.elementRefs[b.elementIndex], b.propertyName, currentValue);
} else if (b.isElementStyle()) {
var unit = isPresent(b.propertyUnit) ? b.propertyUnit : '';
this.renderer.setElementStyle(this.elementRefs[b.elementIndex], b.propertyName,
`${currentValue}${unit}`);
} else if (b.isTextNode()) {
this.renderer.setText(this.render, b.elementIndex, currentValue);
} else {
throw new BaseException('Unsupported directive record');
}
}
2015-06-12 09:45:31 -07:00
notifyOnAllChangesDone(): void {
var ei = this.elementInjectors;
for (var i = ei.length - 1; i >= 0; i--) {
if (isPresent(ei[i])) ei[i].onAllChangesDone();
}
}
getDirectiveFor(directive: DirectiveIndex): any {
var elementInjector = this.elementInjectors[directive.elementIndex];
return elementInjector.getDirectiveAtIndex(directive.directiveIndex);
}
getDetectorFor(directive: DirectiveIndex): any {
var childView = this.componentChildViews[directive.elementIndex];
return isPresent(childView) ? childView.changeDetector : null;
}
invokeElementMethod(elementIndex: number, methodName: string, args: List<any>) {
this.renderer.invokeElementMethod(this.elementRefs[elementIndex], methodName, args);
}
// implementation of EventDispatcher#dispatchEvent
// returns false if preventDefault must be applied to the DOM event
2015-05-20 09:48:15 -07:00
dispatchEvent(elementIndex: number, eventName: string, locals: Map<string, any>): boolean {
// Most of the time the event will be fired only when the view is in the live document.
// However, in a rare circumstance the view might get dehydrated, in between the event
// queuing up and firing.
var allowDefaultBehavior = true;
if (this.hydrated()) {
var elBinder = this.proto.elementBinders[elementIndex];
if (isBlank(elBinder.hostListeners)) return allowDefaultBehavior;
var eventMap = elBinder.hostListeners[eventName];
if (isBlank(eventMap)) return allowDefaultBehavior;
MapWrapper.forEach(eventMap, (expr, directiveIndex) => {
var context;
if (directiveIndex === -1) {
context = this.context;
} else {
context = this.elementInjectors[elementIndex].getDirectiveAtIndex(directiveIndex);
}
var result = expr.eval(context, new Locals(this.locals, locals));
if (isPresent(result)) {
allowDefaultBehavior = allowDefaultBehavior && result == true;
}
});
}
return allowDefaultBehavior;
}
2014-09-28 16:29:11 -07:00
}
/**
*
*/
export class AppProtoView {
2015-06-12 22:38:44 +02:00
elementBinders: List<ElementBinder> = [];
protoLocals: Map<string, any> = new Map();
2015-05-20 09:48:15 -07:00
constructor(public render: renderApi.RenderProtoViewRef,
public protoChangeDetector: ProtoChangeDetector,
public variableBindings: Map<string, string>,
public variableLocations: Map<string, number>) {
if (isPresent(variableBindings)) {
MapWrapper.forEach(variableBindings,
(templateName, _) => { this.protoLocals.set(templateName, null); });
}
}
2015-05-20 09:48:15 -07:00
bindElement(parent: ElementBinder, distanceToParent: int,
protoElementInjector: ProtoElementInjector,
componentDirective: DirectiveBinding = null): ElementBinder {
var elBinder = new ElementBinder(this.elementBinders.length, parent, distanceToParent,
protoElementInjector, componentDirective);
this.elementBinders.push(elBinder);
return elBinder;
}
2014-11-19 14:54:07 -08:00
/**
* Adds an event binding for the last created ElementBinder via bindElement.
*
* If the directive index is a positive integer, the event is evaluated in the context of
* the given directive.
*
* If the directive index is -1, the event is evaluated in the context of the enclosing view.
*
* @param {string} eventName
* @param {AST} expression
* @param {int} directiveIndex The directive index in the binder or -1 when the event is not bound
* to a directive
2014-11-19 14:54:07 -08:00
*/
2015-05-20 09:48:15 -07:00
bindEvent(eventBindings: List<renderApi.EventBinding>, boundElementIndex: number,
directiveIndex: int = -1): void {
var elBinder = this.elementBinders[boundElementIndex];
var events = elBinder.hostListeners;
if (isBlank(events)) {
events = StringMapWrapper.create();
elBinder.hostListeners = events;
}
for (var i = 0; i < eventBindings.length; i++) {
var eventBinding = eventBindings[i];
var eventName = eventBinding.fullName;
var event = StringMapWrapper.get(events, eventName);
if (isBlank(event)) {
event = new Map();
StringMapWrapper.set(events, eventName, event);
}
event.set(directiveIndex, eventBinding.source);
2014-11-19 14:54:07 -08:00
}
}
}