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

feat(router): RouterActive #6407

Closed
wants to merge 1 commit 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
4 changes: 3 additions & 1 deletion modules/angular2/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
export {Router} from './src/router/router';
export {RouterOutlet} from './src/router/router_outlet';
export {RouterLink} from './src/router/router_link';
export {RouterActive} from './src/router/router_active';
export {RouteParams, RouteData} from './src/router/instruction';
export {PlatformLocation} from './src/router/platform_location';
export {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './src/router/route_registry';
Expand All @@ -27,6 +28,7 @@ import {PathLocationStrategy} from './src/router/path_location_strategy';
import {Router, RootRouter} from './src/router/router';
import {RouterOutlet} from './src/router/router_outlet';
import {RouterLink} from './src/router/router_link';
import {RouterActive} from './src/router/router_active';
import {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './src/router/route_registry';
import {Location} from './src/router/location';
import {ApplicationRef, provide, OpaqueToken, Provider} from 'angular2/core';
Expand Down Expand Up @@ -55,7 +57,7 @@ import {BaseException} from 'angular2/src/facade/exceptions';
* bootstrap(AppCmp, [ROUTER_PROVIDERS]);
* ```
*/
export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink, RouterActive]);

/**
* A list of {@link Provider}s. To use the router, you must add this to your application.
Expand Down
32 changes: 32 additions & 0 deletions modules/angular2/src/router/router_active.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {isPresent} from 'angular2/src/facade/lang';
import {Directive, Query, Attribute, ElementRef, Renderer, QueryList} from 'angular2/core';
import {Router} from './router';
import {RouterLink} from './router_link';

/**
* RouterActive dynamically finds the first element with routerLink and toggles the active class
*
* ## Use
*
* ```
* <li router-active="active"><a [routerLink]=" ['/Home'] ">Home</a></li>
* <li [routerActive]="'active'"><a [routerLink]=" ['/Home'] ">Home</a></li>
* ```
*/
@Directive({selector: '[router-active]', inputs: ['routerActive']})
export class RouterActive {
routerActive: string = null;
routerActiveAttr: string = 'active';

constructor(router: Router, element: ElementRef, renderer: Renderer,
@Query(RouterLink) routerLink: QueryList<RouterLink>,
@Attribute('router-active') routerActiveAttr: string) {
router.subscribe(() => {
let active = routerLink.first.isRouteActive;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the RouterActive @directive idea, but my only concern is that when I have a child route then in this case the child route will be active as well which I not expect:

/home (if this is active)
/home/users (then let active = routerLink.first.isRouteActive; is also will be active)

Would not be better to use similar line to the following during set the active value:
let active = StringUtil.startsWith(_location.path(), _routerLink.first.visibleHref);
// StringUtil.startsWith(str, prefix)

Thanks for your feedback @gdi2290

renderer.setElementClass(element.nativeElement, this._propOrAttr(), active);
});
}
private _propOrAttr() {
return isPresent(this.routerActive) ? this.routerActive : this.routerActiveAttr;
}
}