2016-05-26 19:51:56 -04:00
|
|
|
import 'rxjs/add/operator/map';
|
|
|
|
import 'rxjs/add/operator/mergeMap';
|
2016-06-08 14:23:23 -04:00
|
|
|
import 'rxjs/add/operator/mergeAll';
|
2016-06-21 13:35:42 -04:00
|
|
|
import 'rxjs/add/operator/every';
|
2016-06-08 14:23:23 -04:00
|
|
|
import 'rxjs/add/observable/from';
|
2016-05-26 19:51:56 -04:00
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
import {Location} from '@angular/common';
|
|
|
|
import {ComponentResolver, Injector, ReflectiveInjector, Type} from '@angular/core';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Subject} from 'rxjs/Subject';
|
|
|
|
import {Subscription} from 'rxjs/Subscription';
|
|
|
|
import {of } from 'rxjs/observable/of';
|
|
|
|
|
2016-06-08 19:14:26 -04:00
|
|
|
import {applyRedirects} from './apply_redirects';
|
2016-06-16 17:36:51 -04:00
|
|
|
import {RouterConfig, 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';
|
|
|
|
import {resolve} from './resolve';
|
|
|
|
import {RouterOutletMap} from './router_outlet_map';
|
|
|
|
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState} from './router_state';
|
|
|
|
import {PRIMARY_OUTLET, Params} from './shared';
|
|
|
|
import {UrlSerializer} from './url_serializer';
|
|
|
|
import {UrlTree, createEmptyUrlTree} from './url_tree';
|
2016-06-08 14:23:23 -04:00
|
|
|
import {forEach, shallowEqual} from './utils/collection';
|
2016-06-08 14:13:41 -04:00
|
|
|
import {TreeNode} from './utils/tree';
|
|
|
|
|
|
|
|
export interface NavigationExtras {
|
|
|
|
relativeTo?: ActivatedRoute;
|
|
|
|
queryParams?: Params;
|
|
|
|
fragment?: string;
|
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-06-03 17:28:41 -04:00
|
|
|
/**
|
|
|
|
* An event triggered when a navigation starts
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
export class NavigationStart {
|
2016-06-14 17:55:59 -04:00
|
|
|
constructor(public id: number, public url: string) {}
|
2016-06-09 17:32:03 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* An event triggered when a navigation ends successfully
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
export class NavigationEnd {
|
2016-06-14 17:55:59 -04:00
|
|
|
constructor(public id: number, public url: string, public urlAfterRedirects: string) {}
|
2016-06-09 17:32:03 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* An event triggered when a navigation is canceled
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
export class NavigationCancel {
|
2016-06-14 17:55:59 -04:00
|
|
|
constructor(public id: number, public url: string) {}
|
2016-06-09 17:32:03 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* An event triggered when a navigation fails due to unexpected error
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
export class NavigationError {
|
2016-06-14 17:55:59 -04:00
|
|
|
constructor(public id: number, public url: string, public error: any) {}
|
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
|
|
|
/**
|
|
|
|
* An event triggered when routes are recognized
|
|
|
|
*/
|
|
|
|
export class RoutesRecognized {
|
2016-06-09 17:33:09 -04:00
|
|
|
constructor(
|
2016-06-14 17:55:59 -04:00
|
|
|
public id: number, public url: string, public urlAfterRedirects: string,
|
2016-06-09 17:33:09 -04:00
|
|
|
public state: RouterStateSnapshot) {}
|
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-03 17:25:18 -04:00
|
|
|
export type Event = NavigationStart | NavigationEnd | NavigationCancel | NavigationError;
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
|
|
|
* The `Router` is responsible for mapping URLs to components.
|
|
|
|
*/
|
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-06-16 17:36:51 -04:00
|
|
|
private config: RouterConfig;
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
constructor(
|
|
|
|
private rootComponentType: Type, private resolver: ComponentResolver,
|
|
|
|
private urlSerializer: UrlSerializer, private outletMap: RouterOutletMap,
|
2016-06-16 17:36:51 -04:00
|
|
|
private location: Location, private injector: Injector, config: RouterConfig) {
|
|
|
|
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-06-14 17:55:59 -04:00
|
|
|
this.currentRouterState = createEmptyState(this.currentUrlTree, this.rootComponentType);
|
2016-06-06 17:05:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-06-08 14:13:41 -04:00
|
|
|
initialNavigation(): void {
|
2016-05-24 16:23:27 -04:00
|
|
|
this.setUpLocationChangeListener();
|
|
|
|
this.navigateByUrl(this.location.path());
|
|
|
|
}
|
|
|
|
|
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-06-16 17:36:51 -04:00
|
|
|
resetConfig(config: RouterConfig): void {
|
|
|
|
validateConfig(config);
|
|
|
|
this.config = config;
|
|
|
|
}
|
2016-05-26 19:51:56 -04:00
|
|
|
|
2016-05-24 17:33:34 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
dispose(): void { this.locationSubscription.unsubscribe(); }
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-05-26 19:51:56 -04:00
|
|
|
/**
|
|
|
|
* Applies an array of commands to the current url tree and creates
|
|
|
|
* a new url tree.
|
|
|
|
*
|
|
|
|
* 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]);
|
|
|
|
*
|
|
|
|
* // you can collapse static fragments like this
|
|
|
|
* router.createUrlTree(['/team/33/user', userId]);
|
|
|
|
*
|
|
|
|
* // 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-06-08 14:13:41 -04:00
|
|
|
createUrlTree(commands: any[], {relativeTo, queryParams, fragment}: NavigationExtras = {}):
|
|
|
|
UrlTree {
|
2016-05-26 19:51:56 -04:00
|
|
|
const a = relativeTo ? relativeTo : this.routerState.root;
|
2016-06-06 19:34:48 -04:00
|
|
|
return createUrlTree(a, this.currentUrlTree, commands, queryParams, fragment);
|
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");
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
navigateByUrl(url: string|UrlTree): Promise<boolean> {
|
|
|
|
if (url instanceof UrlTree) {
|
|
|
|
return this.scheduleNavigation(url, false);
|
|
|
|
} else {
|
|
|
|
const urlTree = this.urlSerializer.parse(url);
|
|
|
|
return this.scheduleNavigation(urlTree, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* router.navigate(['team', 33, 'team', '11], {relativeTo: route});
|
|
|
|
* ```
|
|
|
|
*/
|
2016-06-03 17:07:01 -04:00
|
|
|
navigate(commands: any[], extras: NavigationExtras = {}): Promise<boolean> {
|
|
|
|
return this.scheduleNavigation(this.createUrlTree(commands, extras), false);
|
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); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a string into a {@link UrlTree}.
|
|
|
|
*/
|
|
|
|
parseUrl(url: string): UrlTree { return this.urlSerializer.parse(url); }
|
|
|
|
|
2016-06-10 11:57:03 -04:00
|
|
|
private scheduleNavigation(url: UrlTree, preventPushState: boolean): 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-06-10 11:57:03 -04:00
|
|
|
return Promise.resolve().then((_) => this.runNavigate(url, preventPushState, id));
|
2016-06-03 17:07:01 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:23:27 -04:00
|
|
|
private setUpLocationChangeListener(): void {
|
|
|
|
this.locationSubscription = <any>this.location.subscribe((change) => {
|
2016-06-03 17:07:01 -04:00
|
|
|
return this.scheduleNavigation(this.urlSerializer.parse(change['url']), change['pop']);
|
2016-05-24 16:23:27 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-10 11:57:03 -04:00
|
|
|
private runNavigate(url: UrlTree, preventPushState: 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-06-14 17:55:59 -04:00
|
|
|
this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url)));
|
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 updatedUrl: UrlTree;
|
|
|
|
let state: RouterState;
|
2016-06-08 19:14:26 -04:00
|
|
|
applyRedirects(url, this.config)
|
|
|
|
.mergeMap(u => {
|
|
|
|
updatedUrl = u;
|
2016-06-14 17:55:59 -04:00
|
|
|
return recognize(
|
|
|
|
this.rootComponentType, this.config, updatedUrl, this.serializeUrl(updatedUrl));
|
2016-06-08 19:14:26 -04:00
|
|
|
})
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
.mergeMap((newRouterStateSnapshot) => {
|
2016-06-14 17:55:59 -04:00
|
|
|
this.routerEvents.next(new RoutesRecognized(
|
|
|
|
id, this.serializeUrl(url), this.serializeUrl(updatedUrl), newRouterStateSnapshot));
|
2016-06-08 14:13:41 -04:00
|
|
|
return resolve(this.resolver, newRouterStateSnapshot);
|
|
|
|
|
|
|
|
})
|
|
|
|
.map((routerStateSnapshot) => {
|
|
|
|
return createRouterState(routerStateSnapshot, this.currentRouterState);
|
|
|
|
|
|
|
|
})
|
|
|
|
.map((newState: RouterState) => {
|
|
|
|
state = newState;
|
|
|
|
|
|
|
|
})
|
|
|
|
.mergeMap(_ => {
|
|
|
|
return new GuardChecks(state.snapshot, this.currentRouterState.snapshot, this.injector)
|
|
|
|
.check(this.outletMap);
|
|
|
|
|
|
|
|
})
|
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-14 17:55:59 -04:00
|
|
|
this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url)));
|
2016-06-08 14:13:41 -04:00
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
new ActivateRoutes(state, this.currentRouterState).activate(this.outletMap);
|
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
this.currentUrlTree = updatedUrl;
|
2016-06-08 14:13:41 -04:00
|
|
|
this.currentRouterState = state;
|
2016-06-10 11:57:03 -04:00
|
|
|
if (!preventPushState) {
|
|
|
|
let path = this.urlSerializer.serialize(updatedUrl);
|
|
|
|
if (this.location.isCurrentPathEqualTo(path)) {
|
|
|
|
this.location.replaceState(path);
|
|
|
|
} else {
|
|
|
|
this.location.go(path);
|
|
|
|
}
|
2016-06-08 14:13:41 -04:00
|
|
|
}
|
2016-06-15 19:45:19 -04:00
|
|
|
return Promise.resolve(true);
|
2016-06-08 14:13:41 -04:00
|
|
|
})
|
|
|
|
.then(
|
|
|
|
() => {
|
2016-06-14 17:55:59 -04:00
|
|
|
this.routerEvents.next(
|
|
|
|
new NavigationEnd(id, this.serializeUrl(url), this.serializeUrl(updatedUrl)));
|
2016-06-08 14:13:41 -04:00
|
|
|
resolvePromise(true);
|
|
|
|
|
|
|
|
},
|
|
|
|
e => {
|
2016-06-14 17:55:59 -04:00
|
|
|
this.routerEvents.next(new NavigationError(id, this.serializeUrl(url), e));
|
2016-06-08 14:13:41 -04:00
|
|
|
rejectPromise(e);
|
|
|
|
});
|
2016-05-26 19:51:56 -04:00
|
|
|
});
|
2016-05-24 16:23:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 14:13:41 -04:00
|
|
|
class CanActivate {
|
|
|
|
constructor(public route: ActivatedRouteSnapshot) {}
|
|
|
|
}
|
|
|
|
class CanDeactivate {
|
|
|
|
constructor(public component: Object, public route: ActivatedRouteSnapshot) {}
|
|
|
|
}
|
2016-06-02 17:44:57 -04:00
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
class GuardChecks {
|
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
|
|
|
|
|
|
|
check(parentOutletMap: RouterOutletMap): Observable<boolean> {
|
|
|
|
const futureRoot = this.future._root;
|
|
|
|
const currRoot = this.curr ? this.curr._root : null;
|
|
|
|
this.traverseChildRoutes(futureRoot, currRoot, parentOutletMap);
|
2016-06-08 14:13:41 -04:00
|
|
|
if (this.checks.length === 0) return of (true);
|
2016-06-15 18:45:42 -04:00
|
|
|
|
2016-06-08 14:23:23 -04:00
|
|
|
return Observable.from(this.checks)
|
|
|
|
.map(s => {
|
|
|
|
if (s instanceof CanActivate) {
|
|
|
|
return this.runCanActivate(s.route);
|
|
|
|
} else if (s instanceof CanDeactivate) {
|
|
|
|
return this.runCanDeactivate(s.component, s.route);
|
|
|
|
} else {
|
|
|
|
throw new Error('Cannot be reached');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.mergeAll()
|
|
|
|
.every(result => result === true);
|
2016-06-01 17:32:15 -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>,
|
|
|
|
outletMap: RouterOutletMap): void {
|
|
|
|
const prevChildren: {[key: string]: any} = nodeChildrenAsMap(currNode);
|
2016-06-01 17:32:15 -04:00
|
|
|
futureNode.children.forEach(c => {
|
|
|
|
this.traverseRoutes(c, prevChildren[c.value.outlet], outletMap);
|
|
|
|
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>,
|
|
|
|
parentOutletMap: RouterOutletMap): 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-06-02 17:44:57 -04:00
|
|
|
this.checks.push(new CanDeactivate(outlet.component, curr), new CanActivate(future));
|
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) {
|
|
|
|
this.traverseChildRoutes(futureNode, currNode, outlet ? outlet.outletMap : null);
|
|
|
|
|
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
|
|
|
} else {
|
|
|
|
this.traverseChildRoutes(futureNode, currNode, parentOutletMap);
|
|
|
|
}
|
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-06-02 17:44:57 -04:00
|
|
|
this.checks.push(new CanActivate(future));
|
2016-06-19 17:44:20 -04:00
|
|
|
// If we have a component, we need to go through an outlet.
|
|
|
|
if (future.component) {
|
|
|
|
this.traverseChildRoutes(futureNode, null, outlet ? outlet.outletMap : null);
|
|
|
|
|
|
|
|
// if we have a componentless route, we recurse but keep the same outlet map.
|
|
|
|
} else {
|
|
|
|
this.traverseChildRoutes(futureNode, null, parentOutletMap);
|
|
|
|
}
|
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-06-08 14:13:41 -04:00
|
|
|
if (!canActivate || canActivate.length === 0) return of (true);
|
2016-06-08 14:23:23 -04:00
|
|
|
return Observable.from(canActivate)
|
|
|
|
.map(c => {
|
|
|
|
const guard = this.injector.get(c);
|
|
|
|
if (guard.canActivate) {
|
|
|
|
return wrapIntoObservable(guard.canActivate(future, this.future));
|
|
|
|
} else {
|
|
|
|
return wrapIntoObservable(guard(future, this.future));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.mergeAll()
|
|
|
|
.every(result => result === true);
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
2016-06-02 17:44:57 -04:00
|
|
|
|
|
|
|
private runCanDeactivate(component: Object, curr: ActivatedRouteSnapshot): Observable<boolean> {
|
|
|
|
const canDeactivate = curr._routeConfig ? curr._routeConfig.canDeactivate : null;
|
2016-06-08 14:13:41 -04:00
|
|
|
if (!canDeactivate || canDeactivate.length === 0) return of (true);
|
2016-06-08 14:23:23 -04:00
|
|
|
return Observable.from(canDeactivate)
|
|
|
|
.map(c => {
|
|
|
|
const guard = this.injector.get(c);
|
2016-06-19 17:44:20 -04:00
|
|
|
|
2016-06-08 14:23:23 -04:00
|
|
|
if (guard.canDeactivate) {
|
|
|
|
return wrapIntoObservable(guard.canDeactivate(component, curr, this.curr));
|
|
|
|
} else {
|
|
|
|
return wrapIntoObservable(guard(component, curr, this.curr));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.mergeAll()
|
|
|
|
.every(result => result === true);
|
2016-06-02 17:44:57 -04:00
|
|
|
}
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
|
|
|
|
2016-06-06 13:55:12 -04:00
|
|
|
function wrapIntoObservable<T>(value: T | Observable<T>): Observable<T> {
|
|
|
|
if (value instanceof Observable) {
|
|
|
|
return value;
|
|
|
|
} else {
|
2016-06-08 14:13:41 -04:00
|
|
|
return of (value);
|
2016-06-06 13:55:12 -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-01 17:32:15 -04:00
|
|
|
|
|
|
|
pushQueryParamsAndFragment(this.futureState);
|
|
|
|
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-05-24 16:23:27 -04:00
|
|
|
const resolved = ReflectiveInjector.resolve([
|
|
|
|
{provide: ActivatedRoute, useValue: future},
|
|
|
|
{provide: RouterOutletMap, useValue: outletMap}
|
|
|
|
]);
|
2016-06-15 12:01:05 -04:00
|
|
|
outlet.activate(future._futureSnapshot._resolvedComponentFactory, future, 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-06-01 17:32:15 -04:00
|
|
|
function pushQueryParamsAndFragment(state: RouterState): void {
|
2016-06-01 20:55:21 -04:00
|
|
|
if (!shallowEqual(state.snapshot.queryParams, (<any>state.queryParams).value)) {
|
|
|
|
(<any>state.queryParams).next(state.snapshot.queryParams);
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
if (state.snapshot.fragment !== (<any>state.fragment).value) {
|
|
|
|
(<any>state.fragment).next(state.snapshot.fragment);
|
2016-06-01 17:32:15 -04:00
|
|
|
}
|
2016-06-01 16:34:48 -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;
|
|
|
|
}
|