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 {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 {map} from 'rxjs/operator/map';
|
|
|
|
import {mergeMap} from 'rxjs/operator/mergeMap';
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2016-06-08 19:14:26 -04:00
|
|
|
import {applyRedirects} from './apply_redirects';
|
2017-07-25 14:13:15 -04:00
|
|
|
import {LoadedRouterConfig, QueryParamsHandling, Route, Routes, validateConfig} from './config';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {createRouterState} from './create_router_state';
|
|
|
|
import {createUrlTree} from './create_url_tree';
|
2017-09-06 14:00:32 -04:00
|
|
|
import {ActivationEnd, ChildActivationEnd, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RoutesRecognized} from './events';
|
2017-07-25 14:13:15 -04:00
|
|
|
import {PreActivation} from './pre_activation';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {recognize} from './recognize';
|
2017-05-03 05:17:27 -04:00
|
|
|
import {DefaultRouteReuseStrategy, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy';
|
2017-04-04 19:00:40 -04:00
|
|
|
import {RouterConfigLoader} from './router_config_loader';
|
2017-07-25 14:13:15 -04:00
|
|
|
import {ChildrenOutletContexts} from './router_outlet_context';
|
|
|
|
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState} from './router_state';
|
2017-05-17 20:47:34 -04:00
|
|
|
import {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-07-25 14:13:15 -04:00
|
|
|
import {forEach} from './utils/collection';
|
|
|
|
import {TreeNode, nodeChildrenAsMap} from './utils/tree';
|
2016-06-08 14:13:41 -04:00
|
|
|
|
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
|
|
|
/**
|
2017-10-06 21:15:19 -04:00
|
|
|
* Enables relative navigation from the current ActivatedRoute.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
* }]
|
2017-10-06 21:15:19 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Navigate to list route from child route:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component({...})
|
|
|
|
* class ChildComponent {
|
2016-08-19 18:48:09 -04:00
|
|
|
* 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
|
|
|
* }
|
|
|
|
* }
|
2017-10-06 21:15:19 -04:00
|
|
|
* ```
|
|
|
|
*/
|
2017-04-14 19:01:48 -04:00
|
|
|
relativeTo?: ActivatedRoute|null;
|
2016-09-10 19:51:27 -04:00
|
|
|
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2017-10-06 21:15:19 -04:00
|
|
|
* Sets query parameters to the URL.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Navigate to /results?page=1
|
|
|
|
* this.router.navigate(['/results'], { queryParams: { page: 1 } });
|
|
|
|
* ```
|
|
|
|
*/
|
2017-04-14 19:01:48 -04:00
|
|
|
queryParams?: Params|null;
|
2016-09-10 19:51:27 -04:00
|
|
|
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2017-10-06 21:15:19 -04:00
|
|
|
* Sets the hash fragment for the URL.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Navigate to /results#top
|
|
|
|
* this.router.navigate(['/results'], { fragment: 'top' });
|
|
|
|
* ```
|
|
|
|
*/
|
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
|
|
|
/**
|
2017-10-06 21:15:19 -04:00
|
|
|
* Preserves the query parameters for the next navigation.
|
|
|
|
*
|
|
|
|
* deprecated, use `queryParamsHandling` instead
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Preserve query params from /results?page=1 to /view?page=1
|
|
|
|
* this.router.navigate(['/view'], { preserveQueryParams: true });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @deprecated since v4
|
|
|
|
*/
|
2016-07-20 17:30:04 -04:00
|
|
|
preserveQueryParams?: boolean;
|
2017-01-25 04:33:13 -05:00
|
|
|
|
|
|
|
/**
|
2017-10-06 21:15:19 -04: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" });
|
|
|
|
* ```
|
|
|
|
*/
|
2017-04-14 19:01:48 -04:00
|
|
|
queryParamsHandling?: QueryParamsHandling|null;
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2017-10-06 21:15:19 -04:00
|
|
|
* Preserves the fragment for the next navigation
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Preserve fragment from /results#top to /view#top
|
|
|
|
* this.router.navigate(['/view'], { preserveFragment: true });
|
|
|
|
* ```
|
|
|
|
*/
|
2016-07-20 17:30:04 -04:00
|
|
|
preserveFragment?: boolean;
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2017-10-06 21:15:19 -04:00
|
|
|
* Navigates without pushing a new state into history.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Navigate silently to /view
|
|
|
|
* this.router.navigate(['/view'], { skipLocationChange: true });
|
|
|
|
* ```
|
|
|
|
*/
|
2016-08-04 14:46:09 -04:00
|
|
|
skipLocationChange?: boolean;
|
2016-08-19 18:48:09 -04:00
|
|
|
/**
|
2017-10-06 21:15:19 -04:00
|
|
|
* Navigates while replacing the current state in history.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // Navigate to /view
|
|
|
|
* this.router.navigate(['/view'], { replaceUrl: true });
|
|
|
|
* ```
|
|
|
|
*/
|
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 14:13:13 -04:00
|
|
|
return of (null) as any;
|
2017-03-07 17:27:20 -05:00
|
|
|
}
|
|
|
|
|
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;
|
2017-04-17 14:13:13 -04:00
|
|
|
private navigations = new BehaviorSubject<NavigationParams>(null !);
|
2016-10-20 13:44:44 -04:00
|
|
|
|
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
|
|
|
|
2017-09-11 15:39:44 -04:00
|
|
|
public readonly events: Observable<Event> = new Subject<Event>();
|
|
|
|
public readonly routerState: RouterState;
|
|
|
|
|
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 14:13:13 -04:00
|
|
|
private rootComponentType: Type<any>|null, private urlSerializer: UrlSerializer,
|
2017-05-17 20:47:34 -04:00
|
|
|
private rootContexts: ChildrenOutletContexts, 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);
|
2017-09-11 15:39:44 -04:00
|
|
|
this.routerState = 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.
|
2017-09-11 15:39:44 -04:00
|
|
|
this.routerState.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 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
|
|
|
|
2017-02-15 13:57:03 -05:00
|
|
|
/** @internal */
|
2017-09-11 15:39:44 -04:00
|
|
|
triggerEvent(e: Event): void { (this.events as Subject<Event>).next(e); }
|
2017-02-15 13:57:03 -05:00
|
|
|
|
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;
|
2017-03-28 15:11:44 -04:00
|
|
|
this.navigated = false;
|
2016-06-16 17:36:51 -04:00
|
|
|
}
|
2016-05-26 19:51:56 -04:00
|
|
|
|
2016-12-09 13:44:46 -05:00
|
|
|
/** @docsNotRequired */
|
2017-03-28 15:11:44 -04:00
|
|
|
ngOnDestroy(): void { this.dispose(); }
|
2016-08-02 05:32:27 -04:00
|
|
|
|
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 14:13:13 -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});
|
|
|
|
* ```
|
|
|
|
*/
|
2017-06-12 13:59:29 -04:00
|
|
|
createUrlTree(commands: any[], navigationExtras: NavigationExtras = {}): UrlTree {
|
|
|
|
const {relativeTo, queryParams, fragment,
|
|
|
|
preserveQueryParams, queryParamsHandling, preserveFragment} = navigationExtras;
|
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 14:13:13 -04:00
|
|
|
let q: Params|null = 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 14:13:13 -04:00
|
|
|
q = queryParams || null;
|
2017-01-25 04:33:13 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-17 14:13:13 -04:00
|
|
|
q = preserveQueryParams ? this.currentUrlTree.queryParams : queryParams || null;
|
2017-01-25 04:33:13 -05:00
|
|
|
}
|
2017-04-17 14:13:13 -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);
|
|
|
|
}
|
2017-05-03 05:17:27 -04:00
|
|
|
|
|
|
|
const urlTree = this.urlSerializer.parse(url);
|
|
|
|
return containsTree(this.currentUrlTree, urlTree, exact);
|
2016-07-28 20:59:05 -04:00
|
|
|
}
|
|
|
|
|
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 14:13:13 -04:00
|
|
|
return Promise.resolve(true); // 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
|
2017-10-06 21:15:19 -04:00
|
|
|
// flicker.
|
2016-12-21 15:47:58 -05:00
|
|
|
if (lastNavigation && source == 'hashchange' && lastNavigation.source === 'popstate' &&
|
|
|
|
lastNavigation.rawUrl.toString() === rawUrl.toString()) {
|
2017-04-17 14:13:13 -04:00
|
|
|
return Promise.resolve(true); // 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;
|
|
|
|
|
2017-08-02 20:32:02 -04:00
|
|
|
const promise = new Promise<boolean>((res, rej) => {
|
2016-10-28 17:56:08 -04:00
|
|
|
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
|
|
|
|
2017-10-06 21:15:19 -04:00
|
|
|
if (urlTransition && this.urlHandlingStrategy.shouldProcessUrl(rawUrl)) {
|
2017-09-11 15:39:44 -04:00
|
|
|
(this.events as Subject<Event>).next(new NavigationStart(id, this.serializeUrl(url)));
|
2016-10-28 17:56:08 -04:00
|
|
|
Promise.resolve()
|
|
|
|
.then(
|
|
|
|
(_) => this.runNavigate(
|
2017-04-17 14:13:13 -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)) {
|
2017-09-11 15:39:44 -04:00
|
|
|
(this.events as Subject<Event>).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(
|
2017-10-06 21:15:16 -04:00
|
|
|
url: UrlTree, rawUrl: UrlTree, shouldPreventPushState: boolean, shouldReplaceUrl: boolean,
|
|
|
|
id: number, precreatedState: RouterStateSnapshot|null): Promise<boolean> {
|
2016-06-03 17:07:01 -04:00
|
|
|
if (id !== this.navigationId) {
|
2017-10-06 21:15:16 -04:00
|
|
|
this.location.go(this.urlSerializer.serialize(this.currentUrlTree));
|
2017-09-11 15:39:44 -04:00
|
|
|
(this.events as Subject<Event>)
|
|
|
|
.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
|
|
|
|
2017-09-11 15:39:44 -04:00
|
|
|
(this.events as Subject<Event>)
|
|
|
|
.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-05-03 05:17:27 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
const preactivationSetup$ = map.call(
|
2017-03-07 17:27:20 -05:00
|
|
|
beforePreactivationDone$,
|
|
|
|
({appliedUrl, snapshot}: {appliedUrl: string, snapshot: RouterStateSnapshot}) => {
|
2017-03-14 19:26:17 -04:00
|
|
|
const moduleInjector = this.ngModule.injector;
|
2017-07-25 14:13:15 -04:00
|
|
|
preActivation = new PreActivation(
|
2017-09-11 15:39:44 -04:00
|
|
|
snapshot, this.routerState.snapshot, moduleInjector,
|
2017-09-05 14:54:33 -04:00
|
|
|
(evt: Event) => this.triggerEvent(evt));
|
2017-09-28 14:06:08 -04:00
|
|
|
preActivation.initialize(this.rootContexts);
|
2017-03-07 17:27:20 -05:00
|
|
|
return {appliedUrl, snapshot};
|
|
|
|
});
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2017-03-07 17:27:20 -05:00
|
|
|
const preactivationCheckGuards$ = mergeMap.call(
|
2017-07-25 14:13:15 -04:00
|
|
|
preactivationSetup$,
|
2017-03-07 17:27:20 -05:00
|
|
|
({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
|
|
|
|
2017-07-01 13:30:17 -04:00
|
|
|
this.triggerEvent(
|
|
|
|
new GuardsCheckStart(id, this.serializeUrl(url), appliedUrl, snapshot));
|
|
|
|
|
2016-11-15 22:00:20 -05:00
|
|
|
return map.call(preActivation.checkGuards(), (shouldActivate: boolean) => {
|
2017-07-01 13:30:17 -04:00
|
|
|
this.triggerEvent(new GuardsCheckEnd(
|
|
|
|
id, this.serializeUrl(url), appliedUrl, snapshot, shouldActivate));
|
2016-11-15 22:00:20 -05:00
|
|
|
return {appliedUrl: appliedUrl, snapshot: snapshot, shouldActivate: shouldActivate};
|
|
|
|
});
|
|
|
|
});
|
2016-08-30 17:25:46 -04:00
|
|
|
|
2017-07-01 13:30:17 -04:00
|
|
|
const preactivationResolveData$ = mergeMap.call(
|
|
|
|
preactivationCheckGuards$,
|
|
|
|
(p: {appliedUrl: string, snapshot: RouterStateSnapshot, shouldActivate: boolean}) => {
|
|
|
|
if (this.navigationId !== id) return of (false);
|
2016-10-28 17:56:08 -04:00
|
|
|
|
2017-07-01 13:30:17 -04:00
|
|
|
if (p.shouldActivate && preActivation.isActivating()) {
|
|
|
|
this.triggerEvent(
|
|
|
|
new ResolveStart(id, this.serializeUrl(url), p.appliedUrl, p.snapshot));
|
|
|
|
return map.call(preActivation.resolveData(), () => {
|
|
|
|
this.triggerEvent(
|
|
|
|
new ResolveEnd(id, this.serializeUrl(url), p.appliedUrl, p.snapshot));
|
|
|
|
return p;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
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) {
|
2017-09-11 15:39:44 -04:00
|
|
|
const state = createRouterState(this.routeReuseStrategy, snapshot, this.routerState);
|
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;
|
2017-09-11 15:39:44 -04:00
|
|
|
const storedState = this.routerState;
|
2016-11-15 22:00:20 -05:00
|
|
|
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);
|
|
|
|
|
2017-09-11 15:39:44 -04:00
|
|
|
(this as{routerState: RouterState}).routerState = state;
|
2016-07-12 15:28:08 -04:00
|
|
|
|
2017-10-06 21:15:16 -04:00
|
|
|
if (!shouldPreventPushState) {
|
2016-11-02 14:30:00 -04:00
|
|
|
const path = this.urlSerializer.serialize(this.rawUrlTree);
|
2017-10-06 21:15:16 -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
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
new ActivateRoutes(
|
|
|
|
this.routeReuseStrategy, state, storedState, (evt: Event) => this.triggerEvent(evt))
|
2017-05-17 20:47:34 -04:00
|
|
|
.activate(this.rootContexts);
|
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;
|
2017-09-11 15:39:44 -04:00
|
|
|
(this.events as Subject<Event>)
|
|
|
|
.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();
|
2017-09-11 15:39:44 -04:00
|
|
|
(this.events as Subject<Event>)
|
|
|
|
.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;
|
2017-09-11 15:39:44 -04:00
|
|
|
(this.events as Subject<Event>)
|
|
|
|
.next(new NavigationCancel(id, this.serializeUrl(url), e.message));
|
2016-08-24 13:20:44 -04:00
|
|
|
resolvePromise(false);
|
|
|
|
} else {
|
2017-09-11 15:39:44 -04:00
|
|
|
(this.events as Subject<Event>)
|
|
|
|
.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
|
|
|
|
2017-09-11 15:39:44 -04:00
|
|
|
(this as{routerState: RouterState}).routerState = storedState;
|
2016-11-02 14:30:00 -04:00
|
|
|
this.currentUrlTree = storedUrl;
|
|
|
|
this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, rawUrl);
|
2017-10-06 21:15:16 -04: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 {
|
2017-10-06 21:15:16 -04: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
|
|
|
}
|
|
|
|
|
|
|
|
class ActivateRoutes {
|
2016-11-30 02:21:41 -05:00
|
|
|
constructor(
|
|
|
|
private routeReuseStrategy: RouteReuseStrategy, private futureState: RouterState,
|
2017-09-05 14:54:33 -04:00
|
|
|
private currState: RouterState, private forwardEvent: (evt: Event) => void) {}
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2017-05-17 20:47:34 -04:00
|
|
|
activate(parentContexts: ChildrenOutletContexts): 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
|
|
|
|
2017-05-17 20:47:34 -04:00
|
|
|
this.deactivateChildRoutes(futureRoot, currRoot, parentContexts);
|
2016-06-26 14:52:32 -04:00
|
|
|
advanceActivatedRoute(this.futureState.root);
|
2017-05-17 20:47:34 -04:00
|
|
|
this.activateChildRoutes(futureRoot, currRoot, parentContexts);
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
2017-05-03 05:17:27 -04:00
|
|
|
// De-activate the child route that are not re-used for the future state
|
2016-11-03 19:26:10 -04:00
|
|
|
private deactivateChildRoutes(
|
2017-04-17 14:13:13 -04:00
|
|
|
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>|null,
|
2017-05-17 20:47:34 -04:00
|
|
|
contexts: ChildrenOutletContexts): void {
|
|
|
|
const children: {[outletName: string]: TreeNode<ActivatedRoute>} = nodeChildrenAsMap(currNode);
|
2017-05-03 05:17:27 -04:00
|
|
|
|
|
|
|
// Recurse on the routes active in the future state to de-activate deeper children
|
2017-05-17 20:47:34 -04:00
|
|
|
futureNode.children.forEach(futureChild => {
|
|
|
|
const childOutletName = futureChild.value.outlet;
|
|
|
|
this.deactivateRoutes(futureChild, children[childOutletName], contexts);
|
|
|
|
delete children[childOutletName];
|
2016-06-01 17:32:15 -04:00
|
|
|
});
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2017-05-03 05:17:27 -04:00
|
|
|
// De-activate the routes that will not be re-used
|
2017-05-17 20:47:34 -04:00
|
|
|
forEach(children, (v: TreeNode<ActivatedRoute>, childName: string) => {
|
|
|
|
this.deactivateRouteAndItsChildren(v, contexts);
|
|
|
|
});
|
2016-11-03 19:26:10 -04:00
|
|
|
}
|
|
|
|
|
2017-05-03 05:17:27 -04:00
|
|
|
private deactivateRoutes(
|
2016-11-03 19:26:10 -04:00
|
|
|
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
|
2017-05-17 20:47:34 -04:00
|
|
|
parentContext: ChildrenOutletContexts): void {
|
2016-11-03 19:26:10 -04:00
|
|
|
const future = futureNode.value;
|
|
|
|
const curr = currNode ? currNode.value : null;
|
|
|
|
|
|
|
|
if (future === curr) {
|
2017-05-17 20:47:34 -04:00
|
|
|
// Reusing the node, check to see if the children need to be de-activated
|
2016-11-03 19:26:10 -04:00
|
|
|
if (future.component) {
|
2017-05-03 05:17:27 -04:00
|
|
|
// If we have a normal route, we need to go through an outlet.
|
2017-05-17 20:47:34 -04:00
|
|
|
const context = parentContext.getContext(future.outlet);
|
|
|
|
if (context) {
|
|
|
|
this.deactivateChildRoutes(futureNode, currNode, context.children);
|
|
|
|
}
|
2017-05-18 14:28:50 -04:00
|
|
|
} else {
|
2017-05-03 05:17:27 -04:00
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
2017-05-17 20:47:34 -04:00
|
|
|
this.deactivateChildRoutes(futureNode, currNode, parentContext);
|
2016-11-03 19:26:10 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (curr) {
|
2017-05-17 20:47:34 -04:00
|
|
|
// Deactivate the current route which will not be re-used
|
|
|
|
this.deactivateRouteAndItsChildren(currNode, parentContext);
|
2016-11-03 19:26:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:17:27 -04:00
|
|
|
private deactivateRouteAndItsChildren(
|
2017-05-17 20:47:34 -04:00
|
|
|
route: TreeNode<ActivatedRoute>, parentContexts: ChildrenOutletContexts): void {
|
2017-05-03 05:17:27 -04:00
|
|
|
if (this.routeReuseStrategy.shouldDetach(route.value.snapshot)) {
|
2017-05-17 20:47:34 -04:00
|
|
|
this.detachAndStoreRouteSubtree(route, parentContexts);
|
2017-05-03 05:17:27 -04:00
|
|
|
} else {
|
2017-05-17 20:47:34 -04:00
|
|
|
this.deactivateRouteAndOutlet(route, parentContexts);
|
2017-05-03 05:17:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private detachAndStoreRouteSubtree(
|
2017-05-17 20:47:34 -04:00
|
|
|
route: TreeNode<ActivatedRoute>, parentContexts: ChildrenOutletContexts): void {
|
|
|
|
const context = parentContexts.getContext(route.value.outlet);
|
|
|
|
if (context && context.outlet) {
|
|
|
|
const componentRef = context.outlet.detach();
|
|
|
|
const contexts = context.children.onOutletDeactivated();
|
|
|
|
this.routeReuseStrategy.store(route.value.snapshot, {componentRef, route, contexts});
|
|
|
|
}
|
2017-05-03 05:17:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private deactivateRouteAndOutlet(
|
2017-05-17 20:47:34 -04:00
|
|
|
route: TreeNode<ActivatedRoute>, parentContexts: ChildrenOutletContexts): void {
|
|
|
|
const context = parentContexts.getContext(route.value.outlet);
|
2017-05-03 05:17:27 -04:00
|
|
|
|
2017-05-17 20:47:34 -04:00
|
|
|
if (context) {
|
|
|
|
const children: {[outletName: string]: any} = nodeChildrenAsMap(route);
|
|
|
|
const contexts = route.value.component ? context.children : parentContexts;
|
2017-05-03 05:17:27 -04:00
|
|
|
|
2017-07-27 19:13:16 -04:00
|
|
|
forEach(children, (v: any, k: string) => this.deactivateRouteAndItsChildren(v, contexts));
|
2017-05-03 05:17:27 -04:00
|
|
|
|
2017-05-17 20:47:34 -04:00
|
|
|
if (context.outlet) {
|
|
|
|
// Destroy the component
|
|
|
|
context.outlet.deactivate();
|
|
|
|
// Destroy the contexts for all the outlets that were in the component
|
|
|
|
context.children.onOutletDeactivated();
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 05:17:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private activateChildRoutes(
|
|
|
|
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>|null,
|
2017-05-17 20:47:34 -04:00
|
|
|
contexts: ChildrenOutletContexts): void {
|
|
|
|
const children: {[outlet: string]: any} = nodeChildrenAsMap(currNode);
|
2017-09-06 14:00:32 -04:00
|
|
|
futureNode.children.forEach(c => {
|
|
|
|
this.activateRoutes(c, children[c.value.outlet], contexts);
|
|
|
|
this.forwardEvent(new ActivationEnd(c.value.snapshot));
|
|
|
|
});
|
2017-09-04 16:00:59 -04:00
|
|
|
if (futureNode.children.length) {
|
|
|
|
this.forwardEvent(new ChildActivationEnd(futureNode.value.snapshot));
|
2017-07-25 14:13:15 -04:00
|
|
|
}
|
2017-05-03 05:17:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private activateRoutes(
|
2016-06-15 12:14:41 -04:00
|
|
|
futureNode: TreeNode<ActivatedRoute>, currNode: TreeNode<ActivatedRoute>,
|
2017-05-17 20:47:34 -04:00
|
|
|
parentContexts: ChildrenOutletContexts): 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
|
|
|
|
2017-05-03 05:17:27 -04:00
|
|
|
advanceActivatedRoute(future);
|
|
|
|
|
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
|
|
|
if (future.component) {
|
2017-05-03 05:17:27 -04:00
|
|
|
// If we have a normal route, we need to go through an outlet.
|
2017-05-17 20:47:34 -04:00
|
|
|
const context = parentContexts.getOrCreateContext(future.outlet);
|
|
|
|
this.activateChildRoutes(futureNode, currNode, context.children);
|
2017-05-18 14:28:50 -04:00
|
|
|
} else {
|
2017-05-03 05:17:27 -04:00
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
2017-05-17 20:47:34 -04:00
|
|
|
this.activateChildRoutes(futureNode, currNode, parentContexts);
|
2016-06-19 17:44:20 -04:00
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
} else {
|
2016-06-19 17:44:20 -04:00
|
|
|
if (future.component) {
|
2017-05-03 05:17:27 -04:00
|
|
|
// if we have a normal route, we need to place the component into the outlet and recurse.
|
2017-05-17 20:47:34 -04:00
|
|
|
const context = parentContexts.getOrCreateContext(future.outlet);
|
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);
|
2017-05-17 20:47:34 -04:00
|
|
|
context.children.onOutletReAttached(stored.contexts);
|
|
|
|
context.attachRef = stored.componentRef;
|
|
|
|
context.route = stored.route.value;
|
|
|
|
if (context.outlet) {
|
|
|
|
// Attach right away when the outlet has already been instantiated
|
|
|
|
// Otherwise attach from `RouterOutlet.ngOnInit` when it is instantiated
|
|
|
|
context.outlet.attach(stored.componentRef, stored.route.value);
|
|
|
|
}
|
2016-11-30 02:21:41 -05:00
|
|
|
advanceActivatedRouteNodeAndItsChildren(stored.route);
|
|
|
|
} else {
|
2017-05-17 20:47:34 -04:00
|
|
|
const config = parentLoadedConfig(future.snapshot);
|
|
|
|
const cmpFactoryResolver = config ? config.module.componentFactoryResolver : null;
|
|
|
|
|
|
|
|
context.route = future;
|
|
|
|
context.resolver = cmpFactoryResolver;
|
|
|
|
if (context.outlet) {
|
|
|
|
// Activate the outlet when it has already been instantiated
|
|
|
|
// Otherwise it will get activated from its `ngOnInit` when instantiated
|
|
|
|
context.outlet.activateWith(future, cmpFactoryResolver);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.activateChildRoutes(futureNode, null, context.children);
|
2016-11-30 02:21:41 -05:00
|
|
|
}
|
2017-05-18 14:28:50 -04:00
|
|
|
} else {
|
2017-05-03 05:17:27 -04:00
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
2017-05-17 20:47:34 -04:00
|
|
|
this.activateChildRoutes(futureNode, null, parentContexts);
|
2016-06-19 17:44:20 -04:00
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 02:21:41 -05:00
|
|
|
function advanceActivatedRouteNodeAndItsChildren(node: TreeNode<ActivatedRoute>): void {
|
|
|
|
advanceActivatedRoute(node.value);
|
|
|
|
node.children.forEach(advanceActivatedRouteNodeAndItsChildren);
|
|
|
|
}
|
|
|
|
|
2017-04-17 14:13:13 -04:00
|
|
|
function parentLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConfig|null {
|
2017-03-29 12:44:04 -04:00
|
|
|
for (let s = snapshot.parent; s; s = s.parent) {
|
2017-09-11 15:39:44 -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;
|
|
|
|
}
|
|
|
|
|
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}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|