feat(router): implement terminal

This commit is contained in:
vsavkin
2016-06-14 14:55:59 -07:00
parent 503b07f698
commit 127401598b
17 changed files with 1069 additions and 744 deletions

View File

@ -1,40 +1,41 @@
import {PRIMARY_OUTLET} from './shared';
import {DefaultUrlSerializer, serializeSegment} from './url_serializer';
import {shallowEqual} from './utils/collection';
import {Tree, TreeNode} from './utils/tree';
import {DefaultUrlSerializer, serializePath, serializePaths} from './url_serializer';
import {forEach, shallowEqual} from './utils/collection';
export function createEmptyUrlTree() {
return new UrlTree(
new TreeNode<UrlSegment>(new UrlSegment('', {}, PRIMARY_OUTLET), []), {}, null);
return new UrlTree(new UrlSegment([], {}), {}, null);
}
/**
* A URL in the tree form.
*/
export class UrlTree extends Tree<UrlSegment> {
export class UrlTree {
/**
* @internal
*/
constructor(
root: TreeNode<UrlSegment>, public queryParams: {[key: string]: string},
public fragment: string|null) {
super(root);
}
public root: UrlSegment, public queryParams: {[key: string]: string},
public fragment: string|null) {}
toString(): string { return new DefaultUrlSerializer().serialize(this); }
}
export class UrlSegment {
/**
* @internal
*/
public parent: UrlSegment|null = null;
constructor(
public path: string, public parameters: {[key: string]: string}, public outlet: string) {}
public pathsWithParams: UrlPathWithParams[], public children: {[key: string]: UrlSegment}) {
forEach(children, (v, k) => v.parent = this);
}
toString(): string { return serializeSegment(this); }
toString(): string { return serializePaths(this); }
}
export function equalUrlSegments(a: UrlSegment[], b: UrlSegment[]): boolean {
export class UrlPathWithParams {
constructor(public path: string, public parameters: {[key: string]: string}) {}
toString(): string { return serializePath(this); }
}
export function equalPathsWithParams(a: UrlPathWithParams[], b: UrlPathWithParams[]): boolean {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; ++i) {
if (a[i].path !== b[i].path) return false;
@ -42,3 +43,35 @@ export function equalUrlSegments(a: UrlSegment[], b: UrlSegment[]): boolean {
}
return true;
}
export function mapChildren(segment: UrlSegment, fn: (v: UrlSegment, k: string) => UrlSegment):
{[name: string]: UrlSegment} {
const newChildren = {};
forEach(segment.children, (child, childOutlet) => {
if (childOutlet === PRIMARY_OUTLET) {
newChildren[childOutlet] = fn(child, childOutlet);
}
});
forEach(segment.children, (child, childOutlet) => {
if (childOutlet !== PRIMARY_OUTLET) {
newChildren[childOutlet] = fn(child, childOutlet);
}
});
return newChildren;
}
export function mapChildrenIntoArray<T>(
segment: UrlSegment, fn: (v: UrlSegment, k: string) => T[]): T[] {
let res = [];
forEach(segment.children, (child, childOutlet) => {
if (childOutlet === PRIMARY_OUTLET) {
res = res.concat(fn(child, childOutlet));
}
});
forEach(segment.children, (child, childOutlet) => {
if (childOutlet !== PRIMARY_OUTLET) {
res = res.concat(fn(child, childOutlet));
}
});
return res;
}