@@ -24,6 +24,7 @@ final CONTENT_PORT_KEY = new Key(ContentPort);
24
24
final TEMPLATE_LOADER_KEY = new Key (TemplateLoader );
25
25
final SHADOW_ROOT_KEY = new Key (ShadowRoot );
26
26
27
+ // Visibility < 0 for directives
27
28
const int VISIBILITY_LOCAL = - 1 ;
28
29
const int VISIBILITY_DIRECT_CHILD = - 2 ;
29
30
const int VISIBILITY_CHILDREN = - 3 ;
@@ -32,6 +33,7 @@ const int VISIBILITY_COMPONENT_LOCAL = VISIBILITY_LOCAL + VISIBI
32
33
const int VISIBILITY_COMPONENT_DIRECT_CHILD = VISIBILITY_DIRECT_CHILD + VISIBILITY_COMPONENT_OFFSET ;
33
34
const int VISIBILITY_COMPONENT_CHILDREN = VISIBILITY_CHILDREN + VISIBILITY_COMPONENT_OFFSET ;
34
35
36
+ // Visibility >= 0 for predefined types
35
37
const int UNDEFINED_ID = 0 ;
36
38
const int INJECTOR_KEY_ID = 1 ;
37
39
const int DIRECTIVE_INJECTOR_KEY_ID = 2 ;
@@ -54,6 +56,7 @@ const int KEEP_ME_LAST = 18;
54
56
55
57
class DirectiveInjector implements DirectiveBinder {
56
58
static bool _isInit = false ;
59
+
57
60
static initUID () {
58
61
if (_isInit) return ;
59
62
_isInit = true ;
@@ -74,10 +77,12 @@ class DirectiveInjector implements DirectiveBinder {
74
77
CONTENT_PORT_KEY .uid = CONTENT_PORT_KEY_ID ;
75
78
EVENT_HANDLER_KEY .uid = EVENT_HANDLER_KEY_ID ;
76
79
ANIMATE_KEY .uid = ANIMATE_KEY_ID ;
80
+ // todo(vicb) wrap in an assert (or assign uids in the for loop) ?
77
81
for (var i = 1 ; i < KEEP_ME_LAST ; i++ ) {
78
82
if (_KEYS [i].uid != i) throw 'MISSORDERED KEYS ARRAY: ${_KEYS } at $i ' ;
79
83
}
80
84
}
85
+
81
86
static List <Key > _KEYS =
82
87
[ UNDEFINED_ID
83
88
, INJECTOR_KEY
@@ -111,6 +116,7 @@ class DirectiveInjector implements DirectiveBinder {
111
116
NgElement _ngElement;
112
117
ElementProbe _elementProbe;
113
118
119
+ // Keys, instances, parameter keys and factory functions
114
120
Key _key0 = null ; dynamic _obj0; List <Key > _pKeys0; Function _factory0;
115
121
Key _key1 = null ; dynamic _obj1; List <Key > _pKeys1; Function _factory1;
116
122
Key _key2 = null ; dynamic _obj2; List <Key > _pKeys2; Function _factory2;
@@ -122,11 +128,11 @@ class DirectiveInjector implements DirectiveBinder {
122
128
Key _key8 = null ; dynamic _obj8; List <Key > _pKeys8; Function _factory8;
123
129
Key _key9 = null ; dynamic _obj9; List <Key > _pKeys9; Function _factory9;
124
130
125
- static _toVisId (Visibility v) => identical (v, Visibility .LOCAL )
131
+ static int _toVisibilityId (Visibility v) => identical (v, Visibility .LOCAL )
126
132
? VISIBILITY_LOCAL
127
133
: (identical (v, Visibility .CHILDREN ) ? VISIBILITY_CHILDREN : VISIBILITY_DIRECT_CHILD );
128
134
129
- static _toVis (int id) {
135
+ static Visibility _toVisibility (int id) {
130
136
switch (id) {
131
137
case VISIBILITY_LOCAL : return Visibility .LOCAL ;
132
138
case VISIBILITY_DIRECT_CHILD : return Visibility .DIRECT_CHILD ;
@@ -158,7 +164,7 @@ class DirectiveInjector implements DirectiveBinder {
158
164
toInstanceOf,
159
165
inject: const [],
160
166
Visibility visibility: Visibility .LOCAL }) {
161
- if (key == null ) throw 'Key is required' ;
167
+ assert (key != null );
162
168
if (key is ! Key ) key = new Key (key);
163
169
if (inject is ! List ) inject = [inject];
164
170
@@ -170,13 +176,13 @@ class DirectiveInjector implements DirectiveBinder {
170
176
171
177
void bindByKey (Key key, Function factory , List <Key > parameterKeys, [Visibility visibility]) {
172
178
if (visibility == null ) visibility = Visibility .CHILDREN ;
173
- int visibilityId = _toVisId (visibility);
179
+ int visibilityId = _toVisibilityId (visibility);
174
180
int keyVisId = key.uid;
175
181
if (keyVisId != visibilityId) {
176
182
if (keyVisId == null ) {
177
183
key.uid = visibilityId;
178
184
} else {
179
- throw "Can not set $visibility on $key , it alread has ${_toVis (keyVisId )}" ;
185
+ throw "Can not set $visibility on $key , it already has ${_toVisibility (keyVisId )}" ;
180
186
}
181
187
}
182
188
if (_key0 == null || identical (_key0, key)) { _key0 = key; _pKeys0 = parameterKeys; _factory0 = factory ; }
@@ -213,7 +219,7 @@ class DirectiveInjector implements DirectiveBinder {
213
219
return isDirective ? _getDirectiveByKey (key, uid, appInjector) : _getById (uid);
214
220
}
215
221
216
- Object _getDirectiveByKey (Key k, int visType , Injector i) {
222
+ Object _getDirectiveByKey (Key k, int visibility , Injector i) {
217
223
do {
218
224
if (_key0 == null ) break ; if (identical (_key0, k)) return _obj0 == null ? _obj0 = _new (_pKeys0, _factory0) : _obj0;
219
225
if (_key1 == null ) break ; if (identical (_key1, k)) return _obj1 == null ? _obj1 = _new (_pKeys1, _factory1) : _obj1;
@@ -226,7 +232,7 @@ class DirectiveInjector implements DirectiveBinder {
226
232
if (_key8 == null ) break ; if (identical (_key8, k)) return _obj8 == null ? _obj8 = _new (_pKeys8, _factory8) : _obj8;
227
233
if (_key9 == null ) break ; if (identical (_key9, k)) return _obj9 == null ? _obj9 = _new (_pKeys9, _factory9) : _obj9;
228
234
} while (false );
229
- switch (visType ) {
235
+ switch (visibility ) {
230
236
case VISIBILITY_LOCAL : return appInjector.getByKey (k);
231
237
case VISIBILITY_DIRECT_CHILD : return parent._getDirectiveByKey (k, VISIBILITY_LOCAL , i);
232
238
case VISIBILITY_CHILDREN : return parent._getDirectiveByKey (k, VISIBILITY_CHILDREN , i);
@@ -321,7 +327,6 @@ class DirectiveInjector implements DirectiveBinder {
321
327
return obj;
322
328
}
323
329
324
-
325
330
ElementProbe get elementProbe {
326
331
if (_elementProbe == null ) {
327
332
ElementProbe parentProbe = parent is DirectiveInjector ? parent.elementProbe : null ;
@@ -344,15 +349,15 @@ class TemplateDirectiveInjector extends DirectiveInjector {
344
349
BoundViewFactory _boundViewFactory;
345
350
346
351
TemplateDirectiveInjector (DirectiveInjector parent, Injector appInjector,
347
- Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
348
- Scope scope, Animate animate, this ._viewFactory)
352
+ Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
353
+ Scope scope, Animate animate, this ._viewFactory)
349
354
: super (parent, appInjector, node, nodeAttrs, eventHandler, scope, animate);
350
355
351
356
352
357
Object _getById (int keyId) {
353
358
switch (keyId) {
354
359
case VIEW_FACTORY_KEY_ID : return _viewFactory;
355
- case VIEW_PORT_KEY_ID : return (( _viewPort) == null ) ?
360
+ case VIEW_PORT_KEY_ID : return (_viewPort == null ) ?
356
361
_viewPort = new ViewPort (this , scope, _node, _animate) : _viewPort;
357
362
case BOUND_VIEW_FACTORY_KEY_ID : return (_boundViewFactory == null ) ?
358
363
_boundViewFactory = _viewFactory.bind (this .parent) : _boundViewFactory;
@@ -363,56 +368,59 @@ class TemplateDirectiveInjector extends DirectiveInjector {
363
368
}
364
369
365
370
abstract class ComponentDirectiveInjector extends DirectiveInjector {
366
-
367
371
final TemplateLoader _templateLoader;
368
372
final ShadowRoot _shadowRoot;
373
+ final Key _typeKey;
369
374
370
375
ComponentDirectiveInjector (DirectiveInjector parent, Injector appInjector,
371
- EventHandler eventHandler, Scope scope ,
372
- this ._templateLoader, this ._shadowRoot )
373
- : super (parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope ,
376
+ EventHandler eventHandler, this ._templateLoader, this ._shadowRoot ,
377
+ this ._typeKey )
378
+ : super (parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, null ,
374
379
parent._animate);
375
380
376
381
Object _getById (int keyId) {
377
382
switch (keyId) {
378
383
case TEMPLATE_LOADER_KEY_ID : return _templateLoader;
379
384
case SHADOW_ROOT_KEY_ID : return _shadowRoot;
385
+ case SCOPE_KEY_ID :
386
+ if (scope == null ) {
387
+ Scope parentScope = parent is DirectiveInjector ?
388
+ parent.scope :
389
+ parent.getByKey (SCOPE_KEY );
390
+ scope = parentScope.createChild (getByKey (_typeKey));
391
+ }
392
+ return scope;
380
393
default : return super ._getById (keyId);
381
394
}
382
395
}
383
396
384
- _getDirectiveByKey (Key k, int visType , Injector i) =>
385
- super ._getDirectiveByKey (k, visType + VISIBILITY_COMPONENT_OFFSET , i);
397
+ Object _getDirectiveByKey (Key k, int visibility , Injector i) =>
398
+ super ._getDirectiveByKey (k, visibility + VISIBILITY_COMPONENT_OFFSET , i);
386
399
}
387
400
388
401
class ShadowlessComponentDirectiveInjector extends ComponentDirectiveInjector {
389
402
final ContentPort _contentPort;
390
403
391
404
ShadowlessComponentDirectiveInjector (DirectiveInjector parent, Injector appInjector,
392
- EventHandler eventHandler, Scope scope ,
393
- templateLoader, shadowRoot, this ._contentPort)
394
- : super (parent, appInjector, eventHandler, scope, templateLoader, shadowRoot);
405
+ EventHandler eventHandler, templateLoader, shadowRoot ,
406
+ this ._contentPort, Key typeKey )
407
+ : super (parent, appInjector, eventHandler, templateLoader, shadowRoot, typeKey );
395
408
396
- Object _getById (int keyId) {
397
- switch (keyId) {
398
- case CONTENT_PORT_KEY_ID : return _contentPort;
399
- default : return super ._getById (keyId);
400
- }
401
- }
409
+ Object _getById (int keyId) => keyId == CONTENT_PORT_KEY_ID ? _contentPort : super ._getById (keyId);
402
410
}
403
411
404
412
class ShadowDomComponentDirectiveInjector extends ComponentDirectiveInjector {
405
413
ShadowDomComponentDirectiveInjector (DirectiveInjector parent, Injector appInjector,
406
- Scope scope, templateLoader, shadowRoot)
414
+ templateLoader, shadowRoot, Key typeKey )
407
415
: super (parent, appInjector, new ShadowRootEventHandler (shadowRoot,
408
416
parent.getByKey (EXPANDO_KEY ),
409
417
parent.getByKey (EXCEPTION_HANDLER_KEY )),
410
- scope, templateLoader, shadowRoot);
418
+ templateLoader, shadowRoot, typeKey );
411
419
412
420
ElementProbe get elementProbe {
413
421
if (_elementProbe == null ) {
414
422
ElementProbe parentProbe =
415
- parent is DirectiveInjector ? parent.elementProbe : parent.getByKey (ELEMENT_PROBE_KEY );
423
+ parent is DirectiveInjector ? parent.elementProbe : parent.getByKey (ELEMENT_PROBE_KEY );
416
424
_elementProbe = new ElementProbe (parentProbe, _shadowRoot, this , scope);
417
425
}
418
426
return _elementProbe;
@@ -422,12 +430,16 @@ class ShadowDomComponentDirectiveInjector extends ComponentDirectiveInjector {
422
430
@Injectable ()
423
431
class DefaultDirectiveInjector extends DirectiveInjector {
424
432
DefaultDirectiveInjector (Injector appInjector): super ._default (null , appInjector);
433
+
425
434
DefaultDirectiveInjector .newAppInjector (DirectiveInjector parent, Injector appInjector)
426
- : super ._default (parent, appInjector);
435
+ : super ._default (parent, appInjector);
436
+
427
437
428
438
Object getByKey (Key key) => appInjector.getByKey (key);
429
- _getDirectiveByKey (Key key, int visType, Injector i) =>
430
- parent == null ? i.getByKey (key) : parent._getDirectiveByKey (key, visType, i);
439
+
440
+ _getDirectiveByKey (Key key, int visibility, Injector i) =>
441
+ parent == null ? i.getByKey (key) : parent._getDirectiveByKey (key, visibility, i);
442
+
431
443
_getById (int keyId) {
432
444
switch (keyId) {
433
445
case CONTENT_PORT_KEY_ID : return null ;
0 commit comments