Make templates work with nonempty baseUrls (e.g., IIS virtual directories)

This commit is contained in:
Steve Sanderson
2017-07-11 23:45:25 +01:00
parent bb0727c34c
commit e65ecebac6
31 changed files with 65 additions and 44 deletions

View File

@@ -16,7 +16,7 @@ export class Router {
private disposeHistory: () => void;
private clickEventListener: EventListener;
constructor(history: History.History, routes: Route[]) {
constructor(private history: History.History, routes: Route[], basename: string) {
// Reset and configure Crossroads so it matches routes and updates this.currentRoute
crossroads.removeAllRoutes();
crossroads.resetState();
@@ -33,8 +33,9 @@ export class Router {
let target: any = evt.currentTarget;
if (target && target.tagName === 'A') {
let href = target.getAttribute('href');
if (href && href.charAt(0) == '/') {
history.push(href);
if (href && href.indexOf(basename + '/') === 0) {
const hrefAfterBasename = href.substring(basename.length);
history.push(hrefAfterBasename);
evt.preventDefault();
}
}
@@ -46,6 +47,10 @@ export class Router {
crossroads.parse((history as any).location.pathname);
}
public link(url: string): string {
return this.history.createHref({ pathname: url });
}
public dispose() {
this.disposeHistory();
$(document).off('click', 'a', this.clickEventListener);