2015-05-06 18:30:37 -07:00
|
|
|
import {Directive, onAllChangesDone} from 'angular2/src/core/annotations_impl/annotations';
|
2015-04-28 11:20:01 -07:00
|
|
|
import {ElementRef} from 'angular2/core';
|
2015-05-06 18:30:37 -07:00
|
|
|
import {StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
|
2015-04-17 09:59:56 -07:00
|
|
|
|
|
|
|
import {isPresent} from 'angular2/src/facade/lang';
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
|
|
|
|
|
|
|
import {Router} from './router';
|
2015-05-12 16:18:58 -07:00
|
|
|
import {Location} from './location';
|
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:
|
|
|
|
|
|
|
|
* ```
|
|
|
|
* @RouteConfig({
|
|
|
|
* path: '/user', component: UserCmp, alias: 'user'
|
|
|
|
* });
|
|
|
|
* class MyComp {}
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* When linking to a route, you can write:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* <a router-link="user">link to user component</a>
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @exportedAs angular2/router
|
|
|
|
*/
|
2015-04-30 13:38:40 -07:00
|
|
|
@Directive({
|
2015-04-17 09:59:56 -07:00
|
|
|
selector: '[router-link]',
|
|
|
|
properties: {
|
|
|
|
'route': 'routerLink',
|
|
|
|
'params': 'routerParams'
|
2015-05-06 18:30:37 -07:00
|
|
|
},
|
|
|
|
lifecycle: [onAllChangesDone]
|
2015-04-17 09:59:56 -07:00
|
|
|
})
|
|
|
|
export class RouterLink {
|
|
|
|
_domEl;
|
|
|
|
_route:string;
|
|
|
|
_params:any;
|
|
|
|
_router:Router;
|
2015-05-12 16:18:58 -07:00
|
|
|
_location:Location;
|
2015-05-06 18:30:37 -07:00
|
|
|
_href:string;
|
2015-04-17 09:59:56 -07:00
|
|
|
|
2015-05-12 16:18:58 -07:00
|
|
|
constructor(elementRef:ElementRef, router:Router, location:Location) {
|
2015-04-28 11:20:01 -07:00
|
|
|
this._domEl = elementRef.domElement;
|
2015-04-17 09:59:56 -07:00
|
|
|
this._router = router;
|
2015-05-12 16:18:58 -07:00
|
|
|
this._location = location;
|
2015-05-06 18:30:37 -07:00
|
|
|
this._params = StringMapWrapper.create();
|
|
|
|
DOM.on(this._domEl, 'click', (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
this._router.navigate(this._href);
|
|
|
|
});
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
set route(changes) {
|
|
|
|
this._route = changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
set params(changes) {
|
|
|
|
this._params = changes;
|
|
|
|
}
|
|
|
|
|
2015-05-06 18:30:37 -07:00
|
|
|
onAllChangesDone() {
|
2015-04-17 09:59:56 -07:00
|
|
|
if (isPresent(this._route) && isPresent(this._params)) {
|
|
|
|
var newHref = this._router.generate(this._route, this._params);
|
2015-05-12 16:18:58 -07:00
|
|
|
this._href = this._location.normalizeAbsolutely(newHref);
|
2015-05-06 18:30:37 -07:00
|
|
|
// Keeping the link on the element to support contextual menu `copy link`
|
|
|
|
// and other in-browser affordances.
|
2015-05-12 16:18:58 -07:00
|
|
|
DOM.setAttribute(this._domEl, 'href', this._href);
|
2015-04-17 09:59:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|