2015-11-23 10:18:04 -08:00
|
|
|
import {Directive} from 'angular2/core';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {isString} from 'angular2/src/facade/lang';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
import {Router} from './router';
|
2015-05-12 16:18:58 -07:00
|
|
|
import {Location} from './location';
|
refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
{ path: '/foo', redirectTo: '/bar' },
{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
{ path: '/foo', redirectTo: ['Bar'] },
{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
{ path: '/tab', redirectTo: '/tab/users' }
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
2015-11-23 18:07:37 -08:00
|
|
|
import {Instruction} from './instruction';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The RouterLink directive lets you link to specific parts of your app.
|
|
|
|
*
|
|
|
|
* Consider the following route configuration:
|
|
|
|
|
|
|
|
* ```
|
2015-07-02 22:30:18 +03:00
|
|
|
* @RouteConfig([
|
2015-10-06 11:14:28 +02:00
|
|
|
* { path: '/user', component: UserCmp, as: 'User' }
|
2015-07-02 22:30:18 +03:00
|
|
|
* ]);
|
2015-04-17 09:59:56 -07:00
|
|
|
* class MyComp {}
|
|
|
|
* ```
|
|
|
|
*
|
2015-10-06 11:14:28 +02:00
|
|
|
* When linking to this `User` route, you can write:
|
2015-04-17 09:59:56 -07:00
|
|
|
*
|
|
|
|
* ```
|
2015-11-23 16:02:19 -08:00
|
|
|
* <a [routerLink]="['./User']">link to user component</a>
|
2015-04-17 09:59:56 -07:00
|
|
|
* ```
|
|
|
|
*
|
2015-06-30 13:18:51 -07:00
|
|
|
* RouterLink expects the value to be an array of route names, followed by the params
|
2015-10-06 11:14:28 +02:00
|
|
|
* for that level of routing. For instance `['/Team', {teamId: 1}, 'User', {userId: 2}]`
|
|
|
|
* means that we want to generate a link for the `Team` route with params `{teamId: 1}`,
|
|
|
|
* and with a child route `User` with params `{userId: 2}`.
|
2015-06-30 13:18:51 -07:00
|
|
|
*
|
2015-07-06 17:41:15 -07:00
|
|
|
* The first route name should be prepended with `/`, `./`, or `../`.
|
2015-06-30 13:18:51 -07:00
|
|
|
* If the route begins with `/`, the router will look up the route from the root of the app.
|
|
|
|
* If the route begins with `./`, the router will instead look in the current component's
|
2015-07-06 17:41:15 -07:00
|
|
|
* children for the route. And if the route begins with `../`, the router will look at the
|
|
|
|
* current component's parent.
|
2015-04-17 09:59:56 -07:00
|
|
|
*/
|
2015-04-30 13:38:40 -07:00
|
|
|
@Directive({
|
2015-11-23 16:02:19 -08:00
|
|
|
selector: '[routerLink]',
|
2015-11-02 15:11:57 -08:00
|
|
|
inputs: ['routeParams: routerLink', 'target: target'],
|
2015-08-30 21:27:11 -07:00
|
|
|
host: {
|
2015-09-01 08:52:54 -07:00
|
|
|
'(click)': 'onClick()',
|
2015-08-30 21:27:11 -07:00
|
|
|
'[attr.href]': 'visibleHref',
|
|
|
|
'[class.router-link-active]': 'isRouteActive'
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
})
|
|
|
|
export class RouterLink {
|
2015-08-28 11:29:19 -07:00
|
|
|
private _routeParams: any[];
|
2015-05-29 14:58:41 -07:00
|
|
|
|
2015-05-19 23:14:10 -07:00
|
|
|
// the url displayed on the anchor element.
|
2015-06-30 13:18:51 -07:00
|
|
|
visibleHref: string;
|
2015-11-02 15:11:57 -08:00
|
|
|
target: string;
|
2015-07-17 13:36:53 -07:00
|
|
|
|
|
|
|
// the instruction passed to the router to navigate
|
|
|
|
private _navigationInstruction: Instruction;
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-12-07 10:51:01 -08:00
|
|
|
constructor(private _router: Router, private _location: Location) {
|
|
|
|
// we need to update the link whenever a route changes to account for aux routes
|
|
|
|
this._router.subscribe((_) => this._updateLink());
|
|
|
|
}
|
|
|
|
|
|
|
|
// because auxiliary links take existing primary and auxiliary routes into account,
|
|
|
|
// we need to update the link whenever params or other routes change.
|
|
|
|
private _updateLink(): void {
|
|
|
|
this._navigationInstruction = this._router.generate(this._routeParams);
|
|
|
|
var navigationHref = this._navigationInstruction.toLinkUrl();
|
|
|
|
this.visibleHref = this._location.prepareExternalUrl(navigationHref);
|
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-08-30 21:27:11 -07:00
|
|
|
get isRouteActive(): boolean { return this._router.isRouteActive(this._navigationInstruction); }
|
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
set routeParams(changes: any[]) {
|
2015-06-30 13:18:51 -07:00
|
|
|
this._routeParams = changes;
|
2015-12-07 10:51:01 -08:00
|
|
|
this._updateLink();
|
2015-06-30 13:18:51 -07:00
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-06-29 10:37:55 +02:00
|
|
|
onClick(): boolean {
|
2015-11-02 15:11:57 -08:00
|
|
|
// If no target, or if target is _self, prevent default browser behavior
|
|
|
|
if (!isString(this.target) || this.target == '_self') {
|
|
|
|
this._router.navigateByInstruction(this._navigationInstruction);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-06-08 17:06:10 -07:00
|
|
|
}
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|