chore(router): clang-format

This commit is contained in:
vsavkin 2016-05-04 15:01:27 -07:00
parent abfb522f83
commit b30ddfbfc5
8 changed files with 108 additions and 75 deletions

View File

@ -3,7 +3,8 @@ import {isBlank, isPresent, isString, isStringMap} from './facade/lang';
import {BaseException} from './facade/exceptions';
import {ListWrapper, StringMapWrapper} from './facade/collection';
export function link(segment: RouteSegment, routeTree: RouteTree, urlTree: UrlTree, commands: any[]): UrlTree {
export function link(segment: RouteSegment, routeTree: RouteTree, urlTree: UrlTree,
commands: any[]): UrlTree {
if (commands.length === 0) return urlTree;
let normalizedCommands = _normalizeCommands(commands);
@ -12,20 +13,22 @@ export function link(segment: RouteSegment, routeTree: RouteTree, urlTree: UrlTr
}
let startingNode = _findStartingNode(normalizedCommands, urlTree, segment, routeTree);
let updated = normalizedCommands.commands.length > 0 ?
_updateMany(ListWrapper.clone(startingNode.children), normalizedCommands.commands) : [];
let updated =
normalizedCommands.commands.length > 0 ?
_updateMany(ListWrapper.clone(startingNode.children), normalizedCommands.commands) :
[];
let newRoot = _constructNewTree(rootNode(urlTree), startingNode, updated);
return new UrlTree(newRoot);
}
function _navigateToRoot(normalizedChange: _NormalizedNavigationCommands): boolean {
return normalizedChange.isAbsolute && normalizedChange.commands.length === 1 && normalizedChange.commands[0] == "/";
return normalizedChange.isAbsolute && normalizedChange.commands.length === 1 &&
normalizedChange.commands[0] == "/";
}
class _NormalizedNavigationCommands {
constructor(public isAbsolute: boolean,
public numberOfDoubleDots: number,
constructor(public isAbsolute: boolean, public numberOfDoubleDots: number,
public commands: any[]) {}
}
@ -73,7 +76,8 @@ function _normalizeCommands(commands: any[]): _NormalizedNavigationCommands {
return new _NormalizedNavigationCommands(isAbsolute, numberOfDoubleDots, res);
}
function _findUrlSegment(segment: RouteSegment, routeTree: RouteTree, urlTree: UrlTree, numberOfDoubleDots: number): UrlSegment {
function _findUrlSegment(segment: RouteSegment, routeTree: RouteTree, urlTree: UrlTree,
numberOfDoubleDots: number): UrlSegment {
let s = segment;
while (s.urlSegments.length === 0) {
s = routeTree.parent(s);
@ -86,11 +90,13 @@ function _findUrlSegment(segment: RouteSegment, routeTree: RouteTree, urlTree: U
return path[path.length - 1 - numberOfDoubleDots];
}
function _findStartingNode(normalizedChange:_NormalizedNavigationCommands, urlTree:UrlTree, segment:RouteSegment, routeTree:RouteTree):TreeNode<UrlSegment> {
function _findStartingNode(normalizedChange: _NormalizedNavigationCommands, urlTree: UrlTree,
segment: RouteSegment, routeTree: RouteTree): TreeNode<UrlSegment> {
if (normalizedChange.isAbsolute) {
return rootNode(urlTree);
} else {
let urlSegment = _findUrlSegment(segment, routeTree, urlTree, normalizedChange.numberOfDoubleDots);
let urlSegment =
_findUrlSegment(segment, routeTree, urlTree, normalizedChange.numberOfDoubleDots);
return _findMatchingNode(urlSegment, rootNode(urlTree));
}
}
@ -169,7 +175,8 @@ function _compare(path: string, params: {[key: string]: any}, segment: UrlSegmen
return path == segment.segment && StringMapWrapper.equals(params, segment.parameters);
}
function _recurse(urlSegment: UrlSegment, node: TreeNode<UrlSegment>, rest: any[]): TreeNode<UrlSegment> {
function _recurse(urlSegment: UrlSegment, node: TreeNode<UrlSegment>,
rest: any[]): TreeNode<UrlSegment> {
if (rest.length === 0) {
return new TreeNode<UrlSegment>(urlSegment, []);
}

View File

@ -1,4 +1,13 @@
import {RouteSegment, UrlSegment, Tree, TreeNode, rootNode, UrlTree, RouteTree, equalUrlSegments} from './segments';
import {
RouteSegment,
UrlSegment,
Tree,
TreeNode,
rootNode,
UrlTree,
RouteTree,
equalUrlSegments
} from './segments';
import {RoutesMetadata, RouteMetadata} from './metadata/metadata';
import {Type, isBlank, isPresent, stringify} from './facade/lang';
import {ListWrapper, StringMapWrapper} from './facade/collection';
@ -8,14 +17,16 @@ import {ComponentResolver} from '@angular/core';
import {DEFAULT_OUTLET_NAME} from './constants';
import {reflector} from '@angular/core';
export function recognize(componentResolver: ComponentResolver, rootComponent: Type,
url: UrlTree, existingTree: RouteTree): Promise<RouteTree> {
export function recognize(componentResolver: ComponentResolver, rootComponent: Type, url: UrlTree,
existingTree: RouteTree): Promise<RouteTree> {
let matched = new _MatchResult(rootComponent, [url.root], {}, rootNode(url).children, []);
return _constructSegment(componentResolver, matched, rootNode(existingTree)).then(roots => new RouteTree(roots[0]));
return _constructSegment(componentResolver, matched, rootNode(existingTree))
.then(roots => new RouteTree(roots[0]));
}
function _recognize(componentResolver: ComponentResolver, parentComponent: Type,
url: TreeNode<UrlSegment>, existingSegments: TreeNode<RouteSegment>[]): Promise<TreeNode<RouteSegment>[]> {
url: TreeNode<UrlSegment>,
existingSegments: TreeNode<RouteSegment>[]): Promise<TreeNode<RouteSegment>[]> {
let metadata = _readMetadata(parentComponent); // should read from the factory instead
if (isBlank(metadata)) {
throw new BaseException(
@ -30,29 +41,34 @@ function _recognize(componentResolver: ComponentResolver, parentComponent: Type,
}
let segmentsWithRightOutlet = existingSegments.filter(r => r.value.outlet == match.outlet);
let segmentWithRightOutlet = segmentsWithRightOutlet.length > 0 ? segmentsWithRightOutlet[0] : null;
let segmentWithRightOutlet =
segmentsWithRightOutlet.length > 0 ? segmentsWithRightOutlet[0] : null;
let main = _constructSegment(componentResolver, match, segmentWithRightOutlet);
let aux =
_recognizeMany(componentResolver, parentComponent, match.aux, existingSegments).then(_checkOutletNameUniqueness);
let aux = _recognizeMany(componentResolver, parentComponent, match.aux, existingSegments)
.then(_checkOutletNameUniqueness);
return PromiseWrapper.all([main, aux]).then(ListWrapper.flatten);
}
function _recognizeMany(componentResolver: ComponentResolver, parentComponent: Type,
urls: TreeNode<UrlSegment>[], existingSegments: TreeNode<RouteSegment>[]): Promise<TreeNode<RouteSegment>[]> {
let recognized = urls.map(u => _recognize(componentResolver, parentComponent, u, existingSegments));
function _recognizeMany(
componentResolver: ComponentResolver, parentComponent: Type, urls: TreeNode<UrlSegment>[],
existingSegments: TreeNode<RouteSegment>[]): Promise<TreeNode<RouteSegment>[]> {
let recognized =
urls.map(u => _recognize(componentResolver, parentComponent, u, existingSegments));
return PromiseWrapper.all(recognized).then(ListWrapper.flatten);
}
function _constructSegment(componentResolver: ComponentResolver,
matched: _MatchResult, existingSegment: TreeNode<RouteSegment>): Promise<TreeNode<RouteSegment>[]> {
function _constructSegment(
componentResolver: ComponentResolver, matched: _MatchResult,
existingSegment: TreeNode<RouteSegment>): Promise<TreeNode<RouteSegment>[]> {
return componentResolver.resolveComponent(matched.component)
.then(factory => {
let segment = _createOrReuseSegment(matched, factory, existingSegment);
let existingChildren = isPresent(existingSegment) ? existingSegment.children : [];
if (matched.leftOverUrl.length > 0) {
return _recognizeMany(componentResolver, factory.componentType, matched.leftOverUrl, existingChildren)
return _recognizeMany(componentResolver, factory.componentType, matched.leftOverUrl,
existingChildren)
.then(children => [new TreeNode<RouteSegment>(segment, children)]);
} else {
return _recognizeLeftOvers(componentResolver, factory.componentType, existingChildren)
@ -61,11 +77,12 @@ function _constructSegment(componentResolver: ComponentResolver,
});
}
function _createOrReuseSegment(matched: _MatchResult, factory: ComponentFactory<any>, segmentNode: TreeNode<RouteSegment>): RouteSegment {
function _createOrReuseSegment(matched: _MatchResult, factory: ComponentFactory<any>,
segmentNode: TreeNode<RouteSegment>): RouteSegment {
let segment = isPresent(segmentNode) ? segmentNode.value : null;
if (isPresent(segment) && equalUrlSegments(segment.urlSegments, matched.consumedUrlSegments)
&& StringMapWrapper.equals(segment.parameters, matched.parameters) &&
if (isPresent(segment) && equalUrlSegments(segment.urlSegments, matched.consumedUrlSegments) &&
StringMapWrapper.equals(segment.parameters, matched.parameters) &&
segment.outlet == matched.outlet && factory.componentType == segment.type) {
return segment;
} else {
@ -74,8 +91,9 @@ function _createOrReuseSegment(matched: _MatchResult, factory: ComponentFactory<
}
}
function _recognizeLeftOvers(componentResolver: ComponentResolver,
parentComponent: Type, existingSegments: TreeNode<RouteSegment>[]): Promise<TreeNode<RouteSegment>[]> {
function _recognizeLeftOvers(
componentResolver: ComponentResolver, parentComponent: Type,
existingSegments: TreeNode<RouteSegment>[]): Promise<TreeNode<RouteSegment>[]> {
return componentResolver.resolveComponent(parentComponent)
.then(factory => {
let metadata = _readMetadata(factory.componentType);
@ -87,14 +105,20 @@ function _recognizeLeftOvers(componentResolver: ComponentResolver,
if (r.length === 0) {
return PromiseWrapper.resolve([]);
} else {
let segmentsWithMatchingOutlet = existingSegments.filter(r => r.value.outlet == DEFAULT_OUTLET_NAME);
let segmentWithMatchingOutlet = segmentsWithMatchingOutlet.length > 0 ? segmentsWithMatchingOutlet[0] : null;
let existingChildren = isPresent(segmentWithMatchingOutlet) ? segmentWithMatchingOutlet.children : [];
let segmentsWithMatchingOutlet =
existingSegments.filter(r => r.value.outlet == DEFAULT_OUTLET_NAME);
let segmentWithMatchingOutlet =
segmentsWithMatchingOutlet.length > 0 ? segmentsWithMatchingOutlet[0] : null;
let existingChildren =
isPresent(segmentWithMatchingOutlet) ? segmentWithMatchingOutlet.children : [];
return _recognizeLeftOvers(componentResolver, r[0].component, existingChildren).then(children => {
return _recognizeLeftOvers(componentResolver, r[0].component, existingChildren)
.then(children => {
return componentResolver.resolveComponent(r[0].component)
.then(factory => {
let segment = _createOrReuseSegment(new _MatchResult(r[0].component, [], {}, [], []), factory, segmentWithMatchingOutlet);
let segment =
_createOrReuseSegment(new _MatchResult(r[0].component, [], {}, [], []),
factory, segmentWithMatchingOutlet);
return [new TreeNode<RouteSegment>(segment, children)];
});
});
@ -182,8 +206,7 @@ class _MatchResult {
public leftOverUrl: TreeNode<UrlSegment>[], public aux: TreeNode<UrlSegment>[]) {}
get outlet(): string {
return this.consumedUrlSegments.length === 0 ||
isBlank(this.consumedUrlSegments[0].outlet) ?
return this.consumedUrlSegments.length === 0 || isBlank(this.consumedUrlSegments[0].outlet) ?
DEFAULT_OUTLET_NAME :
this.consumedUrlSegments[0].outlet;
}

View File

@ -96,7 +96,8 @@ export function main() {
.toHaveText('team 22 { hello victor, aux: simple }');
})));
it('should deactivate outlets', fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
it('should deactivate outlets',
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
let fixture = tcb.createFakeAsync(RootCmp);
router.navigateByUrl('/team/22/user/victor(/simple)');

View File

@ -191,12 +191,14 @@ export function main() {
it("should reuse existing segments",
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
recognize(resolver, ComponentA, tree("/b/1/d"), emptyRouteTree).then(t1 => {
recognize(resolver, ComponentA, tree("/b/1/e"), t1).then(t2 => {
recognize(resolver, ComponentA, tree("/b/1/d"), emptyRouteTree)
.then(t1 => {
recognize(resolver, ComponentA, tree("/b/1/e"), t1)
.then(t2 => {
expect(t1.root).toBe(t2.root);
expect(t1.firstChild(t1.root)).toBe(t2.firstChild(t2.root));
expect(t1.firstChild(t1.firstChild(t1.root))).not.toBe(
t2.firstChild(t2.firstChild(t2.root)));
expect(t1.firstChild(t1.firstChild(t1.root)))
.not.toBe(t2.firstChild(t2.firstChild(t2.root)));
async.done();
});