Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit fffd8f7

Browse files
vsavkinchirayuk
authored andcommitted
feature(directive_injector): add View to DirectiveInjector
Some directives require access to the current view. Add the view field to the DirectiveInjector class, so it can be injected. Closes #1293
1 parent 20defb2 commit fffd8f7

6 files changed

+29
-22
lines changed

lib/core_dom/directive_injector.dart

+12-9
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class DirectiveInjector implements DirectiveBinder {
107107
final Animate _animate;
108108
final EventHandler _eventHandler;
109109
Scope scope; //TODO(misko): this should be final after we get rid of controller
110+
final View _view;
110111

111112
NgElement _ngElement;
112113
ElementProbe _elementProbe;
@@ -141,7 +142,7 @@ class DirectiveInjector implements DirectiveBinder {
141142
static Binding _tempBinding = new Binding();
142143

143144
DirectiveInjector(parent, appInjector, this._node, this._nodeAttrs, this._eventHandler,
144-
this.scope, this._animate)
145+
this.scope, this._view, this._animate)
145146
: appInjector = appInjector,
146147
parent = parent == null ? new DefaultDirectiveInjector(appInjector) : parent;
147148

@@ -150,6 +151,7 @@ class DirectiveInjector implements DirectiveBinder {
150151
_nodeAttrs = null,
151152
_eventHandler = null,
152153
scope = null,
154+
_view = null,
153155
_animate = null;
154156

155157
void bind(key, {dynamic toValue: DEFAULT_VALUE,
@@ -266,6 +268,7 @@ class DirectiveInjector implements DirectiveBinder {
266268
case NG_ELEMENT_KEY_ID: return ngElement;
267269
case EVENT_HANDLER_KEY_ID: return _eventHandler;
268270
case CONTENT_PORT_KEY_ID: return parent._getById(keyId);
271+
case VIEW_KEY_ID: return _view;
269272
default: new NoProviderError(_KEYS[keyId]);
270273
}
271274
}
@@ -345,8 +348,8 @@ class TemplateDirectiveInjector extends DirectiveInjector {
345348

346349
TemplateDirectiveInjector(DirectiveInjector parent, Injector appInjector,
347350
Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
348-
Scope scope, Animate animate, this._viewFactory)
349-
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, animate);
351+
Scope scope, View view, Animate animate, this._viewFactory)
352+
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, view, animate);
350353

351354

352355
Object _getById(int keyId) {
@@ -368,10 +371,10 @@ abstract class ComponentDirectiveInjector extends DirectiveInjector {
368371
final ShadowRoot _shadowRoot;
369372

370373
ComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
371-
EventHandler eventHandler, Scope scope,
374+
EventHandler eventHandler, Scope scope, View view,
372375
this._templateLoader, this._shadowRoot)
373376
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope,
374-
parent._animate);
377+
view, parent._animate);
375378

376379
Object _getById(int keyId) {
377380
switch(keyId) {
@@ -389,9 +392,9 @@ class ShadowlessComponentDirectiveInjector extends ComponentDirectiveInjector {
389392
final ContentPort _contentPort;
390393

391394
ShadowlessComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
392-
EventHandler eventHandler, Scope scope,
395+
EventHandler eventHandler, Scope scope, View view,
393396
templateLoader, shadowRoot, this._contentPort)
394-
: super(parent, appInjector, eventHandler, scope, templateLoader, shadowRoot);
397+
: super(parent, appInjector, eventHandler, scope, view, templateLoader, shadowRoot);
395398

396399
Object _getById(int keyId) {
397400
switch(keyId) {
@@ -403,11 +406,11 @@ class ShadowlessComponentDirectiveInjector extends ComponentDirectiveInjector {
403406

404407
class ShadowDomComponentDirectiveInjector extends ComponentDirectiveInjector {
405408
ShadowDomComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
406-
Scope scope, templateLoader, shadowRoot)
409+
Scope scope, View view, templateLoader, shadowRoot)
407410
: super(parent, appInjector, new ShadowRootEventHandler(shadowRoot,
408411
parent.getByKey(EXPANDO_KEY),
409412
parent.getByKey(EXCEPTION_HANDLER_KEY)),
410-
scope, templateLoader, shadowRoot);
413+
scope, view, templateLoader, shadowRoot);
411414

412415
ElementProbe get elementProbe {
413416
if (_elementProbe == null) {

lib/core_dom/element_binder.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,11 @@ class ElementBinder {
252252
DirectiveInjector nodeInjector;
253253
if (this is TemplateElementBinder) {
254254
nodeInjector = new TemplateDirectiveInjector(parentInjector, parentInjector.appInjector,
255-
node, nodeAttrs, eventHandler, scope, animate,
255+
node, nodeAttrs, eventHandler, scope, view, animate,
256256
(this as TemplateElementBinder).templateViewFactory);
257257
} else {
258258
nodeInjector = new DirectiveInjector(parentInjector, parentInjector.appInjector,
259-
node, nodeAttrs, eventHandler, scope, animate);
259+
node, nodeAttrs, eventHandler, scope, view, animate);
260260
}
261261

262262
for(var i = 0; i < directiveRefs.length; i++) {

lib/core_dom/shadow_dom_component_factory.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ class BoundShadowDomComponentFactory implements BoundComponentFactory {
119119
}
120120

121121
List<Key> get callArgs => _CALL_ARGS;
122-
static final _CALL_ARGS = [DIRECTIVE_INJECTOR_KEY, SCOPE_KEY, NG_BASE_CSS_KEY,
122+
static final _CALL_ARGS = [DIRECTIVE_INJECTOR_KEY, SCOPE_KEY, VIEW_KEY, NG_BASE_CSS_KEY,
123123
EVENT_HANDLER_KEY];
124124
Function call(dom.Element element) {
125-
return (DirectiveInjector injector, Scope scope, NgBaseCss baseCss,
126-
EventHandler eventHandler) {
125+
return (DirectiveInjector injector, Scope scope, View view,
126+
NgBaseCss baseCss, EventHandler eventHandler) {
127127
var shadowDom = element.createShadowRoot()
128128
..applyAuthorStyles = _component.applyAuthorStyles
129129
..resetStyleInheritance = _component.resetStyleInheritance;
@@ -165,7 +165,7 @@ class BoundShadowDomComponentFactory implements BoundComponentFactory {
165165

166166
var probe;
167167
shadowInjector = new ShadowDomComponentDirectiveInjector(injector, injector.appInjector,
168-
shadowScope, templateLoader, shadowDom);
168+
shadowScope, view, templateLoader, shadowDom);
169169
shadowInjector.bindByKey(_ref.typeKey, _ref.factory, _ref.paramKeys, _ref.annotation.visibility);
170170

171171
if (_f.config.elementProbeEnabled) {

lib/core_dom/transcluding_component_factory.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
9797
}
9898

9999
List<Key> get callArgs => _CALL_ARGS;
100-
static var _CALL_ARGS = [ DIRECTIVE_INJECTOR_KEY, SCOPE_KEY,
100+
static var _CALL_ARGS = [ DIRECTIVE_INJECTOR_KEY, SCOPE_KEY, VIEW_KEY,
101101
VIEW_CACHE_KEY, HTTP_KEY, TEMPLATE_CACHE_KEY,
102102
DIRECTIVE_MAP_KEY, NG_BASE_CSS_KEY, EVENT_HANDLER_KEY];
103103
Function call(dom.Node node) {
@@ -106,7 +106,7 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
106106
_component.cssUrls.isEmpty);
107107

108108
var element = node as dom.Element;
109-
return (DirectiveInjector injector, Scope scope,
109+
return (DirectiveInjector injector, Scope scope, View view,
110110
ViewCache viewCache, Http http, TemplateCache templateCache,
111111
DirectiveMap directives, NgBaseCss baseCss, EventHandler eventHandler) {
112112

@@ -143,7 +143,7 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
143143
Scope shadowScope = scope.createChild(new HashMap());
144144

145145
childInjector = new ShadowlessComponentDirectiveInjector(injector, injector.appInjector,
146-
eventHandler, shadowScope, templateLoader, new ShadowlessShadowRoot(element),
146+
eventHandler, shadowScope, view, templateLoader, new ShadowlessShadowRoot(element),
147147
contentPort);
148148
childInjector.bindByKey(_ref.typeKey, _ref.factory, _ref.paramKeys, _ref.annotation.visibility);
149149

test/core_dom/directive_injector_spec.dart

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void main() {
1414
describe('base', () {
1515
DirectiveInjector injector;
1616
Scope scope;
17+
View view;
1718
Animate animate;
1819

1920
addDirective(Type type, [Visibility visibility]) {
@@ -29,7 +30,9 @@ void main() {
2930
beforeEach((Scope _scope, Animate _animate) {
3031
scope = _scope;
3132
animate = _animate;
32-
injector = new DirectiveInjector(null, appInjector, div, new NodeAttrs(div), eventHandler, scope, animate);
33+
view = new View([], scope, eventHandler);
34+
injector = new DirectiveInjector(null, appInjector, div, new NodeAttrs(div), eventHandler,
35+
scope, view, animate);
3336
});
3437

3538
it('should return basic types', () {
@@ -39,6 +42,7 @@ void main() {
3942
expect(injector.get(Injector)).toBe(appInjector);
4043
expect(injector.get(DirectiveInjector)).toBe(injector);
4144
expect(injector.get(Scope)).toBe(scope);
45+
expect((injector.get(View))).toBe(view);
4246
expect(injector.get(Node)).toBe(div);
4347
expect(injector.get(Element)).toBe(div);
4448
expect((injector.get(NodeAttrs) as NodeAttrs).element).toBe(div);
@@ -71,8 +75,8 @@ void main() {
7175
DirectiveInjector leafInjector;
7276

7377
beforeEach(() {
74-
childInjector = new DirectiveInjector(injector, appInjector, span, null, null, null, null);
75-
leafInjector = new DirectiveInjector(childInjector, appInjector, span, null, null, null, null);
78+
childInjector = new DirectiveInjector(injector, appInjector, span, null, null, null, null, null);
79+
leafInjector = new DirectiveInjector(childInjector, appInjector, span, null, null, null, null, null);
7680
});
7781

7882
it('should not allow reseting visibility', () {

test/directive/ng_model_spec.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void main() {
4747

4848
beforeEach((TestBed tb) {
4949
_ = tb;
50-
dirInjector = new DirectiveInjector(null, _.injector, null, null, null, null, null);
50+
dirInjector = new DirectiveInjector(null, _.injector, null, null, null, null, null, null);
5151
});
5252

5353
describe('type="text" like', () {

0 commit comments

Comments
 (0)