From 90a1f7d5c422ab77a9c6faeeaef63bb7bd26bf67 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Fri, 22 Apr 2016 12:04:27 -0700 Subject: [PATCH] feat(router): add UrlSegment, RouteSegment, and Tree --- modules/angular2/src/alt_router/segments.ts | 66 +++++++++++++++++++ modules/angular2/test/alt_router/tree_spec.ts | 43 ++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 modules/angular2/src/alt_router/segments.ts create mode 100644 modules/angular2/test/alt_router/tree_spec.ts diff --git a/modules/angular2/src/alt_router/segments.ts b/modules/angular2/src/alt_router/segments.ts new file mode 100644 index 0000000000..8f7b560d65 --- /dev/null +++ b/modules/angular2/src/alt_router/segments.ts @@ -0,0 +1,66 @@ +import {ComponentFactory} from 'angular2/core'; +import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection'; +import {Type, isBlank} from 'angular2/src/facade/lang'; + +export class Tree { + constructor(private _nodes: T[]) {} + + get root(): T { return this._nodes[0]; } + + parent(t: T): T { + let index = this._nodes.indexOf(t); + return index > 0 ? this._nodes[index - 1] : null; + } + + children(t: T): T[] { + let index = this._nodes.indexOf(t); + return index > -1 && index < this._nodes.length - 1 ? [this._nodes[index + 1]] : []; + } + + firstChild(t: T): T { + let index = this._nodes.indexOf(t); + return index > -1 && index < this._nodes.length - 1 ? this._nodes[index + 1] : null; + } + + pathToRoot(t: T): T[] { + let index = this._nodes.indexOf(t); + return index > -1 ? this._nodes.slice(0, index + 1) : null; + } +} + +export class UrlSegment { + constructor(public segment: string, public parameters: {[key: string]: string}, + public outlet: string) {} +} + +export class RouteSegment { + /** @internal */ + _type: Type; + + /** @internal */ + _componentFactory: ComponentFactory; + + /** @internal */ + _parameters: {[key: string]: string}; + + constructor(public urlSegments: UrlSegment[], parameters: {[key: string]: string}, + public outlet: string, type: Type, componentFactory: ComponentFactory) { + this._type = type; + this._componentFactory = componentFactory; + this._parameters = parameters; + } + + getParam(param: string): string { return this._parameters[param]; } + + get type(): Type { return this._type; } +} + +export function equalSegments(a: RouteSegment, b: RouteSegment): boolean { + if (isBlank(a) && !isBlank(b)) return false; + if (!isBlank(a) && isBlank(b)) return false; + return a._type === b._type && StringMapWrapper.equals(a._parameters, b._parameters); +} + +export function routeSegmentComponentFactory(a: RouteSegment): ComponentFactory { + return a._componentFactory; +} \ No newline at end of file diff --git a/modules/angular2/test/alt_router/tree_spec.ts b/modules/angular2/test/alt_router/tree_spec.ts new file mode 100644 index 0000000000..8dd32c8ec6 --- /dev/null +++ b/modules/angular2/test/alt_router/tree_spec.ts @@ -0,0 +1,43 @@ +import { + ComponentFixture, + AsyncTestCompleter, + TestComponentBuilder, + beforeEach, + ddescribe, + xdescribe, + describe, + el, + expect, + iit, + inject, + beforeEachProviders, + it, + xit +} from 'angular2/testing_internal'; + +import {Tree} from 'angular2/src/alt_router/segments'; + +export function main() { + describe('tree', () => { + it("should return the root of the tree", () => { + let t = new Tree([1, 2, 3]); + expect(t.root).toEqual(1); + }); + + it("should return the parent of a node", () => { + let t = new Tree([1, 2, 3]); + expect(t.parent(1)).toEqual(null); + expect(t.parent(2)).toEqual(1); + }); + + it("should return the children of a node", () => { + let t = new Tree([1, 2, 3]); + expect(t.children(1)).toEqual([2]); + }); + + it("should return the path to the root", () => { + let t = new Tree([1, 2, 3]); + expect(t.pathToRoot(2)).toEqual([1, 2]); + }); + }); +} \ No newline at end of file