angular-cn/modules/@angular/router/src/router_state.ts

67 lines
2.0 KiB
TypeScript
Raw Normal View History

2016-05-23 19:14:23 -04:00
import { Tree, TreeNode } from './tree';
import { UrlSegment } from './url_tree';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { ComponentFactory, Type } from '@angular/core';
2016-05-24 16:41:37 -04:00
/**
* A collection of parameters.
*/
2016-05-23 19:14:23 -04:00
export type Params = { [key: string]: string };
2016-05-24 16:41:37 -04:00
/**
* Name of the primary outlet.
* @type {string}
*/
export const PRIMARY_OUTLET: string = "PRIMARY_OUTLET";
2016-05-23 19:14:23 -04:00
2016-05-24 16:41:37 -04:00
/**
* The state of the router at a particular moment in time.
*
* ### 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> {
constructor(root: TreeNode<ActivatedRoute>, public queryParams: Observable<Params>, public fragment: Observable<string>) {
super(root);
}
}
export function createEmptyState(rootComponent: Type): RouterState {
const emptyUrl = new BehaviorSubject([new UrlSegment("", {})]);
const emptyParams = new BehaviorSubject({});
const emptyQueryParams = new BehaviorSubject({});
const fragment = new BehaviorSubject("");
const activated = new ActivatedRoute(emptyUrl, emptyParams, PRIMARY_OUTLET, rootComponent, <any>null);
return new RouterState(new TreeNode<ActivatedRoute>(activated, []), emptyQueryParams, fragment);
}
2016-05-24 16:41:37 -04:00
/**
* Contains the information about a component loaded in an outlet.
*
* ### 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 {
constructor(public urlSegments: Observable<UrlSegment[]>,
public params: Observable<Params>,
public outlet: string,
public component: Type,
public factory: ComponentFactory<any>) {}
}