46
46
*
47
47
* @preferred @module ng2
48
48
*/ /** */
49
- import { Injector } from "@angular/core" ;
49
+ import { Injector , OpaqueToken } from "@angular/core" ;
50
50
import { UIRouter } from "../router" ;
51
51
import { PathNode } from "../path/node" ;
52
52
import { StateRegistry } from "../state/stateRegistry" ;
@@ -66,9 +66,31 @@ import {ProviderLike} from "../state/interface";
66
66
import { Resolvable } from "../resolve/resolvable" ;
67
67
import { ngModuleResolvablesBuilder } from "./statebuilders/lazyLoadNgModuleResolvable" ;
68
68
69
- let uiRouterFactory = ( routerConfig : UIRouterConfig , location : UIRouterLocation , injector : Injector ) => {
69
+ export const NG1_UIROUTER_TOKEN = new OpaqueToken ( "$uiRouter" ) ;
70
+
71
+ /**
72
+ * This is a provider factory for a UIRouter instance which is configured for Angular 2
73
+ */
74
+ let uiRouterFactory = ( injector : Injector ) => {
75
+ // ----------------- ng1-to-ng2 short circuit ------
76
+ // Before creating a UIRouter instance, see if there is
77
+ // already one created (from ng1-to-ng2 as NG1_UIROUTER_TOKEN)
78
+ let $uiRouter = injector . get ( NG1_UIROUTER_TOKEN , null ) ;
79
+ if ( $uiRouter ) return $uiRouter ;
80
+
81
+
82
+ // ----------------- Get DI dependencies -----------
83
+ // Get the DI deps manually from the injector
84
+ // (no UIRouterConfig is provided when in hybrid mode)
85
+ let routerConfig : UIRouterConfig = injector . get ( UIRouterConfig ) ;
86
+ let location : UIRouterLocation = injector . get ( UIRouterLocation ) ;
87
+
88
+
89
+ // ----------------- Monkey Patches ----------------
90
+ // Monkey patch the services.$injector to the ng2 Injector
70
91
services . $injector . get = injector . get . bind ( injector ) ;
71
92
93
+ // Monkey patch the services.$location with ng2 Location implementation
72
94
location . init ( ) ;
73
95
74
96
@@ -86,14 +108,21 @@ let uiRouterFactory = (routerConfig: UIRouterConfig, location: UIRouterLocation,
86
108
registry . stateQueue . flush ( router . stateService ) ;
87
109
registry . decorator ( 'resolvables' , ngModuleResolvablesBuilder ) ;
88
110
89
- router . stateRegistry . stateQueue . autoFlush ( router . stateService ) ;
90
-
91
- let ng2InjectorResolvable = new Resolvable ( NG2_INJECTOR_TOKEN , ( ) => injector , null , { when : "EAGER" } , injector ) ;
111
+ // Prep the tree of NgModule by placing the root NgModule's Injector on the root state.
112
+ let ng2InjectorResolvable = Resolvable . fromData ( NG2_INJECTOR_TOKEN , injector ) ;
92
113
router . stateRegistry . root ( ) . resolvables . push ( ng2InjectorResolvable ) ;
93
114
115
+
116
+ // ----------------- Initialize router -------------
117
+ // Allow states to be registered
118
+ router . stateRegistry . stateQueue . autoFlush ( router . stateService ) ;
119
+
94
120
setTimeout ( ( ) => {
121
+ // Let the app apply custom configuration...
122
+ // (global transition hooks, deferIntercept, otherwise, etc)
95
123
routerConfig . configure ( router ) ;
96
124
125
+ // Start monitoring the URL
97
126
if ( ! router . urlRouterProvider . interceptDeferred ) {
98
127
router . urlRouter . listen ( ) ;
99
128
router . urlRouter . sync ( ) ;
@@ -106,35 +135,19 @@ let uiRouterFactory = (routerConfig: UIRouterConfig, location: UIRouterLocation,
106
135
/**
107
136
* The UI-Router providers, for use in your application bootstrap
108
137
*
109
- * @example
110
- * ```js
111
- *
112
- * bootstrap(UIView, [
113
- * ...UIROUTER_PROVIDERS,
114
- * ...HTTP_PROVIDERS,
115
- * provide(UIRouterConfig, { useClass: MyUIRouterConfig })
116
- * ]);
117
- * ```
138
+ * @deprecated use [[UIRouterModule]]
118
139
*/
119
-
120
140
export const UIROUTER_PROVIDERS : ProviderLike [ ] = [
121
- { provide : UIRouter , useFactory : uiRouterFactory , deps : [ UIRouterConfig , UIRouterLocation , Injector ] } ,
122
-
123
141
{ provide : UIRouterLocation , useClass : UIRouterLocation } ,
124
-
125
- { provide : StateService , useFactory : ( r : UIRouter ) => { return r . stateService ; } , deps : [ UIRouter ] } ,
126
-
127
- { provide : TransitionService , useFactory : ( r : UIRouter ) => { return r . transitionService ; } , deps : [ UIRouter ] } ,
128
-
129
- { provide : UrlMatcherFactory , useFactory : ( r : UIRouter ) => { return r . urlMatcherFactory ; } , deps : [ UIRouter ] } ,
130
-
131
- { provide : UrlRouter , useFactory : ( r : UIRouter ) => { return r . urlRouter ; } , deps : [ UIRouter ] } ,
132
-
133
- { provide : ViewService , useFactory : ( r : UIRouter ) => { return r . viewService ; } , deps : [ UIRouter ] } ,
134
-
135
- { provide : StateRegistry , useFactory : ( r : UIRouter ) => { return r . stateRegistry ; } , deps : [ UIRouter ] } ,
136
-
137
- { provide : Globals , useFactory : ( r : UIRouter ) => { return r . globals ; } , deps : [ UIRouter ] } ,
142
+ { provide : UIRouter , useFactory : uiRouterFactory , deps : [ Injector ] } ,
143
+
144
+ { provide : StateService , useFactory : ( r : UIRouter ) => r . stateService , deps : [ UIRouter ] } ,
145
+ { provide : TransitionService , useFactory : ( r : UIRouter ) => r . transitionService , deps : [ UIRouter ] } ,
146
+ { provide : UrlMatcherFactory , useFactory : ( r : UIRouter ) => r . urlMatcherFactory , deps : [ UIRouter ] } ,
147
+ { provide : UrlRouter , useFactory : ( r : UIRouter ) => r . urlRouter , deps : [ UIRouter ] } ,
148
+ { provide : ViewService , useFactory : ( r : UIRouter ) => r . viewService , deps : [ UIRouter ] } ,
149
+ { provide : StateRegistry , useFactory : ( r : UIRouter ) => r . stateRegistry , deps : [ UIRouter ] } ,
150
+ { provide : Globals , useFactory : ( r : UIRouter ) => r . globals , deps : [ UIRouter ] } ,
138
151
139
152
{ provide : UIView . PARENT_INJECT , useFactory : ( r : StateRegistry ) => { return { fqn : null , context : r . root ( ) } as ParentUIViewInject } , deps : [ StateRegistry ] }
140
153
] ;
0 commit comments