Repository navigation
Expand file tree
/
Copy pathuiRouterNgModule.ts
More file actions
234 lines (221 loc) · 8.24 KB
/
Copy pathuiRouterNgModule.ts
File metadata and controls
234 lines (221 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/** @ng2api @module core */
/** */
import { Ng2StateDeclaration } from "./interface";
import {
NgModule, OpaqueToken, ModuleWithProviders, ANALYZE_FOR_ENTRY_COMPONENTS, Provider, Injector
} from "@angular/core";
import { CommonModule, LocationStrategy, HashLocationStrategy, PathLocationStrategy } from "@angular/common";
import { _UIROUTER_DIRECTIVES } from "./directives/directives";
import { UIView } from "./directives/uiView";
import { UrlRuleHandlerFn, TargetState, TargetStateDef, UIRouter } from "@uirouter/core";
import { _UIROUTER_INSTANCE_PROVIDERS, _UIROUTER_SERVICE_PROVIDERS } from "./providers";
import { ROUTES } from "@angular/router";
/** @hidden */ export const UIROUTER_ROOT_MODULE = new OpaqueToken("UIRouter Root Module");
/** @hidden */ export const UIROUTER_MODULE_TOKEN = new OpaqueToken("UIRouter Module");
/** @hidden */ export const UIROUTER_STATES = new OpaqueToken("UIRouter States");
// /** @hidden */ export const ROUTES = UIROUTER_STATES;
export function makeRootProviders(module: StatesModule): Provider[] {
return [
{ provide: UIROUTER_ROOT_MODULE, useValue: module, multi: true},
{ provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true },
{ provide: ROUTES, useValue: module.states || [], multi: true },
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true },
];
}
export function makeChildProviders(module: StatesModule): Provider[] {
return [
{ provide: UIROUTER_MODULE_TOKEN, useValue: module, multi: true },
{ provide: ROUTES, useValue: module.states || [], multi: true },
{ provide: ANALYZE_FOR_ENTRY_COMPONENTS, useValue: module.states || [], multi: true },
];
}
export function locationStrategy(useHash) {
return { provide: LocationStrategy, useClass: useHash ? HashLocationStrategy : PathLocationStrategy };
}
/**
* Creates UI-Router Modules
*
* This class has two static factory methods which create UIRouter Modules.
* A UI-Router Module is an [Angular NgModule](https://angular.io/docs/ts/latest/guide/ngmodule.html)
* with support for UI-Router.
*
* ### UIRouter Directives
*
* When a UI-Router Module is imported into a `NgModule`, that module's components
* can use the UIRouter Directives such as [[UIView]], [[UISref]], [[UISrefActive]].
*
* ### State Definitions
*
* State definitions found in the `states:` property are provided to the Dependency Injector.
* This enables UI-Router to automatically register the states with the [[StateRegistry]] at bootstrap (and during lazy load).
*
* ### Entry Components
*
* Any routed components are added as `entryComponents:` so they will get compiled.
*/
@NgModule({
imports: [CommonModule],
declarations: [_UIROUTER_DIRECTIVES],
exports: [_UIROUTER_DIRECTIVES],
entryComponents: [UIView],
})
export class UIRouterModule {
/**
* Creates a UI-Router Module for the root (bootstrapped) application module to import
*
* This factory function creates an [Angular NgModule](https://angular.io/docs/ts/latest/guide/ngmodule.html)
* with UI-Router support.
*
* The `forRoot` module should be added to the `imports:` of the `NgModule` being bootstrapped.
* An application should only create and import a single `NgModule` using `forRoot()`.
* All other modules should be created using [[UIRouterModule.forChild]].
*
* Unlike `forChild`, an `NgModule` returned by this factory provides the [[UIRouter]] singleton object.
* This factory also accepts root-level router configuration.
* These are the only differences between `forRoot` and `forChild`.
*
* Example:
* ```js
* let routerConfig = {
* otherwise: '/home',
* states: [homeState, aboutState]
* };
*
* @ NgModule({
* imports: [
* BrowserModule,
* UIRouterModule.forRoot(routerConfig),
* FeatureModule1
* ]
* })
* class MyRootAppModule {}
*
* browserPlatformDynamic.bootstrapModule(MyRootAppModule);
* ```
*
* @param config declarative UI-Router configuration
* @returns an `NgModule` which provides the [[UIRouter]] singleton instance
*/
static forRoot(config: RootModule = {}): ModuleWithProviders {
return {
ngModule: UIRouterModule,
providers: [
_UIROUTER_INSTANCE_PROVIDERS,
_UIROUTER_SERVICE_PROVIDERS,
locationStrategy(config.useHash),
...makeRootProviders(config),
]
};
}
/**
* Creates an `NgModule` for a UIRouter module
*
* This function creates an [Angular NgModule](https://angular.io/docs/ts/latest/guide/ngmodule.html)
* with UI-Router support.
*
* #### Example:
* ```js
* var homeState = { name: 'home', url: '/home', component: Home };
* var aboutState = { name: 'about', url: '/about', component: About };
*
* @ NgModule({
* imports: [
* UIRouterModule.forChild({ states: [ homeState, aboutState ] }),
* SharedModule,
* ],
* declarations: [ Home, About ],
* })
* export class AppModule {};
* ```
*
* @param module UI-Router module options
* @returns an `NgModule`
*/
static forChild(module: StatesModule = {}): ModuleWithProviders {
return {
ngModule: UIRouterModule,
providers: makeChildProviders(module),
};
}
}
/**
* UI-Router declarative configuration which can be provided to [[UIRouterModule.forRoot]]
*/
export interface RootModule extends StatesModule {
/**
* Chooses a `LocationStrategy`.
*
* The location strategy enables either HTML5 Push State
* (Requires server-side support) or "HashBang" URLs.
*
* When `false`, uses [`PathLocationStrategy`](https://angular.io/docs/ts/latest/api/common/index/PathLocationStrategy-class.html)
* When `true`, uses [`HashLocationStrategy`](https://angular.io/docs/ts/latest/api/common/index/HashLocationStrategy-class.html)
*
* Defaults to `false`
*/
useHash?: boolean;
/**
* Sets [[UrlRouterProvider.otherwise]].
*/
otherwise?: (string | UrlRuleHandlerFn | TargetState | TargetStateDef);
/**
* Sets [[UrlRouterProvider.deferIntercept]]
*/
deferIntercept?: boolean;
}
/**
* UI-Router Module declarative configuration which can be passed to [[UIRouterModule.forChild]]
*/
export interface StatesModule {
/**
* The module's UI-Router states
*
* This list of [[Ng2StateDeclaration]] objects will be registered with the [[StateRegistry]].
* Also, the components that the states route to will be added to `entryComponents` so they will be compiled.
*/
states?: Ng2StateDeclaration[];
/**
* A UI-Router Module's imperative configuration
*
* If a UI-Router Module needs to perform some configuration (such as registering
* parameter types or Transition Hooks) a `configFn` should be supplied.
* The function will be passed the `UIRouter` instance, the module's `Injector`,
* and the module object.
*
* #### Example:
* ```js
* import { Injector } from "@angular/core";
* import { UIRouter } from "@uirouter/angular";
* import { requireAuthHook } from "./requireAuthHook";
* import { MyService } from "./myService";
*
* export function configureMyModule(uiRouter: UIRouter, injector: Injector, module: StatesModule) {
* // Get UIRouter services off the UIRouter object
* let urlConfig = uiRouter.urlService.config;
* let transitionService = uiRouter.transitionService;
* uiRouter.trace.enable("TRANSITION");
*
* transitionService.onBefore({ to: (state) => state.requiresAuth }, requireAuthHook);
*
* // Create a slug type based on the string type
* let builtInStringType = urlConfig.type('string');
* let slugType = Object.assign({}, builtInStringType, { encode: (str) => str, decode: (str) => str });
* urlConfig.type('slug', slugType);
*
* // Inject arbitrary services from DI using the Injector argument
* let myService: MyService = injector.get(MyService)
* myService.useFastMode();
* }
* ```
*
* ```js
* @NgModule({
* imports: [
* UIRouterModule.forChild({ states: STATES, config: configureMyModule });
* ]
* })
* class MyModule {}
* ```
*/
config?: (uiRouterInstance: UIRouter, injector: Injector, module: StatesModule) => any;
}