46
46
*
47
47
* @preferred @module ng2
48
48
*/ /** */
49
- import { Injector , OpaqueToken } from "@angular/core" ;
49
+ import { Injector , OpaqueToken , Provider } from "@angular/core" ;
50
+ import { ClassProvider , ExistingProvider , FactoryProvider , TypeProvider , ValueProvider } from "@angular/core" ; // has or is using
50
51
import { UIRouter } from "../router" ;
51
52
import { PathNode } from "../path/node" ;
52
53
import { StateRegistry } from "../state/stateRegistry" ;
@@ -62,17 +63,20 @@ import {UIRouterConfig} from "./uiRouterConfig";
62
63
import { Globals } from "../globals" ;
63
64
import { UIRouterLocation } from "./location" ;
64
65
import { services } from "../common/coreservices" ;
65
- import { ProviderLike } from "../state/interface" ;
66
66
import { Resolvable } from "../resolve/resolvable" ;
67
67
import { ngModuleResolvablesBuilder } from "./statebuilders/lazyLoadNgModuleResolvable" ;
68
68
import { flattenR } from "../common/common" ;
69
69
import { UIROUTER_STATES_TOKEN } from "./uiRouterNgModule" ;
70
70
import { UIRouterRx } from "./rx" ;
71
+ import { LocationStrategy , HashLocationStrategy , PathLocationStrategy } from "@angular/common" ;
71
72
72
73
export const NG1_UIROUTER_TOKEN = new OpaqueToken ( "$uiRouter" ) ;
73
74
74
75
/**
75
- * This is a provider factory for a UIRouter instance which is configured for Angular 2
76
+ * This is a factory function for a UIRouter instance
77
+ *
78
+ * Creates a UIRouter instance and configures it for Angular 2, then invokes router bootstrap.
79
+ * This function is used as an Angular 2 `useFactory` Provider.
76
80
*/
77
81
let uiRouterFactory = ( injector : Injector ) => {
78
82
// ----------------- ng1-to-ng2 short circuit ------
@@ -140,10 +144,12 @@ let uiRouterFactory = (injector: Injector) => {
140
144
return router ;
141
145
} ;
142
146
143
- export const _UIROUTER_PROVIDERS : ProviderLike [ ] = [
144
- { provide : UIRouterLocation , useClass : UIRouterLocation } ,
147
+ export const _UIROUTER_INSTANCE_PROVIDERS : Provider [ ] = [
145
148
{ provide : UIRouter , useFactory : uiRouterFactory , deps : [ Injector ] } ,
149
+ { provide : UIRouterLocation , useClass : UIRouterLocation } ,
150
+ ] ;
146
151
152
+ export const _UIROUTER_PROVIDERS : Provider [ ] = [
147
153
{ provide : StateService , useFactory : ( r : UIRouter ) => r . stateService , deps : [ UIRouter ] } ,
148
154
{ provide : TransitionService , useFactory : ( r : UIRouter ) => r . transitionService , deps : [ UIRouter ] } ,
149
155
{ provide : UrlMatcherFactory , useFactory : ( r : UIRouter ) => r . urlMatcherFactory , deps : [ UIRouter ] } ,
@@ -153,10 +159,41 @@ export const _UIROUTER_PROVIDERS: ProviderLike[] = [
153
159
{ provide : Globals , useFactory : ( r : UIRouter ) => r . globals , deps : [ UIRouter ] } ,
154
160
155
161
{ provide : UIView . PARENT_INJECT , useFactory : ( r : StateRegistry ) => { return { fqn : null , context : r . root ( ) } as ParentUIViewInject } , deps : [ StateRegistry ] }
156
- ]
162
+ ] ;
163
+
164
+ /**
165
+ * Provides an Instance of UI-Router for NG2.
166
+ *
167
+ * Use this on the root NgModule to configure and create an instance of the Angular 2 UIRouter.
168
+ *
169
+ * @example
170
+ * ```js
171
+ *
172
+ * @UIRouterModule ({
173
+ * states: [ homeState, aboutState ],
174
+ * providers: [ provideUIRouter({ configClass: MyUIRouterConfig, useHash: true }) ],
175
+ * bootstrap: [ UIView ]
176
+ * }) class RootNgModule {}
177
+ *
178
+ * platformBrowserDynamic().bootstrapModule(RootNgModule);
179
+ * ```
180
+ *
181
+ * Note: UIRouter should only be provided *once*, on the root module, when bootstrapping the application.
182
+ */
183
+ export function provideUIRouter ( rootConfig : { configClass ?: typeof UIRouterConfig , useHash ?: boolean } = { } ) {
184
+ // Provide the UIRouter instance providers
185
+ return _UIROUTER_INSTANCE_PROVIDERS . concat (
186
+ // Provide the user-supplied UIRouterConfig class, or use base UIRouterConfig (as a no-op config)
187
+ { provide : UIRouterConfig , useClass : ( rootConfig . configClass as any || UIRouterConfig ) } ,
188
+ // Provide the PathLocationStrategy by default unless `useHash` is `true`
189
+ { provide : LocationStrategy , useClass : ( rootConfig . useHash ? HashLocationStrategy : PathLocationStrategy ) }
190
+ ) ;
191
+ }
192
+
157
193
/**
158
194
* The UI-Router providers, for use in your application bootstrap
159
195
*
160
- * @deprecated use [[UIRouterModule]]
196
+ * @deprecated use [[UIRouterModule]] and [[provideUIRouter]]
161
197
*/
162
- export const UIROUTER_PROVIDERS = _UIROUTER_PROVIDERS ;
198
+ export const UIROUTER_PROVIDERS : Provider [ ] = _UIROUTER_INSTANCE_PROVIDERS . concat ( _UIROUTER_PROVIDERS ) ;
199
+
0 commit comments