@@ -7,39 +7,36 @@ part of angular.core.dom;
7
7
* BoundBlockFactory can only be used from within a specific Directive such
8
8
* as [NgRepeat] , but it can not be stored in a cache.
9
9
*
10
- * The BoundBlockFactory needs [Scope] to be created.
10
+ * The BoundBlockFactory needs a [Scope] to be created.
11
11
*/
12
- class BoundBlockFactory {
13
- BlockFactory blockFactory;
14
-
15
- Injector injector;
12
+ class BoundBlockFactory implements Function {
13
+ final BlockFactory blockFactory;
14
+ final Injector injector;
16
15
17
16
BoundBlockFactory (this .blockFactory, this .injector);
18
17
19
- Block call (Scope scope) {
20
- return blockFactory (injector.createChild ([new Module ()..value (Scope , scope)]));
21
- }
18
+ Block call (Scope scope) =>
19
+ blockFactory (injector.createChild ([new Module ()..value (Scope , scope)]));
22
20
}
23
21
24
22
/**
25
23
* BlockFactory is used to create new [Block] s. BlockFactory is created by the
26
24
* [Compiler] as a result of compiling a template.
27
25
*/
28
- class BlockFactory {
26
+ class BlockFactory implements Function {
29
27
final List directivePositions;
30
28
final List <dom.Node > templateElements;
31
29
final Profiler _perf;
32
30
final Expando _expando;
33
31
34
- BlockFactory (this .templateElements, this .directivePositions, this ._perf, this ._expando);
32
+ BlockFactory (this .templateElements, this .directivePositions, this ._perf,
33
+ this ._expando);
35
34
36
35
BoundBlockFactory bind (Injector injector) =>
37
- new BoundBlockFactory (this , injector);
36
+ new BoundBlockFactory (this , injector);
38
37
39
38
Block call (Injector injector, [List <dom.Node > elements]) {
40
- if (elements == null ) {
41
- elements = cloneElements (templateElements);
42
- }
39
+ if (elements == null ) elements = cloneElements (templateElements);
43
40
var timerId;
44
41
try {
45
42
assert ((timerId = _perf.startTimer ('ng.block' )) != false );
@@ -51,7 +48,8 @@ class BlockFactory {
51
48
}
52
49
}
53
50
54
- _link (Block block, List <dom.Node > nodeList, List directivePositions, Injector parentInjector) {
51
+ _link (Block block, List <dom.Node > nodeList, List directivePositions,
52
+ Injector parentInjector) {
55
53
var preRenderedIndexOffset = 0 ;
56
54
var directiveDefsByName = {};
57
55
@@ -93,8 +91,7 @@ class BlockFactory {
93
91
}
94
92
95
93
Injector _instantiateDirectives (Block block, Injector parentInjector,
96
- dom.Node node, List <DirectiveRef > directiveRefs,
97
- Parser parser) {
94
+ dom.Node node, List <DirectiveRef > directiveRefs, Parser parser) {
98
95
var timerId;
99
96
assert ((timerId = _perf.startTimer ('ng.block.link.setUp' , _html (node))) != false );
100
97
Injector nodeInjector;
@@ -105,17 +102,20 @@ class BlockFactory {
105
102
ElementProbe probe;
106
103
107
104
try {
108
- if (directiveRefs == null || directiveRefs.length == 0 ) return parentInjector;
109
- var nodeModule = new Module ();
105
+ if (directiveRefs == null || directiveRefs.length == 0 ) {
106
+ return parentInjector;
107
+ }
110
108
var blockHoleFactory = (_) => null ;
111
109
var blockFactory = (_) => null ;
112
110
var boundBlockFactory = (_) => null ;
113
111
var nodesAttrsDirectives = null ;
114
112
115
- nodeModule.value (Block , block);
116
- nodeModule.value (dom.Element , node);
117
- nodeModule.value (dom.Node , node);
118
- nodeModule.value (NodeAttrs , nodeAttrs);
113
+ var nodeModule = new Module ()
114
+ ..value (Block , block)
115
+ ..value (dom.Element , node)
116
+ ..value (dom.Node , node)
117
+ ..value (NodeAttrs , nodeAttrs);
118
+
119
119
directiveRefs.forEach ((DirectiveRef ref) {
120
120
NgAnnotation annotation = ref.annotation;
121
121
var visibility = _elementOnly;
@@ -163,20 +163,23 @@ class BlockFactory {
163
163
injector.get (dom.NodeTreeSanitizer ), _expando);
164
164
if (fctrs == null ) fctrs = new Map <Type , _ComponentFactory >();
165
165
fctrs[ref.type] = componentFactory;
166
- return componentFactory.call (injector, compiler, scope, blockCache, http, templateCache, directives);
166
+ return componentFactory.call (injector, compiler, scope, blockCache,
167
+ http, templateCache, directives);
167
168
}, visibility: visibility);
168
169
} else {
169
170
nodeModule.type (ref.type, visibility: visibility);
170
171
}
171
172
for (var publishType in ref.annotation.publishTypes) {
172
- nodeModule.factory (publishType, (Injector injector) => injector.get (ref.type), visibility: visibility);
173
+ nodeModule.factory (publishType, (Injector injector) =>
174
+ injector.get (ref.type), visibility: visibility);
173
175
}
174
176
if (annotation.children == NgAnnotation .TRANSCLUDE_CHILDREN ) {
175
177
// Currently, transclude is only supported for NgDirective.
176
178
assert (annotation is NgDirective );
177
179
blockHoleFactory = (_) => new BlockHole ([node]);
178
180
blockFactory = (_) => ref.blockFactory;
179
- boundBlockFactory = (Injector injector) => ref.blockFactory.bind (injector);
181
+ boundBlockFactory = (Injector injector) =>
182
+ ref.blockFactory.bind (injector);
180
183
}
181
184
});
182
185
nodeModule
@@ -198,7 +201,9 @@ class BlockFactory {
198
201
var controller = nodeInjector.get (ref.type);
199
202
probe.directives.add (controller);
200
203
assert ((linkMapTimer = _perf.startTimer ('ng.block.link.map' , ref.type)) != false );
201
- var shadowScope = (fctrs != null && fctrs.containsKey (ref.type)) ? fctrs[ref.type].shadowScope : null ;
204
+ var shadowScope = (fctrs != null && fctrs.containsKey (ref.type))
205
+ ? fctrs[ref.type].shadowScope
206
+ : null ;
202
207
if (ref.annotation is NgController ) {
203
208
scope.context[(ref.annotation as NgController ).publishAs] = controller;
204
209
} else if (ref.annotation is NgComponent ) {
@@ -252,21 +257,17 @@ class BlockFactory {
252
257
}
253
258
254
259
// DI visibility callback allowing node-local visibility.
255
-
256
260
static final Function _elementOnly = (Injector requesting, Injector defining) {
257
- if (requesting.name == _SHADOW ) {
258
- requesting = requesting.parent;
259
- }
261
+ if (requesting.name == _SHADOW ) requesting = requesting.parent;
260
262
return identical (requesting, defining);
261
263
};
262
264
263
265
// DI visibility callback allowing visibility from direct child into parent.
264
-
265
- static final Function _elementDirectChildren = (Injector requesting, Injector defining) {
266
- if (requesting.name == _SHADOW ) {
267
- requesting = requesting.parent;
268
- }
269
- return _elementOnly (requesting, defining) || identical (requesting.parent, defining);
266
+ static final Function _elementDirectChildren = (Injector requesting,
267
+ Injector defining) {
268
+ if (requesting.name == _SHADOW ) requesting = requesting.parent;
269
+ return _elementOnly (requesting, defining) ||
270
+ identical (requesting.parent, defining);
270
271
};
271
272
}
272
273
@@ -278,16 +279,11 @@ class BlockFactory {
278
279
@NgInjectableService ()
279
280
class BlockCache {
280
281
// _blockFactoryCache is unbounded
281
- Cache <String , BlockFactory > _blockFactoryCache =
282
- new LruCache <String , BlockFactory >(capacity: 0 );
283
-
284
- Http $http;
285
-
286
- TemplateCache $templateCache;
287
-
288
- Compiler compiler;
289
-
290
- dom.NodeTreeSanitizer treeSanitizer;
282
+ final _blockFactoryCache = new LruCache <String , BlockFactory >(capacity: 0 );
283
+ final Http $http;
284
+ final TemplateCache $templateCache;
285
+ final Compiler compiler;
286
+ final dom.NodeTreeSanitizer treeSanitizer;
291
287
292
288
BlockCache (this .$http, this .$templateCache, this .compiler, this .treeSanitizer);
293
289
@@ -313,7 +309,7 @@ class BlockCache {
313
309
* the shadowDom, fetching template, importing styles, setting up attribute
314
310
* mappings, publishing the controller, and compiling and caching the template.
315
311
*/
316
- class _ComponentFactory {
312
+ class _ComponentFactory implements Function {
317
313
318
314
final dom.Element element;
319
315
final Type type;
@@ -330,7 +326,9 @@ class _ComponentFactory {
330
326
_ComponentFactory (this .element, this .type, this .component, this .treeSanitizer,
331
327
this ._expando);
332
328
333
- dynamic call (Injector injector, Compiler compiler, Scope scope, BlockCache $blockCache, Http $http, TemplateCache $templateCache, DirectiveMap directives) {
329
+ dynamic call (Injector injector, Compiler compiler, Scope scope,
330
+ BlockCache $blockCache, Http $http, TemplateCache $templateCache,
331
+ DirectiveMap directives) {
334
332
this .compiler = compiler;
335
333
shadowDom = element.createShadowRoot ();
336
334
shadowDom.applyAuthorStyles = component.applyAuthorStyles;
@@ -346,30 +344,34 @@ class _ComponentFactory {
346
344
var cssUrls = component.cssUrls;
347
345
if (cssUrls.isNotEmpty) {
348
346
cssUrls.forEach ((css) => cssFutures.add (
349
- $http.getString (css, cache: $templateCache).catchError ((e) => '/*\n $e \n */\n ' )
350
- ) );
347
+ $http.getString (css, cache: $templateCache).catchError ((e) =>
348
+ '/*\n $e \n */\n ' )
349
+ ));
351
350
} else {
352
351
cssFutures.add ( new async .Future .value (null ) );
353
352
}
354
353
var blockFuture;
355
354
if (component.template != null ) {
356
- blockFuture = new async .Future .value ($blockCache.fromHtml (component.template, directives));
355
+ blockFuture = new async .Future .value ($blockCache.fromHtml (
356
+ component.template, directives));
357
357
} else if (component.templateUrl != null ) {
358
358
blockFuture = $blockCache.fromUrl (component.templateUrl, directives);
359
359
}
360
- TemplateLoader templateLoader = new TemplateLoader ( async .Future .wait (cssFutures).then ((Iterable <String > cssList) {
361
- if (cssList != null ) {
362
- var filteredCssList = cssList.where ((css) => css != null );
363
- shadowDom.setInnerHtml ('<style>${filteredCssList .join ('' )}</style>' , treeSanitizer: treeSanitizer);
364
- }
365
- if (blockFuture != null ) {
366
- return blockFuture.then ((BlockFactory blockFactory) {
367
- if (! shadowScope.isAttached) return shadowDom;
368
- return attachBlockToShadowDom (blockFactory);
369
- });
370
- }
371
- return shadowDom;
372
- }));
360
+ TemplateLoader templateLoader = new TemplateLoader (
361
+ async .Future .wait (cssFutures).then ((Iterable <String > cssList) {
362
+ if (cssList != null ) {
363
+ var filteredCssList = cssList.where ((css) => css != null );
364
+ shadowDom.setInnerHtml ('<style>${filteredCssList .join ('' )}</style>' ,
365
+ treeSanitizer: treeSanitizer);
366
+ }
367
+ if (blockFuture != null ) {
368
+ return blockFuture.then ((BlockFactory blockFactory) {
369
+ if (! shadowScope.isAttached) return shadowDom;
370
+ return attachBlockToShadowDom (blockFactory);
371
+ });
372
+ }
373
+ return shadowDom;
374
+ }));
373
375
controller = createShadowInjector (injector, templateLoader).get (type);
374
376
if (controller is NgShadowRootAware ) {
375
377
templateLoader.template.then ((_) {
@@ -423,21 +425,21 @@ String _html(obj) {
423
425
if (obj is String ) {
424
426
return obj;
425
427
} else if (obj is List ) {
426
- return (obj as List ).fold ( '' , (v, e) => v + _html (e));
428
+ return (obj as List ).map (( e) => _html (e)). join ( );
427
429
} else if (obj is dom.Element ) {
428
430
var text = (obj as dom.Element ).outerHtml;
429
431
return text.substring (0 , text.indexOf ('>' ) + 1 );
430
- } else {
431
- return obj.nodeName;
432
432
}
433
+ return obj.nodeName;
433
434
}
434
435
435
436
/**
436
- * [ElementProbe] is attached to each [Element] in the DOM. Its sole purpose is to
437
- * allow access to the [Injector] , [Scope] , and Directives for debugging and automated
438
- * test purposes. The information here is not used by Angular in any way.
437
+ * [ElementProbe] is attached to each [Element] in the DOM. Its sole purpose is
438
+ * to allow access to the [Injector] , [Scope] , and Directives for debugging and
439
+ * automated test purposes. The information here is not used by Angular in any
440
+ * way.
439
441
*
440
- * SEE : [ngInjector] , [ngScope] , [ngDirectives]
442
+ * see : [ngInjector] , [ngScope] , [ngDirectives]
441
443
*/
442
444
class ElementProbe {
443
445
final ElementProbe parent;
0 commit comments