2014-10-10 20:44:55 -07:00
|
|
|
import {DOM, Element, Node, Text, DocumentFragment, TemplateElement} from 'facade/dom';
|
2014-11-14 12:34:35 -08:00
|
|
|
import {ListWrapper, MapWrapper} from 'facade/collection';
|
2014-09-28 16:29:11 -07:00
|
|
|
import {ProtoWatchGroup, WatchGroup, WatchGroupDispatcher} from 'change_detection/watch_group';
|
|
|
|
import {Record} from 'change_detection/record';
|
2014-11-11 17:33:47 -08:00
|
|
|
import {AST} from 'change_detection/parser/ast';
|
2014-11-13 15:15:55 -08:00
|
|
|
|
2014-11-14 10:58:58 -08:00
|
|
|
import {ProtoElementInjector, ElementInjector, PreBuiltObjects} from './element_injector';
|
2014-11-11 17:33:47 -08:00
|
|
|
import {ElementBinder} from './element_binder';
|
2014-11-13 15:15:55 -08:00
|
|
|
import {AnnotatedType} from './annotated_type';
|
2014-10-30 23:47:22 -07:00
|
|
|
import {SetterFn} from 'change_detection/parser/closure_map';
|
2014-10-27 11:47:13 -04:00
|
|
|
import {FIELD, IMPLEMENTS, int, isPresent, isBlank} from 'facade/lang';
|
2014-09-28 13:55:01 -07:00
|
|
|
import {List} from 'facade/collection';
|
2014-10-29 15:41:50 -07:00
|
|
|
import {Injector} from 'di/di';
|
2014-11-14 10:58:58 -08:00
|
|
|
import {NgElement} from 'core/dom/element';
|
2014-09-28 16:29:11 -07:00
|
|
|
|
2014-11-11 17:33:47 -08:00
|
|
|
const NG_BINDING_CLASS = 'ng-binding';
|
|
|
|
|
2014-10-10 20:44:55 -07:00
|
|
|
/***
|
|
|
|
* Const of making objects: http://jsperf.com/instantiate-size-of-object
|
|
|
|
*/
|
2014-09-28 16:29:11 -07:00
|
|
|
@IMPLEMENTS(WatchGroupDispatcher)
|
|
|
|
export class View {
|
|
|
|
/// This list matches the _nodes list. It is sparse, since only Elements have ElementInjector
|
2014-10-10 20:44:55 -07:00
|
|
|
@FIELD('final rootElementInjectors:List<ElementInjector>')
|
|
|
|
@FIELD('final elementInjectors:List<ElementInjector>')
|
|
|
|
@FIELD('final bindElements:List<Element>')
|
|
|
|
@FIELD('final textNodes:List<Text>')
|
|
|
|
@FIELD('final watchGroup:WatchGroup')
|
2014-09-28 16:29:11 -07:00
|
|
|
/// When the view is part of render tree, the DocumentFragment is empty, which is why we need
|
|
|
|
/// to keep track of the nodes.
|
2014-10-10 20:44:55 -07:00
|
|
|
@FIELD('final nodes:List<Node>')
|
|
|
|
@FIELD('final onChangeDispatcher:OnChangeDispatcher')
|
2014-11-11 17:33:47 -08:00
|
|
|
constructor(nodes:List<Node>, elementInjectors:List,
|
2014-10-29 15:41:50 -07:00
|
|
|
rootElementInjectors:List, textNodes:List, bindElements:List,
|
|
|
|
protoWatchGroup:ProtoWatchGroup, context) {
|
2014-11-11 17:33:47 -08:00
|
|
|
this.nodes = nodes;
|
|
|
|
this.elementInjectors = elementInjectors;
|
2014-10-27 11:47:13 -04:00
|
|
|
this.rootElementInjectors = rootElementInjectors;
|
2014-10-10 20:44:55 -07:00
|
|
|
this.onChangeDispatcher = null;
|
2014-10-27 11:47:13 -04:00
|
|
|
this.textNodes = textNodes;
|
2014-10-27 23:16:31 -07:00
|
|
|
this.bindElements = bindElements;
|
2014-11-14 12:34:35 -08:00
|
|
|
this.watchGroup = protoWatchGroup.instantiate(this, MapWrapper.create());
|
2014-10-29 15:41:50 -07:00
|
|
|
this.watchGroup.setContext(context);
|
2014-09-28 16:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
onRecordChange(record:Record, target) {
|
|
|
|
// dispatch to element injector or text nodes based on context
|
2014-10-10 20:44:55 -07:00
|
|
|
if (target instanceof DirectivePropertyMemento) {
|
|
|
|
// we know that it is DirectivePropertyMemento
|
|
|
|
var directiveMemento:DirectivePropertyMemento = target;
|
|
|
|
directiveMemento.invoke(record, this.elementInjectors);
|
2014-10-27 23:16:31 -07:00
|
|
|
} else if (target instanceof ElementPropertyMemento) {
|
2014-10-10 20:44:55 -07:00
|
|
|
var elementMemento:ElementPropertyMemento = target;
|
|
|
|
elementMemento.invoke(record, this.bindElements);
|
2014-09-28 16:29:11 -07:00
|
|
|
} else {
|
2014-10-10 20:44:55 -07:00
|
|
|
// we know it refers to _textNodes.
|
2014-09-28 16:29:11 -07:00
|
|
|
var textNodeIndex:number = target;
|
2014-10-10 20:44:55 -07:00
|
|
|
DOM.setText(this.textNodes[textNodeIndex], record.currentValue);
|
2014-09-28 16:29:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 20:02:32 -07:00
|
|
|
export class ProtoView {
|
2014-11-11 17:33:47 -08:00
|
|
|
@FIELD('final element:Element')
|
|
|
|
@FIELD('final elementBinders:List<ElementBinder>')
|
|
|
|
@FIELD('final protoWatchGroup:ProtoWatchGroup')
|
2014-09-28 20:02:32 -07:00
|
|
|
constructor(
|
2014-11-11 17:33:47 -08:00
|
|
|
template:Element,
|
|
|
|
protoWatchGroup:ProtoWatchGroup) {
|
|
|
|
this.element = template;
|
|
|
|
this.elementBinders = [];
|
|
|
|
this.protoWatchGroup = protoWatchGroup;
|
|
|
|
this.textNodesWithBindingCount = 0;
|
|
|
|
this.elementsWithBindingCount = 0;
|
2014-09-28 20:02:32 -07:00
|
|
|
}
|
|
|
|
|
2014-10-29 15:41:50 -07:00
|
|
|
instantiate(context, appInjector:Injector):View {
|
2014-11-11 17:33:47 -08:00
|
|
|
var clone = DOM.clone(this.element);
|
2014-11-14 10:37:42 -08:00
|
|
|
var elements;
|
|
|
|
if (clone instanceof TemplateElement) {
|
|
|
|
elements = ListWrapper.clone(DOM.querySelectorAll(clone.content, `.${NG_BINDING_CLASS}`));
|
|
|
|
} else {
|
|
|
|
elements = ListWrapper.clone(DOM.getElementsByClassName(clone, NG_BINDING_CLASS));
|
|
|
|
}
|
2014-11-11 17:33:47 -08:00
|
|
|
if (DOM.hasClass(clone, NG_BINDING_CLASS)) {
|
|
|
|
ListWrapper.insert(elements, 0, clone);
|
|
|
|
}
|
|
|
|
var binders = this.elementBinders;
|
2014-10-27 11:47:13 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: vsavkin: benchmark
|
2014-10-29 15:41:50 -07:00
|
|
|
* If this performs poorly, the five loops can be collapsed into one.
|
2014-10-27 11:47:13 -04:00
|
|
|
*/
|
2014-10-30 14:41:19 -07:00
|
|
|
var elementInjectors = ProtoView._createElementInjectors(elements, binders);
|
2014-10-27 11:47:13 -04:00
|
|
|
var rootElementInjectors = ProtoView._rootElementInjectors(elementInjectors);
|
2014-10-30 14:41:19 -07:00
|
|
|
var textNodes = ProtoView._textNodes(elements, binders);
|
|
|
|
var bindElements = ProtoView._bindElements(elements, binders);
|
2014-10-27 11:47:13 -04:00
|
|
|
|
2014-11-11 17:33:47 -08:00
|
|
|
var viewNodes;
|
|
|
|
if (clone instanceof TemplateElement) {
|
|
|
|
viewNodes = ListWrapper.clone(clone.content.childNodes);
|
|
|
|
} else {
|
|
|
|
viewNodes = [clone];
|
|
|
|
}
|
2014-11-14 10:58:58 -08:00
|
|
|
var view = new View(viewNodes, elementInjectors, rootElementInjectors, textNodes,
|
2014-11-11 17:33:47 -08:00
|
|
|
bindElements, this.protoWatchGroup, context);
|
2014-11-14 10:58:58 -08:00
|
|
|
|
|
|
|
ProtoView._instantiateDirectives(view, elements, elementInjectors, appInjector);
|
|
|
|
|
|
|
|
return view;
|
2014-11-11 17:33:47 -08:00
|
|
|
}
|
|
|
|
|
2014-11-13 15:15:55 -08:00
|
|
|
bindElement(protoElementInjector:ProtoElementInjector,
|
|
|
|
componentDirective:AnnotatedType = null, templateDirective:AnnotatedType = null):ElementBinder {
|
|
|
|
var elBinder = new ElementBinder(protoElementInjector, componentDirective, templateDirective);
|
2014-11-11 17:33:47 -08:00
|
|
|
ListWrapper.push(this.elementBinders, elBinder);
|
|
|
|
return elBinder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a text node binding for the last created ElementBinder via bindElement
|
|
|
|
*/
|
|
|
|
bindTextNode(indexInParent:int, expression:AST) {
|
|
|
|
var elBinder = this.elementBinders[this.elementBinders.length-1];
|
|
|
|
ListWrapper.push(elBinder.textNodeIndices, indexInParent);
|
|
|
|
this.protoWatchGroup.watch(expression, this.textNodesWithBindingCount++);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an element property binding for the last created ElementBinder via bindElement
|
|
|
|
*/
|
|
|
|
bindElementProperty(propertyName:string, expression:AST) {
|
|
|
|
var elBinder = this.elementBinders[this.elementBinders.length-1];
|
|
|
|
if (!elBinder.hasElementPropertyBindings) {
|
|
|
|
elBinder.hasElementPropertyBindings = true;
|
|
|
|
this.elementsWithBindingCount++;
|
|
|
|
}
|
|
|
|
this.protoWatchGroup.watch(expression,
|
|
|
|
new ElementPropertyMemento(
|
|
|
|
this.elementsWithBindingCount-1,
|
|
|
|
propertyName
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a directive property binding for the last created ElementBinder via bindElement
|
|
|
|
*/
|
|
|
|
bindDirectiveProperty(
|
|
|
|
directiveIndex:number,
|
|
|
|
expression:AST,
|
|
|
|
setterName:string,
|
|
|
|
setter:SetterFn) {
|
|
|
|
this.protoWatchGroup.watch(
|
|
|
|
expression,
|
|
|
|
new DirectivePropertyMemento(
|
|
|
|
this.elementBinders.length-1,
|
|
|
|
directiveIndex,
|
|
|
|
setterName,
|
|
|
|
setter
|
|
|
|
)
|
|
|
|
);
|
2014-10-27 11:47:13 -04:00
|
|
|
}
|
|
|
|
|
2014-10-30 14:41:19 -07:00
|
|
|
static _createElementInjectors(elements, binders) {
|
|
|
|
var injectors = ListWrapper.createFixedSize(binders.length);
|
|
|
|
for (var i = 0; i < binders.length; ++i) {
|
2014-11-04 17:03:35 -08:00
|
|
|
var proto = binders[i].protoElementInjector;
|
2014-11-11 17:33:47 -08:00
|
|
|
if (isPresent(proto)) {
|
|
|
|
var parentElementInjector = isPresent(proto.parent) ? injectors[proto.parent.index] : null;
|
|
|
|
injectors[i] = ProtoView._createElementInjector(elements[i], parentElementInjector, proto);
|
|
|
|
} else {
|
|
|
|
injectors[i] = null;
|
|
|
|
}
|
2014-10-27 23:16:31 -07:00
|
|
|
}
|
2014-10-27 11:47:13 -04:00
|
|
|
return injectors;
|
|
|
|
}
|
|
|
|
|
2014-10-29 15:41:50 -07:00
|
|
|
static _instantiateDirectives(
|
2014-11-14 10:58:58 -08:00
|
|
|
view: View, elements:List, injectors:List<ElementInjectors>, appInjector:Injector) {
|
2014-10-29 15:41:50 -07:00
|
|
|
for (var i = 0; i < injectors.length; ++i) {
|
2014-11-14 10:58:58 -08:00
|
|
|
var preBuiltObjs = new PreBuiltObjects(view, new NgElement(elements[i]));
|
|
|
|
if (injectors[i] != null) injectors[i].instantiateDirectives(appInjector, null, preBuiltObjs);
|
2014-10-29 15:41:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-04 17:03:35 -08:00
|
|
|
static _createElementInjector(element, parent:ElementInjector, proto:ProtoElementInjector) {
|
2014-11-14 10:58:58 -08:00
|
|
|
return proto.instantiate(parent, null);
|
2014-10-27 11:47:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static _rootElementInjectors(injectors) {
|
|
|
|
return ListWrapper.filter(injectors, inj => isPresent(inj) && isBlank(inj.parent));
|
|
|
|
}
|
|
|
|
|
2014-10-30 14:41:19 -07:00
|
|
|
static _textNodes(elements, binders) {
|
2014-10-27 11:47:13 -04:00
|
|
|
var textNodes = [];
|
2014-10-30 14:41:19 -07:00
|
|
|
for (var i = 0; i < binders.length; ++i) {
|
2014-10-27 23:16:31 -07:00
|
|
|
ProtoView._collectTextNodes(textNodes, elements[i],
|
2014-10-30 14:41:19 -07:00
|
|
|
binders[i].textNodeIndices);
|
2014-10-27 11:47:13 -04:00
|
|
|
}
|
|
|
|
return textNodes;
|
|
|
|
}
|
|
|
|
|
2014-10-30 14:41:19 -07:00
|
|
|
static _bindElements(elements, binders):List<Element> {
|
2014-10-27 23:16:31 -07:00
|
|
|
var bindElements = [];
|
2014-10-30 14:41:19 -07:00
|
|
|
for (var i = 0; i < binders.length; ++i) {
|
|
|
|
if (binders[i].hasElementPropertyBindings) ListWrapper.push(
|
2014-10-27 23:16:31 -07:00
|
|
|
bindElements, elements[i]);
|
|
|
|
}
|
|
|
|
return bindElements;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _collectTextNodes(allTextNodes, element, indices) {
|
2014-11-14 10:37:42 -08:00
|
|
|
var childNodes = DOM.templateAwareRoot(element).childNodes;
|
2014-10-27 23:16:31 -07:00
|
|
|
for (var i = 0; i < indices.length; ++i) {
|
|
|
|
ListWrapper.push(allTextNodes, childNodes[indices[i]]);
|
|
|
|
}
|
2014-09-28 20:02:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:44:55 -07:00
|
|
|
export class ElementPropertyMemento {
|
|
|
|
@FIELD('final _elementIndex:int')
|
2014-11-11 17:33:47 -08:00
|
|
|
@FIELD('final _propertyName:string')
|
2014-10-10 20:44:55 -07:00
|
|
|
constructor(elementIndex:int, propertyName:string) {
|
|
|
|
this._elementIndex = elementIndex;
|
|
|
|
this._propertyName = propertyName;
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:16:31 -07:00
|
|
|
invoke(record:Record, bindElements:List<Element>) {
|
|
|
|
var element:Element = bindElements[this._elementIndex];
|
2014-10-10 20:44:55 -07:00
|
|
|
DOM.setProperty(element, this._propertyName, record.currentValue);
|
|
|
|
}
|
|
|
|
}
|
2014-09-28 16:29:11 -07:00
|
|
|
|
2014-10-10 20:44:55 -07:00
|
|
|
export class DirectivePropertyMemento {
|
2014-09-28 16:29:11 -07:00
|
|
|
@FIELD('final _elementInjectorIndex:int')
|
|
|
|
@FIELD('final _directiveIndex:int')
|
2014-10-27 23:16:31 -07:00
|
|
|
@FIELD('final _setterName:string')
|
2014-09-28 16:29:11 -07:00
|
|
|
@FIELD('final _setter:SetterFn')
|
|
|
|
constructor(
|
|
|
|
elementInjectorIndex:number,
|
|
|
|
directiveIndex:number,
|
2014-10-27 23:16:31 -07:00
|
|
|
setterName:string,
|
|
|
|
setter:SetterFn) {
|
2014-09-28 16:29:11 -07:00
|
|
|
this._elementInjectorIndex = elementInjectorIndex;
|
|
|
|
this._directiveIndex = directiveIndex;
|
|
|
|
this._setterName = setterName;
|
|
|
|
this._setter = setter;
|
|
|
|
}
|
|
|
|
|
|
|
|
invoke(record:Record, elementInjectors:List<ElementInjector>) {
|
|
|
|
var elementInjector:ElementInjector = elementInjectors[this._elementInjectorIndex];
|
2014-10-27 23:16:31 -07:00
|
|
|
var directive = elementInjector.getAtIndex(this._directiveIndex);
|
2014-09-28 16:29:11 -07:00
|
|
|
this._setter(directive, record.currentValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO(tbosch): I don't like to have done be called from a different place than notify
|
|
|
|
// notify is called by change detection, but done is called by our wrapper on detect changes.
|
|
|
|
export class OnChangeDispatcher {
|
|
|
|
|
|
|
|
@FIELD('_lastView:View')
|
2014-10-10 20:44:55 -07:00
|
|
|
@FIELD('_lastTarget:DirectivePropertyMemento')
|
2014-09-28 16:29:11 -07:00
|
|
|
constructor() {
|
|
|
|
this._lastView = null;
|
|
|
|
this._lastTarget = null;
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:44:55 -07:00
|
|
|
notify(view:View, eTarget:DirectivePropertyMemento) {
|
2014-09-28 16:29:11 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
done() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|