Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/routing/Router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import global from '../shim/global';
import Evented from '../core/Evented';
import {
RouteConfig,
Expand Down Expand Up @@ -179,7 +180,7 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
private _register(config: RouteConfig[], routes?: Route[], parentRoute?: Route): void {
routes = routes ? routes : this._routes;
for (let i = 0; i < config.length; i++) {
let { path, outlet, children, defaultRoute = false, defaultParams = {}, id } = config[i];
let { path, outlet, children, defaultRoute = false, defaultParams = {}, id, title } = config[i];
let [parsedPath, queryParamString] = path.split('?');
let queryParams: string[] = [];
parsedPath = this._stripLeadingSlash(parsedPath);
Expand All @@ -189,6 +190,7 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
params: [],
id,
outlet,
title,
path: parsedPath,
segments,
defaultParams: parentRoute ? { ...parentRoute.defaultParams, ...defaultParams } : defaultParams,
Expand Down Expand Up @@ -326,6 +328,17 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
matchedRoute.type = 'error';
}
matchedRouteId = matchedRoute.route.id;
const title = this._options.setDocumentTitle
? this._options.setDocumentTitle({
id: matchedRouteId,
title: matchedRoute.route.title,
params: matchedRoute.params,
queryParams: this._currentQueryParams
})
: matchedRoute.route.title;
if (title) {
global.document.title = title;
}
while (matchedRoute) {
let { type, params, route } = matchedRoute;
let parent: RouteWrapper | undefined = matchedRoute.parent;
Expand Down
17 changes: 17 additions & 0 deletions src/routing/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Route {
id: string;
path: string;
outlet: string;
title?: string;
params: string[];
segments: string[];
children: Route[];
Expand All @@ -35,6 +36,7 @@ export interface RouteConfig {
children?: RouteConfig[];
defaultParams?: Params;
defaultRoute?: boolean;
title?: string;
}

/**
Expand Down Expand Up @@ -207,9 +209,24 @@ export interface History {
readonly current: string;
}

/**
* Document title option
*/
export interface DocumentTitleOptions {
id: string;
params: Params;
queryParams: Params;
title?: string;
}

export interface SetDocumentTitle {
(options: DocumentTitleOptions): string | undefined;
}

export interface RouterOptions {
autostart?: boolean;
window?: Window;
base?: string;
HistoryManager?: HistoryConstructor;
setDocumentTitle?: SetDocumentTitle;
}
25 changes: 24 additions & 1 deletion tests/routing/unit/Router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { describe, it } = intern.getInterface('bdd');
const { it } = intern.getInterface('bdd');
const { describe } = intern.getPlugin('jsdom');
const { assert } = intern.getPlugin('chai');

import global from '../../../src/shim/global';
import { Router } from '../../../src/routing/Router';
import { MemoryHistory as HistoryManager } from '../../../src/routing/history/MemoryHistory';

Expand Down Expand Up @@ -579,6 +581,27 @@ describe('Router', () => {
assert.isTrue(initialNavEvent);
});

describe('Document Title', () => {
it('should set the title as defined in the routing config', () => {
const router = new Router([{ id: 'foo', outlet: 'foo', path: 'foo/{id}?{query}', title: 'foo' }], {
HistoryManager
});
router.setPath('/foo/id-value?query=queryValue');
assert.strictEqual(global.document.title, 'foo');
});

it('should set the title as using the set document title callback', () => {
const router = new Router([{ id: 'foo', outlet: 'foo', path: 'foo/{id}?{query}', title: 'static-foo' }], {
HistoryManager,
setDocumentTitle({ title, params, queryParams, id }) {
return `${title}-${id}-${params.id}-${queryParams.query}`;
}
});
router.setPath('/foo/id-value?query=queryValue');
assert.strictEqual(global.document.title, 'static-foo-foo-id-value-queryValue');
});
});

describe('outlets', () => {
it('should match against all routes for an outlet', () => {
const router = new Router(
Expand Down