2016-07-06 18:36:50 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-07-28 15:10:36 -04:00
|
|
|
import {APP_BASE_HREF, HashLocationStrategy, Location, LocationStrategy, PathLocationStrategy, PlatformLocation} from '@angular/common';
|
2016-08-24 16:39:44 -04:00
|
|
|
import {ANALYZE_FOR_ENTRY_COMPONENTS, APP_BOOTSTRAP_LISTENER, ApplicationRef, BaseException, Compiler, Inject, Injector, ModuleWithProviders, NgModule, NgModuleFactoryLoader, OpaqueToken, Optional, Provider, SkipSelf, SystemJsNgModuleLoader} from '@angular/core';
|
2016-07-06 18:36:50 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
import {Route, Routes} from './config';
|
2016-07-06 19:19:52 -04:00
|
|
|
import {RouterLink, RouterLinkWithHref} from './directives/router_link';
|
|
|
|
import {RouterLinkActive} from './directives/router_link_active';
|
|
|
|
import {RouterOutlet} from './directives/router_outlet';
|
2016-08-25 10:56:30 -04:00
|
|
|
import {ErrorHandler, Router} from './router';
|
2016-07-06 19:19:52 -04:00
|
|
|
import {ROUTES} from './router_config_loader';
|
2016-07-06 18:36:50 -04:00
|
|
|
import {RouterOutletMap} from './router_outlet_map';
|
|
|
|
import {ActivatedRoute} from './router_state';
|
|
|
|
import {DefaultUrlSerializer, UrlSerializer} from './url_tree';
|
2016-08-16 16:40:28 -04:00
|
|
|
import {flatten} from './utils/collection';
|
2016-07-06 18:36:50 -04:00
|
|
|
|
|
|
|
|
2016-07-25 19:10:10 -04:00
|
|
|
|
2016-07-06 18:36:50 -04:00
|
|
|
/**
|
|
|
|
* @stable
|
|
|
|
*/
|
2016-08-26 12:33:37 -04:00
|
|
|
const ROUTER_DIRECTIVES = [RouterOutlet, RouterLink, RouterLinkWithHref, RouterLinkActive];
|
2016-07-06 18:36:50 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
/**
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export const ROUTER_CONFIGURATION = new OpaqueToken('ROUTER_CONFIGURATION');
|
|
|
|
|
2016-08-23 14:57:58 -04:00
|
|
|
export const ROUTER_FORROOT_GUARD = new OpaqueToken('ROUTER_FORROOT_GUARD');
|
|
|
|
|
2016-07-27 12:54:19 -04:00
|
|
|
const pathLocationStrategy = {
|
|
|
|
provide: LocationStrategy,
|
|
|
|
useClass: PathLocationStrategy
|
|
|
|
};
|
|
|
|
const hashLocationStrategy = {
|
|
|
|
provide: LocationStrategy,
|
|
|
|
useClass: HashLocationStrategy
|
|
|
|
};
|
|
|
|
|
2016-08-24 16:39:44 -04:00
|
|
|
export const ROUTER_PROVIDERS: Provider[] = [
|
2016-07-27 12:54:19 -04:00
|
|
|
Location, {provide: UrlSerializer, useClass: DefaultUrlSerializer}, {
|
2016-07-07 17:13:32 -04:00
|
|
|
provide: Router,
|
|
|
|
useFactory: setupRouter,
|
|
|
|
deps: [
|
2016-08-16 16:40:28 -04:00
|
|
|
ApplicationRef, UrlSerializer, RouterOutletMap, Location, Injector, NgModuleFactoryLoader,
|
|
|
|
Compiler, ROUTES, ROUTER_CONFIGURATION
|
2016-07-07 17:13:32 -04:00
|
|
|
]
|
|
|
|
},
|
2016-07-25 19:10:10 -04:00
|
|
|
RouterOutletMap, {provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]},
|
2016-07-18 06:50:31 -04:00
|
|
|
{provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader},
|
2016-07-07 17:13:32 -04:00
|
|
|
{provide: ROUTER_CONFIGURATION, useValue: {enableTracing: false}}
|
|
|
|
];
|
2016-07-06 18:36:50 -04:00
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
/**
|
2016-07-27 12:54:19 -04:00
|
|
|
* Router module.
|
|
|
|
*
|
|
|
|
* When registered at the root, it should be used as follows:
|
2016-07-18 06:50:31 -04:00
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```
|
2016-07-27 12:54:19 -04:00
|
|
|
* bootstrap(AppCmp, {imports: [RouterModule.forRoot(ROUTES)]});
|
2016-07-18 06:50:31 -04:00
|
|
|
* ```
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* For submodules and lazy loaded submodules it should be used as follows:
|
2016-07-06 18:36:50 -04:00
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```
|
2016-07-27 12:54:19 -04:00
|
|
|
* @NgModule({
|
|
|
|
* imports: [RouterModule.forChild(CHILD_ROUTES)]
|
|
|
|
* })
|
|
|
|
* class Lazy {}
|
2016-07-06 18:36:50 -04:00
|
|
|
* ```
|
|
|
|
*
|
2016-08-17 18:35:30 -04:00
|
|
|
* @stable
|
2016-07-06 18:36:50 -04:00
|
|
|
*/
|
2016-07-27 12:54:19 -04:00
|
|
|
@NgModule({declarations: ROUTER_DIRECTIVES, exports: ROUTER_DIRECTIVES})
|
2016-07-07 14:59:08 -04:00
|
|
|
export class RouterModule {
|
2016-08-23 14:57:58 -04:00
|
|
|
constructor(@Optional() @Inject(ROUTER_FORROOT_GUARD) guard: any) {}
|
|
|
|
|
2016-07-27 12:54:19 -04:00
|
|
|
static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders {
|
|
|
|
return {
|
|
|
|
ngModule: RouterModule,
|
|
|
|
providers: [
|
2016-08-23 14:57:58 -04:00
|
|
|
ROUTER_PROVIDERS, provideRoutes(routes), {
|
|
|
|
provide: ROUTER_FORROOT_GUARD,
|
|
|
|
useFactory: provideForRootGuard,
|
|
|
|
deps: [[Router, new Optional(), new SkipSelf()]]
|
|
|
|
},
|
2016-08-01 18:51:22 -04:00
|
|
|
{provide: ROUTER_CONFIGURATION, useValue: config ? config : {}}, {
|
2016-07-28 15:10:36 -04:00
|
|
|
provide: LocationStrategy,
|
|
|
|
useFactory: provideLocationStrategy,
|
2016-07-28 17:36:05 -04:00
|
|
|
deps: [
|
|
|
|
PlatformLocation, [new Inject(APP_BASE_HREF), new Optional()], ROUTER_CONFIGURATION
|
|
|
|
]
|
2016-08-02 08:22:44 -04:00
|
|
|
},
|
|
|
|
provideRouterInitializer()
|
2016-07-27 12:54:19 -04:00
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static forChild(routes: Routes): ModuleWithProviders {
|
|
|
|
return {ngModule: RouterModule, providers: [provideRoutes(routes)]};
|
|
|
|
}
|
2016-07-18 06:50:31 -04:00
|
|
|
}
|
2016-07-28 15:10:36 -04:00
|
|
|
|
2016-08-01 18:51:22 -04:00
|
|
|
export function provideLocationStrategy(
|
2016-07-28 15:10:36 -04:00
|
|
|
platformLocationStrategy: PlatformLocation, baseHref: string, options: ExtraOptions = {}) {
|
|
|
|
return options.useHash ? new HashLocationStrategy(platformLocationStrategy, baseHref) :
|
|
|
|
new PathLocationStrategy(platformLocationStrategy, baseHref);
|
2016-08-16 16:40:28 -04:00
|
|
|
}
|
|
|
|
|
2016-08-23 14:57:58 -04:00
|
|
|
export function provideForRootGuard(router: Router): any {
|
|
|
|
if (router) {
|
|
|
|
throw new BaseException(
|
|
|
|
`RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead.`);
|
|
|
|
}
|
|
|
|
return 'guarded';
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
/**
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export function provideRoutes(routes: Routes): any {
|
|
|
|
return [
|
|
|
|
{provide: ANALYZE_FOR_ENTRY_COMPONENTS, multi: true, useValue: routes},
|
|
|
|
{provide: ROUTES, multi: true, useValue: routes}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-08-25 10:56:30 -04:00
|
|
|
* Extra options used to configure the router.
|
|
|
|
*
|
|
|
|
* Set `enableTracing` to log router events to the console.
|
|
|
|
* Set 'useHash' to true to enable HashLocationStrategy.
|
|
|
|
* Set `errorHandler` to enable a custom ErrorHandler.
|
|
|
|
*
|
2016-08-16 16:40:28 -04:00
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export interface ExtraOptions {
|
|
|
|
enableTracing?: boolean;
|
|
|
|
useHash?: boolean;
|
2016-08-25 11:48:31 -04:00
|
|
|
initialNavigation?: boolean;
|
2016-08-25 10:56:30 -04:00
|
|
|
errorHandler?: ErrorHandler;
|
2016-08-16 16:40:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setupRouter(
|
|
|
|
ref: ApplicationRef, urlSerializer: UrlSerializer, outletMap: RouterOutletMap,
|
|
|
|
location: Location, injector: Injector, loader: NgModuleFactoryLoader, compiler: Compiler,
|
|
|
|
config: Route[][], opts: ExtraOptions = {}) {
|
|
|
|
if (ref.componentTypes.length == 0) {
|
|
|
|
throw new Error('Bootstrap at least one component before injecting Router.');
|
|
|
|
}
|
|
|
|
const componentType = ref.componentTypes[0];
|
|
|
|
const r = new Router(
|
|
|
|
componentType, urlSerializer, outletMap, location, injector, loader, compiler,
|
|
|
|
flatten(config));
|
|
|
|
|
2016-08-25 10:56:30 -04:00
|
|
|
if (opts.errorHandler) {
|
|
|
|
r.errorHandler = opts.errorHandler;
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
if (opts.enableTracing) {
|
|
|
|
r.events.subscribe(e => {
|
|
|
|
console.group(`Router Event: ${(<any>e.constructor).name}`);
|
|
|
|
console.log(e.toString());
|
|
|
|
console.log(e);
|
|
|
|
console.groupEnd();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function rootRoute(router: Router): ActivatedRoute {
|
|
|
|
return router.routerState.root;
|
|
|
|
}
|
|
|
|
|
2016-08-25 11:48:31 -04:00
|
|
|
export function initialRouterNavigation(router: Router, opts: ExtraOptions) {
|
|
|
|
return () => {
|
|
|
|
if (opts.initialNavigation === false) {
|
|
|
|
router.setUpLocationChangeListener();
|
|
|
|
} else {
|
|
|
|
router.initialNavigation();
|
|
|
|
}
|
|
|
|
};
|
2016-08-16 16:40:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function provideRouterInitializer() {
|
|
|
|
return {
|
|
|
|
provide: APP_BOOTSTRAP_LISTENER,
|
|
|
|
multi: true,
|
|
|
|
useFactory: initialRouterNavigation,
|
2016-08-25 11:48:31 -04:00
|
|
|
deps: [Router, ROUTER_CONFIGURATION]
|
2016-08-16 16:40:28 -04:00
|
|
|
};
|
2016-08-19 18:48:09 -04:00
|
|
|
}
|