Skip to content

Commit

Permalink
feat(router): empty-path routes should inherit matrix params
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Jul 8, 2016
1 parent 34b3c53 commit a77db44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
26 changes: 15 additions & 11 deletions modules/@angular/router/src/recognize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ class NoMatch {

class InheritedFromParent {
constructor(
public parent: InheritedFromParent, public params: Params, public data: Data,
public resolve: InheritedResolve) {}
public parent: InheritedFromParent, public snapshot: ActivatedRouteSnapshot,
public params: Params, public data: Data, public resolve: InheritedResolve) {}

get allParams(): Params {
return this.parent ? merge(this.parent.allParams, this.params) : this.params;
}

get allData(): Data { return this.parent ? merge(this.parent.allData, this.data) : this.data; }

static get empty(): InheritedFromParent {
return new InheritedFromParent(null, {}, {}, new InheritedResolve(null, {}));
static empty(snapshot: ActivatedRouteSnapshot): InheritedFromParent {
return new InheritedFromParent(null, snapshot, {}, {}, new InheritedResolve(null, {}));
}
}

export function recognize(rootComponentType: Type, config: Routes, urlTree: UrlTree, url: string):
Observable<RouterStateSnapshot> {
try {
const children =
processSegment(config, urlTree.root, InheritedFromParent.empty, PRIMARY_OUTLET);
processSegment(config, urlTree.root, InheritedFromParent.empty(null), PRIMARY_OUTLET);
const root = new ActivatedRouteSnapshot(
[], {}, {}, PRIMARY_OUTLET, rootComponentType, null, urlTree.root, -1,
InheritedResolve.empty);
Expand Down Expand Up @@ -119,12 +119,10 @@ function processPathsWithParamsAgainstRoute(
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, [])];
}

const {consumedPaths, parameters, lastChild} = match(rawSegment, route, paths);
const {consumedPaths, parameters, lastChild} =
match(rawSegment, route, paths, inherited.snapshot);
const rawSlicedPath = paths.slice(lastChild);
const childConfig = getChildConfig(route);
const newInherited = route.component ?
InheritedFromParent.empty :
new InheritedFromParent(inherited, parameters, getData(route), newInheritedResolve);

const {segment, slicedPath} = split(rawSegment, consumedPaths, rawSlicedPath, childConfig);

Expand All @@ -134,6 +132,10 @@ function processPathsWithParamsAgainstRoute(
getSourceSegment(rawSegment), getPathIndexShift(rawSegment) + pathIndex + lastChild - 1,
newInheritedResolve);

const newInherited = route.component ?
InheritedFromParent.empty(snapshot) :
new InheritedFromParent(inherited, snapshot, parameters, getData(route), newInheritedResolve);

if (slicedPath.length === 0 && segment.hasChildren()) {
const children = processSegmentChildren(childConfig, segment, newInherited);
return [new TreeNode<ActivatedRouteSnapshot>(snapshot, children)];
Expand All @@ -158,13 +160,15 @@ function getChildConfig(route: Route): Route[] {
}
}

function match(segment: UrlSegment, route: Route, paths: UrlPathWithParams[]) {
function match(
segment: UrlSegment, route: Route, paths: UrlPathWithParams[], parent: ActivatedRouteSnapshot) {
if (route.path === '') {
if ((route.terminal || route.pathMatch === 'full') &&
(segment.hasChildren() || paths.length > 0)) {
throw new NoMatch();
} else {
return {consumedPaths: [], lastChild: 0, parameters: {}};
const params = parent ? parent.params : {};
return {consumedPaths: [], lastChild: 0, parameters: params};
}
}

Expand Down
17 changes: 17 additions & 0 deletions modules/@angular/router/test/recognize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ describe('recognize', () => {
expect(c2._lastPathIndex).toBe(-1);
});
});

it('should inherit params', () => {
checkRecognize(
[{
path: 'a',
component: ComponentA,
children: [
{path: '', component: ComponentB, children: [{path: '', component: ComponentC}]}
]
}],
'/a;p=1', (s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), 'a', {p: '1'}, ComponentA);
checkActivatedRoute(s.firstChild(s.firstChild(s.root)), '', {p: '1'}, ComponentB);
checkActivatedRoute(
s.firstChild(s.firstChild(s.firstChild(s.root))), '', {p: '1'}, ComponentC);
});
});
});

describe('aux split is in the middle', () => {
Expand Down

0 comments on commit a77db44

Please sign in to comment.