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';
|
2016-08-16 16:40:28 -04:00
|
|
|
import {Compiler, ComponentFactoryResolver, Injector, NgModuleFactoryLoader, ReflectiveInjector, Type} from '@angular/core';
|
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-08-30 17:25:46 -04:00
|
|
|
import {every} from 'rxjs/operator/every';
|
|
|
|
import {map} from 'rxjs/operator/map';
|
|
|
|
import {mergeAll} from 'rxjs/operator/mergeAll';
|
|
|
|
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';
|
2016-07-06 19:19:52 -04:00
|
|
|
import {ResolveData, Routes, 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';
|
|
|
|
import {recognize} from './recognize';
|
2016-07-13 21:12:59 -04:00
|
|
|
import {LoadedRouterConfig, RouterConfigLoader} from './router_config_loader';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {RouterOutletMap} from './router_outlet_map';
|
2016-07-06 14:02:16 -04:00
|
|
|
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState} from './router_state';
|
2016-08-24 13:20:44 -04:00
|
|
|
import {NavigationCancelingError, PRIMARY_OUTLET, Params} from './shared';
|
2016-07-28 20:59:05 -04:00
|
|
|
import {UrlSerializer, UrlTree, containsTree, createEmptyUrlTree} from './url_tree';
|
2016-07-26 17:39:02 -04:00
|
|
|
import {andObservables, forEach, merge, shallowEqual, waitForMap, wrapIntoObservable} from './utils/collection';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {TreeNode} from './utils/tree';
|
|
|
|
|
2016-07-15 17:59:59 -04:00
|
|
|
declare var Zone: any;
|
|
|
|
|
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,
|
|
|
|
* children: [
|
|
|
|
* {
|
|
|
|
* path: 'list',
|
|
|
|
* component: ListComponent
|
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* path: 'child',
|
|
|
|
* component: ChildComponent
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // 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
|
|
|
* ```
|
|
|
|
*/
|
2016-07-20 17:30:04 -04:00
|
|
|
preserveQueryParams?: boolean;
|
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-06-03 17:28:41 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* @whatItDoes Represents an event triggered when a navigation starts.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-06-03 17:28:41 -04:00
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
export class NavigationStart {
|
2016-09-10 19:51:27 -04:00
|
|
|
// TODO: vsavkin: make internal
|
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public id: number,
|
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public url: string) {}
|
2016-06-09 17:32:03 -04:00
|
|
|
|
2016-09-10 19:51:27 -04:00
|
|
|
/** @docsNotRequired */
|
2016-06-09 17:33:09 -04:00
|
|
|
toString(): string { return `NavigationStart(id: ${this.id}, url: '${this.url}')`; }
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
2016-06-03 17:28:41 -04:00
|
|
|
|
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* @whatItDoes Represents an event triggered when a navigation ends successfully.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-06-03 17:28:41 -04:00
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
export class NavigationEnd {
|
2016-09-10 19:51:27 -04:00
|
|
|
// TODO: vsavkin: make internal
|
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public id: number,
|
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public url: string,
|
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public urlAfterRedirects: string) {}
|
2016-06-09 17:32:03 -04:00
|
|
|
|
2016-09-10 19:51:27 -04:00
|
|
|
/** @docsNotRequired */
|
2016-06-14 17:55:59 -04:00
|
|
|
toString(): string {
|
|
|
|
return `NavigationEnd(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}')`;
|
|
|
|
}
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
2016-06-03 17:28:41 -04:00
|
|
|
|
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* @whatItDoes Represents an event triggered when a navigation is canceled.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-06-03 17:28:41 -04:00
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
export class NavigationCancel {
|
2016-09-10 19:51:27 -04:00
|
|
|
// TODO: vsavkin: make internal
|
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public id: number,
|
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public url: string,
|
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public reason: string) {}
|
2016-06-09 17:32:03 -04:00
|
|
|
|
2016-09-10 19:51:27 -04:00
|
|
|
/** @docsNotRequired */
|
2016-06-09 17:33:09 -04:00
|
|
|
toString(): string { return `NavigationCancel(id: ${this.id}, url: '${this.url}')`; }
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
2016-06-03 17:28:41 -04:00
|
|
|
|
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* @whatItDoes Represents an event triggered when a navigation fails due to an unexpected error.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-06-03 17:28:41 -04:00
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
export class NavigationError {
|
2016-09-10 19:51:27 -04:00
|
|
|
// TODO: vsavkin: make internal
|
|
|
|
constructor(
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public id: number,
|
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public url: string,
|
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public error: any) {}
|
2016-06-09 17:32:03 -04:00
|
|
|
|
2016-09-10 19:51:27 -04:00
|
|
|
/** @docsNotRequired */
|
2016-06-09 17:32:03 -04:00
|
|
|
toString(): string {
|
|
|
|
return `NavigationError(id: ${this.id}, url: '${this.url}', error: ${this.error})`;
|
|
|
|
}
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
2016-06-03 17:28:41 -04:00
|
|
|
|
2016-06-09 17:31:49 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* @whatItDoes Represents an event triggered when routes are recognized.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-06-09 17:31:49 -04:00
|
|
|
*/
|
|
|
|
export class RoutesRecognized {
|
2016-09-10 19:51:27 -04:00
|
|
|
// TODO: vsavkin: make internal
|
2016-06-09 17:33:09 -04:00
|
|
|
constructor(
|
2016-09-10 19:51:27 -04:00
|
|
|
/** @docsNotRequired */
|
|
|
|
public id: number,
|
|
|
|
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public url: string,
|
|
|
|
/** @docsNotRequired */
|
|
|
|
public urlAfterRedirects: string,
|
|
|
|
/** @docsNotRequired */
|
2016-06-09 17:33:09 -04:00
|
|
|
public state: RouterStateSnapshot) {}
|
2016-06-09 17:32:03 -04:00
|
|
|
|
2016-09-10 19:51:27 -04:00
|
|
|
/** @docsNotRequired */
|
2016-06-09 17:32:03 -04:00
|
|
|
toString(): string {
|
|
|
|
return `RoutesRecognized(id: ${this.id}, url: '${this.url}', urlAfterRedirects: '${this.urlAfterRedirects}', state: ${this.state})`;
|
|
|
|
}
|
2016-06-09 17:31:49 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 15:27:23 -04:00
|
|
|
/**
|
2016-09-10 19:51:27 -04:00
|
|
|
* @whatItDoes Represents a router event.
|
|
|
|
*
|
|
|
|
* Please see {@link NavigationStart}, {@link NavigationEnd}, {@link NavigationCancel}, {@link
|
|
|
|
* NavigationError},
|
|
|
|
* {@link RoutesRecognized} for more information.
|
|
|
|
*
|
2016-06-28 17:49:29 -04:00
|
|
|
* @stable
|
2016-06-27 15:27:23 -04:00
|
|
|
*/
|
2016-08-05 11:24:40 -04:00
|
|
|
export type Event =
|
|
|
|
NavigationStart | NavigationEnd | NavigationCancel | NavigationError | RoutesRecognized;
|
2016-06-03 17:25:18 -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-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;
|
|
|
|
private currentRouterState: RouterState;
|
2016-05-24 16:23:27 -04:00
|
|
|
private locationSubscription: Subscription;
|
2016-06-03 17:25:18 -04:00
|
|
|
private routerEvents: Subject<Event>;
|
2016-06-03 17:07:01 -04:00
|
|
|
private navigationId: number = 0;
|
2016-07-06 14:02:16 -04:00
|
|
|
private configLoader: RouterConfigLoader;
|
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;
|
|
|
|
|
2016-07-20 20:51:21 -04:00
|
|
|
/**
|
|
|
|
* Indicates if at least one navigation happened.
|
|
|
|
*/
|
|
|
|
navigated: boolean = false;
|
|
|
|
|
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(
|
2016-08-16 16:40:28 -04:00
|
|
|
private rootComponentType: Type<any>, private urlSerializer: UrlSerializer,
|
|
|
|
private outletMap: RouterOutletMap, private location: Location, private injector: Injector,
|
|
|
|
loader: NgModuleFactoryLoader, compiler: Compiler, public config: Routes) {
|
2016-06-16 17:36:51 -04:00
|
|
|
this.resetConfig(config);
|
2016-06-03 17:25:18 -04:00
|
|
|
this.routerEvents = new Subject<Event>();
|
2016-06-07 12:50:35 -04:00
|
|
|
this.currentUrlTree = createEmptyUrlTree();
|
2016-08-16 00:11:09 -04:00
|
|
|
this.configLoader = new RouterConfigLoader(loader, compiler);
|
2016-06-14 17:55:59 -04:00
|
|
|
this.currentRouterState = createEmptyState(this.currentUrlTree, this.rootComponentType);
|
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();
|
2016-08-12 17:30:51 -04:00
|
|
|
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
|
|
|
|
this.locationSubscription = <any>this.location.subscribe(Zone.current.wrap((change: any) => {
|
|
|
|
const tree = this.urlSerializer.parse(change['url']);
|
|
|
|
// we fire multiple events for a single URL change
|
|
|
|
// we should navigate only once
|
|
|
|
return this.currentUrlTree.toString() !== tree.toString() ?
|
|
|
|
this.scheduleNavigation(tree, {skipLocationChange: change['pop'], replaceUrl: true}) :
|
|
|
|
null;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
|
|
|
* Returns 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-06-14 17:55:59 -04:00
|
|
|
* Returns the current url.
|
2016-05-26 19:51:56 -04:00
|
|
|
*/
|
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-06-03 17:28:41 -04:00
|
|
|
/**
|
|
|
|
* Returns an observable of route events
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
get events(): Observable<Event> { return this.routerEvents; }
|
2016-06-03 17:25:18 -04: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-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-09-10 19:51:27 -04:00
|
|
|
/**
|
|
|
|
* @docsNotRequired
|
|
|
|
*/
|
2016-08-02 05:32:27 -04:00
|
|
|
ngOnDestroy() { this.dispose(); }
|
|
|
|
|
2016-05-24 17:33:34 -04:00
|
|
|
/**
|
2016-07-14 20:29:01 -04:00
|
|
|
* Disposes of the router.
|
2016-05-24 17:33:34 -04:00
|
|
|
*/
|
|
|
|
dispose(): void { this.locationSubscription.unsubscribe(); }
|
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(
|
|
|
|
commands: any[], {relativeTo, queryParams, fragment, preserveQueryParams,
|
|
|
|
preserveFragment}: NavigationExtras = {}): UrlTree {
|
2016-05-26 19:51:56 -04:00
|
|
|
const a = relativeTo ? relativeTo : this.routerState.root;
|
2016-07-20 17:30:04 -04:00
|
|
|
const q = preserveQueryParams ? this.currentUrlTree.queryParams : queryParams;
|
|
|
|
const f = preserveFragment ? this.currentUrlTree.fragment : fragment;
|
|
|
|
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:
|
|
|
|
* - is resolved with 'true' when navigation succeeds
|
|
|
|
* - is resolved with 'false' when navigation fails
|
|
|
|
* - is rejected when an error happens
|
|
|
|
*
|
|
|
|
* ### 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> {
|
2016-06-15 11:30:49 -04:00
|
|
|
if (url instanceof UrlTree) {
|
2016-08-04 14:46:09 -04:00
|
|
|
return this.scheduleNavigation(url, extras);
|
2016-06-15 11:30:49 -04:00
|
|
|
} else {
|
|
|
|
const urlTree = this.urlSerializer.parse(url);
|
2016-08-04 14:46:09 -04:00
|
|
|
return this.scheduleNavigation(urlTree, 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:
|
|
|
|
* - is resolved with 'true' when navigation succeeds
|
|
|
|
* - is resolved with 'false' when navigation fails
|
|
|
|
* - is rejected when an error happens
|
|
|
|
*
|
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-09-07 17:10:19 -04: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-07-22 21:32:26 -04:00
|
|
|
* In opposite to `navigateByUrl`, `navigate` always takes a delta
|
2016-07-22 16:25:48 -04:00
|
|
|
* 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> {
|
|
|
|
return this.scheduleNavigation(this.createUrlTree(commands, extras), extras);
|
2016-05-26 19:51:56 -04:00
|
|
|
}
|
2016-06-06 19:34:48 -04:00
|
|
|
|
2016-05-26 19:51:56 -04:00
|
|
|
/**
|
|
|
|
* Serializes a {@link UrlTree} into a string.
|
|
|
|
*/
|
|
|
|
serializeUrl(url: UrlTree): string { return this.urlSerializer.serialize(url); }
|
|
|
|
|
|
|
|
/**
|
2016-09-10 19:51:27 -04: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-07-28 20:59:05 -04:00
|
|
|
/**
|
|
|
|
* Returns if the url is activated or not.
|
|
|
|
*/
|
|
|
|
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-08-04 14:46:09 -04:00
|
|
|
private scheduleNavigation(url: UrlTree, extras: NavigationExtras): Promise<boolean> {
|
2016-06-08 14:13:41 -04:00
|
|
|
const id = ++this.navigationId;
|
2016-06-14 17:55:59 -04:00
|
|
|
this.routerEvents.next(new NavigationStart(id, this.serializeUrl(url)));
|
2016-08-12 17:30:51 -04:00
|
|
|
return Promise.resolve().then(
|
|
|
|
(_) => this.runNavigate(url, extras.skipLocationChange, extras.replaceUrl, id));
|
2016-06-03 17:07:01 -04:00
|
|
|
}
|
|
|
|
|
2016-08-12 17:30:51 -04:00
|
|
|
private runNavigate(
|
|
|
|
url: UrlTree, shouldPreventPushState: boolean, shouldReplaceUrl: boolean,
|
|
|
|
id: number): 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-06-15 19:45:19 -04:00
|
|
|
let state: RouterState;
|
2016-06-26 14:52:32 -04:00
|
|
|
let navigationIsSuccessful: boolean;
|
2016-06-27 17:00:07 -04:00
|
|
|
let preActivation: PreActivation;
|
2016-07-12 15:28:08 -04:00
|
|
|
|
|
|
|
let appliedUrl: UrlTree;
|
|
|
|
|
|
|
|
const storedState = this.currentRouterState;
|
|
|
|
const storedUrl = this.currentUrlTree;
|
|
|
|
|
2016-08-30 17:25:46 -04:00
|
|
|
const redirectsApplied$ = applyRedirects(this.injector, this.configLoader, url, this.config);
|
2016-06-08 19:14:26 -04:00
|
|
|
|
2016-08-30 17:25:46 -04:00
|
|
|
const snapshot$ = mergeMap.call(redirectsApplied$, (u: UrlTree) => {
|
|
|
|
appliedUrl = u;
|
|
|
|
return recognize(
|
|
|
|
this.rootComponentType, this.config, appliedUrl, this.serializeUrl(appliedUrl));
|
|
|
|
});
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2016-08-30 17:25:46 -04:00
|
|
|
const emitRecognzied$ = map.call(snapshot$, (newRouterStateSnapshot: RouterStateSnapshot) => {
|
|
|
|
this.routerEvents.next(new RoutesRecognized(
|
|
|
|
id, this.serializeUrl(url), this.serializeUrl(appliedUrl), newRouterStateSnapshot));
|
|
|
|
return newRouterStateSnapshot;
|
|
|
|
});
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2016-08-30 17:25:46 -04:00
|
|
|
const routerState$ = map.call(emitRecognzied$, (routerStateSnapshot: RouterStateSnapshot) => {
|
|
|
|
return createRouterState(routerStateSnapshot, this.currentRouterState);
|
|
|
|
});
|
2016-06-27 17:00:07 -04:00
|
|
|
|
2016-08-30 17:25:46 -04:00
|
|
|
const preactivation$ = map.call(routerState$, (newState: RouterState) => {
|
|
|
|
state = newState;
|
|
|
|
preActivation =
|
|
|
|
new PreActivation(state.snapshot, this.currentRouterState.snapshot, this.injector);
|
|
|
|
preActivation.traverse(this.outletMap);
|
|
|
|
});
|
2016-06-08 14:13:41 -04:00
|
|
|
|
2016-08-30 17:25:46 -04:00
|
|
|
const preactivation2$ =
|
|
|
|
mergeMap.call(preactivation$, () => { return preActivation.checkGuards(); });
|
|
|
|
|
|
|
|
const resolveData$ = mergeMap.call(preactivation2$, (shouldActivate: boolean) => {
|
|
|
|
if (shouldActivate) {
|
|
|
|
return map.call(preActivation.resolveData(), () => shouldActivate);
|
|
|
|
} else {
|
|
|
|
return of (shouldActivate);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
resolveData$
|
2016-06-15 19:45:19 -04:00
|
|
|
.forEach((shouldActivate: boolean) => {
|
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-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-07-12 15:28:08 -04:00
|
|
|
let path = this.urlSerializer.serialize(appliedUrl);
|
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
|
|
|
|
|
|
|
new ActivateRoutes(state, storedState).activate(this.outletMap);
|
|
|
|
|
2016-06-25 16:31:48 -04:00
|
|
|
navigationIsSuccessful = true;
|
2016-06-08 14:13:41 -04:00
|
|
|
})
|
|
|
|
.then(
|
|
|
|
() => {
|
2016-07-20 20:51:21 -04:00
|
|
|
this.navigated = true;
|
2016-08-12 01:59:53 -04:00
|
|
|
if (navigationIsSuccessful) {
|
|
|
|
this.routerEvents.next(
|
|
|
|
new NavigationEnd(id, this.serializeUrl(url), this.serializeUrl(appliedUrl)));
|
|
|
|
resolvePromise(true);
|
|
|
|
} else {
|
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) => {
|
2016-08-24 13:20:44 -04:00
|
|
|
if (e instanceof NavigationCancelingError) {
|
|
|
|
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
|
|
|
|
|
|
|
if (id === this.navigationId) {
|
|
|
|
this.currentRouterState = storedState;
|
|
|
|
this.currentUrlTree = storedUrl;
|
|
|
|
this.location.replaceState(this.serializeUrl(storedUrl));
|
|
|
|
}
|
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-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 {
|
|
|
|
constructor(public component: Object, public route: ActivatedRouteSnapshot) {}
|
|
|
|
}
|
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 {
|
2016-06-15 19:45:19 -04:00
|
|
|
private checks: Array<CanActivate|CanDeactivate> = [];
|
2016-06-08 14:13:41 -04:00
|
|
|
constructor(
|
|
|
|
private future: RouterStateSnapshot, private curr: RouterStateSnapshot,
|
|
|
|
private injector: 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> {
|
2016-07-15 19:27:54 -04:00
|
|
|
if (this.checks.length === 0) return of (true);
|
2016-08-30 17:25:46 -04:00
|
|
|
const checks$ = from(this.checks);
|
|
|
|
const runningChecks$ = map.call(checks$, (s: any) => {
|
|
|
|
if (s instanceof CanActivate) {
|
|
|
|
return andObservables(
|
|
|
|
from([this.runCanActivateChild(s.path), this.runCanActivate(s.route)]));
|
|
|
|
} else if (s instanceof CanDeactivate) {
|
|
|
|
// workaround https://github.com/Microsoft/TypeScript/issues/7271
|
|
|
|
const s2 = s as CanDeactivate;
|
|
|
|
return this.runCanDeactivate(s2.component, s2.route);
|
|
|
|
} else {
|
|
|
|
throw new Error('Cannot be reached');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const mergedChecks$ = mergeAll.call(runningChecks$);
|
|
|
|
return every.call(mergedChecks$, (result: any) => result === true);
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 17:00:07 -04:00
|
|
|
resolveData(): Observable<any> {
|
2016-07-15 19:27:54 -04:00
|
|
|
if (this.checks.length === 0) return of (null);
|
2016-08-30 17:25:46 -04:00
|
|
|
const checks$ = from(this.checks);
|
|
|
|
const runningChecks$ = mergeMap.call(checks$, (s: any) => {
|
|
|
|
if (s instanceof CanActivate) {
|
|
|
|
return this.runResolve(s.route);
|
|
|
|
} else {
|
|
|
|
return of (null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return reduce.call(runningChecks$, (_: any, __: any) => _);
|
2016-06-27 17:00:07 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
private traverseChildRoutes(
|
2016-06-15 12:14:41 -04:00
|
|
|
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>,
|
2016-07-13 18:15:20 -04:00
|
|
|
outletMap: RouterOutletMap, futurePath: ActivatedRouteSnapshot[]): void {
|
2016-06-15 12:14:41 -04:00
|
|
|
const prevChildren: {[key: string]: any} = 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(
|
|
|
|
prevChildren,
|
|
|
|
(v: any, k: string) => this.deactivateOutletAndItChildren(v, outletMap._outlets[k]));
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
traverseRoutes(
|
2016-06-15 12:14:41 -04:00
|
|
|
futureNode: TreeNode<ActivatedRouteSnapshot>, currNode: TreeNode<ActivatedRouteSnapshot>,
|
2016-07-13 18:15:20 -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) {
|
2016-06-01 17:32:15 -04:00
|
|
|
if (!shallowEqual(future.params, curr.params)) {
|
2016-07-13 18:15:20 -04:00
|
|
|
this.checks.push(new CanDeactivate(outlet.component, curr), new CanActivate(futurePath));
|
2016-08-10 18:53:57 -04:00
|
|
|
} else {
|
|
|
|
// we need to set the data
|
|
|
|
future.data = curr.data;
|
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) {
|
|
|
|
// if we had a normal route, we need to deactivate only that outlet.
|
|
|
|
if (curr.component) {
|
|
|
|
this.deactivateOutletAndItChildren(curr, outlet);
|
|
|
|
|
|
|
|
// if we had a componentless route, we need to deactivate everything!
|
|
|
|
} else {
|
|
|
|
this.deactivateOutletMap(parentOutletMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 18:15:20 -04:00
|
|
|
this.checks.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:44:57 -04:00
|
|
|
private deactivateOutletAndItChildren(route: ActivatedRouteSnapshot, outlet: RouterOutlet): void {
|
|
|
|
if (outlet && outlet.isActivated) {
|
2016-06-19 17:44:20 -04:00
|
|
|
this.deactivateOutletMap(outlet.outletMap);
|
2016-06-07 12:50:35 -04:00
|
|
|
this.checks.push(new CanDeactivate(outlet.component, route));
|
2016-06-02 17:44:57 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-01 17:32:15 -04:00
|
|
|
|
2016-06-19 17:44:20 -04:00
|
|
|
private deactivateOutletMap(outletMap: RouterOutletMap): void {
|
|
|
|
forEach(outletMap._outlets, (v: RouterOutlet) => {
|
|
|
|
if (v.isActivated) {
|
|
|
|
this.deactivateOutletAndItChildren(v.activatedRoute.snapshot, v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-07-13 18:15:20 -04:00
|
|
|
if (guard.canActivate) {
|
|
|
|
return wrapIntoObservable(guard.canActivate(future, this.future));
|
|
|
|
} else {
|
|
|
|
return wrapIntoObservable(guard(future, this.future));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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) => {
|
2016-08-10 18:53:57 -04:00
|
|
|
const guard = this.getToken(c, c.node);
|
2016-07-13 18:15:20 -04:00
|
|
|
if (guard.canActivateChild) {
|
2016-07-13 21:12:59 -04:00
|
|
|
return wrapIntoObservable(guard.canActivateChild(future, this.future));
|
2016-07-13 18:15:20 -04:00
|
|
|
} else {
|
2016-07-13 21:12:59 -04:00
|
|
|
return wrapIntoObservable(guard(future, this.future));
|
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):
|
|
|
|
{node: ActivatedRouteSnapshot, guards: any[]} {
|
|
|
|
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
|
|
|
|
2016-06-02 17:44:57 -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-08-30 17:25:46 -04:00
|
|
|
const canDeactivate$ = map.call(from(canDeactivate), (c: any) => {
|
|
|
|
const guard = this.getToken(c, curr);
|
|
|
|
if (guard.canDeactivate) {
|
|
|
|
return wrapIntoObservable(guard.canDeactivate(component, curr, this.curr));
|
|
|
|
} else {
|
|
|
|
return wrapIntoObservable(guard(component, curr, this.curr));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const merged$ = mergeAll.call(canDeactivate$);
|
|
|
|
return every.call(merged$, (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-08-30 17:25:46 -04:00
|
|
|
return map.call(this.resolveNode(resolve.current, future), (resolvedData: any): any => {
|
2016-06-27 17:00:07 -04:00
|
|
|
resolve.resolvedData = resolvedData;
|
|
|
|
future.data = merge(future.data, resolve.flattenedResolvedData);
|
|
|
|
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);
|
2016-07-13 21:12:59 -04:00
|
|
|
const injector = config ? config.injector : this.injector;
|
|
|
|
return injector.get(token);
|
|
|
|
}
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:23:27 -04:00
|
|
|
class ActivateRoutes {
|
2016-06-01 17:32:15 -04:00
|
|
|
constructor(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-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-06-08 14:13:41 -04:00
|
|
|
private activateChildRoutes(
|
2016-06-15 12:14:41 -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 => {
|
|
|
|
this.activateRoutes(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-06-15 19:45:19 -04:00
|
|
|
forEach(
|
|
|
|
prevChildren,
|
|
|
|
(v: any, k: string) => this.deactivateOutletAndItChildren(outletMap._outlets[k]));
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
const outlet = getOutlet(parentOutletMap, futureNode.value);
|
|
|
|
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 (curr) {
|
|
|
|
// if we had a normal route, we need to deactivate only that outlet.
|
|
|
|
if (curr.component) {
|
|
|
|
const outlet = getOutlet(parentOutletMap, futureNode.value);
|
|
|
|
this.deactivateOutletAndItChildren(outlet);
|
|
|
|
|
|
|
|
// if we had a componentless route, we need to deactivate everything!
|
|
|
|
} else {
|
|
|
|
this.deactivateOutletMap(parentOutletMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
const outletMap = new RouterOutletMap();
|
|
|
|
this.placeComponentIntoOutlet(outletMap, future, outlet);
|
|
|
|
this.activateChildRoutes(futureNode, null, outletMap);
|
|
|
|
|
|
|
|
// 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-07-06 14:02:16 -04:00
|
|
|
const resolved = <any[]>[{provide: ActivatedRoute, useValue: future}, {
|
|
|
|
provide: RouterOutletMap,
|
|
|
|
useValue: outletMap
|
|
|
|
}];
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
const config = parentLoadedConfig(future.snapshot);
|
|
|
|
|
2016-07-06 14:02:16 -04:00
|
|
|
let loadedFactoryResolver: ComponentFactoryResolver = null;
|
2016-07-18 19:07:12 -04:00
|
|
|
let loadedInjector: Injector = null;
|
2016-07-06 14:02:16 -04:00
|
|
|
|
2016-07-13 21:12:59 -04:00
|
|
|
if (config) {
|
2016-07-18 19:07:12 -04:00
|
|
|
loadedFactoryResolver = config.factoryResolver;
|
|
|
|
loadedInjector = config.injector;
|
|
|
|
resolved.push({provide: ComponentFactoryResolver, useValue: loadedFactoryResolver});
|
2016-08-10 18:53:57 -04:00
|
|
|
}
|
2016-07-18 19:07:12 -04:00
|
|
|
outlet.activate(
|
|
|
|
future, loadedFactoryResolver, loadedInjector, ReflectiveInjector.resolve(resolved),
|
|
|
|
outletMap);
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private deactivateOutletAndItChildren(outlet: RouterOutlet): void {
|
|
|
|
if (outlet && outlet.isActivated) {
|
2016-06-19 17:44:20 -04:00
|
|
|
this.deactivateOutletMap(outlet.outletMap);
|
2016-05-24 16:23:27 -04:00
|
|
|
outlet.deactivate();
|
|
|
|
}
|
|
|
|
}
|
2016-06-19 17:44:20 -04:00
|
|
|
|
|
|
|
private deactivateOutletMap(outletMap: RouterOutletMap): void {
|
|
|
|
forEach(outletMap._outlets, (v: RouterOutlet) => this.deactivateOutletAndItChildren(v));
|
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
function parentLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConfig {
|
|
|
|
let s = snapshot.parent;
|
|
|
|
while (s) {
|
|
|
|
const c: any = s._routeConfig;
|
|
|
|
if (c && c._loadedConfig) return c._loadedConfig;
|
|
|
|
if (c && c.component) return null;
|
|
|
|
s = s.parent;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function closestLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConfig {
|
|
|
|
if (!snapshot) return null;
|
|
|
|
|
|
|
|
let s = snapshot.parent;
|
|
|
|
while (s) {
|
|
|
|
const c: any = s._routeConfig;
|
|
|
|
if (c && c._loadedConfig) return c._loadedConfig;
|
|
|
|
s = s.parent;
|
|
|
|
}
|
|
|
|
return null;
|
2016-07-13 21:12:59 -04:00
|
|
|
}
|
|
|
|
|
2016-06-15 12:14:41 -04:00
|
|
|
function nodeChildrenAsMap(node: TreeNode<any>) {
|
2016-06-15 19:45:19 -04:00
|
|
|
return node ? node.children.reduce((m: any, c: TreeNode<any>) => {
|
2016-06-08 14:13:41 -04:00
|
|
|
m[c.value.outlet] = c;
|
|
|
|
return m;
|
|
|
|
}, {}) : {};
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getOutlet(outletMap: RouterOutletMap, route: ActivatedRoute): RouterOutlet {
|
|
|
|
let outlet = outletMap._outlets[route.outlet];
|
|
|
|
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;
|
|
|
|
}
|