fix(router): strip base href from URLs when navigating
This commit is contained in:
36
modules/angular2/src/router/browser_location.js
vendored
Normal file
36
modules/angular2/src/router/browser_location.js
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
import {DOM} from 'angular2/src/dom/dom_adapter';
|
||||
|
||||
export class BrowserLocation {
|
||||
_location;
|
||||
_history;
|
||||
_baseHref:string;
|
||||
constructor() {
|
||||
this._location = DOM.getLocation();
|
||||
this._history = DOM.getHistory();
|
||||
this._baseHref = DOM.getBaseHref();
|
||||
}
|
||||
|
||||
onPopState(fn) {
|
||||
DOM.getGlobalEventTarget('window').addEventListener('popstate', fn, false);
|
||||
}
|
||||
|
||||
getBaseHref() {
|
||||
return this._baseHref;
|
||||
}
|
||||
|
||||
path() {
|
||||
return this._location.pathname;
|
||||
}
|
||||
|
||||
pushState(state:any, title:string, url:string) {
|
||||
this._history.pushState(state, title, url);
|
||||
}
|
||||
|
||||
forward() {
|
||||
this._history.forward();
|
||||
}
|
||||
|
||||
back() {
|
||||
this._history.back();
|
||||
}
|
||||
}
|
47
modules/angular2/src/router/location.js
vendored
47
modules/angular2/src/router/location.js
vendored
@ -1,40 +1,63 @@
|
||||
import {DOM} from 'angular2/src/dom/dom_adapter';
|
||||
import {BrowserLocation} from './browser_location';
|
||||
import {StringWrapper} from 'angular2/src/facade/lang';
|
||||
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
||||
|
||||
export class Location {
|
||||
_location;
|
||||
_subject:EventEmitter;
|
||||
_history;
|
||||
constructor() {
|
||||
_browserLocation:BrowserLocation;
|
||||
_baseHref:string;
|
||||
constructor(browserLocation:BrowserLocation) {
|
||||
this._subject = new EventEmitter();
|
||||
this._location = DOM.getLocation();
|
||||
this._history = DOM.getHistory();
|
||||
DOM.getGlobalEventTarget('window').addEventListener('popstate', (_) => this._onPopState(_), false);
|
||||
this._browserLocation = browserLocation;
|
||||
this._baseHref = stripIndexHtml(this._browserLocation.getBaseHref());
|
||||
this._browserLocation.onPopState((_) => this._onPopState(_));
|
||||
}
|
||||
|
||||
_onPopState(_) {
|
||||
ObservableWrapper.callNext(this._subject, {
|
||||
'url': this._location.pathname
|
||||
'url': this.path()
|
||||
});
|
||||
}
|
||||
|
||||
path() {
|
||||
return this._location.pathname;
|
||||
return this.normalize(this._browserLocation.path());
|
||||
}
|
||||
|
||||
normalize(url) {
|
||||
return this._stripBaseHref(stripIndexHtml(url));
|
||||
}
|
||||
|
||||
_stripBaseHref(url) {
|
||||
if (this._baseHref.length > 0 && StringWrapper.startsWith(url, this._baseHref)) {
|
||||
return StringWrapper.substring(url, this._baseHref.length);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
go(url:string) {
|
||||
this._history.pushState(null, null, url);
|
||||
url = this._stripBaseHref(url);
|
||||
this._browserLocation.pushState(null, null, url);
|
||||
}
|
||||
|
||||
forward() {
|
||||
this._history.forward();
|
||||
this._browserLocation.forward();
|
||||
}
|
||||
|
||||
back() {
|
||||
this._history.back()
|
||||
this._browserLocation.back();
|
||||
}
|
||||
|
||||
subscribe(onNext, onThrow = null, onReturn = null) {
|
||||
ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
function stripIndexHtml(url) {
|
||||
// '/index.html'.length == 11
|
||||
if (url.length > 10 && StringWrapper.substring(url, url.length - 11) == '/index.html') {
|
||||
return StringWrapper.substring(url, 0, url.length - 11);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
Reference in New Issue
Block a user