feat(ElementInjector): add NgElement
This commit is contained in:
@ -6,6 +6,7 @@ import {Parent, Ancestor} from 'core/annotations/visibility';
|
||||
import {StaticKeys} from './static_keys';
|
||||
// Comment out as dartanalyzer does not look into @FIELD
|
||||
// import {View} from './view';
|
||||
import {NgElement} from 'core/dom/element';
|
||||
|
||||
var _MAX_DIRECTIVE_CONSTRUCTION_COUNTER = 10;
|
||||
|
||||
@ -75,6 +76,15 @@ class DirectiveDependency extends Dependency {
|
||||
}
|
||||
}
|
||||
|
||||
export class PreBuiltObjects {
|
||||
@FIELD('final view:View')
|
||||
@FIELD('final element:NgElement')
|
||||
constructor(view, element:NgElement) {
|
||||
this.view = view;
|
||||
this.element = element;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Difference between di.Injector and ElementInjector
|
||||
@ -152,8 +162,8 @@ export class ProtoElementInjector {
|
||||
}
|
||||
}
|
||||
|
||||
instantiate(parent:ElementInjector, host:ElementInjector, view):ElementInjector {
|
||||
return new ElementInjector(this, parent, host, view);
|
||||
instantiate(parent:ElementInjector, host:ElementInjector):ElementInjector {
|
||||
return new ElementInjector(this, parent, host);
|
||||
}
|
||||
|
||||
_createBinding(bindingOrType) {
|
||||
@ -170,49 +180,6 @@ export class ProtoElementInjector {
|
||||
}
|
||||
|
||||
export class ElementInjector extends TreeNode {
|
||||
/*
|
||||
_protoInjector:ProtoElementInjector;
|
||||
injector:Injector;
|
||||
_parent:ElementInjector;
|
||||
_next:ElementInjector;
|
||||
_prev:ElementInjector;
|
||||
_head:ElementInjector;
|
||||
_tail:ElementInjector;
|
||||
|
||||
|
||||
// For performance reasons the Injector only supports 10 directives per element.
|
||||
// NOTE: linear search over fields is faster than HashMap lookup.
|
||||
_cObj; // Component only
|
||||
_obj0;
|
||||
_obj1;
|
||||
_obj2;
|
||||
_obj3;
|
||||
_obj4;
|
||||
_obj5;
|
||||
_obj6;
|
||||
_obj7;
|
||||
_obj8;
|
||||
_obj9;
|
||||
|
||||
element:Element;
|
||||
ngElement:NgElement;
|
||||
shadowRoot:ShadowRoot;
|
||||
elementProbe:ElementProbe;
|
||||
view:View;
|
||||
viewPort:ViewPort;
|
||||
viewFactory:ViewFactory;
|
||||
animate:Animate;
|
||||
destinationLightDom:DestinationLightDom;
|
||||
sourceLightDom:SourceLightDom;
|
||||
|
||||
|
||||
// For performance reasons the Injector only supports 2 [Query] per element.
|
||||
// NOTE: linear search over fields is faster than HashMap lookup.
|
||||
_query0:Query;
|
||||
_query1:Query;
|
||||
|
||||
*/
|
||||
|
||||
@FIELD('_proto:ProtoElementInjector')
|
||||
@FIELD('_lightDomAppInjector:Injector')
|
||||
@FIELD('_shadowDomAppInjector:Injector')
|
||||
@ -228,7 +195,7 @@ export class ElementInjector extends TreeNode {
|
||||
@FIELD('_obj8:Object')
|
||||
@FIELD('_obj9:Object')
|
||||
@FIELD('_view:View')
|
||||
constructor(proto:ProtoElementInjector, parent:ElementInjector, host:ElementInjector, view) {
|
||||
constructor(proto:ProtoElementInjector, parent:ElementInjector, host:ElementInjector) {
|
||||
super(parent);
|
||||
if (isPresent(parent) && isPresent(host)) {
|
||||
throw new BaseException('Only either parent or host is allowed');
|
||||
@ -241,9 +208,9 @@ export class ElementInjector extends TreeNode {
|
||||
}
|
||||
|
||||
this._proto = proto;
|
||||
this._view = view;
|
||||
|
||||
//we cannot call clearDirectives because fields won't be detected
|
||||
this._preBuiltObjects = null;
|
||||
this._lightDomAppInjector = null;
|
||||
this._shadowDomAppInjector = null;
|
||||
this._obj0 = null;
|
||||
@ -260,6 +227,7 @@ export class ElementInjector extends TreeNode {
|
||||
}
|
||||
|
||||
clearDirectives() {
|
||||
this._preBuiltObjects = null;
|
||||
this._lightDomAppInjector = null;
|
||||
this._shadowDomAppInjector = null;
|
||||
|
||||
@ -276,16 +244,14 @@ export class ElementInjector extends TreeNode {
|
||||
this._constructionCounter = 0;
|
||||
}
|
||||
|
||||
instantiateDirectives(lightDomAppInjector:Injector, shadowDomAppInjector:Injector) {
|
||||
var p = this._proto;
|
||||
if (this._proto._binding0IsComponent && isBlank(shadowDomAppInjector)) {
|
||||
throw new BaseException('A shadowDomAppInjector is required as this ElementInjector contains a component');
|
||||
} else if (!this._proto._binding0IsComponent && isPresent(shadowDomAppInjector)) {
|
||||
throw new BaseException('No shadowDomAppInjector allowed as there is not component stored in this ElementInjector');
|
||||
}
|
||||
instantiateDirectives(lightDomAppInjector:Injector, shadowDomAppInjector:Injector, preBuiltObjects:PreBuiltObjects) {
|
||||
this._checkShadowDomAppInjector(shadowDomAppInjector);
|
||||
|
||||
this._preBuiltObjects = preBuiltObjects;
|
||||
this._lightDomAppInjector = lightDomAppInjector;
|
||||
this._shadowDomAppInjector = shadowDomAppInjector;
|
||||
|
||||
var p = this._proto;
|
||||
if (isPresent(p._keyId0)) this._getDirectiveByKeyId(p._keyId0);
|
||||
if (isPresent(p._keyId1)) this._getDirectiveByKeyId(p._keyId1);
|
||||
if (isPresent(p._keyId2)) this._getDirectiveByKeyId(p._keyId2);
|
||||
@ -298,6 +264,14 @@ export class ElementInjector extends TreeNode {
|
||||
if (isPresent(p._keyId9)) this._getDirectiveByKeyId(p._keyId9);
|
||||
}
|
||||
|
||||
_checkShadowDomAppInjector(shadowDomAppInjector:Injector) {
|
||||
if (this._proto._binding0IsComponent && isBlank(shadowDomAppInjector)) {
|
||||
throw new BaseException('A shadowDomAppInjector is required as this ElementInjector contains a component');
|
||||
} else if (!this._proto._binding0IsComponent && isPresent(shadowDomAppInjector)) {
|
||||
throw new BaseException('No shadowDomAppInjector allowed as there is not component stored in this ElementInjector');
|
||||
}
|
||||
}
|
||||
|
||||
get(token) {
|
||||
return this._getByKey(Key.get(token), 0, null);
|
||||
}
|
||||
@ -370,7 +344,7 @@ export class ElementInjector extends TreeNode {
|
||||
* This would allows to do the lookup more efficiently.
|
||||
*
|
||||
* for example
|
||||
* we would lookup special objects only when metadata = 'special'
|
||||
* we would lookup pre built objects only when metadata = 'preBuilt'
|
||||
* we would lookup directives only when metadata = 'directive'
|
||||
*
|
||||
* Write benchmarks before doing this optimization.
|
||||
@ -384,8 +358,8 @@ export class ElementInjector extends TreeNode {
|
||||
}
|
||||
|
||||
while (ei != null && depth >= 0) {
|
||||
var specObj = ei._getSpecialObjectByKeyId(key.id);
|
||||
if (specObj !== _undefined) return specObj;
|
||||
var preBuiltObj = ei._getPreBuiltObjectByKeyId(key.id);
|
||||
if (preBuiltObj !== _undefined) return preBuiltObj;
|
||||
|
||||
var dir = ei._getDirectiveByKeyId(key.id);
|
||||
if (dir !== _undefined) return dir;
|
||||
@ -397,13 +371,15 @@ export class ElementInjector extends TreeNode {
|
||||
if (isPresent(this._host) && this._host._isComponentKey(key)) {
|
||||
return this._host.getComponent();
|
||||
} else {
|
||||
var appInjector;
|
||||
if (isPresent(requestor) && this._isComponentKey(requestor)) {
|
||||
appInjector = this._shadowDomAppInjector;
|
||||
} else {
|
||||
appInjector = this._lightDomAppInjector;
|
||||
}
|
||||
return appInjector.get(key);
|
||||
return this._appInjector(requestor).get(key);
|
||||
}
|
||||
}
|
||||
|
||||
_appInjector(requestor:Key) {
|
||||
if (isPresent(requestor) && this._isComponentKey(requestor)) {
|
||||
return this._shadowDomAppInjector;
|
||||
} else {
|
||||
return this._lightDomAppInjector;
|
||||
}
|
||||
}
|
||||
|
||||
@ -411,9 +387,10 @@ export class ElementInjector extends TreeNode {
|
||||
return depth === 0;
|
||||
}
|
||||
|
||||
_getSpecialObjectByKeyId(keyId:int) {
|
||||
_getPreBuiltObjectByKeyId(keyId:int) {
|
||||
var staticKeys = StaticKeys.instance();
|
||||
if (keyId === staticKeys.viewId) return this._view;
|
||||
if (keyId === staticKeys.viewId) return this._preBuiltObjects.view;
|
||||
if (keyId === staticKeys.ngElementId) return this._preBuiltObjects.element;
|
||||
//TODO add other objects as needed
|
||||
return _undefined;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import {View} from 'core/compiler/view';
|
||||
import {NgElement} from 'core/dom/element';
|
||||
import {Key} from 'di/di';
|
||||
import {isBlank} from 'facade/lang';
|
||||
|
||||
@ -8,6 +9,7 @@ export class StaticKeys {
|
||||
constructor() {
|
||||
//TODO: vsavkin Key.annotate(Key.get(View), 'static')
|
||||
this.viewId = Key.get(View).id;
|
||||
this.ngElementId = Key.get(NgElement).id;
|
||||
}
|
||||
|
||||
static instance() {
|
||||
|
@ -4,13 +4,14 @@ import {ProtoWatchGroup, WatchGroup, WatchGroupDispatcher} from 'change_detectio
|
||||
import {Record} from 'change_detection/record';
|
||||
import {AST} from 'change_detection/parser/ast';
|
||||
|
||||
import {ProtoElementInjector, ElementInjector} from './element_injector';
|
||||
import {ProtoElementInjector, ElementInjector, PreBuiltObjects} from './element_injector';
|
||||
import {ElementBinder} from './element_binder';
|
||||
import {AnnotatedType} from './annotated_type';
|
||||
import {SetterFn} from 'change_detection/parser/closure_map';
|
||||
import {FIELD, IMPLEMENTS, int, isPresent, isBlank} from 'facade/lang';
|
||||
import {List} from 'facade/collection';
|
||||
import {Injector} from 'di/di';
|
||||
import {NgElement} from 'core/dom/element';
|
||||
|
||||
const NG_BINDING_CLASS = 'ng-binding';
|
||||
|
||||
@ -94,7 +95,6 @@ export class ProtoView {
|
||||
var rootElementInjectors = ProtoView._rootElementInjectors(elementInjectors);
|
||||
var textNodes = ProtoView._textNodes(elements, binders);
|
||||
var bindElements = ProtoView._bindElements(elements, binders);
|
||||
ProtoView._instantiateDirectives(elementInjectors, appInjector);
|
||||
|
||||
var viewNodes;
|
||||
if (clone instanceof TemplateElement) {
|
||||
@ -102,8 +102,12 @@ export class ProtoView {
|
||||
} else {
|
||||
viewNodes = [clone];
|
||||
}
|
||||
return new View(viewNodes, elementInjectors, rootElementInjectors, textNodes,
|
||||
var view = new View(viewNodes, elementInjectors, rootElementInjectors, textNodes,
|
||||
bindElements, this.protoWatchGroup, context);
|
||||
|
||||
ProtoView._instantiateDirectives(view, elements, elementInjectors, appInjector);
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
bindElement(protoElementInjector:ProtoElementInjector,
|
||||
@ -173,15 +177,15 @@ export class ProtoView {
|
||||
}
|
||||
|
||||
static _instantiateDirectives(
|
||||
injectors:List<ElementInjectors>, appInjector:Injector) {
|
||||
view: View, elements:List, injectors:List<ElementInjectors>, appInjector:Injector) {
|
||||
for (var i = 0; i < injectors.length; ++i) {
|
||||
if (injectors[i] != null) injectors[i].instantiateDirectives(appInjector, null);
|
||||
var preBuiltObjs = new PreBuiltObjects(view, new NgElement(elements[i]));
|
||||
if (injectors[i] != null) injectors[i].instantiateDirectives(appInjector, null, preBuiltObjs);
|
||||
}
|
||||
}
|
||||
|
||||
static _createElementInjector(element, parent:ElementInjector, proto:ProtoElementInjector) {
|
||||
//TODO: vsavkin: pass element to `proto.instantiate()` once https://github.com/angular/angular/pull/98 is merged
|
||||
return proto.instantiate(parent, null, null);
|
||||
return proto.instantiate(parent, null);
|
||||
}
|
||||
|
||||
static _rootElementInjectors(injectors) {
|
||||
|
5
modules/core/src/dom/element.js
Normal file
5
modules/core/src/dom/element.js
Normal file
@ -0,0 +1,5 @@
|
||||
export class NgElement {
|
||||
constructor(domElement) {
|
||||
this.domElement = domElement;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user