1
1
import * as angular from 'angular' ;
2
2
3
- import { $InjectorLike , Ng1ViewConfig , StateObject , StateProvider } from '@uirouter/angularjs' ;
3
+
4
4
import { Component , ElementRef , Inject , Injector , Input , NgModule } from '@angular/core' ;
5
5
import { downgradeComponent , UpgradeModule } from '@angular/upgrade/static' ;
6
-
7
6
import { } from '@angular/upgrade' ;
8
7
8
+ import { StateObject , forEach , PathNode , Resolvable , StateRegistry , UIRouter , ViewConfig , ViewService } from '@uirouter/core' ;
9
9
import {
10
- _UIROUTER_SERVICE_PROVIDERS , applyModuleConfig , forEach , NATIVE_INJECTOR_TOKEN , ng2LazyLoadBuilder , Ng2ViewConfig ,
11
- Ng2ViewDeclaration , ParentUIViewInject , PathNode , Resolvable , StateRegistry , StatesModule , UIRouter ,
12
- UIROUTER_MODULE_TOKEN , UIROUTER_ROOT_MODULE , UIRouterModule , UIView , ViewConfig , ViewService
10
+ _UIROUTER_SERVICE_PROVIDERS , applyModuleConfig , NATIVE_INJECTOR_TOKEN , ng2LazyLoadBuilder , Ng2ViewConfig , UIView ,
11
+ Ng2ViewDeclaration , ParentUIViewInject , StatesModule , UIROUTER_MODULE_TOKEN , UIROUTER_ROOT_MODULE , UIRouterModule
13
12
} from '@uirouter/angular' ;
14
-
13
+ import { $InjectorLike , Ng1ViewConfig , StateProvider } from '@uirouter/angularjs' ;
15
14
import { UIRouterRx } from '@uirouter/rx' ;
16
15
17
16
/**
18
17
* Create a ng1 module for the ng1 half of the hybrid application to depend on.
19
18
*
20
19
* Example:
21
- * let myApp = angular.module('myApp', ['ui.router.upgrade']);
20
+ * const myApp = angular.module('myApp', ['ui.router.upgrade']);
22
21
*/
23
- export let upgradeModule = angular . module ( 'ui.router.upgrade' , [ 'ui.router' ] ) ;
24
- export let ng1InitModule = angular . module ( 'ui.router.init' ) ;
22
+ export const upgradeModule = angular . module ( 'ui.router.upgrade' , [ 'ui.router' ] ) ;
23
+ export const ng1InitModule = angular . module ( 'ui.router.init' ) ;
25
24
26
25
/**
27
26
* UIViewNgUpgrade is a component bridge from ng1 ui-view to ng2 ui-view
@@ -111,21 +110,21 @@ export class UIViewNgUpgrade {
111
110
112
111
// The ng2 ui-view component is inside this ui-view-ng-upgrade directive, which is inside the ng1 "host" ui-view.
113
112
// Both ui-views share the same "view context" information (the view's fqn and created-by-state context information)
114
- let ng1elem = angular . element ( ref . nativeElement ) . parent ( ) . parent ( ) ;
113
+ const ng1elem = angular . element ( ref . nativeElement ) . parent ( ) . parent ( ) ;
115
114
116
115
// Expose getters on PARENT_INJECT for context (creation state) and fqn (view address)
117
116
// These will be used by further nested UIView
118
117
Object . defineProperty ( parent , "context" , {
119
118
get : function ( ) {
120
- let data = ng1elem [ 'inheritedData' ] ( '$uiView' ) ;
119
+ const data = ng1elem [ 'inheritedData' ] ( '$uiView' ) ;
121
120
return ( data && data . $cfg ) ? data . $cfg . viewDecl . $context : registry . root ( ) ;
122
121
} ,
123
122
enumerable : true
124
123
} ) ;
125
124
126
125
Object . defineProperty ( parent , "fqn" , {
127
126
get : function ( ) {
128
- let data = ng1elem [ 'inheritedData' ] ( '$uiView' ) ;
127
+ const data = ng1elem [ 'inheritedData' ] ( '$uiView' ) ;
129
128
return ( data && data . $uiView ) ? data . $uiView . fqn : null ;
130
129
} ,
131
130
enumerable : true
@@ -139,7 +138,7 @@ export class UIViewNgUpgrade {
139
138
140
139
// Register the ng1 DI '$uiRouter' object as an ng2 Provider.
141
140
function uiRouterUpgradeFactory ( router : UIRouter , injector : Injector ) {
142
- let modules : StatesModule [ ] = injector . get ( UIROUTER_MODULE_TOKEN , [ ] ) ;
141
+ const modules : StatesModule [ ] = injector . get ( UIROUTER_MODULE_TOKEN , [ ] ) ;
143
142
modules . forEach ( module => applyModuleConfig ( router , injector , module ) ) ;
144
143
return router ;
145
144
}
@@ -182,20 +181,20 @@ upgradeModule.directive("uiViewNgUpgrade", <any> downgradeComponent({
182
181
} ) ) ;
183
182
184
183
upgradeModule . run ( [ '$injector' , ( ng1Injector : $InjectorLike ) => {
185
- let $uiRouter : UIRouter = ng1Injector . get ( '$uiRouter' ) ;
184
+ const $uiRouter : UIRouter = ng1Injector . get ( '$uiRouter' ) ;
186
185
new UIRouterRx ( $uiRouter ) ;
187
186
188
187
// Expose a merged ng1/ng2 injector as a Resolvable (on the root state).
189
188
// This mimics how ui-router-ng2 exposes the root ng2 Injector, but
190
189
// it retrieves from ng1 injector first, then ng2 injector if the token isn't found.
191
190
const mergedInjector = {
192
191
get : function ( token : any , ng2NotFoundValue ?: any ) {
193
- let ng2Injector = ng1Injector . get ( '$$angularInjector' ) ;
192
+ const ng2Injector = ng1Injector . get ( '$$angularInjector' ) ;
194
193
return ( ng1Injector . has ( token ) && ng1Injector . get ( token ) ) || ng2Injector . get ( token , ng2NotFoundValue )
195
194
}
196
195
} ;
197
196
198
- let ng2InjectorResolvable = Resolvable . fromData ( NATIVE_INJECTOR_TOKEN , mergedInjector ) ;
197
+ const ng2InjectorResolvable = Resolvable . fromData ( NATIVE_INJECTOR_TOKEN , mergedInjector ) ;
199
198
$uiRouter . stateRegistry . root ( ) . resolvables . push ( ng2InjectorResolvable ) ;
200
199
} ] ) ;
201
200
@@ -223,7 +222,7 @@ function applyHybridAdapter(ng2Injector: Injector) {
223
222
} ) ) ;
224
223
225
224
upgradeModule . run ( [ '$injector' , ( ng1Injector : $InjectorLike ) => {
226
- let $uiRouter : UIRouter = ng1Injector . get ( '$uiRouter' ) ;
225
+ const $uiRouter : UIRouter = ng1Injector . get ( '$uiRouter' ) ;
227
226
228
227
// Expose a merged ng1/ng2 injector as a Resolvable (on the root state).
229
228
// This mimics how @uirouter /angular exposes the root ng2 Injector, but
@@ -234,7 +233,7 @@ function applyHybridAdapter(ng2Injector: Injector) {
234
233
}
235
234
} ;
236
235
237
- let ng2InjectorResolvable = Resolvable . fromData ( NATIVE_INJECTOR_TOKEN , mergedInjector ) ;
236
+ const ng2InjectorResolvable = Resolvable . fromData ( NATIVE_INJECTOR_TOKEN , mergedInjector ) ;
238
237
$uiRouter . stateRegistry . root ( ) . resolvables . push ( ng2InjectorResolvable ) ;
239
238
} ] ) ;
240
239
@@ -243,7 +242,7 @@ function applyHybridAdapter(ng2Injector: Injector) {
243
242
} ] ) ;
244
243
245
244
upgradeModule . config ( [ '$uiRouterProvider' , ( $uiRouterProvider : UIRouter ) => {
246
- let registry = $uiRouterProvider . stateRegistry ;
245
+ const registry = $uiRouterProvider . stateRegistry ;
247
246
248
247
/** Applies the `UIRouterRx` plugin for observable states/params */
249
248
$uiRouterProvider . plugin ( UIRouterRx ) ;
@@ -265,7 +264,7 @@ function applyHybridAdapter(ng2Injector: Injector) {
265
264
* which that provides a ng1 -> ng2 boundary in the component tree.
266
265
*/
267
266
registry . decorator ( 'views' , function ( state : StateObject , parentFn : Function ) {
268
- let views = parentFn ( state ) ;
267
+ const views = parentFn ( state ) ;
269
268
270
269
forEach ( views , ( viewDecl : any , viewName : string ) => {
271
270
if ( viewDecl . $type === 'ng1-to-ng2' || isNg2ComponentClass ( viewDecl . component ) ) {
@@ -290,8 +289,8 @@ function applyHybridAdapter(ng2Injector: Injector) {
290
289
// Register a ViewConfig factory for views of type `ng1-to-ng2`.
291
290
// Returns both an ng1 config and an ng2 config allowing either ng1 or ng2 ui-view components to be targeted.
292
291
$view . _pluginapi . _viewConfigFactory ( 'ng1-to-ng2' , ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => {
293
- let ng1ViewConfig : ViewConfig = < any > new Ng1ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng1' } ) , $templateFactory ) ;
294
- let ng2ViewConfig : ViewConfig = < any > new Ng2ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng2' } ) ) ;
292
+ const ng1ViewConfig : ViewConfig = < any > new Ng1ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng1' } ) , $templateFactory ) ;
293
+ const ng2ViewConfig : ViewConfig = < any > new Ng2ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng2' } ) ) ;
295
294
296
295
return [ ng2ViewConfig , ng1ViewConfig ] ;
297
296
} ) ;
@@ -308,8 +307,8 @@ function applyHybridAdapter(ng2Injector: Injector) {
308
307
* which that provides a ng1 -> ng2 boundary in the component tree.
309
308
*/
310
309
upgradeModule . config ( [ '$stateProvider' , ( $stateProvider : StateProvider ) => {
311
- $stateProvider . decorator ( 'views' , function ( state : State , parentFn : Function ) {
312
- let views = parentFn ( state ) ;
310
+ $stateProvider . decorator ( 'views' , function ( state : StateObject , parentFn : Function ) {
311
+ const views = parentFn ( state ) ;
313
312
314
313
forEach ( views , ( viewDecl : any , viewName : string ) => {
315
314
if ( viewDecl . $type === 'ng1-to-ng2' || isNg2ComponentClass ( viewDecl . component ) ) {
@@ -327,19 +326,19 @@ upgradeModule.config(['$stateProvider', ($stateProvider: StateProvider) => {
327
326
328
327
// UI-Router ViewConfig factories take a view declaration object from a state.views: { foo: <ViewDeclaration> }
329
328
// and return a runtime config object (a ViewConfig)
330
- upgradeModule . run ( [ '$view' , ( $view : ViewService ) => {
329
+ upgradeModule . run ( [ '$view' , '$templateFactory' , ( $view : ViewService , $templateFactory : any ) => {
331
330
// Register a ViewConfig factory for views of type `ng2`
332
- $view . viewConfigFactory ( 'ng2' , ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => new Ng2ViewConfig ( path , config ) ) ;
331
+ $view . _pluginapi . _viewConfigFactory ( 'ng2' , ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => new Ng2ViewConfig ( path , config ) ) ;
333
332
334
333
// Register a ViewConfig factory for views of type `ng1-to-ng2`.
335
334
// Returns both an ng1 config and an ng2 config allowing either ng1 or ng2 ui-view components to be targeted.
336
- $view . viewConfigFactory ( 'ng1-to-ng2' , ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => {
337
- var ng1ViewConfig : ViewConfig = < any > new Ng1ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng1' } ) ) ;
338
- var ng2ViewConfig : ViewConfig = < any > new Ng2ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng2' } ) ) ;
335
+ $view . _pluginapi . _viewConfigFactory ( 'ng1-to-ng2' , ( path : PathNode [ ] , config : Ng2ViewDeclaration ) => {
336
+ const ng1ViewConfig : ViewConfig = < any > new Ng1ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng1' } ) , $templateFactory ) ;
337
+ const ng2ViewConfig : ViewConfig = < any > new Ng2ViewConfig ( < any > path , < any > Object . assign ( { } , config , { $type : 'ng2' } ) ) ;
339
338
340
339
return [ ng2ViewConfig , ng1ViewConfig ] ;
341
340
} ) ;
342
- } ] )
341
+ } ] ) ;
343
342
344
343
/** Predicate fn that returns true if an object is a NG2 Component Class */
345
344
export function isNg2ComponentClass ( def : any ) {
0 commit comments