Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Router) Fix detect active route in depth. #8178

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 23 additions & 15 deletions modules/angular2/src/router/router.ts
Expand Up @@ -13,6 +13,7 @@ import {
import {RouterOutlet} from './directives/router_outlet';
import {getCanActivateHook} from './lifecycle/route_lifecycle_reflector';
import {RouteDefinition} from './route_config/route_config_impl';
import {DefaultInstruction} from "./instruction";

let _resolveToTrue = PromiseWrapper.resolve(true);
let _resolveToFalse = PromiseWrapper.resolve(false);
Expand Down Expand Up @@ -135,8 +136,9 @@ export class Router {
*/
isRouteActive(instruction: Instruction): boolean {
var router: Router = this;
var currentInstruction = this.currentInstruction;

if (isBlank(this.currentInstruction)) {
if (isBlank(currentInstruction)) {
return false;
}

Expand All @@ -146,22 +148,28 @@ export class Router {
instruction = instruction.child;
}

if (isBlank(instruction.component) || isBlank(this.currentInstruction.component) ||
this.currentInstruction.component.routeName != instruction.component.routeName) {
return false;
}

let paramEquals = true;
let reason = true;

if (isPresent(this.currentInstruction.component.params)) {
StringMapWrapper.forEach(instruction.component.params, (value, key) => {
if (this.currentInstruction.component.params[key] !== value) {
paramEquals = false;
}
});
}
// check the instructions in depth
do {
if (isBlank(instruction.component) || isBlank(currentInstruction.component) ||
currentInstruction.component.routeName != instruction.component.routeName) {
return false;
}
if (isPresent(instruction.component.params)) {
StringMapWrapper.forEach(instruction.component.params, (value, key) => {
if (currentInstruction.component.params[key] !== value) {
reason = false;
}
});
}
currentInstruction = currentInstruction.child;
instruction = instruction.child;
} while (isPresent(currentInstruction) && isPresent(instruction) &&
!(instruction instanceof DefaultInstruction) && reason);

return paramEquals;
// ignore DefaultInstruction
return reason && (isBlank(instruction) || instruction instanceof DefaultInstruction);
}


Expand Down
45 changes: 45 additions & 0 deletions modules/angular2/test/router/integration/router_link_spec.ts
Expand Up @@ -303,6 +303,51 @@ export function main() {
});
}));

it('should not be added to links in other child routes',
inject([AsyncTestCompleter], (async) => {
router.config([
new Route({path: '/child', component: HelloCmp, name: 'Child'}),
new Route({
path: '/child-with-grandchild/...',
component: ParentCmp,
name: 'ChildWithGrandchild'
}),
new Route({
path: '/child-with-other-grandchild/...',
component: ParentCmp,
name: 'ChildWithOtherGrandchild'
})
])
.then((_) => compile(`<a [routerLink]="['./Child']" class="child-link">Child</a>
<a [routerLink]="['./ChildWithGrandchild/Grandchild']" class="child-with-grandchild-link">Better Child</a>
<a [routerLink]="['./ChildWithOtherGrandchild/Grandchild']" class="child-with-other-grandchild-link">Better Child</a>
<router-outlet></router-outlet>`))
.then((_) => {
var element = fixture.debugElement.nativeElement;

fixture.detectChanges();

var link1 = DOM.querySelector(element, '.child-link');
var link2 = DOM.querySelector(element, '.child-with-grandchild-link');
var link3 = DOM.querySelector(element, '.child-with-other-grandchild-link');

expect(link1).not.toHaveCssClass('router-link-active');
expect(link2).not.toHaveCssClass('router-link-active');
expect(link3).not.toHaveCssClass('router-link-active');

router.subscribe((_) => {
fixture.detectChanges();

expect(link1).not.toHaveCssClass('router-link-active');
expect(link2).toHaveCssClass('router-link-active');
expect(link3).not.toHaveCssClass('router-link-active');

async.done();
});
router.navigateByUrl('/child-with-grandchild/grandchild?extra=0');
});
}));


describe('router link dsl', () => {
it('should generate link hrefs with params', inject([AsyncTestCompleter], (async) => {
Expand Down