@@ -4,19 +4,36 @@ import { Component, ElementRef, Inject, Injector, Input, ModuleWithProviders, Ng
4
4
import { downgradeComponent , UpgradeModule } from '@angular/upgrade/static' ;
5
5
6
6
import {
7
- StateObject , forEach , PathNode , Resolvable , StateRegistry , UIRouter , ViewConfig , ViewService
7
+ StateObject ,
8
+ forEach ,
9
+ PathNode ,
10
+ Resolvable ,
11
+ StateRegistry ,
12
+ UIRouter ,
13
+ ViewConfig ,
14
+ ViewService ,
8
15
} from '@uirouter/core' ;
9
16
10
17
import {
11
- applyModuleConfig , NATIVE_INJECTOR_TOKEN , ng2LazyLoadBuilder , Ng2ViewConfig , UIView , _UIROUTER_SERVICE_PROVIDERS ,
12
- Ng2ViewDeclaration , ParentUIViewInject , StatesModule , UIROUTER_MODULE_TOKEN , UIROUTER_ROOT_MODULE , UIRouterModule ,
18
+ applyModuleConfig ,
19
+ NATIVE_INJECTOR_TOKEN ,
20
+ ng2LazyLoadBuilder ,
21
+ Ng2ViewConfig ,
22
+ UIView ,
23
+ _UIROUTER_SERVICE_PROVIDERS ,
24
+ Ng2ViewDeclaration ,
25
+ ParentUIViewInject ,
26
+ StatesModule ,
27
+ UIROUTER_MODULE_TOKEN ,
28
+ UIROUTER_ROOT_MODULE ,
29
+ UIRouterModule ,
13
30
makeChildProviders ,
14
31
} from '@uirouter/angular' ;
15
32
16
33
import { $InjectorLike , Ng1ViewConfig } from '@uirouter/angularjs' ;
17
34
18
35
import { UIRouterRx } from '@uirouter/rx' ;
19
- import { NgHybridStatesModule } from './interfaces' ;
36
+ import { NgHybridStatesModule } from './interfaces' ;
20
37
21
38
/**
22
39
* Create a ng1 module for the ng1 half of the hybrid application to depend on.
@@ -26,7 +43,9 @@ import { NgHybridStatesModule } from './interfaces';
26
43
*/
27
44
export const upgradeModule = angular . module ( 'ui.router.upgrade' , [ 'ui.router' ] ) ;
28
45
29
- export function objectFactory ( ) { return { } ; }
46
+ export function objectFactory ( ) {
47
+ return { } ;
48
+ }
30
49
31
50
/**
32
51
* UIViewNgUpgrade is a component bridge from ng1 ui-view to ng2 ui-view
@@ -99,41 +118,44 @@ export function objectFactory() { return {}; }
99
118
template : `<ui-view [name]="name"></ui-view>` ,
100
119
// provide a blank object as PARENT_INJECT.
101
120
// The component will add property getters when it is constructed.
102
- viewProviders : [ { provide : UIView . PARENT_INJECT , useFactory : objectFactory } ] ,
121
+ viewProviders : [ { provide : UIView . PARENT_INJECT , useFactory : objectFactory } ] ,
103
122
} )
104
123
export class UIViewNgUpgrade {
105
124
// The ui-view's name (or '$default')
106
125
@Input ( ) name : string ;
107
126
108
127
constructor (
109
- ref : ElementRef ,
110
- @Inject ( UIView . PARENT_INJECT ) parent : ParentUIViewInject ,
111
- registry : StateRegistry // access the root state
128
+ ref : ElementRef ,
129
+ @Inject ( UIView . PARENT_INJECT ) parent : ParentUIViewInject ,
130
+ registry : StateRegistry , // access the root state
112
131
) {
113
132
// From the ui-view-ng-upgrade component's element ref, walk up the DOM two elements...
114
133
// There will first be an ng1 ui-view which hosts this element, and then that ui-view's parent element.
115
134
// That (parent) element has access to the proper "parent viewcontext"
116
135
117
136
// The ng2 ui-view component is inside this ui-view-ng-upgrade directive, which is inside the ng1 "host" ui-view.
118
137
// Both ui-views share the same "view context" information (the view's fqn and created-by-state context information)
119
- const ng1elem = angular . element ( ref . nativeElement ) . parent ( ) . parent ( ) ;
138
+ const ng1elem = angular
139
+ . element ( ref . nativeElement )
140
+ . parent ( )
141
+ . parent ( ) ;
120
142
121
143
// Expose getters on PARENT_INJECT for context (creation state) and fqn (view address)
122
144
// These will be used by further nested UIView
123
- Object . defineProperty ( parent , " context" , {
145
+ Object . defineProperty ( parent , ' context' , {
124
146
get : function ( ) {
125
147
const data = ng1elem [ 'inheritedData' ] ( '$uiView' ) ;
126
- return ( data && data . $cfg ) ? data . $cfg . viewDecl . $context : registry . root ( ) ;
148
+ return data && data . $cfg ? data . $cfg . viewDecl . $context : registry . root ( ) ;
127
149
} ,
128
- enumerable : true
150
+ enumerable : true ,
129
151
} ) ;
130
152
131
- Object . defineProperty ( parent , " fqn" , {
153
+ Object . defineProperty ( parent , ' fqn' , {
132
154
get : function ( ) {
133
155
const data = ng1elem [ 'inheritedData' ] ( '$uiView' ) ;
134
- return ( data && data . $uiView ) ? data . $uiView . fqn : null ;
156
+ return data && data . $uiView ? data . $uiView . fqn : null ;
135
157
} ,
136
- enumerable : true
158
+ enumerable : true ,
137
159
} ) ;
138
160
}
139
161
}
@@ -165,7 +187,7 @@ export function getParentUIViewInject(r: StateRegistry): ParentUIViewInject {
165
187
declarations : [ UIViewNgUpgrade ] ,
166
188
providers : [
167
189
// @uirouter /angular code will use the ng1 $uiRouter instance instead of creating its own.
168
- { provide : '$uiRouter' , useFactory : getUIRouter , deps : [ '$injector' ] } ,
190
+ { provide : '$uiRouter' , useFactory : getUIRouter , deps : [ '$injector' ] } ,
169
191
170
192
{ provide : UIRouter , useFactory : uiRouterUpgradeFactory , deps : [ '$uiRouter' , Injector ] } ,
171
193
@@ -175,11 +197,10 @@ export function getParentUIViewInject(r: StateRegistry): ParentUIViewInject {
175
197
176
198
..._UIROUTER_SERVICE_PROVIDERS ,
177
199
] ,
178
- entryComponents : [
179
- UIViewNgUpgrade
180
- ] ,
181
- exports : [ UIViewNgUpgrade , UIRouterModule ]
182
- } ) export class UIRouterUpgradeModule {
200
+ entryComponents : [ UIViewNgUpgrade ] ,
201
+ exports : [ UIViewNgUpgrade , UIRouterModule ] ,
202
+ } )
203
+ export class UIRouterUpgradeModule {
183
204
static forChild ( module : NgHybridStatesModule = { } ) : ModuleWithProviders {
184
205
return {
185
206
ngModule : UIRouterUpgradeModule ,
@@ -191,35 +212,41 @@ export function getParentUIViewInject(r: StateRegistry): ParentUIViewInject {
191
212
// Downgrade the UIViewNgUpgrade ng2 Component to an ng1 directive.
192
213
// The directive is used in a (generated) view template by the (host) ng1 ui-router,
193
214
// whenever it finds a view configured with a `component: <Ng2ComponentClass>`
194
- upgradeModule . directive ( " uiViewNgUpgrade" , < any > downgradeComponent ( {
215
+ upgradeModule . directive ( ' uiViewNgUpgrade' , < any > downgradeComponent ( {
195
216
component : UIViewNgUpgrade ,
196
- inputs : [ 'name' ]
217
+ inputs : [ 'name' ] ,
197
218
} ) ) ;
198
219
199
- upgradeModule . run ( [ '$injector' , ( ng1Injector : $InjectorLike ) => {
200
- const $uiRouter : UIRouter = ng1Injector . get ( '$uiRouter' ) ;
201
-
202
- /** Add support for observable state and param changes */
203
- $uiRouter . plugin ( UIRouterRx ) ;
204
-
205
- // Expose a merged ng1/ng2 injector as a Resolvable (on the root state).
206
- // This mimics how ui-router-ng2 exposes the root ng2 Injector, but
207
- // it retrieves from ng1 injector first, then ng2 injector if the token isn't found.
208
- const mergedInjector = {
209
- get : function ( token : any , ng2NotFoundValue ?: any ) {
210
- const ng2Injector = ng1Injector . get ( '$$angularInjector' ) ;
211
- return ( ng1Injector . has ( token ) && ng1Injector . get ( token ) ) || ng2Injector . get ( token , ng2NotFoundValue )
212
- }
213
- } ;
220
+ upgradeModule . run ( [
221
+ '$injector' ,
222
+ ( ng1Injector : $InjectorLike ) => {
223
+ const $uiRouter : UIRouter = ng1Injector . get ( '$uiRouter' ) ;
224
+
225
+ /** Add support for observable state and param changes */
226
+ $uiRouter . plugin ( UIRouterRx ) ;
227
+
228
+ // Expose a merged ng1/ng2 injector as a Resolvable (on the root state).
229
+ // This mimics how ui-router-ng2 exposes the root ng2 Injector, but
230
+ // it retrieves from ng1 injector first, then ng2 injector if the token isn't found.
231
+ const mergedInjector = {
232
+ get : function ( token : any , ng2NotFoundValue ?: any ) {
233
+ const ng2Injector = ng1Injector . get ( '$$angularInjector' ) ;
234
+ return ( ng1Injector . has ( token ) && ng1Injector . get ( token ) ) || ng2Injector . get ( token , ng2NotFoundValue ) ;
235
+ } ,
236
+ } ;
214
237
215
- const ng2InjectorResolvable = Resolvable . fromData ( NATIVE_INJECTOR_TOKEN , mergedInjector ) ;
216
- $uiRouter . stateRegistry . root ( ) . resolvables . push ( ng2InjectorResolvable ) ;
217
- } ] ) ;
238
+ const ng2InjectorResolvable = Resolvable . fromData ( NATIVE_INJECTOR_TOKEN , mergedInjector ) ;
239
+ $uiRouter . stateRegistry . root ( ) . resolvables . push ( ng2InjectorResolvable ) ;
240
+ } ,
241
+ ] ) ;
218
242
219
243
/** Adds support for `loadChildren`: Angular NgModule lazy loading via @gntools/webpack */
220
- upgradeModule . config ( [ '$stateRegistryProvider' , ( $stateRegistry : StateRegistry ) => {
221
- $stateRegistry . decorator ( 'lazyLoad' , ng2LazyLoadBuilder ) ;
222
- } ] ) ;
244
+ upgradeModule . config ( [
245
+ '$stateRegistryProvider' ,
246
+ ( $stateRegistry : StateRegistry ) => {
247
+ $stateRegistry . decorator ( 'lazyLoad' , ng2LazyLoadBuilder ) ;
248
+ } ,
249
+ ] ) ;
223
250
224
251
/**
225
252
* Define a stateProvider `views` builder decorator.
@@ -230,36 +257,53 @@ upgradeModule.config(['$stateRegistryProvider', ($stateRegistry: StateRegistry)
230
257
* In place of the template provider, it simply puts a <ui-view-ng-upgrade/> component
231
258
* which that provides a ng1 -> ng2 boundary in the component tree.
232
259
*/
233
- upgradeModule . config ( [ '$stateRegistryProvider' , ( $stateRegistry : StateRegistry ) => {
234
- $stateRegistry . decorator ( 'views' , function ( state : StateObject , parentFn : Function ) {
235
- const views = parentFn ( state ) ;
236
-
237
- forEach ( views , ( viewDecl : any , viewName : string ) => {
238
- if ( viewDecl . $type === 'ng1-to-ng2' || typeof viewDecl . component === 'function' ) {
239
- // Update the view config.
240
- // Override default ng1 `component:` behavior (of defining a templateProvider)
241
- // with a <ui-view-ng-upgrade> adapter directive template
242
- viewDecl . $type = "ng1-to-ng2" ;
243
- viewDecl . templateProvider = null ;
244
- viewDecl . template = `<ui-view-ng-upgrade name='${ viewDecl . $uiViewName } '></ui-view-ng-upgrade>` ;
245
- }
260
+ upgradeModule . config ( [
261
+ '$stateRegistryProvider' ,
262
+ ( $stateRegistry : StateRegistry ) => {
263
+ $stateRegistry . decorator ( 'views' , function ( state : StateObject , parentFn : Function ) {
264
+ const views = parentFn ( state ) ;
265
+
266
+ forEach ( views , ( viewDecl : any , viewName : string ) => {
267
+ if ( viewDecl . $type === 'ng1-to-ng2' || typeof viewDecl . component === 'function' ) {
268
+ // Update the view config.
269
+ // Override default ng1 `component:` behavior (of defining a templateProvider)
270
+ // with a <ui-view-ng-upgrade> adapter directive template
271
+ viewDecl . $type = 'ng1-to-ng2' ;
272
+ viewDecl . templateProvider = null ;
273
+ viewDecl . template = `<ui-view-ng-upgrade name='${ viewDecl . $uiViewName } '></ui-view-ng-upgrade>` ;
274
+ }
275
+ } ) ;
276
+ return views ;
246
277
} ) ;
247
- return views ;
248
- } ) ;
249
- } ] ) ;
278
+ } ,
279
+ ] ) ;
250
280
251
281
// UI-Router ViewConfig factories take a view declaration object from a state.views: { foo: <ViewDeclaration> }
252
282
// and return a runtime config object (a ViewConfig)
253
- upgradeModule . run ( [ '$view' , '$templateFactory' , ( $view : ViewService , $templateFactory : any ) => {
254
- // Register a ViewConfig factory for views of type `ng2`
255
- $view . _pluginapi . _viewConfigFactory ( 'ng2' , ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => new Ng2ViewConfig ( path , config ) ) ;
256
-
257
- // Register a ViewConfig factory for views of type `ng1-to-ng2`.
258
- // Returns both an ng1 config and an ng2 config allowing either ng1 or ng2 ui-view components to be targeted.
259
- $view . _pluginapi . _viewConfigFactory ( 'ng1-to-ng2' , ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => {
260
- const ng1ViewConfig : ViewConfig = < any > new Ng1ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng1' } ) , $templateFactory ) ;
261
- const ng2ViewConfig : ViewConfig = < any > new Ng2ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng2' } ) ) ;
262
-
263
- return [ ng2ViewConfig , ng1ViewConfig ] ;
264
- } ) ;
265
- } ] ) ;
283
+ upgradeModule . run ( [
284
+ '$view' ,
285
+ '$templateFactory' ,
286
+ ( $view : ViewService , $templateFactory : any ) => {
287
+ // Register a ViewConfig factory for views of type `ng2`
288
+ $view . _pluginapi . _viewConfigFactory (
289
+ 'ng2' ,
290
+ ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => new Ng2ViewConfig ( path , config ) ,
291
+ ) ;
292
+
293
+ // Register a ViewConfig factory for views of type `ng1-to-ng2`.
294
+ // Returns both an ng1 config and an ng2 config allowing either ng1 or ng2 ui-view components to be targeted.
295
+ $view . _pluginapi . _viewConfigFactory ( 'ng1-to-ng2' , ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => {
296
+ const ng1ViewConfig : ViewConfig = < any > new Ng1ViewConfig (
297
+ < any > path ,
298
+ < any > Object . assign ( { } , config , { $type : 'ng1' } ) ,
299
+ $templateFactory ,
300
+ ) ;
301
+ const ng2ViewConfig : ViewConfig = < any > new Ng2ViewConfig (
302
+ < any > path ,
303
+ < any > Object . assign ( { } , config , { $type : 'ng2' } ) ,
304
+ ) ;
305
+
306
+ return [ ng2ViewConfig , ng1ViewConfig ] ;
307
+ } ) ;
308
+ } ,
309
+ ] ) ;
0 commit comments