4646 *
4747 * @preferred @module ng2
4848 */ /** */
49- import { Injector } from "@angular/core" ;
49+ import { Injector , OpaqueToken } from "@angular/core" ;
5050import { UIRouter } from "../router" ;
5151import { PathNode } from "../path/node" ;
5252import { StateRegistry } from "../state/stateRegistry" ;
@@ -66,9 +66,31 @@ import {ProviderLike} from "../state/interface";
6666import { Resolvable } from "../resolve/resolvable" ;
6767import { ngModuleResolvablesBuilder } from "./statebuilders/lazyLoadNgModuleResolvable" ;
6868
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
7091 services . $injector . get = injector . get . bind ( injector ) ;
7192
93+ // Monkey patch the services.$location with ng2 Location implementation
7294 location . init ( ) ;
7395
7496
@@ -86,14 +108,21 @@ let uiRouterFactory = (routerConfig: UIRouterConfig, location: UIRouterLocation,
86108 registry . stateQueue . flush ( router . stateService ) ;
87109 registry . decorator ( 'resolvables' , ngModuleResolvablesBuilder ) ;
88110
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 ) ;
92113 router . stateRegistry . root ( ) . resolvables . push ( ng2InjectorResolvable ) ;
93114
115+
116+ // ----------------- Initialize router -------------
117+ // Allow states to be registered
118+ router . stateRegistry . stateQueue . autoFlush ( router . stateService ) ;
119+
94120 setTimeout ( ( ) => {
121+ // Let the app apply custom configuration...
122+ // (global transition hooks, deferIntercept, otherwise, etc)
95123 routerConfig . configure ( router ) ;
96124
125+ // Start monitoring the URL
97126 if ( ! router . urlRouterProvider . interceptDeferred ) {
98127 router . urlRouter . listen ( ) ;
99128 router . urlRouter . sync ( ) ;
@@ -106,35 +135,19 @@ let uiRouterFactory = (routerConfig: UIRouterConfig, location: UIRouterLocation,
106135/**
107136 * The UI-Router providers, for use in your application bootstrap
108137 *
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]]
118139 */
119-
120140export const UIROUTER_PROVIDERS : ProviderLike [ ] = [
121- { provide : UIRouter , useFactory : uiRouterFactory , deps : [ UIRouterConfig , UIRouterLocation , Injector ] } ,
122-
123141 { 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 ] } ,
138151
139152 { provide : UIView . PARENT_INJECT , useFactory : ( r : StateRegistry ) => { return { fqn : null , context : r . root ( ) } as ParentUIViewInject } , deps : [ StateRegistry ] }
140153] ;
0 commit comments