fix(router): router link should navigate to non-base Url.
While still displaying full base + custom part of url in the href.
This commit is contained in:
parent
826af401a9
commit
c45283216f
3
modules/angular2/src/mock/location_mock.js
vendored
3
modules/angular2/src/mock/location_mock.js
vendored
@ -45,7 +45,8 @@ export class SpyLocation extends SpyObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go(url:string) {
|
go(url:string) {
|
||||||
if (this._path === url) {
|
url = this.normalizeAbsolutely(url);
|
||||||
|
if (this._path == url) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._path = url;
|
this._path = url;
|
||||||
|
13
modules/angular2/src/router/router_link.js
vendored
13
modules/angular2/src/router/router_link.js
vendored
@ -43,7 +43,10 @@ export class RouterLink {
|
|||||||
_params:StringMap<string, string>;
|
_params:StringMap<string, string>;
|
||||||
_router:Router;
|
_router:Router;
|
||||||
_location:Location;
|
_location:Location;
|
||||||
_href:string;
|
// the url displayed on the anchor element.
|
||||||
|
_visibleHref: string;
|
||||||
|
// the url passed to the router navigation.
|
||||||
|
_navigationHref: string;
|
||||||
|
|
||||||
constructor(elementRef:ElementRef, router:Router, location:Location) {
|
constructor(elementRef:ElementRef, router:Router, location:Location) {
|
||||||
this._domEl = elementRef.domElement;
|
this._domEl = elementRef.domElement;
|
||||||
@ -52,7 +55,7 @@ export class RouterLink {
|
|||||||
this._params = StringMapWrapper.create();
|
this._params = StringMapWrapper.create();
|
||||||
DOM.on(this._domEl, 'click', (evt) => {
|
DOM.on(this._domEl, 'click', (evt) => {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
this._router.navigate(this._href);
|
this._router.navigate(this._navigationHref);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,11 +69,11 @@ export class RouterLink {
|
|||||||
|
|
||||||
onAllChangesDone(): void {
|
onAllChangesDone(): void {
|
||||||
if (isPresent(this._route) && isPresent(this._params)) {
|
if (isPresent(this._route) && isPresent(this._params)) {
|
||||||
var newHref = this._router.generate(this._route, this._params);
|
this._navigationHref = this._router.generate(this._route, this._params);
|
||||||
this._href = this._location.normalizeAbsolutely(newHref);
|
this._visibleHref = this._location.normalizeAbsolutely(this._navigationHref);
|
||||||
// Keeping the link on the element to support contextual menu `copy link`
|
// Keeping the link on the element to support contextual menu `copy link`
|
||||||
// and other in-browser affordances.
|
// and other in-browser affordances.
|
||||||
DOM.setAttribute(this._domEl, 'href', this._href);
|
DOM.setAttribute(this._domEl, 'href', this._visibleHref);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
80
modules/angular2/test/router/outlet_spec.js
vendored
80
modules/angular2/test/router/outlet_spec.js
vendored
@ -135,6 +135,9 @@ export function main() {
|
|||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
function getHref(view) {
|
||||||
|
return DOM.getAttribute(view.rootNodes[0].childNodes[0], 'href');
|
||||||
|
}
|
||||||
|
|
||||||
it('should generate absolute hrefs that include the base href', inject([AsyncTestCompleter], (async) => {
|
it('should generate absolute hrefs that include the base href', inject([AsyncTestCompleter], (async) => {
|
||||||
location.setBaseHref('/my/base');
|
location.setBaseHref('/my/base');
|
||||||
@ -143,7 +146,7 @@ export function main() {
|
|||||||
.then((_) => rtr.navigate('/a/b'))
|
.then((_) => rtr.navigate('/a/b'))
|
||||||
.then((_) => {
|
.then((_) => {
|
||||||
view.detectChanges();
|
view.detectChanges();
|
||||||
expect(DOM.getAttribute(view.rootNodes[0].childNodes[0], 'href')).toEqual('/my/base/user');
|
expect(getHref(view)).toEqual('/my/base/user');
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
@ -155,13 +158,13 @@ export function main() {
|
|||||||
.then((_) => rtr.navigate('/a/b'))
|
.then((_) => rtr.navigate('/a/b'))
|
||||||
.then((_) => {
|
.then((_) => {
|
||||||
view.detectChanges();
|
view.detectChanges();
|
||||||
expect(DOM.getAttribute(view.rootNodes[0].childNodes[0], 'href')).toEqual('/user');
|
expect(getHref(view)).toEqual('/user');
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
it('should reuse common parent components', inject([AsyncTestCompleter, Location], (async, location) => {
|
it('should reuse common parent components', inject([AsyncTestCompleter], (async) => {
|
||||||
compile()
|
compile()
|
||||||
.then((_) => rtr.config({'path': '/team/:id', 'component': TeamCmp }))
|
.then((_) => rtr.config({'path': '/team/:id', 'component': TeamCmp }))
|
||||||
.then((_) => rtr.navigate('/team/angular/user/rado'))
|
.then((_) => rtr.navigate('/team/angular/user/rado'))
|
||||||
@ -193,27 +196,64 @@ export function main() {
|
|||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
describe('when clicked', () => {
|
||||||
|
|
||||||
it('should generate link hrefs without params', inject([AsyncTestCompleter], (async) => {
|
var clickOnElement = function(view) {
|
||||||
compile('<a href="hello" router-link="user"></a>')
|
var anchorEl = view.rootNodes[0].childNodes[0];
|
||||||
.then((_) => rtr.config({'path': '/user', 'component': UserCmp, 'as': 'user'}))
|
var dispatchedEvent = DOM.createMouseEvent('click');
|
||||||
.then((_) => rtr.navigate('/a/b'))
|
DOM.dispatchEvent(anchorEl, dispatchedEvent);
|
||||||
.then((_) => {
|
return dispatchedEvent;
|
||||||
view.detectChanges();
|
};
|
||||||
var anchorEl = view.rootNodes[0].childNodes[0];
|
|
||||||
expect(DOM.getAttribute(anchorEl, 'href')).toEqual('/user');
|
|
||||||
|
|
||||||
var dispatchedEvent = DOM.createMouseEvent('click');
|
it('test', inject([AsyncTestCompleter], (async) => {
|
||||||
DOM.dispatchEvent(anchorEl, dispatchedEvent);
|
async.done();
|
||||||
expect(dispatchedEvent.defaultPrevented).toBe(true);
|
}));
|
||||||
|
|
||||||
// router navigation is async.
|
it('should navigate to link hrefs without params', inject([AsyncTestCompleter], (async) => {
|
||||||
rtr.subscribe((_) => {
|
compile('<a href="hello" router-link="user"></a>')
|
||||||
expect(location.urlChanges).toEqual(['/user']);
|
.then((_) => rtr.config({
|
||||||
async.done();
|
'path': '/user',
|
||||||
|
'component': UserCmp,
|
||||||
|
'as': 'user'
|
||||||
|
}))
|
||||||
|
.then((_) => rtr.navigate('/a/b'))
|
||||||
|
.then((_) => {
|
||||||
|
view.detectChanges();
|
||||||
|
|
||||||
|
var dispatchedEvent = clickOnElement(view);
|
||||||
|
expect(dispatchedEvent.defaultPrevented).toBe(true);
|
||||||
|
|
||||||
|
// router navigation is async.
|
||||||
|
rtr.subscribe((_) => {
|
||||||
|
expect(location.urlChanges).toEqual(['/user']);
|
||||||
|
async.done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}));
|
||||||
}));
|
|
||||||
|
it('should navigate to link hrefs in presence of base href', inject([AsyncTestCompleter], (async) => {
|
||||||
|
location.setBaseHref('/base');
|
||||||
|
compile('<a href="hello" router-link="user"></a>')
|
||||||
|
.then((_) => rtr.config({
|
||||||
|
'path': '/user',
|
||||||
|
'component': UserCmp,
|
||||||
|
'as': 'user'
|
||||||
|
}))
|
||||||
|
.then((_) => rtr.navigate('/a/b'))
|
||||||
|
.then((_) => {
|
||||||
|
view.detectChanges();
|
||||||
|
|
||||||
|
var dispatchedEvent = clickOnElement(view);
|
||||||
|
expect(dispatchedEvent.defaultPrevented).toBe(true);
|
||||||
|
|
||||||
|
// router navigation is async.
|
||||||
|
rtr.subscribe((_) => {
|
||||||
|
expect(location.urlChanges).toEqual(['/base/user']);
|
||||||
|
async.done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user