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

42 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Tree, TreeNode } from './utils/tree';
import { shallowEqual } from './utils/collection';
import { PRIMARY_OUTLET } from './shared';
export function createEmptyUrlTree() {
return new UrlTree(new TreeNode<UrlSegment>(new UrlSegment("", {}, PRIMARY_OUTLET), []), {}, null);
}
2016-05-21 20:35:55 -04:00
2016-05-24 16:41:37 -04:00
/**
* A URL in the tree form.
*/
2016-05-21 20:35:55 -04:00
export class UrlTree extends Tree<UrlSegment> {
constructor(root: TreeNode<UrlSegment>, public queryParameters: {[key: string]: string}, public fragment: string | null) {
super(root);
}
}
export class UrlSegment {
constructor(public path: string, public parameters: {[key: string]: string}, public outlet: string) {}
2016-05-23 19:14:23 -04:00
toString() {
const params = [];
2016-05-23 19:14:23 -04:00
for (let prop in this.parameters) {
if (this.parameters.hasOwnProperty(prop)) {
params.push(`${prop}=${this.parameters[prop]}`);
}
}
const paramsString = params.length > 0 ? `(${params.join(',')})` : '';
const outlet = this.outlet === PRIMARY_OUTLET ? '' : `${this.outlet}:`;
return `${outlet}${this.path}${paramsString}`;
2016-05-23 19:14:23 -04:00
}
}
export function equalUrlSegments(a: UrlSegment[], b: UrlSegment[]): 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;
if (!shallowEqual(a[i].parameters, b[i].parameters)) return false;
}
return true;
}