feat(router): implements support for router-link-active
This commit is contained in:
parent
db95fd6ca9
commit
ec4ca0eace
|
@ -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> {
|
||||
|
@ -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;
|
||||
|
@ -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>[]) {}
|
||||
}
|
||||
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue