Skip to content

Commit

Permalink
feat(router): implements support for router-link-active
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed May 2, 2016
1 parent db95fd6 commit ec4ca0e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
38 changes: 35 additions & 3 deletions modules/angular2/src/alt_router/segments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class Tree<T> {
}

pathFromRoot(t: T): T[] { return _findPath(t, this._root, []).map(s => s.value); }

contains(tree: Tree<T>): boolean {
return _contains(this._root, tree._root);
}
}

export class UrlTree extends Tree<UrlSegment> {
Expand Down Expand Up @@ -55,9 +59,8 @@ function _findPath<T>(expected: T, c: TreeNode<T>, collected: TreeNode<T>[]): Tr
collected.push(c);

// TODO: vsavkin remove it once recognize is fixed
if (expected instanceof RouteSegment && equalSegments(<any>expected, <any>c.value))
return collected;
if (expected === c.value) return collected;
if(_equalValues(expected, c.value)) return collected;

for (let cc of c.children) {
let r = _findPath(expected, cc, ListWrapper.clone(collected));
if (isPresent(r)) return r;
Expand All @@ -66,6 +69,24 @@ function _findPath<T>(expected: T, c: TreeNode<T>, collected: TreeNode<T>[]): Tr
return null;
}

function _contains<T>(tree: TreeNode<T>, subtree: TreeNode<T>): boolean {
if (!_equalValues(tree.value, subtree.value)) return false;

for (let subtreeNode of subtree.children) {
let s = tree.children.filter(child => _equalValues(child.value, subtreeNode.value));
if (s.length === 0) return false;
if (!_contains(s[0], subtreeNode)) return false;
}

return true;
}

function _equalValues(a:any, b:any):boolean {
if (a instanceof RouteSegment) return equalSegments(<any>a, <any>b);
if (a instanceof UrlSegment) return equalUrlSegments(<any>a, <any>b)
return a === b;
}

export class TreeNode<T> {
constructor(public value: T, public children: TreeNode<T>[]) {}
}
Expand Down Expand Up @@ -125,6 +146,17 @@ export function equalSegments(a: RouteSegment, b: RouteSegment): boolean {
if (isBlank(a) && !isBlank(b)) return false;
if (!isBlank(a) && isBlank(b)) return false;
if (a._type !== b._type) return false;
if (a.outlet != b.outlet) return false;
if (isBlank(a.parameters) && !isBlank(b.parameters)) return false;
if (!isBlank(a.parameters) && isBlank(b.parameters)) return false;
if (isBlank(a.parameters) && isBlank(b.parameters)) return true;
return StringMapWrapper.equals(a.parameters, b.parameters);
}

export function equalUrlSegments(a: UrlSegment, b: UrlSegment): boolean {
if (isBlank(a) && !isBlank(b)) return false;
if (!isBlank(a) && isBlank(b)) return false;
if (a.outlet != b.outlet) return false;
if (isBlank(a.parameters) && !isBlank(b.parameters)) return false;
if (!isBlank(a.parameters) && isBlank(b.parameters)) return false;
if (isBlank(a.parameters) && isBlank(b.parameters)) return true;
Expand Down
17 changes: 17 additions & 0 deletions modules/angular2/test/alt_router/tree_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,22 @@ export function main() {
let t = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
expect(t.pathFromRoot(2)).toEqual([1, 2]);
});

describe("contains", () => {
it("should work", () => {
let tree = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, []), new TreeNode<number>(3, [])]));
let subtree1 = new Tree<any>(new TreeNode<number>(1, []));
let subtree2 = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
let subtree3 = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(3, [])]));
let notSubtree1 = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(4, [])]));
let notSubtree2 = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [new TreeNode<number>(4, [])])]));

expect(tree.contains(subtree1)).toEqual(true);
expect(tree.contains(subtree2)).toEqual(true);
expect(tree.contains(subtree3)).toEqual(true);
expect(tree.contains(notSubtree1)).toEqual(false);
expect(tree.contains(notSubtree2)).toEqual(false);
});
});
});
}

0 comments on commit ec4ca0e

Please sign in to comment.