2016-06-23 12:47:54 -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-06-08 14:13:41 -04:00
|
|
|
import {Location} from '@angular/common';
|
2017-03-14 19:26:17 -04:00
|
|
|
import {Compiler, Injector, NgModuleFactoryLoader, NgModuleRef, Type, isDevMode} from '@angular/core';
|
2016-10-28 17:56:08 -04:00
|
|
|
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Subject} from 'rxjs/Subject';
|
|
|
|
import {Subscription} from 'rxjs/Subscription';
|
2016-07-15 19:27:54 -04:00
|
|
|
import {from} from 'rxjs/observable/from';
|
|
|
|
import {of } from 'rxjs/observable/of';
|
2016-10-05 17:43:26 -04:00
|
|
|
import {concatMap} from 'rxjs/operator/concatMap';
|
2016-08-30 17:25:46 -04:00
|
|
|
import {every} from 'rxjs/operator/every';
|
2016-11-03 00:56:04 -04:00
|
|
|
import {first} from 'rxjs/operator/first';
|
2016-08-30 17:25:46 -04:00
|
|
|
import {map} from 'rxjs/operator/map';
|
|
|
|
import {mergeMap} from 'rxjs/operator/mergeMap';
|
|
|
|
import {reduce} from 'rxjs/operator/reduce';
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2016-06-08 19:14:26 -04:00
|
|
|
import {applyRedirects} from './apply_redirects';
|
2017-04-11 11:34:58 -04:00
|
|
|
import {LoadedRouterConfig, QueryParamsHandling, ResolveData, Route, Routes, RunGuardsAndResolvers, validateConfig} from './config';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {createRouterState} from './create_router_state';
|
|
|
|
import {createUrlTree} from './create_url_tree';
|
|
|
|
import {RouterOutlet} from './directives/router_outlet';
|
2017-02-15 13:57:03 -05:00
|
|
|
import {Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, RouteConfigLoadEnd, RouteConfigLoadStart, RoutesRecognized} from './events';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {recognize} from './recognize';
|
2016-11-30 02:21:41 -05:00
|
|
|
import {DetachedRouteHandle, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy';
|
2017-04-04 19:00:40 -04:00
|
|
|
import {RouterConfigLoader} from './router_config_loader';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {RouterOutletMap} from './router_outlet_map';
|
2016-10-28 18:17:00 -04:00
|
|
|
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState, equalParamsAndUrlSegments, inheritedParamsDataResolve} from './router_state';
|
2017-01-24 12:05:34 -05:00
|
|
|
import {PRIMARY_OUTLET, Params, isNavigationCancelingError} from './shared';
|
2016-10-20 13:44:44 -04:00
|
|
|
import {DefaultUrlHandlingStrategy, UrlHandlingStrategy} from './url_handling_strategy';
|
2016-07-28 20:59:05 -04:00
|
|
|
import {UrlSerializer, UrlTree, containsTree, createEmptyUrlTree} from './url_tree';
|
2017-03-26 10:15:10 -04:00
|
|
|
import {andObservables, forEach, shallowEqual, waitForMap, wrapIntoObservable} from './utils/collection';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {TreeNode} from './utils/tree';
|
|
|
|
|
2016-12-02 18:34:28 -05:00
|
|
|
declare let Zone: any;
|
2016-07-15 17:59:59 -04:00
|
|
|
|
2016-07-18 19:42:33 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* @whatItDoes Represents the extra options used during navigation.
|
|
|
|
*
|
2016-08-30 18:57:24 -04:00
|
|
|
* @stable
|
2016-07-18 19:42:33 -04:00
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
export interface NavigationExtras {
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* Enables relative navigation from the current ActivatedRoute.
|
2016-08-19 18:48:09 -04:00
|
|
|
*
|
2016-09-10 19:51:27 -04:00
|
|
|
* Configuration:
|
2016-08-19 18:48:09 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: 'parent',
|
|
|
|
* component: ParentComponent,
|
2016-12-02 18:34:28 -05:00
|
|
|
* children: [{
|
|
|
|
* path: 'list',
|
|
|
|
* component: ListComponent
|
|
|
|
* },{
|
|
|
|
* path: 'child',
|
|
|
|
* component: ChildComponent
|
|
|
|
* }]
|
2016-08-19 18:48:09 -04:00
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
2016-09-10 19:51:27 -04:00
|
|
|
* Navigate to list route from child route:
|
2016-08-19 18:48:09 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component({...})
|
|
|
|
* class ChildComponent {
|
|
|
|
* constructor(private router: Router, private route: ActivatedRoute) {}
|
|
|
|
*
|
|
|
|
* go() {
|
2016-09-02 16:42:51 -04:00
|
|
|
* this.router.navigate(['../list'], { relativeTo: this.route });
|
2016-08-19 18:48:09 -04:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
relativeTo?: ActivatedRoute;
|
2016-09-10 19:51:27 -04:00
|
|
|
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* Sets query parameters to the URL.
|
2016-08-19 18:48:09 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Navigate to /results?page=1
|
2016-09-02 16:42:51 -04:00
|
|
|
* this.router.navigate(['/results'], { queryParams: { page: 1 } });
|
2016-08-19 18:48:09 -04:00
|
|
|
* ```
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
queryParams?: Params;
|
2016-09-10 19:51:27 -04:00
|
|
|
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* Sets the hash fragment for the URL.
|
2016-08-19 18:48:09 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Navigate to /results#top
|
2016-09-02 16:42:51 -04:00
|
|
|
* this.router.navigate(['/results'], { fragment: 'top' });
|
2016-08-19 18:48:09 -04:00
|
|
|
* ```
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
fragment?: string;
|
2016-09-10 19:51:27 -04:00
|
|
|
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* Preserves the query parameters for the next navigation.
|
2016-08-19 18:48:09 -04:00
|
|
|
*
|
2017-01-25 04:33:13 -05:00
|
|
|
* deprecated, use `queryParamsHandling` instead
|
|
|
|
*
|
2016-08-19 18:48:09 -04:00
|
|
|
* ```
|
|
|
|
* // Preserve query params from /results?page=1 to /view?page=1
|
2016-09-02 16:42:51 -04:00
|
|
|
* this.router.navigate(['/view'], { preserveQueryParams: true });
|
2016-08-19 18:48:09 -04:00
|
|
|
* ```
|
2017-01-25 04:33:13 -05:00
|
|
|
*
|
2017-03-15 17:32:03 -04:00
|
|
|
* @deprecated since v4
|
2016-08-19 18:48:09 -04:00
|
|
|
*/
|
2016-07-20 17:30:04 -04:00
|
|
|
preserveQueryParams?: boolean;
|
2017-01-25 04:33:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* config strategy to handle the query parameters for the next navigation.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // from /results?page=1 to /view?page=1&page=2
|
|
|
|
* this.router.navigate(['/view'], { queryParams: { page: 2 }, queryParamsHandling: "merge" });
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
queryParamsHandling?: QueryParamsHandling;
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
|
|
|
* Preserves the fragment for the next navigation
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Preserve fragment from /results#top to /view#top
|
2016-09-02 16:42:51 -04:00
|
|
|
* this.router.navigate(['/view'], { preserveFragment: true });
|
2016-08-19 18:48:09 -04:00
|
|
|
* ```
|
|
|
|
*/
|
2016-07-20 17:30:04 -04:00
|
|
|
preserveFragment?: boolean;
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* Navigates without pushing a new state into history.
|
2016-08-19 18:48:09 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Navigate silently to /view
|
2016-09-02 16:42:51 -04:00
|
|
|
* this.router.navigate(['/view'], { skipLocationChange: true });
|
2016-08-19 18:48:09 -04:00
|
|
|
* ```
|
|
|
|
*/
|
2016-08-04 14:46:09 -04:00
|
|
|
skipLocationChange?: boolean;
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* Navigates while replacing the current state in history.
|
2016-08-19 18:48:09 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Navigate to /view
|
2016-09-02 16:42:51 -04:00
|
|
|
* this.router.navigate(['/view'], { replaceUrl: true });
|
2016-08-19 18:48:09 -04:00
|
|
|
* ```
|
|
|
|
*/
|
2016-08-12 17:30:51 -04:00
|
|
|
replaceUrl?: boolean;
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-08-25 10:56:30 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* @whatItDoes Error handler that is invoked when a navigation errors.
|
2016-08-25 10:56:30 -04:00
|
|
|
*
|
2016-09-10 19:51:27 -04:00
|
|
|
* @description
|
|
|
|
* If the handler returns a value, the navigation promise will be resolved with this value.
|
2016-08-25 10:56:30 -04:00
|
|
|
* If the handler throws an exception, the navigation promise will be rejected with
|
|
|
|
* the exception.
|
|
|
|
*
|
|
|
|
* @stable
|
|
|
|
*/
|
|
|
|
export type ErrorHandler = (error: any) => any;
|
|
|
|
|
|
|
|
function defaultErrorHandler(error: any): any {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2016-12-21 15:47:58 -05:00
|
|
|
type NavigationSource = 'imperative' | 'popstate' | 'hashchange';
|
|
|
|
|
2016-10-28 17:56:08 -04:00
|
|
|
type NavigationParams = {
|
|
|
|
id: number,
|
|
|
|
rawUrl: UrlTree,
|
|
|
|
extras: NavigationExtras,
|
|
|
|
resolve: any,
|
|
|
|
reject: any,
|
2016-12-02 18:19:00 -05:00
|
|
|
promise: Promise<boolean>,
|
2016-12-21 15:47:58 -05:00
|
|
|
source: NavigationSource,
|
2016-10-28 17:56:08 -04:00
|
|
|
};
|
|
|
|
|
2017-03-07 17:27:20 -05:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export type RouterHook = (snapshot: RouterStateSnapshot) => Observable<void>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
function defaultRouterHook(snapshot: RouterStateSnapshot): Observable<void> {
|
2017-04-17 12:47:59 -04:00
|
|
|
return of (null);
|
2017-03-07 17:27:20 -05:00
|
|
|
}
|
|
|
|
|
2016-11-30 02:21:41 -05:00
|
|
|
/**
|
|
|
|
* Does not detach any subtrees. Reuses routes as long as their route config is the same.
|
|
|
|
*/
|
|
|
|
export class DefaultRouteReuseStrategy implements RouteReuseStrategy {
|
|
|
|
shouldDetach(route: ActivatedRouteSnapshot): boolean { return false; }
|
|
|
|
store(route: ActivatedRouteSnapshot, detachedTree: DetachedRouteHandle): void {}
|
|
|
|
shouldAttach(route: ActivatedRouteSnapshot): boolean { return false; }
|
2017-04-17 12:47:59 -04:00
|
|
|
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { return null; }
|
2016-11-30 02:21:41 -05:00
|
|
|
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
|
|
|
|
return future.routeConfig === curr.routeConfig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* @whatItDoes Provides the navigation and url manipulation capabilities.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-07-08 02:02:35 -04:00
|
|
|
* See {@link Routes} for more details and examples.
|
2016-06-28 17:49:29 -04:00
|
|
|
*
|
2016-09-10 19:51:27 -04:00
|
|
|
* @ngModule RouterModule
|
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-05-24 16:41:37 -04:00
|
|
|
*/
|
2016-05-24 16:23:27 -04:00
|
|
|
export class Router {
|
2016-05-26 19:51:56 -04:00
|
|
|
private currentUrlTree: UrlTree;
|
2016-10-20 13:44:44 -04:00
|
|
|
private rawUrlTree: UrlTree;
|
2016-10-28 17:56:08 -04:00
|
|
|
|
2017-04-17 12:47:59 -04:00
|
|
|
private navigations = new BehaviorSubject<NavigationParams>(null);
|
2017-02-15 13:57:03 -05:00
|
|
|
private routerEvents = new Subject<Event>();
|
2016-10-20 13:44:44 -04:00
|
|
|
|
2016-05-26 19:51:56 -04:00
|
|
|
private currentRouterState: RouterState;
|
2016-05-24 16:23:27 -04:00
|
|
|
private locationSubscription: Subscription;
|
2016-06-03 17:07:01 -04:00
|
|
|
private navigationId: number = 0;
|
2016-07-06 14:02:16 -04:00
|
|
|
private configLoader: RouterConfigLoader;
|
2017-03-14 19:26:17 -04:00
|
|
|
private ngModule: NgModuleRef<any>;
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-09-10 19:51:27 -04:00
|
|
|
/**
|
|
|
|
* Error handler that is invoked when a navigation errors.
|
|
|
|
*
|
|
|
|
* See {@link ErrorHandler} for more information.
|
|
|
|
*/
|
2016-08-25 10:56:30 -04:00
|
|
|
errorHandler: ErrorHandler = defaultErrorHandler;
|
|
|
|
|
2017-03-07 17:27:20 -05:00
|
|
|
|
|
|
|
|
2016-07-20 20:51:21 -04:00
|
|
|
/**
|
|
|
|
* Indicates if at least one navigation happened.
|
|
|
|
*/
|
|
|
|
navigated: boolean = false;
|
|
|
|
|
2017-03-07 17:27:20 -05:00
|
|
|
/**
|
|
|
|
* Used by RouterModule. This allows us to
|
|
|
|
* pause the navigation either before preactivation or after it.
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
hooks: {beforePreactivation: RouterHook, afterPreactivation: RouterHook} = {
|
|
|
|
beforePreactivation: defaultRouterHook,
|
|
|
|
afterPreactivation: defaultRouterHook
|
|
|
|
};
|
|
|
|
|
2016-10-20 13:44:44 -04:00
|
|
|
/**
|
2017-01-27 01:30:42 -05:00
|
|
|
* Extracts and merges URLs. Used for AngularJS to Angular migrations.
|
2016-10-20 13:44:44 -04:00
|
|
|
*/
|
|
|
|
urlHandlingStrategy: UrlHandlingStrategy = new DefaultUrlHandlingStrategy();
|
|
|
|
|
2016-11-30 02:21:41 -05:00
|
|
|
routeReuseStrategy: RouteReuseStrategy = new DefaultRouteReuseStrategy();
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
2016-06-28 19:53:54 -04:00
|
|
|
* Creates the router service.
|
2016-05-24 16:41:37 -04:00
|
|
|
*/
|
2016-09-10 19:51:27 -04:00
|
|
|
// TODO: vsavkin make internal after the final is out.
|
2016-06-08 14:13:41 -04:00
|
|
|
constructor(
|
2017-04-17 12:47:59 -04:00
|
|
|
private rootComponentType: Type<any>, private urlSerializer: UrlSerializer,
|
2017-03-14 19:26:17 -04:00
|
|
|
private outletMap: RouterOutletMap, private location: Location, injector: Injector,
|
2016-08-16 16:40:28 -04:00
|
|
|
loader: NgModuleFactoryLoader, compiler: Compiler, public config: Routes) {
|
2017-02-15 13:57:03 -05:00
|
|
|
const onLoadStart = (r: Route) => this.triggerEvent(new RouteConfigLoadStart(r));
|
|
|
|
const onLoadEnd = (r: Route) => this.triggerEvent(new RouteConfigLoadEnd(r));
|
|
|
|
|
2017-03-14 19:26:17 -04:00
|
|
|
this.ngModule = injector.get(NgModuleRef);
|
|
|
|
|
2016-06-16 17:36:51 -04:00
|
|
|
this.resetConfig(config);
|
2016-06-07 12:50:35 -04:00
|
|
|
this.currentUrlTree = createEmptyUrlTree();
|
2016-10-20 13:44:44 -04:00
|
|
|
this.rawUrlTree = this.currentUrlTree;
|
2017-02-15 13:57:03 -05:00
|
|
|
|
|
|
|
this.configLoader = new RouterConfigLoader(loader, compiler, onLoadStart, onLoadEnd);
|
2016-06-14 17:55:59 -04:00
|
|
|
this.currentRouterState = createEmptyState(this.currentUrlTree, this.rootComponentType);
|
2016-10-28 17:56:08 -04:00
|
|
|
this.processNavigations();
|
2016-06-06 17:05:57 -04:00
|
|
|
}
|
|
|
|
|
2016-09-16 18:08:15 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* TODO: this should be removed once the constructor of the router made internal
|
|
|
|
*/
|
|
|
|
resetRootComponentType(rootComponentType: Type<any>): void {
|
|
|
|
this.rootComponentType = rootComponentType;
|
2016-09-21 14:37:43 -04:00
|
|
|
// TODO: vsavkin router 4.0 should make the root component set to null
|
|
|
|
// this will simplify the lifecycle of the router.
|
|
|
|
this.currentRouterState.root.component = this.rootComponentType;
|
2016-09-16 18:08:15 -04:00
|
|
|
}
|
|
|
|
|
2016-06-06 17:05:57 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* Sets up the location change listener and performs the initial navigation.
|
2016-06-06 17:05:57 -04:00
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
initialNavigation(): void {
|
2016-05-24 16:23:27 -04:00
|
|
|
this.setUpLocationChangeListener();
|
2017-01-14 17:05:24 -05:00
|
|
|
if (this.navigationId === 0) {
|
|
|
|
this.navigateByUrl(this.location.path(true), {replaceUrl: true});
|
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
2016-08-25 11:48:31 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* Sets up the location change listener.
|
2016-08-25 11:48:31 -04:00
|
|
|
*/
|
|
|
|
setUpLocationChangeListener(): void {
|
|
|
|
// Zone.current.wrap is needed because of the issue with RxJS scheduler,
|
|
|
|
// which does not work properly with zone.js in IE and Safari
|
2016-11-22 17:50:52 -05:00
|
|
|
if (!this.locationSubscription) {
|
|
|
|
this.locationSubscription = <any>this.location.subscribe(Zone.current.wrap((change: any) => {
|
|
|
|
const rawUrlTree = this.urlSerializer.parse(change['url']);
|
2016-12-21 15:47:58 -05:00
|
|
|
const source: NavigationSource = change['type'] === 'popstate' ? 'popstate' : 'hashchange';
|
|
|
|
setTimeout(() => { this.scheduleNavigation(rawUrlTree, source, {replaceUrl: true}); }, 0);
|
2016-11-22 17:50:52 -05:00
|
|
|
}));
|
|
|
|
}
|
2016-08-25 11:48:31 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The current route state */
|
2016-06-08 14:13:41 -04:00
|
|
|
get routerState(): RouterState { return this.currentRouterState; }
|
2016-05-26 19:51:56 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** The current url */
|
2016-06-14 17:55:59 -04:00
|
|
|
get url(): string { return this.serializeUrl(this.currentUrlTree); }
|
2016-05-24 16:41:37 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** An observable of router events */
|
2016-06-08 14:13:41 -04:00
|
|
|
get events(): Observable<Event> { return this.routerEvents; }
|
2016-06-03 17:25:18 -04:00
|
|
|
|
2017-02-15 13:57:03 -05:00
|
|
|
/** @internal */
|
|
|
|
triggerEvent(e: Event) { this.routerEvents.next(e); }
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
|
|
|
* Resets the configuration used for navigation and generating links.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* router.resetConfig([
|
2016-05-26 19:51:56 -04:00
|
|
|
* { path: 'team/:id', component: TeamCmp, children: [
|
|
|
|
* { path: 'simple', component: SimpleCmp },
|
|
|
|
* { path: 'user/:name', component: UserCmp }
|
2016-12-09 13:44:46 -05:00
|
|
|
* ]}
|
2016-05-24 16:41:37 -04:00
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*/
|
2016-07-06 19:19:52 -04:00
|
|
|
resetConfig(config: Routes): void {
|
2016-06-16 17:36:51 -04:00
|
|
|
validateConfig(config);
|
|
|
|
this.config = config;
|
|
|
|
}
|
2016-05-26 19:51:56 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** @docsNotRequired */
|
2016-08-02 05:32:27 -04:00
|
|
|
ngOnDestroy() { this.dispose(); }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** Disposes of the router */
|
2016-11-22 17:50:52 -05:00
|
|
|
dispose(): void {
|
|
|
|
if (this.locationSubscription) {
|
|
|
|
this.locationSubscription.unsubscribe();
|
2017-04-17 12:47:59 -04:00
|
|
|
this.locationSubscription = null;
|
2016-11-22 17:50:52 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-05-26 19:51:56 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* Applies an array of commands to the current url tree and creates a new url tree.
|
2016-05-26 19:51:56 -04:00
|
|
|
*
|
|
|
|
* When given an activate route, applies the given commands starting from the route.
|
|
|
|
* When not given a route, applies the given command starting from the root.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // create /team/33/user/11
|
|
|
|
* router.createUrlTree(['/team', 33, 'user', 11]);
|
|
|
|
*
|
|
|
|
* // create /team/33;expand=true/user/11
|
|
|
|
* router.createUrlTree(['/team', 33, {expand: true}, 'user', 11]);
|
|
|
|
*
|
2016-08-04 20:19:23 -04:00
|
|
|
* // you can collapse static segments like this (this works only with the first passed-in value):
|
2016-05-26 19:51:56 -04:00
|
|
|
* router.createUrlTree(['/team/33/user', userId]);
|
|
|
|
*
|
2016-08-16 22:38:23 -04:00
|
|
|
* // If the first segment can contain slashes, and you do not want the router to split it, you
|
|
|
|
* // can do the following:
|
2016-08-04 20:19:23 -04:00
|
|
|
*
|
|
|
|
* router.createUrlTree([{segmentPath: '/one/two'}]);
|
|
|
|
*
|
2016-08-16 22:37:53 -04:00
|
|
|
* // create /team/33/(user/11//right:chat)
|
2016-07-22 16:25:48 -04:00
|
|
|
* router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: 'chat'}}]);
|
|
|
|
*
|
|
|
|
* // remove the right secondary node
|
|
|
|
* router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: null}}]);
|
2016-07-12 12:49:55 -04:00
|
|
|
*
|
2016-05-26 19:51:56 -04:00
|
|
|
* // assuming the current url is `/team/33/user/11` and the route points to `user/11`
|
|
|
|
*
|
|
|
|
* // navigate to /team/33/user/11/details
|
|
|
|
* router.createUrlTree(['details'], {relativeTo: route});
|
|
|
|
*
|
|
|
|
* // navigate to /team/33/user/22
|
|
|
|
* router.createUrlTree(['../22'], {relativeTo: route});
|
|
|
|
*
|
|
|
|
* // navigate to /team/44/user/22
|
|
|
|
* router.createUrlTree(['../../team/44/user/22'], {relativeTo: route});
|
|
|
|
* ```
|
|
|
|
*/
|
2016-07-20 17:30:04 -04:00
|
|
|
createUrlTree(
|
2017-01-25 04:33:13 -05:00
|
|
|
commands: any[], {relativeTo, queryParams, fragment, preserveQueryParams, queryParamsHandling,
|
2016-07-20 17:30:04 -04:00
|
|
|
preserveFragment}: NavigationExtras = {}): UrlTree {
|
2017-01-25 04:33:13 -05:00
|
|
|
if (isDevMode() && preserveQueryParams && <any>console && <any>console.warn) {
|
|
|
|
console.warn('preserveQueryParams is deprecated, use queryParamsHandling instead.');
|
|
|
|
}
|
2016-12-09 13:44:46 -05:00
|
|
|
const a = relativeTo || this.routerState.root;
|
2016-07-20 17:30:04 -04:00
|
|
|
const f = preserveFragment ? this.currentUrlTree.fragment : fragment;
|
2017-04-17 12:47:59 -04:00
|
|
|
let q: Params = null;
|
2017-01-25 04:33:13 -05:00
|
|
|
if (queryParamsHandling) {
|
|
|
|
switch (queryParamsHandling) {
|
|
|
|
case 'merge':
|
2017-03-26 10:15:10 -04:00
|
|
|
q = {...this.currentUrlTree.queryParams, ...queryParams};
|
2017-01-25 04:33:13 -05:00
|
|
|
break;
|
|
|
|
case 'preserve':
|
|
|
|
q = this.currentUrlTree.queryParams;
|
|
|
|
break;
|
|
|
|
default:
|
2017-04-17 12:47:59 -04:00
|
|
|
q = queryParams;
|
2017-01-25 04:33:13 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-17 12:47:59 -04:00
|
|
|
q = preserveQueryParams ? this.currentUrlTree.queryParams : queryParams;
|
2017-01-25 04:33:13 -05:00
|
|
|
}
|
2017-04-17 12:47:59 -04:00
|
|
|
return createUrlTree(a, this.currentUrlTree, commands, q, f);
|
2016-05-26 19:51:56 -04:00
|
|
|
}
|
|
|
|
|
2016-06-15 11:30:49 -04:00
|
|
|
/**
|
|
|
|
* Navigate based on the provided url. This navigation is always absolute.
|
|
|
|
*
|
|
|
|
* Returns a promise that:
|
2016-12-09 13:44:46 -05:00
|
|
|
* - resolves to 'true' when navigation succeeds,
|
|
|
|
* - resolves to 'false' when navigation fails,
|
|
|
|
* - is rejected when an error happens.
|
2016-06-15 11:30:49 -04:00
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* router.navigateByUrl("/team/33/user/11");
|
2016-08-04 14:46:09 -04:00
|
|
|
*
|
|
|
|
* // Navigate without updating the URL
|
|
|
|
* router.navigateByUrl("/team/33/user/11", { skipLocationChange: true });
|
2016-06-15 11:30:49 -04:00
|
|
|
* ```
|
2016-07-22 16:25:48 -04:00
|
|
|
*
|
|
|
|
* In opposite to `navigate`, `navigateByUrl` takes a whole URL
|
|
|
|
* and does not apply any delta to the current one.
|
2016-06-15 11:30:49 -04:00
|
|
|
*/
|
2016-08-04 14:46:09 -04:00
|
|
|
navigateByUrl(url: string|UrlTree, extras: NavigationExtras = {skipLocationChange: false}):
|
|
|
|
Promise<boolean> {
|
2017-02-15 14:32:15 -05:00
|
|
|
const urlTree = url instanceof UrlTree ? url : this.parseUrl(url);
|
|
|
|
const mergedTree = this.urlHandlingStrategy.merge(urlTree, this.rawUrlTree);
|
2016-12-09 13:44:46 -05:00
|
|
|
|
2017-02-15 14:32:15 -05:00
|
|
|
return this.scheduleNavigation(mergedTree, 'imperative', extras);
|
2016-06-15 11:30:49 -04:00
|
|
|
}
|
|
|
|
|
2016-05-26 19:51:56 -04:00
|
|
|
/**
|
|
|
|
* Navigate based on the provided array of commands and a starting point.
|
|
|
|
* If no starting route is provided, the navigation is absolute.
|
|
|
|
*
|
2016-06-03 17:28:41 -04:00
|
|
|
* Returns a promise that:
|
2016-12-09 13:44:46 -05:00
|
|
|
* - resolves to 'true' when navigation succeeds,
|
|
|
|
* - resolves to 'false' when navigation fails,
|
|
|
|
* - is rejected when an error happens.
|
2016-06-03 17:28:41 -04:00
|
|
|
*
|
2016-05-26 19:51:56 -04:00
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
2016-09-07 17:10:19 -04:00
|
|
|
* router.navigate(['team', 33, 'user', 11], {relativeTo: route});
|
2016-08-04 14:46:09 -04:00
|
|
|
*
|
|
|
|
* // Navigate without updating the URL
|
2016-12-09 13:44:46 -05:00
|
|
|
* router.navigate(['team', 33, 'user', 11], {relativeTo: route, skipLocationChange: true});
|
2016-05-26 19:51:56 -04:00
|
|
|
* ```
|
2016-07-22 16:25:48 -04:00
|
|
|
*
|
2016-12-09 13:44:46 -05:00
|
|
|
* In opposite to `navigateByUrl`, `navigate` always takes a delta that is applied to the current
|
|
|
|
* URL.
|
2016-05-26 19:51:56 -04:00
|
|
|
*/
|
2016-08-04 14:46:09 -04:00
|
|
|
navigate(commands: any[], extras: NavigationExtras = {skipLocationChange: false}):
|
|
|
|
Promise<boolean> {
|
2016-12-11 17:33:21 -05:00
|
|
|
validateCommands(commands);
|
2016-11-10 17:41:19 -05:00
|
|
|
if (typeof extras.queryParams === 'object' && extras.queryParams !== null) {
|
|
|
|
extras.queryParams = this.removeEmptyProps(extras.queryParams);
|
|
|
|
}
|
2016-10-28 17:56:08 -04:00
|
|
|
return this.navigateByUrl(this.createUrlTree(commands, extras), extras);
|
2016-05-26 19:51:56 -04:00
|
|
|
}
|
2016-06-06 19:34:48 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** Serializes a {@link UrlTree} into a string */
|
2016-05-26 19:51:56 -04:00
|
|
|
serializeUrl(url: UrlTree): string { return this.urlSerializer.serialize(url); }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** Parses a string into a {@link UrlTree} */
|
2016-05-26 19:51:56 -04:00
|
|
|
parseUrl(url: string): UrlTree { return this.urlSerializer.parse(url); }
|
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** Returns whether the url is activated */
|
2016-07-28 20:59:05 -04:00
|
|
|
isActive(url: string|UrlTree, exact: boolean): boolean {
|
|
|
|
if (url instanceof UrlTree) {
|
|
|
|
return containsTree(this.currentUrlTree, url, exact);
|
|
|
|
} else {
|
|
|
|
const urlTree = this.urlSerializer.parse(url);
|
|
|
|
return containsTree(this.currentUrlTree, urlTree, exact);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 17:41:19 -05:00
|
|
|
private removeEmptyProps(params: Params): Params {
|
|
|
|
return Object.keys(params).reduce((result: Params, key: string) => {
|
|
|
|
const value: any = params[key];
|
|
|
|
if (value !== null && value !== undefined) {
|
|
|
|
result[key] = value;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2016-10-28 17:56:08 -04:00
|
|
|
private processNavigations(): void {
|
|
|
|
concatMap
|
|
|
|
.call(
|
|
|
|
this.navigations,
|
|
|
|
(nav: NavigationParams) => {
|
|
|
|
if (nav) {
|
|
|
|
this.executeScheduledNavigation(nav);
|
|
|
|
// a failed navigation should not stop the router from processing
|
|
|
|
// further navigations => the catch
|
|
|
|
return nav.promise.catch(() => {});
|
|
|
|
} else {
|
|
|
|
return <any>of (null);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.subscribe(() => {});
|
|
|
|
}
|
|
|
|
|
2016-12-21 15:47:58 -05:00
|
|
|
private scheduleNavigation(rawUrl: UrlTree, source: NavigationSource, extras: NavigationExtras):
|
2016-12-02 18:19:00 -05:00
|
|
|
Promise<boolean> {
|
2016-12-21 15:47:58 -05:00
|
|
|
const lastNavigation = this.navigations.value;
|
|
|
|
|
|
|
|
// If the user triggers a navigation imperatively (e.g., by using navigateByUrl),
|
|
|
|
// and that navigation results in 'replaceState' that leads to the same URL,
|
|
|
|
// we should skip those.
|
|
|
|
if (lastNavigation && source !== 'imperative' && lastNavigation.source === 'imperative' &&
|
|
|
|
lastNavigation.rawUrl.toString() === rawUrl.toString()) {
|
2017-04-17 12:47:59 -04:00
|
|
|
return null; // return value is not used
|
2016-12-21 15:47:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Because of a bug in IE and Edge, the location class fires two events (popstate and
|
2017-02-15 14:32:15 -05:00
|
|
|
// hashchange) every single time. The second one should be ignored. Otherwise, the URL will
|
|
|
|
// flicker.
|
2016-12-21 15:47:58 -05:00
|
|
|
if (lastNavigation && source == 'hashchange' && lastNavigation.source === 'popstate' &&
|
|
|
|
lastNavigation.rawUrl.toString() === rawUrl.toString()) {
|
2017-04-17 12:47:59 -04:00
|
|
|
return null; // return value is not used
|
2016-12-21 15:47:58 -05:00
|
|
|
}
|
|
|
|
|
2016-10-28 17:56:08 -04:00
|
|
|
let resolve: any = null;
|
|
|
|
let reject: any = null;
|
|
|
|
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
});
|
|
|
|
|
|
|
|
const id = ++this.navigationId;
|
2016-12-21 15:47:58 -05:00
|
|
|
this.navigations.next({id, source, rawUrl, extras, resolve, reject, promise});
|
2016-10-28 17:56:08 -04:00
|
|
|
|
2016-11-10 18:26:32 -05:00
|
|
|
// Make sure that the error is propagated even though `processNavigations` catch
|
|
|
|
// handler does not rethrow
|
|
|
|
return promise.catch((e: any) => Promise.reject(e));
|
2016-10-28 17:56:08 -04:00
|
|
|
}
|
2016-10-20 13:44:44 -04:00
|
|
|
|
2016-12-02 18:19:00 -05:00
|
|
|
private executeScheduledNavigation({id, rawUrl, extras, resolve, reject}: NavigationParams):
|
|
|
|
void {
|
2016-10-28 17:56:08 -04:00
|
|
|
const url = this.urlHandlingStrategy.extract(rawUrl);
|
2016-12-02 18:19:00 -05:00
|
|
|
const urlTransition = !this.navigated || url.toString() !== this.currentUrlTree.toString();
|
2016-10-28 17:56:08 -04:00
|
|
|
|
|
|
|
if (urlTransition && this.urlHandlingStrategy.shouldProcessUrl(rawUrl)) {
|
|
|
|
this.routerEvents.next(new NavigationStart(id, this.serializeUrl(url)));
|
|
|
|
Promise.resolve()
|
|
|
|
.then(
|
|
|
|
(_) => this.runNavigate(
|
2017-04-17 12:47:59 -04:00
|
|
|
url, rawUrl, extras.skipLocationChange, extras.replaceUrl, id, null))
|
2016-10-28 17:56:08 -04:00
|
|
|
.then(resolve, reject);
|
2016-10-20 13:44:44 -04:00
|
|
|
|
|
|
|
// we cannot process the current URL, but we could process the previous one =>
|
|
|
|
// we need to do some cleanup
|
2016-10-28 17:56:08 -04:00
|
|
|
} else if (
|
2016-12-02 18:19:00 -05:00
|
|
|
urlTransition && this.rawUrlTree &&
|
|
|
|
this.urlHandlingStrategy.shouldProcessUrl(this.rawUrlTree)) {
|
2016-10-20 13:44:44 -04:00
|
|
|
this.routerEvents.next(new NavigationStart(id, this.serializeUrl(url)));
|
2016-10-28 17:56:08 -04:00
|
|
|
Promise.resolve()
|
|
|
|
.then(
|
|
|
|
(_) => this.runNavigate(
|
2016-11-15 22:00:20 -05:00
|
|
|
url, rawUrl, false, false, id,
|
|
|
|
createEmptyState(url, this.rootComponentType).snapshot))
|
2016-10-28 17:56:08 -04:00
|
|
|
.then(resolve, reject);
|
2016-10-20 13:44:44 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
this.rawUrlTree = rawUrl;
|
2016-10-28 17:56:08 -04:00
|
|
|
resolve(null);
|
2016-10-20 13:44:44 -04:00
|
|
|
}
|
2016-06-03 17:07:01 -04:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:30:51 -04:00
|
|
|
private runNavigate(
|
2016-10-28 17:56:08 -04:00
|
|
|
url: UrlTree, rawUrl: UrlTree, shouldPreventPushState: boolean, shouldReplaceUrl: boolean,
|
2017-04-17 12:47:59 -04:00
|
|
|
id: number, precreatedState: RouterStateSnapshot): Promise<boolean> {
|
2016-06-03 17:07:01 -04:00
|
|
|
if (id !== this.navigationId) {
|
2016-06-09 14:02:57 -04:00
|
|
|
this.location.go(this.urlSerializer.serialize(this.currentUrlTree));
|
2016-08-24 13:20:44 -04:00
|
|
|
this.routerEvents.next(new NavigationCancel(
|
|
|
|
id, this.serializeUrl(url),
|
|
|
|
`Navigation ID ${id} is not equal to the current navigation id ${this.navigationId}`));
|
2016-06-03 17:07:01 -04:00
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
2016-06-01 16:34:48 -04:00
|
|
|
|
2016-06-03 17:07:01 -04:00
|
|
|
return new Promise((resolvePromise, rejectPromise) => {
|
2016-11-15 22:00:20 -05:00
|
|
|
// create an observable of the url and route state snapshot
|
|
|
|
// this operation do not result in any side effects
|
|
|
|
let urlAndSnapshot$: Observable<{appliedUrl: UrlTree, snapshot: RouterStateSnapshot}>;
|
2016-10-20 13:44:44 -04:00
|
|
|
if (!precreatedState) {
|
2017-03-14 19:26:17 -04:00
|
|
|
const moduleInjector = this.ngModule.injector;
|
2016-10-20 13:44:44 -04:00
|
|
|
const redirectsApplied$ =
|
2017-03-14 19:26:17 -04:00
|
|
|
applyRedirects(moduleInjector, this.configLoader, this.urlSerializer, url, this.config);
|
2016-10-20 13:44:44 -04:00
|
|
|
|
2016-11-15 22:00:20 -05:00
|
|
|
urlAndSnapshot$ = mergeMap.call(redirectsApplied$, (appliedUrl: UrlTree) => {
|
|
|
|
return map.call(
|
|
|
|
recognize(
|
|
|
|
this.rootComponentType, this.config, appliedUrl, this.serializeUrl(appliedUrl)),
|
|
|
|
(snapshot: any) => {
|
2016-10-20 13:44:44 -04:00
|
|
|
|
2016-11-15 22:00:20 -05:00
|
|
|
this.routerEvents.next(new RoutesRecognized(
|
|
|
|
id, this.serializeUrl(url), this.serializeUrl(appliedUrl), snapshot));
|
2016-10-20 13:44:44 -04:00
|
|
|
|
2016-11-15 22:00:20 -05:00
|
|
|
return {appliedUrl, snapshot};
|
|
|
|
});
|
2016-10-20 13:44:44 -04:00
|
|
|
});
|
|
|
|
} else {
|
2016-11-15 22:00:20 -05:00
|
|
|
urlAndSnapshot$ = of ({appliedUrl: url, snapshot: precreatedState});
|
2016-10-20 13:44:44 -04:00
|
|
|
}
|
2016-06-27 17:00:07 -04:00
|
|
|
|
2017-03-07 17:27:20 -05:00
|
|
|
const beforePreactivationDone$ = mergeMap.call(
|
|
|
|
urlAndSnapshot$, (p: {appliedUrl: string, snapshot: RouterStateSnapshot}) => {
|
|
|
|
return map.call(this.hooks.beforePreactivation(p.snapshot), () => p);
|
|
|
|
});
|
2016-11-15 22:00:20 -05:00
|
|
|
|
|
|
|
// run preactivation: guards and data resolvers
|
|
|
|
let preActivation: PreActivation;
|
2017-03-07 17:27:20 -05:00
|
|
|
const preactivationTraverse$ = map.call(
|
|
|
|
beforePreactivationDone$,
|
|
|
|
({appliedUrl, snapshot}: {appliedUrl: string, snapshot: RouterStateSnapshot}) => {
|
2017-03-14 19:26:17 -04:00
|
|
|
const moduleInjector = this.ngModule.injector;
|
2017-03-07 17:27:20 -05:00
|
|
|
preActivation =
|
2017-03-14 19:26:17 -04:00
|
|
|
new PreActivation(snapshot, this.currentRouterState.snapshot, moduleInjector);
|
2017-03-07 17:27:20 -05:00
|
|
|
preActivation.traverse(this.outletMap);
|
|
|
|
return {appliedUrl, snapshot};
|
|
|
|
});
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2017-03-07 17:27:20 -05:00
|
|
|
const preactivationCheckGuards$ = mergeMap.call(
|
|
|
|
preactivationTraverse$,
|
|
|
|
({appliedUrl, snapshot}: {appliedUrl: string, snapshot: RouterStateSnapshot}) => {
|
2016-11-15 22:00:20 -05:00
|
|
|
if (this.navigationId !== id) return of (false);
|
2016-10-28 17:56:08 -04:00
|
|
|
|
2016-11-15 22:00:20 -05:00
|
|
|
return map.call(preActivation.checkGuards(), (shouldActivate: boolean) => {
|
|
|
|
return {appliedUrl: appliedUrl, snapshot: snapshot, shouldActivate: shouldActivate};
|
|
|
|
});
|
|
|
|
});
|
2016-08-30 17:25:46 -04:00
|
|
|
|
2017-03-07 17:27:20 -05:00
|
|
|
const preactivationResolveData$ = mergeMap.call(preactivationCheckGuards$, (p: any) => {
|
2016-10-28 17:56:08 -04:00
|
|
|
if (this.navigationId !== id) return of (false);
|
|
|
|
|
2016-11-15 22:00:20 -05:00
|
|
|
if (p.shouldActivate) {
|
|
|
|
return map.call(preActivation.resolveData(), () => p);
|
2016-08-30 17:25:46 -04:00
|
|
|
} else {
|
2016-11-15 22:00:20 -05:00
|
|
|
return of (p);
|
2016-08-30 17:25:46 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-07 17:27:20 -05:00
|
|
|
const preactivationDone$ = mergeMap.call(preactivationResolveData$, (p: any) => {
|
|
|
|
return map.call(this.hooks.afterPreactivation(p.snapshot), () => p);
|
|
|
|
});
|
|
|
|
|
2016-11-15 22:00:20 -05:00
|
|
|
|
|
|
|
// create router state
|
|
|
|
// this operation has side effects => route state is being affected
|
|
|
|
const routerState$ =
|
2017-03-07 17:27:20 -05:00
|
|
|
map.call(preactivationDone$, ({appliedUrl, snapshot, shouldActivate}: any) => {
|
2016-11-15 22:00:20 -05:00
|
|
|
if (shouldActivate) {
|
2016-11-30 02:21:41 -05:00
|
|
|
const state =
|
|
|
|
createRouterState(this.routeReuseStrategy, snapshot, this.currentRouterState);
|
2016-11-15 22:00:20 -05:00
|
|
|
return {appliedUrl, state, shouldActivate};
|
|
|
|
} else {
|
|
|
|
return {appliedUrl, state: null, shouldActivate};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// applied the new router state
|
|
|
|
// this operation has side effects
|
|
|
|
let navigationIsSuccessful: boolean;
|
|
|
|
const storedState = this.currentRouterState;
|
|
|
|
const storedUrl = this.currentUrlTree;
|
|
|
|
|
|
|
|
routerState$
|
|
|
|
.forEach(({appliedUrl, state, shouldActivate}: any) => {
|
2016-06-08 14:13:41 -04:00
|
|
|
if (!shouldActivate || id !== this.navigationId) {
|
2016-06-25 16:31:48 -04:00
|
|
|
navigationIsSuccessful = false;
|
|
|
|
return;
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
|
|
|
|
2016-07-12 15:28:08 -04:00
|
|
|
this.currentUrlTree = appliedUrl;
|
2016-10-20 13:44:44 -04:00
|
|
|
this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, rawUrl);
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
this.currentRouterState = state;
|
2016-07-12 15:28:08 -04:00
|
|
|
|
2016-08-12 17:30:51 -04:00
|
|
|
if (!shouldPreventPushState) {
|
2016-11-02 14:30:00 -04:00
|
|
|
const path = this.urlSerializer.serialize(this.rawUrlTree);
|
2016-08-12 17:30:51 -04:00
|
|
|
if (this.location.isCurrentPathEqualTo(path) || shouldReplaceUrl) {
|
2016-06-10 11:57:03 -04:00
|
|
|
this.location.replaceState(path);
|
|
|
|
} else {
|
|
|
|
this.location.go(path);
|
|
|
|
}
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
2016-08-25 05:35:46 -04:00
|
|
|
|
2016-11-30 02:21:41 -05:00
|
|
|
new ActivateRoutes(this.routeReuseStrategy, state, storedState)
|
|
|
|
.activate(this.outletMap);
|
2016-08-25 05:35:46 -04:00
|
|
|
|
2016-06-25 16:31:48 -04:00
|
|
|
navigationIsSuccessful = true;
|
2016-06-08 14:13:41 -04:00
|
|
|
})
|
|
|
|
.then(
|
|
|
|
() => {
|
2016-08-12 01:59:53 -04:00
|
|
|
if (navigationIsSuccessful) {
|
2017-01-14 14:56:08 -05:00
|
|
|
this.navigated = true;
|
2016-11-15 22:00:20 -05:00
|
|
|
this.routerEvents.next(new NavigationEnd(
|
|
|
|
id, this.serializeUrl(url), this.serializeUrl(this.currentUrlTree)));
|
2016-08-12 01:59:53 -04:00
|
|
|
resolvePromise(true);
|
|
|
|
} else {
|
2016-11-02 14:30:00 -04:00
|
|
|
this.resetUrlToCurrentUrlTree();
|
2016-08-24 13:20:44 -04:00
|
|
|
this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url), ''));
|
2016-08-12 01:59:53 -04:00
|
|
|
resolvePromise(false);
|
|
|
|
}
|
2016-06-08 14:13:41 -04:00
|
|
|
},
|
2016-08-30 17:17:37 -04:00
|
|
|
(e: any) => {
|
2017-01-24 12:05:34 -05:00
|
|
|
if (isNavigationCancelingError(e)) {
|
2016-11-02 14:30:00 -04:00
|
|
|
this.resetUrlToCurrentUrlTree();
|
2016-08-24 13:20:44 -04:00
|
|
|
this.navigated = true;
|
|
|
|
this.routerEvents.next(
|
|
|
|
new NavigationCancel(id, this.serializeUrl(url), e.message));
|
|
|
|
resolvePromise(false);
|
|
|
|
} else {
|
|
|
|
this.routerEvents.next(new NavigationError(id, this.serializeUrl(url), e));
|
2016-08-25 10:56:30 -04:00
|
|
|
try {
|
|
|
|
resolvePromise(this.errorHandler(e));
|
|
|
|
} catch (ee) {
|
|
|
|
rejectPromise(ee);
|
|
|
|
}
|
2016-08-24 13:20:44 -04:00
|
|
|
}
|
2016-08-29 20:51:38 -04:00
|
|
|
|
2016-11-02 14:30:00 -04:00
|
|
|
this.currentRouterState = storedState;
|
|
|
|
this.currentUrlTree = storedUrl;
|
|
|
|
this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, rawUrl);
|
2016-11-22 15:15:54 -05:00
|
|
|
this.location.replaceState(this.serializeUrl(this.rawUrlTree));
|
2016-06-08 14:13:41 -04:00
|
|
|
});
|
2016-05-26 19:51:56 -04:00
|
|
|
});
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
2016-11-02 14:30:00 -04:00
|
|
|
|
|
|
|
private resetUrlToCurrentUrlTree(): void {
|
2016-11-22 15:15:54 -05:00
|
|
|
const path = this.urlSerializer.serialize(this.rawUrlTree);
|
|
|
|
this.location.replaceState(path);
|
2016-11-02 14:30:00 -04:00
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
2016-07-13 18:15:20 -04:00
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
class CanActivate {
|
2016-07-13 18:15:20 -04:00
|
|
|
constructor(public path: ActivatedRouteSnapshot[]) {}
|
|
|
|
get route(): ActivatedRouteSnapshot { return this.path[this.path.length - 1]; }
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
2016-06-27 15:27:23 -04:00
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
class CanDeactivate {
|
2017-04-17 12:47:59 -04:00
|
|
|
constructor(public component: Object, public route: ActivatedRouteSnapshot) {}
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
2016-06-02 17:44:57 -04:00
|
|
|
|
2016-07-13 18:15:20 -04:00
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
export class PreActivation {
|
2017-03-24 14:32:17 -04:00
|
|
|
private canActivateChecks: CanActivate[] = [];
|
|
|
|
private canDeactivateChecks: CanDeactivate[] = [];
|
2016-06-08 14:13:41 -04:00
|
|
|
constructor(
|
|
|
|
private future: RouterStateSnapshot, private curr: RouterStateSnapshot,
|
2017-03-14 19:26:17 -04:00
|
|
|
private moduleInjector: Injector) {}
|
2016-06-01 17:32:15 -04:00
|
|
|
|
2016-06-27 17:00:07 -04:00
|
|
|
traverse(parentOutletMap: RouterOutletMap): void {
|
2016-06-01 17:32:15 -04:00
|
|
|
const futureRoot = this.future._root;
|
|
|
|
const currRoot = this.curr ? this.curr._root : null;
|
2016-07-13 18:15:20 -04:00
|
|
|
this.traverseChildRoutes(futureRoot, currRoot, parentOutletMap, [futureRoot.value]);
|
2016-06-27 17:00:07 -04:00
|
|
|
}
|
2016-06-15 18:45:42 -04:00
|
|
|
|
2016-06-27 17:00:07 -04:00
|
|
|
checkGuards(): Observable<boolean> {
|
2017-03-24 14:32:17 -04:00
|
|
|
if (this.canDeactivateChecks.length === 0 && this.canActivateChecks.length === 0) {
|
|
|
|
return of (true);
|
|
|
|
}
|
|
|
|
const canDeactivate$ = this.runCanDeactivateChecks();
|
|
|
|
return mergeMap.call(
|
|
|
|
canDeactivate$,
|
|
|
|
(canDeactivate: boolean) => canDeactivate ? this.runCanActivateChecks() : of (false));
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 17:00:07 -04:00
|
|
|
resolveData(): Observable<any> {
|
2017-03-24 14:32:17 -04:00
|
|
|
if (this.canActivateChecks.length === 0) return of (null);
|
|
|
|
const checks$ = from(this.canActivateChecks);
|
|
|
|
const runningChecks$ =
|
|
|
|
concatMap.call(checks$, (check: CanActivate) => this.runResolve(check.route));
|
2016-08-30 17:25:46 -04:00
|
|
|
return reduce.call(runningChecks$, (_: any, __: any) => _);
|
2016-06-27 17:00:07 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
private traverseChildRoutes(
|
2017-04-17 12:47:59 -04:00
|
|
|
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>,
|
|
|
|
outletMap: RouterOutletMap, futurePath: ActivatedRouteSnapshot[]): void {
|
2017-03-29 12:44:04 -04:00
|
|
|
const prevChildren = nodeChildrenAsMap(currNode);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
futureNode.children.forEach(c => {
|
2016-07-13 18:15:20 -04:00
|
|
|
this.traverseRoutes(c, prevChildren[c.value.outlet], outletMap, futurePath.concat([c.value]));
|
2016-06-01 17:32:15 -04:00
|
|
|
delete prevChildren[c.value.outlet];
|
|
|
|
});
|
2016-06-15 19:45:19 -04:00
|
|
|
forEach(
|
2017-03-29 12:44:04 -04:00
|
|
|
prevChildren, (v: TreeNode<ActivatedRouteSnapshot>, k: string) =>
|
2017-04-17 12:47:59 -04:00
|
|
|
this.deactiveRouteAndItsChildren(v, outletMap._outlets[k]));
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
|
|
|
|
2017-03-24 14:32:17 -04:00
|
|
|
private traverseRoutes(
|
2016-06-15 12:14:41 -04:00
|
|
|
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>,
|
2017-04-17 12:47:59 -04:00
|
|
|
parentOutletMap: RouterOutletMap, futurePath: ActivatedRouteSnapshot[]): void {
|
2016-06-01 17:32:15 -04:00
|
|
|
const future = futureNode.value;
|
|
|
|
const curr = currNode ? currNode.value : null;
|
2016-06-02 17:44:57 -04:00
|
|
|
const outlet = parentOutletMap ? parentOutletMap._outlets[futureNode.value.outlet] : null;
|
2016-06-01 17:32:15 -04:00
|
|
|
|
2016-06-19 17:44:20 -04:00
|
|
|
// reusing the node
|
2016-06-02 17:44:57 -04:00
|
|
|
if (curr && future._routeConfig === curr._routeConfig) {
|
2017-02-24 01:12:30 -05:00
|
|
|
if (this.shouldRunGuardsAndResolvers(
|
2017-04-17 12:47:59 -04:00
|
|
|
curr, future, future._routeConfig.runGuardsAndResolvers)) {
|
2017-03-24 14:32:17 -04:00
|
|
|
this.canActivateChecks.push(new CanActivate(futurePath));
|
2017-04-17 12:47:59 -04:00
|
|
|
this.canDeactivateChecks.push(new CanDeactivate(outlet.component, curr));
|
2016-08-10 18:53:57 -04:00
|
|
|
} else {
|
|
|
|
// we need to set the data
|
|
|
|
future.data = curr.data;
|
2016-10-25 17:33:18 -04:00
|
|
|
future._resolvedData = curr._resolvedData;
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
2016-06-19 17:44:20 -04:00
|
|
|
|
|
|
|
// If we have a component, we need to go through an outlet.
|
|
|
|
if (future.component) {
|
2016-07-13 18:15:20 -04:00
|
|
|
this.traverseChildRoutes(
|
|
|
|
futureNode, currNode, outlet ? outlet.outletMap : null, futurePath);
|
2016-06-19 17:44:20 -04:00
|
|
|
|
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
|
|
|
} else {
|
2016-07-13 18:15:20 -04:00
|
|
|
this.traverseChildRoutes(futureNode, currNode, parentOutletMap, futurePath);
|
2016-06-19 17:44:20 -04:00
|
|
|
}
|
2016-06-01 17:32:15 -04:00
|
|
|
} else {
|
2016-06-19 17:44:20 -04:00
|
|
|
if (curr) {
|
2016-10-20 16:32:38 -04:00
|
|
|
this.deactiveRouteAndItsChildren(currNode, outlet);
|
2016-06-19 17:44:20 -04:00
|
|
|
}
|
|
|
|
|
2017-03-24 14:32:17 -04:00
|
|
|
this.canActivateChecks.push(new CanActivate(futurePath));
|
2016-06-19 17:44:20 -04:00
|
|
|
// If we have a component, we need to go through an outlet.
|
|
|
|
if (future.component) {
|
2016-07-13 18:15:20 -04:00
|
|
|
this.traverseChildRoutes(futureNode, null, outlet ? outlet.outletMap : null, futurePath);
|
2016-06-19 17:44:20 -04:00
|
|
|
|
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
|
|
|
} else {
|
2016-07-13 18:15:20 -04:00
|
|
|
this.traverseChildRoutes(futureNode, null, parentOutletMap, futurePath);
|
2016-06-19 17:44:20 -04:00
|
|
|
}
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 01:12:30 -05:00
|
|
|
private shouldRunGuardsAndResolvers(
|
|
|
|
curr: ActivatedRouteSnapshot, future: ActivatedRouteSnapshot,
|
2017-04-17 12:47:59 -04:00
|
|
|
mode: RunGuardsAndResolvers): boolean {
|
2017-02-24 01:12:30 -05:00
|
|
|
switch (mode) {
|
|
|
|
case 'always':
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 'paramsOrQueryParamsChange':
|
|
|
|
return !equalParamsAndUrlSegments(curr, future) ||
|
|
|
|
!shallowEqual(curr.queryParams, future.queryParams);
|
|
|
|
|
|
|
|
case 'paramsChange':
|
|
|
|
default:
|
|
|
|
return !equalParamsAndUrlSegments(curr, future);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:38 -04:00
|
|
|
private deactiveRouteAndItsChildren(
|
2017-04-17 12:47:59 -04:00
|
|
|
route: TreeNode<ActivatedRouteSnapshot>, outlet: RouterOutlet): void {
|
2017-03-29 12:44:04 -04:00
|
|
|
const prevChildren = nodeChildrenAsMap(route);
|
2016-11-01 15:11:42 -04:00
|
|
|
const r = route.value;
|
2016-06-01 17:32:15 -04:00
|
|
|
|
2017-03-29 12:44:04 -04:00
|
|
|
forEach(prevChildren, (v: TreeNode<ActivatedRouteSnapshot>, k: string) => {
|
2016-11-01 15:11:42 -04:00
|
|
|
if (!r.component) {
|
|
|
|
this.deactiveRouteAndItsChildren(v, outlet);
|
|
|
|
} else if (!!outlet) {
|
|
|
|
this.deactiveRouteAndItsChildren(v, outlet.outletMap._outlets[k]);
|
|
|
|
} else {
|
|
|
|
this.deactiveRouteAndItsChildren(v, null);
|
|
|
|
}
|
2016-06-19 17:44:20 -04:00
|
|
|
});
|
2016-10-20 16:32:38 -04:00
|
|
|
|
2016-11-01 15:11:42 -04:00
|
|
|
if (!r.component) {
|
2017-03-24 14:32:17 -04:00
|
|
|
this.canDeactivateChecks.push(new CanDeactivate(null, r));
|
2016-11-01 15:11:42 -04:00
|
|
|
} else if (outlet && outlet.isActivated) {
|
2017-03-24 14:32:17 -04:00
|
|
|
this.canDeactivateChecks.push(new CanDeactivate(outlet.component, r));
|
2016-11-01 15:11:42 -04:00
|
|
|
} else {
|
2017-03-24 14:32:17 -04:00
|
|
|
this.canDeactivateChecks.push(new CanDeactivate(null, r));
|
2016-11-01 15:11:42 -04:00
|
|
|
}
|
2016-06-19 17:44:20 -04:00
|
|
|
}
|
|
|
|
|
2017-03-24 14:32:17 -04:00
|
|
|
private runCanDeactivateChecks(): Observable<boolean> {
|
|
|
|
const checks$ = from(this.canDeactivateChecks);
|
|
|
|
const runningChecks$ = mergeMap.call(
|
|
|
|
checks$, (check: CanDeactivate) => this.runCanDeactivate(check.component, check.route));
|
|
|
|
return every.call(runningChecks$, (result: boolean) => result === true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private runCanActivateChecks(): Observable<boolean> {
|
|
|
|
const checks$ = from(this.canActivateChecks);
|
|
|
|
const runningChecks$ = mergeMap.call(
|
|
|
|
checks$, (check: CanActivate) => andObservables(from(
|
|
|
|
[this.runCanActivateChild(check.path), this.runCanActivate(check.route)])));
|
|
|
|
return every.call(runningChecks$, (result: boolean) => result === true);
|
|
|
|
}
|
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
private runCanActivate(future: ActivatedRouteSnapshot): Observable<boolean> {
|
2016-06-01 17:32:15 -04:00
|
|
|
const canActivate = future._routeConfig ? future._routeConfig.canActivate : null;
|
2016-07-15 19:27:54 -04:00
|
|
|
if (!canActivate || canActivate.length === 0) return of (true);
|
2016-08-30 17:25:46 -04:00
|
|
|
const obs = map.call(from(canActivate), (c: any) => {
|
2016-08-10 18:53:57 -04:00
|
|
|
const guard = this.getToken(c, future);
|
2016-11-03 00:56:04 -04:00
|
|
|
let observable: Observable<boolean>;
|
2016-07-13 18:15:20 -04:00
|
|
|
if (guard.canActivate) {
|
2016-11-03 00:56:04 -04:00
|
|
|
observable = wrapIntoObservable(guard.canActivate(future, this.future));
|
2016-07-13 18:15:20 -04:00
|
|
|
} else {
|
2016-11-03 00:56:04 -04:00
|
|
|
observable = wrapIntoObservable(guard(future, this.future));
|
2016-07-13 18:15:20 -04:00
|
|
|
}
|
2016-11-03 00:56:04 -04:00
|
|
|
return first.call(observable);
|
2016-07-13 18:15:20 -04:00
|
|
|
});
|
|
|
|
return andObservables(obs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private runCanActivateChild(path: ActivatedRouteSnapshot[]): Observable<boolean> {
|
|
|
|
const future = path[path.length - 1];
|
|
|
|
|
2016-07-13 21:12:59 -04:00
|
|
|
const canActivateChildGuards = path.slice(0, path.length - 1)
|
|
|
|
.reverse()
|
|
|
|
.map(p => this.extractCanActivateChild(p))
|
|
|
|
.filter(_ => _ !== null);
|
2016-07-13 18:15:20 -04:00
|
|
|
|
2016-08-30 17:25:46 -04:00
|
|
|
return andObservables(map.call(from(canActivateChildGuards), (d: any) => {
|
|
|
|
const obs = map.call(from(d.guards), (c: any) => {
|
2017-01-18 10:07:25 -05:00
|
|
|
const guard = this.getToken(c, d.node);
|
2016-11-03 00:56:04 -04:00
|
|
|
let observable: Observable<boolean>;
|
2016-07-13 18:15:20 -04:00
|
|
|
if (guard.canActivateChild) {
|
2016-11-03 00:56:04 -04:00
|
|
|
observable = wrapIntoObservable(guard.canActivateChild(future, this.future));
|
2016-07-13 18:15:20 -04:00
|
|
|
} else {
|
2016-11-03 00:56:04 -04:00
|
|
|
observable = wrapIntoObservable(guard(future, this.future));
|
2016-07-13 18:15:20 -04:00
|
|
|
}
|
2016-11-03 00:56:04 -04:00
|
|
|
return first.call(observable);
|
2016-07-13 18:15:20 -04:00
|
|
|
});
|
|
|
|
return andObservables(obs);
|
|
|
|
}));
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
2016-06-02 17:44:57 -04:00
|
|
|
|
2016-07-13 21:12:59 -04:00
|
|
|
private extractCanActivateChild(p: ActivatedRouteSnapshot):
|
2017-04-17 12:47:59 -04:00
|
|
|
{node: ActivatedRouteSnapshot, guards: any[]} {
|
2016-07-13 21:12:59 -04:00
|
|
|
const canActivateChild = p._routeConfig ? p._routeConfig.canActivateChild : null;
|
|
|
|
if (!canActivateChild || canActivateChild.length === 0) return null;
|
|
|
|
return {node: p, guards: canActivateChild};
|
|
|
|
}
|
2016-07-13 18:15:20 -04:00
|
|
|
|
2017-04-17 12:47:59 -04:00
|
|
|
private runCanDeactivate(component: Object, curr: ActivatedRouteSnapshot): Observable<boolean> {
|
2016-06-24 14:39:12 -04:00
|
|
|
const canDeactivate = curr && curr._routeConfig ? curr._routeConfig.canDeactivate : null;
|
2016-07-15 19:27:54 -04:00
|
|
|
if (!canDeactivate || canDeactivate.length === 0) return of (true);
|
2016-11-03 00:56:04 -04:00
|
|
|
const canDeactivate$ = mergeMap.call(from(canDeactivate), (c: any) => {
|
2016-08-30 17:25:46 -04:00
|
|
|
const guard = this.getToken(c, curr);
|
2016-11-03 00:56:04 -04:00
|
|
|
let observable: Observable<boolean>;
|
2016-08-30 17:25:46 -04:00
|
|
|
if (guard.canDeactivate) {
|
2016-12-27 17:08:06 -05:00
|
|
|
observable =
|
|
|
|
wrapIntoObservable(guard.canDeactivate(component, curr, this.curr, this.future));
|
2016-08-30 17:25:46 -04:00
|
|
|
} else {
|
2016-12-27 17:08:06 -05:00
|
|
|
observable = wrapIntoObservable(guard(component, curr, this.curr, this.future));
|
2016-08-30 17:25:46 -04:00
|
|
|
}
|
2016-11-03 00:56:04 -04:00
|
|
|
return first.call(observable);
|
2016-08-30 17:25:46 -04:00
|
|
|
});
|
2016-11-03 00:56:04 -04:00
|
|
|
return every.call(canDeactivate$, (result: any) => result === true);
|
2016-06-02 17:44:57 -04:00
|
|
|
}
|
2016-06-27 17:00:07 -04:00
|
|
|
|
|
|
|
private runResolve(future: ActivatedRouteSnapshot): Observable<any> {
|
|
|
|
const resolve = future._resolve;
|
2016-10-25 17:33:18 -04:00
|
|
|
return map.call(this.resolveNode(resolve, future), (resolvedData: any): any => {
|
|
|
|
future._resolvedData = resolvedData;
|
2017-03-26 10:15:10 -04:00
|
|
|
future.data = {...future.data, ...inheritedParamsDataResolve(future).resolve};
|
2016-06-27 17:00:07 -04:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private resolveNode(resolve: ResolveData, future: ActivatedRouteSnapshot): Observable<any> {
|
2016-07-06 14:02:16 -04:00
|
|
|
return waitForMap(resolve, (k, v) => {
|
2016-08-10 18:53:57 -04:00
|
|
|
const resolver = this.getToken(v, future);
|
2016-07-06 14:02:16 -04:00
|
|
|
return resolver.resolve ? wrapIntoObservable(resolver.resolve(future, this.future)) :
|
|
|
|
wrapIntoObservable(resolver(future, this.future));
|
2016-06-27 17:00:07 -04:00
|
|
|
});
|
|
|
|
}
|
2016-07-13 21:12:59 -04:00
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
private getToken(token: any, snapshot: ActivatedRouteSnapshot): any {
|
|
|
|
const config = closestLoadedConfig(snapshot);
|
2017-03-14 19:26:17 -04:00
|
|
|
const injector = config ? config.module.injector : this.moduleInjector;
|
2016-07-13 21:12:59 -04:00
|
|
|
return injector.get(token);
|
|
|
|
}
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:23:27 -04:00
|
|
|
class ActivateRoutes {
|
2016-11-30 02:21:41 -05:00
|
|
|
constructor(
|
|
|
|
private routeReuseStrategy: RouteReuseStrategy, private futureState: RouterState,
|
|
|
|
private currState: RouterState) {}
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
activate(parentOutletMap: RouterOutletMap): void {
|
2016-06-01 16:34:48 -04:00
|
|
|
const futureRoot = this.futureState._root;
|
|
|
|
const currRoot = this.currState ? this.currState._root : null;
|
2016-11-03 19:26:10 -04:00
|
|
|
|
|
|
|
this.deactivateChildRoutes(futureRoot, currRoot, parentOutletMap);
|
2016-06-26 14:52:32 -04:00
|
|
|
advanceActivatedRoute(this.futureState.root);
|
2016-06-01 17:32:15 -04:00
|
|
|
this.activateChildRoutes(futureRoot, currRoot, parentOutletMap);
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
2016-11-03 19:26:10 -04:00
|
|
|
private deactivateChildRoutes(
|
2017-04-17 12:47:59 -04:00
|
|
|
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
|
2016-06-08 14:13:41 -04:00
|
|
|
outletMap: RouterOutletMap): void {
|
2016-06-15 12:14:41 -04:00
|
|
|
const prevChildren: {[key: string]: any} = nodeChildrenAsMap(currNode);
|
2016-06-01 17:32:15 -04:00
|
|
|
futureNode.children.forEach(c => {
|
2016-11-03 19:26:10 -04:00
|
|
|
this.deactivateRoutes(c, prevChildren[c.value.outlet], outletMap);
|
2016-05-24 16:23:27 -04:00
|
|
|
delete prevChildren[c.value.outlet];
|
2016-06-01 17:32:15 -04:00
|
|
|
});
|
2016-10-24 15:50:00 -04:00
|
|
|
forEach(prevChildren, (v: any, k: string) => this.deactiveRouteAndItsChildren(v, outletMap));
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
2016-11-03 19:26:10 -04:00
|
|
|
private activateChildRoutes(
|
2017-04-17 12:47:59 -04:00
|
|
|
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
|
2016-11-03 19:26:10 -04:00
|
|
|
outletMap: RouterOutletMap): void {
|
|
|
|
const prevChildren: {[key: string]: any} = nodeChildrenAsMap(currNode);
|
|
|
|
futureNode.children.forEach(
|
|
|
|
c => { this.activateRoutes(c, prevChildren[c.value.outlet], outletMap); });
|
|
|
|
}
|
|
|
|
|
|
|
|
deactivateRoutes(
|
|
|
|
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
|
|
|
|
parentOutletMap: RouterOutletMap): void {
|
|
|
|
const future = futureNode.value;
|
|
|
|
const curr = currNode ? currNode.value : null;
|
|
|
|
|
|
|
|
// reusing the node
|
|
|
|
if (future === curr) {
|
|
|
|
// If we have a normal route, we need to go through an outlet.
|
|
|
|
if (future.component) {
|
|
|
|
const outlet = getOutlet(parentOutletMap, future);
|
|
|
|
this.deactivateChildRoutes(futureNode, currNode, outlet.outletMap);
|
|
|
|
|
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
|
|
|
} else {
|
|
|
|
this.deactivateChildRoutes(futureNode, currNode, parentOutletMap);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (curr) {
|
|
|
|
this.deactiveRouteAndItsChildren(currNode, parentOutletMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
activateRoutes(
|
2016-06-15 12:14:41 -04:00
|
|
|
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
|
2016-06-08 14:13:41 -04:00
|
|
|
parentOutletMap: RouterOutletMap): void {
|
2016-05-24 16:23:27 -04:00
|
|
|
const future = futureNode.value;
|
|
|
|
const curr = currNode ? currNode.value : null;
|
2016-06-14 17:55:59 -04:00
|
|
|
|
2016-06-19 17:44:20 -04:00
|
|
|
// reusing the node
|
2016-05-24 16:23:27 -04:00
|
|
|
if (future === curr) {
|
2016-06-19 17:44:20 -04:00
|
|
|
// advance the route to push the parameters
|
2016-06-02 17:44:57 -04:00
|
|
|
advanceActivatedRoute(future);
|
2016-06-19 17:44:20 -04:00
|
|
|
|
|
|
|
// If we have a normal route, we need to go through an outlet.
|
|
|
|
if (future.component) {
|
2016-10-24 15:50:00 -04:00
|
|
|
const outlet = getOutlet(parentOutletMap, future);
|
2016-06-19 17:44:20 -04:00
|
|
|
this.activateChildRoutes(futureNode, currNode, outlet.outletMap);
|
|
|
|
|
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
|
|
|
} else {
|
|
|
|
this.activateChildRoutes(futureNode, currNode, parentOutletMap);
|
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
} else {
|
2016-06-19 17:44:20 -04:00
|
|
|
// if we have a normal route, we need to advance the route
|
|
|
|
// and place the component into the outlet. After that recurse.
|
|
|
|
if (future.component) {
|
|
|
|
advanceActivatedRoute(future);
|
|
|
|
const outlet = getOutlet(parentOutletMap, futureNode.value);
|
2016-11-30 02:21:41 -05:00
|
|
|
|
|
|
|
if (this.routeReuseStrategy.shouldAttach(future.snapshot)) {
|
|
|
|
const stored =
|
|
|
|
(<DetachedRouteHandleInternal>this.routeReuseStrategy.retrieve(future.snapshot));
|
|
|
|
this.routeReuseStrategy.store(future.snapshot, null);
|
|
|
|
outlet.attach(stored.componentRef, stored.route.value);
|
|
|
|
advanceActivatedRouteNodeAndItsChildren(stored.route);
|
|
|
|
} else {
|
|
|
|
const outletMap = new RouterOutletMap();
|
|
|
|
this.placeComponentIntoOutlet(outletMap, future, outlet);
|
|
|
|
this.activateChildRoutes(futureNode, null, outletMap);
|
|
|
|
}
|
2016-06-19 17:44:20 -04:00
|
|
|
|
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
|
|
|
} else {
|
|
|
|
advanceActivatedRoute(future);
|
|
|
|
this.activateChildRoutes(futureNode, null, parentOutletMap);
|
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 17:44:20 -04:00
|
|
|
private placeComponentIntoOutlet(
|
2016-06-08 14:13:41 -04:00
|
|
|
outletMap: RouterOutletMap, future: ActivatedRoute, outlet: RouterOutlet): void {
|
2016-08-10 18:53:57 -04:00
|
|
|
const config = parentLoadedConfig(future.snapshot);
|
2017-03-14 19:26:17 -04:00
|
|
|
const cmpFactoryResolver = config ? config.module.componentFactoryResolver : null;
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2017-03-14 19:26:17 -04:00
|
|
|
outlet.activateWith(future, cmpFactoryResolver, outletMap);
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 15:50:00 -04:00
|
|
|
private deactiveRouteAndItsChildren(
|
|
|
|
route: TreeNode<ActivatedRoute>, parentOutletMap: RouterOutletMap): void {
|
2016-11-30 02:21:41 -05:00
|
|
|
if (this.routeReuseStrategy.shouldDetach(route.value.snapshot)) {
|
|
|
|
this.detachAndStoreRouteSubtree(route, parentOutletMap);
|
|
|
|
} else {
|
|
|
|
this.deactiveRouteAndOutlet(route, parentOutletMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private detachAndStoreRouteSubtree(
|
|
|
|
route: TreeNode<ActivatedRoute>, parentOutletMap: RouterOutletMap): void {
|
|
|
|
const outlet = getOutlet(parentOutletMap, route.value);
|
|
|
|
const componentRef = outlet.detach();
|
|
|
|
this.routeReuseStrategy.store(route.value.snapshot, {componentRef, route});
|
|
|
|
}
|
|
|
|
|
|
|
|
private deactiveRouteAndOutlet(route: TreeNode<ActivatedRoute>, parentOutletMap: RouterOutletMap):
|
|
|
|
void {
|
2016-10-24 15:50:00 -04:00
|
|
|
const prevChildren: {[key: string]: any} = nodeChildrenAsMap(route);
|
2017-04-17 12:47:59 -04:00
|
|
|
let outlet: RouterOutlet = null;
|
2016-10-24 15:50:00 -04:00
|
|
|
|
|
|
|
// getOutlet throws when cannot find the right outlet,
|
|
|
|
// which can happen if an outlet was in an NgIf and was removed
|
|
|
|
try {
|
|
|
|
outlet = getOutlet(parentOutletMap, route.value);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const childOutletMap = outlet.outletMap;
|
|
|
|
|
|
|
|
forEach(prevChildren, (v: any, k: string) => {
|
|
|
|
if (route.value.component) {
|
|
|
|
this.deactiveRouteAndItsChildren(v, childOutletMap);
|
|
|
|
} else {
|
|
|
|
this.deactiveRouteAndItsChildren(v, parentOutletMap);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-24 16:23:27 -04:00
|
|
|
if (outlet && outlet.isActivated) {
|
|
|
|
outlet.deactivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 02:21:41 -05:00
|
|
|
function advanceActivatedRouteNodeAndItsChildren(node: TreeNode<ActivatedRoute>): void {
|
|
|
|
advanceActivatedRoute(node.value);
|
|
|
|
node.children.forEach(advanceActivatedRouteNodeAndItsChildren);
|
|
|
|
}
|
|
|
|
|
2017-04-17 12:47:59 -04:00
|
|
|
function parentLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConfig {
|
2017-03-29 12:44:04 -04:00
|
|
|
for (let s = snapshot.parent; s; s = s.parent) {
|
2017-04-11 11:34:58 -04:00
|
|
|
const route = s._routeConfig;
|
2017-03-29 12:44:04 -04:00
|
|
|
if (route && route._loadedConfig) return route._loadedConfig;
|
|
|
|
if (route && route.component) return null;
|
2016-08-10 18:53:57 -04:00
|
|
|
}
|
2017-03-29 12:44:04 -04:00
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-17 12:47:59 -04:00
|
|
|
function closestLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConfig {
|
2016-08-10 18:53:57 -04:00
|
|
|
if (!snapshot) return null;
|
|
|
|
|
2017-03-29 12:44:04 -04:00
|
|
|
for (let s = snapshot.parent; s; s = s.parent) {
|
2017-04-11 11:34:58 -04:00
|
|
|
const route = s._routeConfig;
|
2017-03-29 12:44:04 -04:00
|
|
|
if (route && route._loadedConfig) return route._loadedConfig;
|
2016-08-10 18:53:57 -04:00
|
|
|
}
|
2017-03-29 12:44:04 -04:00
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
return null;
|
2016-07-13 21:12:59 -04:00
|
|
|
}
|
|
|
|
|
2017-04-17 12:47:59 -04:00
|
|
|
function nodeChildrenAsMap<T extends{outlet: string}>(node: TreeNode<T>) {
|
2017-03-29 12:44:04 -04:00
|
|
|
const map: {[key: string]: TreeNode<T>} = {};
|
|
|
|
|
|
|
|
if (node) {
|
|
|
|
node.children.forEach(child => map[child.value.outlet] = child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getOutlet(outletMap: RouterOutletMap, route: ActivatedRoute): RouterOutlet {
|
2016-11-12 08:08:58 -05:00
|
|
|
const outlet = outletMap._outlets[route.outlet];
|
2016-05-24 16:23:27 -04:00
|
|
|
if (!outlet) {
|
2016-06-14 17:55:59 -04:00
|
|
|
const componentName = (<any>route.component).name;
|
2016-05-24 16:23:27 -04:00
|
|
|
if (route.outlet === PRIMARY_OUTLET) {
|
2016-06-14 17:55:59 -04:00
|
|
|
throw new Error(`Cannot find primary outlet to load '${componentName}'`);
|
2016-05-24 16:23:27 -04:00
|
|
|
} else {
|
2016-06-14 17:55:59 -04:00
|
|
|
throw new Error(`Cannot find the outlet ${route.outlet} to load '${componentName}'`);
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return outlet;
|
|
|
|
}
|
2016-12-11 17:33:21 -05:00
|
|
|
|
|
|
|
function validateCommands(commands: string[]): void {
|
|
|
|
for (let i = 0; i < commands.length; i++) {
|
|
|
|
const cmd = commands[i];
|
|
|
|
if (cmd == null) {
|
|
|
|
throw new Error(`The requested path contains ${cmd} segment at index ${i}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|