forked from dart-archive/angular.dart
/
element_binder.dart
419 lines (377 loc) · 14.8 KB
/
element_binder.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
part of angular.core.dom;
@NgInjectableService()
class ElementBinderFactory {
final Parser _parser;
final Profiler _perf;
final Expando _expando;
ElementBinderFactory(this._parser, this._perf, this._expando);
ElementBinder binder() => new ElementBinder(_parser, _perf, _expando);
}
/**
* ElementBinder is created by the Selector and is responsible for instantiating
* individual directives and binding element properties.
*/
class ElementBinder {
// DI Services
final Parser _parser;
final Profiler _perf;
final Expando _expando;
final onEvents = <String, String>{};
// Member fields
var decorators = <DirectiveRef>[];
DirectiveRef template;
ViewFactory templateViewFactory;
DirectiveRef component;
// Can be either COMPILE_CHILDREN or IGNORE_CHILDREN
String childMode = NgAnnotation.COMPILE_CHILDREN;
ElementBinder(this._parser, this._perf, this._expando);
ElementBinder.forTransclusion(ElementBinder other)
: _parser = other._parser,
_perf = other._perf,
_expando = other._expando,
decorators = other.decorators,
component = other.component,
childMode = other.childMode;
addDirective(DirectiveRef ref) {
var annotation = ref.annotation;
var children = annotation.children;
if (annotation.children == NgAnnotation.TRANSCLUDE_CHILDREN) {
template = ref;
} else if (annotation is NgComponent) {
component = ref;
} else {
decorators.add(ref);
}
if (annotation.children == NgAnnotation.IGNORE_CHILDREN) {
childMode = annotation.children;
}
createMappings(ref);
}
bool get hasTemplate => template != null;
bool get shouldCompileChildren =>
childMode == NgAnnotation.COMPILE_CHILDREN;
ElementBinder get templateBinder => new ElementBinder.forTransclusion(this);
List<DirectiveRef> get _usableDirectiveRefs {
if (template != null) return [template];
if (component != null) return new List.from(decorators)..add(component);
return decorators;
}
bool get hasDirectivesOrEvents =>
_usableDirectiveRefs.isNotEmpty || onEvents.isNotEmpty;
// DI visibility strategy allowing node-local visibility.
static final Function localVisibility = (Injector requesting, Injector defining) {
if (requesting.name == _SHADOW) requesting = requesting.parent;
return identical(requesting, defining);
};
// DI visibility strategy allowing visibility from direct child into parent.
static final Function directChildrenVisibility =
(Injector requesting, Injector defining) {
if (requesting.name == _SHADOW) requesting = requesting.parent;
return localVisibility(requesting, defining) ||
identical(requesting.parent, defining);
};
Injector bind(View view, Injector parentInjector, dom.Node node) {
var timerId;
assert((timerId = _perf.startTimer('ng.view.link.setUp', _html(node))) != false);
Injector nodeInjector;
Scope scope = parentInjector.get(Scope);
FilterMap filters = parentInjector.get(FilterMap);
Map<Type, _ComponentFactory> fctrs;
var nodeAttrs = node is dom.Element ? new NodeAttrs(node) : null;
ElementProbe probe;
var directiveRefs = _usableDirectiveRefs;
try {
if (!hasDirectivesOrEvents) return parentInjector;
var viewPortFactory = (_) => null;
var viewFactory = (_) => null;
var boundViewFactory = (_) => null;
var nodesAttrsDirectives = null;
var nodeModule = new Module()
..type(NgElement)
..value(View, view)
..value(dom.Element, node)
..value(dom.Node, node)
..value(NodeAttrs, nodeAttrs);
directiveRefs.forEach((DirectiveRef ref) {
NgAnnotation annotation = ref.annotation;
var visibility = localVisibility;
if (ref.annotation is NgController) {
scope = scope.createChild(new PrototypeMap(scope.context));
nodeModule.value(Scope, scope);
}
switch (ref.annotation.visibility) {
case NgDirective.CHILDREN_VISIBILITY:
visibility = null;
break;
case NgDirective.DIRECT_CHILDREN_VISIBILITY:
visibility = directChildrenVisibility;
break;
}
if (ref.type == NgTextMustacheDirective) {
nodeModule.factory(NgTextMustacheDirective, (Injector injector) {
return new NgTextMustacheDirective(
node, ref.value, injector.get(Interpolate), injector.get(Scope),
injector.get(FilterMap));
});
} else if (ref.type == NgAttrMustacheDirective) {
if (nodesAttrsDirectives == null) {
nodesAttrsDirectives = [];
nodeModule.factory(NgAttrMustacheDirective, (Injector injector) {
var scope = injector.get(Scope);
var interpolate = injector.get(Interpolate);
for (var ref in nodesAttrsDirectives) {
new NgAttrMustacheDirective(nodeAttrs, ref.value, interpolate,
scope, injector.get(FilterMap));
}
});
}
nodesAttrsDirectives.add(ref);
} else if (ref.annotation is NgComponent) {
//nodeModule.factory(type, new ComponentFactory(node, ref.directive), visibility: visibility);
// TODO(misko): there should be no need to wrap function like this.
nodeModule.factory(ref.type, (Injector injector) {
Compiler compiler = injector.get(Compiler);
Scope scope = injector.get(Scope);
ViewCache viewCache = injector.get(ViewCache);
Http http = injector.get(Http);
TemplateCache templateCache = injector.get(TemplateCache);
DirectiveMap directives = injector.get(DirectiveMap);
// This is a bit of a hack since we are returning different type then we are.
var componentFactory = new _ComponentFactory(node, ref.type,
ref.annotation as NgComponent,
injector.get(dom.NodeTreeSanitizer), _expando);
if (fctrs == null) fctrs = new Map<Type, _ComponentFactory>();
fctrs[ref.type] = componentFactory;
return componentFactory.call(injector, scope, viewCache, http,
templateCache, directives);
}, visibility: visibility);
} else {
nodeModule.type(ref.type, visibility: visibility);
}
if (ref.annotation.module != null) {
nodeModule.install(ref.annotation.module());
}
if (annotation.children == NgAnnotation.TRANSCLUDE_CHILDREN) {
// Currently, transclude is only supported for NgDirective.
assert(annotation is NgDirective);
viewPortFactory = (_) => new ViewPort(node,
parentInjector.get(NgAnimate));
viewFactory = (_) => templateViewFactory;
boundViewFactory = (Injector injector) =>
templateViewFactory.bind(injector);
}
});
nodeModule..factory(ViewPort, viewPortFactory)
..factory(ViewFactory, viewFactory)
..factory(BoundViewFactory, boundViewFactory)
..factory(ElementProbe, (_) => probe);
nodeInjector = parentInjector.createChild([nodeModule]);
probe = _expando[node] = new ElementProbe(
parentInjector.get(ElementProbe), node, nodeInjector, scope);
} finally {
assert(_perf.stopTimer(timerId) != false);
}
directiveRefs.forEach((DirectiveRef ref) {
var linkTimer;
try {
var linkMapTimer;
assert((linkTimer = _perf.startTimer('ng.view.link', ref.type)) != false);
var controller = nodeInjector.get(ref.type);
probe.directives.add(controller);
assert((linkMapTimer = _perf.startTimer('ng.view.link.map', ref.type)) != false);
var shadowScope = (fctrs != null && fctrs.containsKey(ref.type)) ?
fctrs[ref.type].shadowScope :
null;
if (ref.annotation is NgController) {
scope.context[(ref.annotation as NgController).publishAs] = controller;
} else if (ref.annotation is NgComponent) {
shadowScope.context[(ref.annotation as NgComponent).publishAs] = controller;
}
if (nodeAttrs == null) nodeAttrs = new _AnchorAttrs(ref);
var attachDelayStatus = controller is NgAttachAware ? [false] : null;
checkAttachReady() {
if (attachDelayStatus.reduce((a, b) => a && b)) {
attachDelayStatus = null;
if (scope.isAttached) {
controller.attach();
}
}
}
for (var map in ref.mappings) {
var notify;
if (attachDelayStatus != null) {
var index = attachDelayStatus.length;
attachDelayStatus.add(false);
notify = () {
if (attachDelayStatus != null) {
attachDelayStatus[index] = true;
checkAttachReady();
}
};
} else {
notify = () => null;
}
map(nodeAttrs, scope, controller, filters, notify);
}
if (attachDelayStatus != null) {
Watch watch;
watch = scope.watch(
'1', // Cheat a bit.
(_, __) {
watch.remove();
attachDelayStatus[0] = true;
checkAttachReady();
});
}
if (controller is NgDetachAware) {
scope.on(ScopeEvent.DESTROY).listen((_) => controller.detach());
}
assert(_perf.stopTimer(linkMapTimer) != false);
} finally {
assert(_perf.stopTimer(linkTimer) != false);
}
});
onEvents.forEach((event, value) {
view.registerEvent(EventHandler.attrNameToEventName(event));
});
return nodeInjector;
}
static RegExp _MAPPING = new RegExp(r'^(\@|=\>\!|\=\>|\<\=\>|\&)\s*(.*)$');
// TODO: Move this into the Selector
createMappings(DirectiveRef ref) {
NgAnnotation annotation = ref.annotation;
if (annotation.map != null) annotation.map.forEach((attrName, mapping) {
Match match = _MAPPING.firstMatch(mapping);
if (match == null) {
throw "Unknown mapping '$mapping' for attribute '$attrName'.";
}
var mode = match[1];
var dstPath = match[2];
String dstExpression = dstPath.isEmpty ? attrName : dstPath;
Expression dstPathFn = _parser(dstExpression);
if (!dstPathFn.isAssignable) {
throw "Expression '$dstPath' is not assignable in mapping '$mapping' "
"for attribute '$attrName'.";
}
ApplyMapping mappingFn;
switch (mode) {
case '@':
mappingFn = (NodeAttrs attrs, Scope scope, Object controller,
FilterMap filters, notify()) {
attrs.observe(attrName, (value) {
dstPathFn.assign(controller, value);
notify();
});
};
break;
case '<=>':
mappingFn = (NodeAttrs attrs, Scope scope, Object controller,
FilterMap filters, notify()) {
if (attrs[attrName] == null) return notify();
String expression = attrs[attrName];
Expression expressionFn = _parser(expression);
var viewOutbound = false;
var viewInbound = false;
scope.watch(
expression, (inboundValue, _) {
if (!viewInbound) {
viewOutbound = true;
scope.rootScope.runAsync(() => viewOutbound = false);
var value = dstPathFn.assign(controller, inboundValue);
notify();
return value;
}
},
filters: filters
);
if (expressionFn.isAssignable) {
scope.watch(
dstExpression, (outboundValue, _) {
if (!viewOutbound) {
viewInbound = true;
scope.rootScope.runAsync(() => viewInbound = false);
expressionFn.assign(scope.context, outboundValue);
notify();
}
},
context: controller,
filters: filters
);
}
};
break;
case '=>':
mappingFn = (NodeAttrs attrs, Scope scope, Object controller,
FilterMap filters, notify()) {
if (attrs[attrName] == null) return notify();
Expression attrExprFn = _parser(attrs[attrName]);
var shadowValue = null;
scope.watch(attrs[attrName], (v, _) {
dstPathFn.assign(controller, shadowValue = v);
notify();
},
filters: filters);
};
break;
case '=>!':
mappingFn = (NodeAttrs attrs, Scope scope, Object controller,
FilterMap filters, notify()) {
if (attrs[attrName] == null) return notify();
Expression attrExprFn = _parser(attrs[attrName]);
var watch;
watch = scope.watch(attrs[attrName], (value, _) {
if (dstPathFn.assign(controller, value) != null) {
watch.remove();
}
},
filters: filters);
notify();
};
break;
case '&':
mappingFn = (NodeAttrs attrs, Scope scope, Object dst,
FilterMap filters, notify()) {
dstPathFn.assign(dst, _parser(attrs[attrName])
.bind(scope.context, ScopeLocals.wrapper));
notify();
};
break;
}
ref.mappings.add(mappingFn);
});
}
toString() => "[ElementBinder decorators:$decorators template:$template]";
}
// Used for walking the DOM
class ElementBinderTreeRef {
final int offsetIndex;
final ElementBinderTree subtree;
ElementBinderTreeRef(this.offsetIndex, this.subtree);
}
class ElementBinderTree {
ElementBinder binder;
List<ElementBinderTreeRef> subtrees;
ElementBinderTree(this.binder, this.subtrees);
}
class TaggedTextBinder {
ElementBinder binder;
final int offsetIndex;
TaggedTextBinder(this.binder, this.offsetIndex);
toString() => "[TaggedTextBinder binder:$binder offset:$offsetIndex]";
}
// Used for the tagging compiler
class TaggedElementBinder {
final ElementBinder binder;
int parentBinderOffset;
var injector;
bool isTopLevel;
List<TaggedTextBinder> textBinders;
TaggedElementBinder(this.binder, this.parentBinderOffset, this.isTopLevel);
void addText(TaggedTextBinder tagged) {
if (textBinders == null) textBinders = [];
textBinders.add(tagged);
}
String toString() => "[TaggedElementBinder binder:$binder parentBinderOffset:"
"$parentBinderOffset textBinders:$textBinders "
"injector:$injector]";
}