2016-05-24 16:57:51 -04:00
|
|
|
import { Tree, TreeNode } from './utils/tree';
|
2016-06-02 17:44:57 -04:00
|
|
|
import { shallowEqual } from './utils/collection';
|
2016-05-23 19:14:23 -04:00
|
|
|
import { UrlSegment } from './url_tree';
|
2016-06-01 16:29:02 -04:00
|
|
|
import { Route } from './config';
|
2016-05-26 19:51:44 -04:00
|
|
|
import { Params, PRIMARY_OUTLET } from './shared';
|
2016-05-23 19:14:23 -04:00
|
|
|
import { Observable } from 'rxjs/Observable';
|
|
|
|
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
|
2016-06-01 16:29:02 -04:00
|
|
|
import { Type, ComponentFactory } from '@angular/core';
|
2016-05-23 19:14:23 -04:00
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
2016-06-01 20:55:21 -04:00
|
|
|
* The state of the router.
|
2016-05-24 16:41:37 -04:00
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* class MyComponent {
|
|
|
|
* constructor(router: Router) {
|
|
|
|
* const state = router.routerState;
|
|
|
|
* const id: Observable<string> = state.firstChild(state.root).params.map(p => p.id);
|
|
|
|
* const isDebug: Observable<string> = state.queryParams.map(q => q.debug);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
2016-05-23 19:14:23 -04:00
|
|
|
export class RouterState extends Tree<ActivatedRoute> {
|
2016-06-02 18:18:34 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-06-01 20:55:21 -04:00
|
|
|
constructor(root: TreeNode<ActivatedRoute>, public queryParams: Observable<Params>, public fragment: Observable<string>, public snapshot: RouterStateSnapshot) {
|
2016-05-23 19:14:23 -04:00
|
|
|
super(root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createEmptyState(rootComponent: Type): RouterState {
|
2016-06-01 20:55:21 -04:00
|
|
|
const snapshot = createEmptyStateSnapshot(rootComponent);
|
2016-05-26 19:51:44 -04:00
|
|
|
const emptyUrl = new BehaviorSubject([new UrlSegment("", {}, PRIMARY_OUTLET)]);
|
2016-05-23 19:14:23 -04:00
|
|
|
const emptyParams = new BehaviorSubject({});
|
|
|
|
const emptyQueryParams = new BehaviorSubject({});
|
|
|
|
const fragment = new BehaviorSubject("");
|
2016-06-01 20:55:21 -04:00
|
|
|
const activated = new ActivatedRoute(emptyUrl, emptyParams, PRIMARY_OUTLET, rootComponent, snapshot.root);
|
2016-06-02 17:44:57 -04:00
|
|
|
activated.snapshot = snapshot.root;
|
2016-06-01 20:55:21 -04:00
|
|
|
return new RouterState(new TreeNode<ActivatedRoute>(activated, []), emptyQueryParams, fragment, snapshot);
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
function createEmptyStateSnapshot(rootComponent: Type): RouterStateSnapshot {
|
2016-06-02 14:30:38 -04:00
|
|
|
const rootUrlSegment = new UrlSegment("", {}, PRIMARY_OUTLET);
|
|
|
|
const emptyUrl = [rootUrlSegment];
|
2016-06-01 16:29:02 -04:00
|
|
|
const emptyParams = {};
|
|
|
|
const emptyQueryParams = {};
|
|
|
|
const fragment = "";
|
2016-06-02 14:30:38 -04:00
|
|
|
const activated = new ActivatedRouteSnapshot(emptyUrl, emptyParams, PRIMARY_OUTLET, rootComponent, null, rootUrlSegment);
|
2016-06-01 20:55:21 -04:00
|
|
|
return new RouterStateSnapshot(new TreeNode<ActivatedRouteSnapshot>(activated, []), emptyQueryParams, fragment);
|
2016-06-01 16:29:02 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:41:37 -04:00
|
|
|
/**
|
2016-06-01 20:55:21 -04:00
|
|
|
* Contains the information about a component loaded in an outlet. The information is provided through
|
|
|
|
* the params and urlSegments observables.
|
2016-05-24 16:41:37 -04:00
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* class MyComponent {
|
|
|
|
* constructor(route: ActivatedRoute) {
|
|
|
|
* const id: Observable<string> = route.params.map(p => p.id);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
2016-05-23 19:14:23 -04:00
|
|
|
export class ActivatedRoute {
|
2016-06-02 17:44:57 -04:00
|
|
|
/** @internal */
|
|
|
|
_futureSnapshot: ActivatedRouteSnapshot;
|
|
|
|
snapshot: ActivatedRouteSnapshot;
|
|
|
|
|
2016-06-02 18:18:34 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-05-23 19:14:23 -04:00
|
|
|
constructor(public urlSegments: Observable<UrlSegment[]>,
|
|
|
|
public params: Observable<Params>,
|
|
|
|
public outlet: string,
|
2016-06-01 17:32:15 -04:00
|
|
|
public component: Type | string,
|
2016-06-02 17:44:57 -04:00
|
|
|
futureSnapshot: ActivatedRouteSnapshot
|
|
|
|
) {
|
|
|
|
this._futureSnapshot = futureSnapshot;
|
|
|
|
}
|
2016-06-01 16:29:02 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
/**
|
|
|
|
* Contains the information about a component loaded in an outlet at a particular moment in time.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* class MyComponent {
|
|
|
|
* constructor(route: ActivatedRoute) {
|
|
|
|
* const id: string = route.snapshot.params.id;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export class ActivatedRouteSnapshot {
|
2016-06-01 16:29:02 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
_resolvedComponentFactory: ComponentFactory<any>;
|
|
|
|
|
|
|
|
/** @internal **/
|
2016-06-02 14:40:47 -04:00
|
|
|
_routeConfig: Route | null;
|
2016-06-01 16:29:02 -04:00
|
|
|
|
2016-06-02 14:30:38 -04:00
|
|
|
/** @internal **/
|
|
|
|
_lastUrlSegment: UrlSegment;
|
|
|
|
|
2016-06-02 18:18:34 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-06-01 16:29:02 -04:00
|
|
|
constructor(public urlSegments: UrlSegment[],
|
|
|
|
public params: Params,
|
|
|
|
public outlet: string,
|
|
|
|
public component: Type | string,
|
2016-06-02 14:40:47 -04:00
|
|
|
routeConfig: Route | null,
|
2016-06-02 14:30:38 -04:00
|
|
|
lastUrlSegment: UrlSegment) {
|
2016-06-01 16:29:02 -04:00
|
|
|
this._routeConfig = routeConfig;
|
2016-06-02 14:30:38 -04:00
|
|
|
this._lastUrlSegment = lastUrlSegment;
|
2016-06-01 16:29:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 20:55:21 -04:00
|
|
|
/**
|
|
|
|
* The state of the router at a particular moment in time.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* class MyComponent {
|
|
|
|
* constructor(router: Router) {
|
|
|
|
* const snapshot = router.routerState.snapshot;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export class RouterStateSnapshot extends Tree<ActivatedRouteSnapshot> {
|
2016-06-02 18:18:34 -04:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-06-02 14:40:47 -04:00
|
|
|
constructor(root: TreeNode<ActivatedRouteSnapshot>, public queryParams: Params, public fragment: string | null) {
|
2016-06-01 16:29:02 -04:00
|
|
|
super(root);
|
|
|
|
}
|
2016-06-02 17:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The expectation is that the activate route is created with the right set of parameters.
|
|
|
|
* So we push new values into the observables only when they are not the initial values.
|
|
|
|
* And we detect that by checking if the snapshot field is set.
|
|
|
|
*/
|
|
|
|
export function advanceActivatedRoute(route: ActivatedRoute): void {
|
|
|
|
if (route.snapshot && !shallowEqual(route.snapshot.params, route._futureSnapshot.params)) {
|
|
|
|
route.snapshot = route._futureSnapshot;
|
|
|
|
(<any>route.urlSegments).next(route.snapshot.urlSegments);
|
|
|
|
(<any>route.params).next(route.snapshot.params);
|
|
|
|
} else {
|
|
|
|
route.snapshot = route._futureSnapshot;
|
|
|
|
}
|
2016-05-23 19:14:23 -04:00
|
|
|
}
|