forked from dart-archive/angular.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement_binder.dart
421 lines (352 loc) · 13.4 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
420
421
part of angular.core.dom_internal;
class TemplateElementBinder extends ElementBinder {
final DirectiveRef template;
ViewFactory templateViewFactory;
final bool hasTemplate = true;
final ElementBinder templateBinder;
var _directiveCache;
List<DirectiveRef> get _usableDirectiveRefs {
if (_directiveCache != null) return _directiveCache;
return _directiveCache = [template];
}
TemplateElementBinder(perf, expando, parser, componentFactory,
transcludingComponentFactory, shadowDomComponentFactory,
this.template, this.templateBinder,
onEvents, bindAttrs, childMode)
: super(perf, expando, parser, componentFactory,
transcludingComponentFactory, shadowDomComponentFactory,
null, null, onEvents, bindAttrs, childMode);
String toString() => "[TemplateElementBinder template:$template]";
_registerViewFactory(node, parentInjector, nodeModule) {
assert(templateViewFactory != null);
nodeModule
..bind(ViewPort, toFactory: (_) =>
new ViewPort(node, parentInjector.get(Animate)))
..bind(ViewFactory, toValue: templateViewFactory)
..bind(BoundViewFactory, toFactory: (Injector injector) =>
templateViewFactory.bind(injector));
}
}
/**
* ElementBinder is created by the Selector and is responsible for instantiating
* individual directives and binding element properties.
*/
class ElementBinder {
// DI Services
final Profiler _perf;
final Expando _expando;
final Parser _parser;
// The default component factory
final ComponentFactory _componentFactory;
final TranscludingComponentFactory _transcludingComponentFactory;
final ShadowDomComponentFactory _shadowDomComponentFactory;
final Map onEvents;
final Map bindAttrs;
// Member fields
final decorators;
final DirectiveRef component;
// Can be either COMPILE_CHILDREN or IGNORE_CHILDREN
final String childMode;
ElementBinder(this._perf, this._expando, this._parser,
this._componentFactory,
this._transcludingComponentFactory,
this._shadowDomComponentFactory,
this.component, this.decorators,
this.onEvents, this.bindAttrs, this.childMode);
final bool hasTemplate = false;
bool get shouldCompileChildren =>
childMode == Directive.COMPILE_CHILDREN;
var _directiveCache;
List<DirectiveRef> get _usableDirectiveRefs {
if (_directiveCache != null) return _directiveCache;
if (component != null) return _directiveCache = new List.from(decorators)..add(component);
return _directiveCache = decorators;
}
bool get hasDirectivesOrEvents =>
_usableDirectiveRefs.isNotEmpty || onEvents.isNotEmpty;
_bindTwoWay(tasks, expression, scope, dstPathFn, controller, formatters, dstExpression) {
var taskId = tasks.registerTask();
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);
tasks.completeTask(taskId);
return value;
}
}, formatters: formatters);
if (expressionFn.isAssignable) {
scope.watch(dstExpression, (outboundValue, _) {
if (!viewOutbound) {
viewInbound = true;
scope.rootScope.runAsync(() => viewInbound = false);
expressionFn.assign(scope.context, outboundValue);
tasks.completeTask(taskId);
}
}, context: controller, formatters: formatters);
}
}
_bindOneWay(tasks, expression, scope, dstPathFn, controller, formatters) {
var taskId = tasks.registerTask();
Expression attrExprFn = _parser(expression);
scope.watch(expression, (v, _) {
dstPathFn.assign(controller, v);
tasks.completeTask(taskId);
}, formatters: formatters);
}
_bindCallback(dstPathFn, controller, expression, scope) {
dstPathFn.assign(controller, _parser(expression).bind(scope.context, ScopeLocals.wrapper));
}
_createAttrMappings(controller, scope, List<MappingParts> mappings, nodeAttrs, formatters, tasks) {
mappings.forEach((MappingParts p) {
var attrName = p.attrName;
var dstExpression = p.dstExpression;
Expression dstPathFn = _parser(dstExpression);
if (!dstPathFn.isAssignable) {
throw "Expression '$dstExpression' is not assignable in mapping '${p.originalValue}' "
"for attribute '$attrName'.";
}
// Check if there is a bind attribute for this mapping.
var bindAttr = bindAttrs["bind-${p.attrName}"];
if (bindAttr != null) {
if (p.mode == '<=>') {
_bindTwoWay(tasks, bindAttr, scope, dstPathFn,
controller, formatters, dstExpression);
} else if(p.mode == '&') {
_bindCallback(dstPathFn, controller, bindAttr, scope);
} else {
_bindOneWay(tasks, bindAttr, scope, dstPathFn, controller, formatters);
}
return;
}
switch (p.mode) {
case '@': // string
var taskId = tasks.registerTask();
nodeAttrs.observe(attrName, (value) {
dstPathFn.assign(controller, value);
tasks.completeTask(taskId);
});
break;
case '<=>': // two-way
if (nodeAttrs[attrName] == null) return;
_bindTwoWay(tasks, nodeAttrs[attrName], scope, dstPathFn,
controller, formatters, dstExpression);
break;
case '=>': // one-way
if (nodeAttrs[attrName] == null) return;
_bindOneWay(tasks, nodeAttrs[attrName], scope,
dstPathFn, controller, formatters);
break;
case '=>!': // one-way, one-time
if (nodeAttrs[attrName] == null) return;
Expression attrExprFn = _parser(nodeAttrs[attrName]);
var watch;
var lastOneTimeValue;
watch = scope.watch(nodeAttrs[attrName], (value, _) {
if ((lastOneTimeValue = dstPathFn.assign(controller, value)) != null && watch != null) {
var watchToRemove = watch;
watch = null;
scope.rootScope.domWrite(() {
if (lastOneTimeValue != null) {
watchToRemove.remove();
} else { // It was set to non-null, but stablized to null, wait.
watch = watchToRemove;
}
});
}
}, formatters: formatters);
break;
case '&': // callback
_bindCallback(dstPathFn, controller, nodeAttrs[attrName], scope);
break;
}
});
}
_link(nodeInjector, probe, scope, nodeAttrs, formatters) {
_usableDirectiveRefs.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);
if (ref.annotation is Controller) {
scope.context[(ref.annotation as Controller).publishAs] = controller;
}
var tasks = new _TaskList(controller is AttachAware ? () {
if (scope.isAttached) controller.attach();
} : null);
if (ref.mappings.isNotEmpty) {
if (nodeAttrs == null) nodeAttrs = new _AnchorAttrs(ref);
_createAttrMappings(controller, scope, ref.mappings, nodeAttrs, formatters, tasks);
}
if (controller is AttachAware) {
var taskId = tasks.registerTask();
Watch watch;
watch = scope.watch('1', // Cheat a bit.
(_, __) {
watch.remove();
tasks.completeTask(taskId);
});
}
tasks.doneRegistering();
if (controller is DetachAware) {
scope.on(ScopeEvent.DESTROY).listen((_) => controller.detach());
}
assert(_perf.stopTimer(linkMapTimer) != false);
} finally {
assert(_perf.stopTimer(linkTimer) != false);
}
});
}
_createDirectiveFactories(DirectiveRef ref, nodeModule, node, nodesAttrsDirectives, nodeAttrs,
visibility) {
if (ref.type == TextMustache) {
nodeModule.bind(TextMustache, toFactory: (Injector injector) {
return new TextMustache(node, ref.value, injector.get(Interpolate),
injector.get(Scope), injector.get(FormatterMap));
});
} else if (ref.type == AttrMustache) {
if (nodesAttrsDirectives.isEmpty) {
nodeModule.bind(AttrMustache, toFactory: (Injector injector) {
var scope = injector.get(Scope);
var interpolate = injector.get(Interpolate);
for (var ref in nodesAttrsDirectives) {
new AttrMustache(nodeAttrs, ref.value, interpolate, scope,
injector.get(FormatterMap));
}
});
}
nodesAttrsDirectives.add(ref);
} else if (ref.annotation is Component) {
var factory;
var annotation = ref.annotation as Component;
if (annotation.useShadowDom == true) {
factory = _shadowDomComponentFactory;
} else if (annotation.useShadowDom == false) {
factory = _transcludingComponentFactory;
} else {
factory = _componentFactory;
}
nodeModule.bind(ref.type, toFactory: factory.call(node, ref), visibility: visibility);
} else {
nodeModule.bind(ref.type, visibility: visibility);
}
}
// Overridden in TemplateElementBinder
_registerViewFactory(node, parentInjector, nodeModule) {
nodeModule..bind(ViewPort, toValue: null)
..bind(ViewFactory, toValue: null)
..bind(BoundViewFactory, toValue: null);
}
Injector bind(View view, Injector parentInjector, dom.Node node) {
Injector nodeInjector;
Scope scope = parentInjector.get(Scope);
FormatterMap formatters = parentInjector.get(FormatterMap);
var nodeAttrs = node is dom.Element ? new NodeAttrs(node) : null;
ElementProbe probe;
var timerId;
assert((timerId = _perf.startTimer('ng.view.link.setUp', _html(node))) != false);
var directiveRefs = _usableDirectiveRefs;
try {
if (!hasDirectivesOrEvents) return parentInjector;
var nodesAttrsDirectives = [];
var nodeModule = new Module()
..bind(NgElement)
..bind(View, toValue: view)
..bind(dom.Element, toValue: node)
..bind(dom.Node, toValue: node)
..bind(NodeAttrs, toValue: nodeAttrs)
..bind(ElementProbe, toFactory: (_) => probe);
directiveRefs.forEach((DirectiveRef ref) {
Directive annotation = ref.annotation;
var visibility = ref.annotation.visibility;
if (ref.annotation is Controller) {
scope = scope.createChild(new PrototypeMap(scope.context));
nodeModule.bind(Scope, toValue: scope);
}
_createDirectiveFactories(ref, nodeModule, node, nodesAttrsDirectives, nodeAttrs,
visibility);
if (ref.annotation.module != null) {
nodeModule.install(ref.annotation.module());
}
});
_registerViewFactory(node, parentInjector, nodeModule);
nodeInjector = parentInjector.createChild([nodeModule]);
probe = _expando[node] = new ElementProbe(
parentInjector.get(ElementProbe), node, nodeInjector, scope);
} finally {
assert(_perf.stopTimer(timerId) != false);
}
_link(nodeInjector, probe, scope, nodeAttrs, formatters);
onEvents.forEach((event, value) {
view.registerEvent(EventHandler.attrNameToEventName(event));
});
return nodeInjector;
}
String toString() => "[ElementBinder decorators:$decorators]";
}
/**
* Private class used for managing controller.attach() calls
*/
class _TaskList {
var onDone;
final List _tasks = [];
bool isDone = false;
_TaskList(this.onDone) {
if (onDone == null) isDone = true;
}
int registerTask() {
if (isDone) return null; // Do nothing if there is nothing to do.
_tasks.add(false);
return _tasks.length - 1;
}
void completeTask(id) {
if (isDone) return;
_tasks[id] = true;
if (_tasks.every((a) => a)) {
onDone();
isDone = true;
}
}
doneRegistering() {
completeTask(registerTask());
}
}
// Used for walking the DOM
class ElementBinderTreeRef {
final int offsetIndex;
final ElementBinderTree subtree;
ElementBinderTreeRef(this.offsetIndex, this.subtree);
}
class ElementBinderTree {
final ElementBinder binder;
final List<ElementBinderTreeRef> subtrees;
ElementBinderTree(this.binder, this.subtrees);
}
class TaggedTextBinder {
final 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]";
}