2
2
/** */
3
3
import {
4
4
Component , ComponentFactoryResolver , ViewContainerRef , Input , ComponentRef , Type , ReflectiveInjector , ViewChild ,
5
- Injector , Inject
5
+ Injector , Inject , ComponentFactory
6
6
} from '@angular/core' ;
7
- import { ɵReflectorReader as ReflectorReader } from '@angular/core' ;
7
+
8
8
import {
9
9
UIRouter , isFunction , Transition , parse , TransitionHookFn , StateDeclaration , inArray , trace , ViewContext , ViewConfig ,
10
10
ActiveUIView , ResolveContext , NATIVE_INJECTOR_TOKEN , flattenR
@@ -35,29 +35,8 @@ interface InputMapping {
35
35
*
36
36
* @internalapi
37
37
*/
38
- const ng2ComponentInputs = ( reflector : ReflectorReader , ng2CompClass : Type < any > , component : any ) => {
39
- /** Get "@Input('foo') _foo" inputs */
40
- let props = reflector . propMetadata ( ng2CompClass ) ;
41
- let _props = Object . keys ( props || { } )
42
- // -> [ { key: string, anno: annotations[] } ] tuples
43
- . map ( key => ( { key, annoArr : props [ key ] } ) )
44
- // -> flattened to [ { key: string, anno: annotation } ] tuples
45
- . reduce ( ( acc , tuple ) => acc . concat ( tuple . annoArr . map ( anno => ( { key : tuple . key , anno } ) ) ) , [ ] )
46
- // Only Inputs
47
- . filter ( tuple => tuple . anno instanceof Input )
48
- // If they have a bindingPropertyName, i.e. "@Input('foo') _foo", then foo, else _foo
49
- . map ( tuple => ( { token : tuple . anno . bindingPropertyName || tuple . key , prop : tuple . key } ) ) ;
50
-
51
- /** Get "inputs: ['foo']" inputs */
52
- let inputs = reflector . annotations ( ng2CompClass )
53
- // Find the ComponentMetadata class annotation
54
- . filter ( x => x instanceof Component && ! ! x . inputs )
55
- // Get the .inputs string array
56
- . map ( x => x . inputs )
57
- . reduce ( flattenR , [ ] )
58
- . map ( input => ( { token : input , prop : input } ) ) ;
59
-
60
- return _props . concat ( inputs ) as InputMapping [ ] ;
38
+ const ng2ComponentInputs = ( factory : ComponentFactory < any > ) : InputMapping [ ] => {
39
+ return factory . inputs . map ( input => ( { prop : input . propName , token : input . templateName } ) ) ;
61
40
} ;
62
41
63
42
/**
@@ -149,7 +128,6 @@ export class UIView {
149
128
public router : UIRouter ,
150
129
@Inject ( UIView . PARENT_INJECT ) parent ,
151
130
public viewContainerRef : ViewContainerRef ,
152
- private reflector : ReflectorReader
153
131
) {
154
132
this . parent = parent ;
155
133
}
@@ -189,7 +167,7 @@ export class UIView {
189
167
const state : StateDeclaration = parse ( "uiViewData.config.viewDecl.$context.self" ) ( this ) ;
190
168
191
169
if ( trans . exiting ( ) . indexOf ( state ) !== - 1 ) {
192
- trans . onStart ( { } , function ( trans ) {
170
+ trans . onStart ( { } , function ( ) {
193
171
return uiCanExitFn . call ( instance , trans ) ;
194
172
} ) ;
195
173
}
@@ -244,7 +222,7 @@ export class UIView {
244
222
this . componentRef = this . componentTarget . createComponent ( compFactory , undefined , componentInjector ) ;
245
223
246
224
// Wire resolves to @Input ()s
247
- this . applyInputBindings ( this . componentRef , context , componentClass ) ;
225
+ this . applyInputBindings ( compFactory , this . componentRef , context , componentClass ) ;
248
226
}
249
227
250
228
/**
@@ -262,7 +240,7 @@ export class UIView {
262
240
let resolvables = context . getTokens ( ) . map ( token => context . getResolvable ( token ) ) . filter ( r => r . resolved ) ;
263
241
let newProviders = resolvables . map ( r => ( { provide : r . token , useValue : r . data } ) ) ;
264
242
265
- var parentInject = { context : this . uiViewData . config . viewDecl . $context , fqn : this . uiViewData . fqn } ;
243
+ let parentInject = { context : this . uiViewData . config . viewDecl . $context , fqn : this . uiViewData . fqn } ;
266
244
newProviders . push ( { provide : UIView . PARENT_INJECT , useValue : parentInject } ) ;
267
245
268
246
let parentComponentInjector = this . viewContainerRef . injector ;
@@ -278,16 +256,24 @@ export class UIView {
278
256
* Finds component inputs which match resolves (by name) and sets the input value
279
257
* to the resolve data.
280
258
*/
281
- applyInputBindings ( ref : ComponentRef < any > , context : ResolveContext , componentClass ) {
259
+ applyInputBindings ( factory : ComponentFactory < any > , ref : ComponentRef < any > , context : ResolveContext , componentClass ) {
282
260
const component = ref . instance ;
283
261
const bindings = this . uiViewData . config . viewDecl [ 'bindings' ] || { } ;
284
262
const explicitBoundProps = Object . keys ( bindings ) ;
285
263
286
- // Supply resolve data to matching @Input ('prop') or inputs: ['prop']
264
+ // Returns the actual component property for a renamed an input renamed using `@Input('foo') _foo`.
265
+ // return the `_foo` property
266
+ const renamedInputProp = ( prop : string ) => {
267
+ const input = factory . inputs . find ( i => i . templateName === prop ) ;
268
+ return input && input . propName || prop ;
269
+ } ;
270
+
271
+ // Supply resolve data to component as specified in the state's `bindings: {}`
287
272
const explicitInputTuples = explicitBoundProps
288
- . reduce ( ( acc , key ) => acc . concat ( [ { prop : key , token : bindings [ key ] } ] ) , [ ] ) ;
273
+ . reduce ( ( acc , key ) => acc . concat ( [ { prop : renamedInputProp ( key ) , token : bindings [ key ] } ] ) , [ ] ) ;
289
274
290
- const implicitInputTuples = ng2ComponentInputs ( this . reflector , componentClass , component )
275
+ // Supply resolve data to matching @Input ('prop') or inputs: ['prop']
276
+ const implicitInputTuples = ng2ComponentInputs ( factory )
291
277
. filter ( tuple => ! inArray ( explicitBoundProps , tuple . prop ) ) ;
292
278
293
279
const addResolvable = ( tuple : InputMapping ) => ( {
@@ -298,7 +284,7 @@ export class UIView {
298
284
explicitInputTuples . concat ( implicitInputTuples )
299
285
. map ( addResolvable )
300
286
. filter ( tuple => tuple . resolvable && tuple . resolvable . resolved )
301
- . forEach ( tuple => { component [ tuple . prop ] = tuple . resolvable . data } ) ;
287
+ . forEach ( tuple => { component [ tuple . prop ] = tuple . resolvable . data ; } ) ;
302
288
303
289
// Initiate change detection for the newly created component
304
290
ref . changeDetectorRef . detectChanges ( ) ;
0 commit comments