2014-10-10 20:44:55 -07:00
|
|
|
import {DOM, Element, Node, Text, DocumentFragment, TemplateElement} from 'facade/dom';
|
2014-09-28 13:55:01 -07:00
|
|
|
import {ListWrapper} 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';
|
|
|
|
import {Module} from 'di/di';
|
|
|
|
import {ProtoElementInjector, ElementInjector} from './element_injector';
|
|
|
|
import {SetterFn} from 'change_detection/facade';
|
2014-10-10 14:07:59 -04:00
|
|
|
import {FIELD, IMPLEMENTS, int} from 'facade/lang';
|
2014-09-28 13:55:01 -07:00
|
|
|
import {List} from 'facade/collection';
|
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-09-28 16:29:11 -07:00
|
|
|
@IMPLEMENTS(WatchGroupDispatcher)
|
|
|
|
export class View {
|
2014-10-10 20:44:55 -07:00
|
|
|
@FIELD('final fragment:DocumentFragment')
|
2014-09-28 16:29:11 -07:00
|
|
|
/// 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-09-28 16:29:11 -07:00
|
|
|
constructor(fragment:DocumentFragment) {
|
2014-10-10 20:44:55 -07:00
|
|
|
this.fragment = fragment;
|
|
|
|
this.nodes = ListWrapper.clone(fragment.childNodes);
|
|
|
|
this.onChangeDispatcher = null;
|
|
|
|
this.elementInjectors = null;
|
|
|
|
this.textNodes = null;
|
|
|
|
this.bindElements = null;
|
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);
|
|
|
|
} else if (target instanceof ElementPropertyMemento) {
|
|
|
|
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-10-10 20:44:55 -07:00
|
|
|
@FIELD('final _template:TemplateElement')
|
|
|
|
@FIELD('final _module:Module')
|
|
|
|
@FIELD('final _protoElementInjectors:List<ProtoElementInjector>')
|
|
|
|
@FIELD('final _protoWatchGroup:ProtoWatchGroup')
|
|
|
|
@FIELD('final _useRootElement:bool')
|
2014-09-28 20:02:32 -07:00
|
|
|
constructor(
|
|
|
|
template:TemplateElement,
|
|
|
|
module:Module,
|
2014-10-10 20:44:55 -07:00
|
|
|
protoElementInjector:List<ProtoElementInjector>,
|
|
|
|
protoWatchGroup:ProtoWatchGroup,
|
2014-10-10 14:07:59 -04:00
|
|
|
useRootElement:boolean)
|
2014-09-28 20:02:32 -07:00
|
|
|
{
|
|
|
|
this._template = template;
|
|
|
|
this._module = module;
|
|
|
|
this._protoElementInjectors = protoElementInjector;
|
|
|
|
this._protoWatchGroup = protoWatchGroup;
|
2014-10-10 20:44:55 -07:00
|
|
|
this._useRootElement = useRootElement;
|
2014-09-28 20:02:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
instantiate():View {
|
|
|
|
return new View(DOM.clone(this._template.content));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:44:55 -07:00
|
|
|
export class ElementPropertyMemento {
|
|
|
|
@FIELD('final _elementIndex:int')
|
|
|
|
@FIELD('final _propertyIndex:string')
|
|
|
|
constructor(elementIndex:int, propertyName:string) {
|
|
|
|
this._elementIndex = elementIndex;
|
|
|
|
this._propertyName = propertyName;
|
|
|
|
}
|
|
|
|
|
|
|
|
invoke(record:Record, elementInjectors:List<Element>) {
|
|
|
|
var element:Element = elementInjectors[this._elementIndex];
|
|
|
|
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')
|
|
|
|
@FIELD('final _setterName:String')
|
|
|
|
@FIELD('final _setter:SetterFn')
|
|
|
|
constructor(
|
|
|
|
elementInjectorIndex:number,
|
|
|
|
directiveIndex:number,
|
|
|
|
setterName:String,
|
|
|
|
setter:SetterFn)
|
|
|
|
{
|
|
|
|
this._elementInjectorIndex = elementInjectorIndex;
|
|
|
|
this._directiveIndex = directiveIndex;
|
|
|
|
this._setterName = setterName;
|
|
|
|
this._setter = setter;
|
|
|
|
}
|
|
|
|
|
|
|
|
invoke(record:Record, elementInjectors:List<ElementInjector>) {
|
|
|
|
var elementInjector:ElementInjector = elementInjectors[this._elementInjectorIndex];
|
|
|
|
var directive = elementInjectors[this._directiveIndex];
|
|
|
|
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() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|