1
- import { List , ListWrapper } from 'angular2/src/facade/collection' ;
1
+ import { ListWrapper , isListLikeIterable , StringMapWrapper } from 'angular2/src/facade/collection' ;
2
2
import { isBlank , isPresent , BaseException , CONST } from 'angular2/src/facade/lang' ;
3
3
import { Pipe , PipeFactory } from './pipe' ;
4
4
import { Injectable } from 'angular2/src/di/decorators' ;
@@ -7,7 +7,25 @@ import {ChangeDetectorRef} from '../change_detector_ref';
7
7
@Injectable ( )
8
8
@CONST ( )
9
9
export class Pipes {
10
- constructor ( public config ) { }
10
+ /**
11
+ * Map of {@link Pipe} names to {@link PipeFactory} lists used to configure the
12
+ * {@link Pipes} registry.
13
+ *
14
+ * #Example
15
+ *
16
+ * ```
17
+ * var pipesConfig = {
18
+ * 'json': [jsonPipeFactory]
19
+ * }
20
+ * @Component ({
21
+ * viewInjector: [
22
+ * bind(Pipes).toValue(new Pipes(pipesConfig))
23
+ * ]
24
+ * })
25
+ * ```
26
+ */
27
+ config : StringMap < string , PipeFactory [ ] > ;
28
+ constructor ( config : StringMap < string , PipeFactory [ ] > ) { this . config = config ; }
11
29
12
30
get ( type : string , obj , cdRef ?: ChangeDetectorRef , existingPipe ?: Pipe ) : Pipe {
13
31
if ( isPresent ( existingPipe ) && existingPipe . supports ( obj ) ) return existingPipe ;
@@ -20,6 +38,56 @@ export class Pipes {
20
38
return factory . create ( cdRef ) ;
21
39
}
22
40
41
+ /**
42
+ * Takes a {@link Pipes} config object and returns a factory used to append the
43
+ * provided config with an existing {@link Pipes} instance to return a new
44
+ * {@link Pipes} instance.
45
+ *
46
+ * If the provided config contains a key that is not yet present in the
47
+ * inherited {@link Pipes}' config, a new {@link PipeFactory} list will be created
48
+ * for that key. Otherwise, the provided config will be merged with the inherited
49
+ * {@link Pipes} instance by appending pipes to their respective keys, without mutating
50
+ * the inherited {@link Pipes}.
51
+ *
52
+ * The following example shows how to append a new {@link PipeFactory} to the
53
+ * existing list of `async` factories, which will only be applied to the injector
54
+ * for this component and its children. This step is all that's required to make a new
55
+ * pipe available to this component's template.
56
+ *
57
+ * # Example
58
+ *
59
+ * ```
60
+ * @Component ({
61
+ * viewInjector: [
62
+ * bind(Pipes).toFactory(Pipes.append({
63
+ * async: [newAsyncPipe]
64
+ * })
65
+ * ]
66
+ * })
67
+ * ```
68
+ */
69
+ static append ( config ) {
70
+ return ( pipes : Pipes ) : Pipes => {
71
+ var mergedConfig : StringMap < string , PipeFactory [ ] > = < StringMap < string , PipeFactory [ ] > > { } ;
72
+
73
+ // Manual deep copy of existing Pipes config,
74
+ // so that lists of PipeFactories don't get mutated.
75
+ StringMapWrapper . forEach ( pipes . config , ( v : PipeFactory [ ] , k : string ) => {
76
+ var localPipeList : PipeFactory [ ] = mergedConfig [ k ] = [ ] ;
77
+ v . forEach ( ( p : PipeFactory ) => { localPipeList . push ( p ) ; } ) ;
78
+ } ) ;
79
+
80
+ StringMapWrapper . forEach ( config , ( v : PipeFactory [ ] , k : string ) => {
81
+ if ( isListLikeIterable ( mergedConfig [ k ] ) ) {
82
+ mergedConfig [ k ] = ListWrapper . concat ( mergedConfig [ k ] , config [ k ] ) ;
83
+ } else {
84
+ mergedConfig [ k ] = config [ k ] ;
85
+ }
86
+ } ) ;
87
+ return new Pipes ( mergedConfig ) ;
88
+ } ;
89
+ }
90
+
23
91
private _getListOfFactories ( type : string , obj : any ) : PipeFactory [ ] {
24
92
var listOfFactories = this . config [ type ] ;
25
93
if ( isBlank ( listOfFactories ) ) {
0 commit comments