Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Set document title on route change #620

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 31 additions & 8 deletions src/routing/Router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import global from '../shim/global';
import Evented from '../core/Evented';
import { RouteConfig, History, OutletContext, Params, RouterInterface, Route, RouterOptions } from './interfaces';
import {
RouteConfig,
History,
OutletContext,
Params,
RouterInterface,
Route,
RouterOptions,
MatchType
} from './interfaces';
import { HashHistory } from './history/HashHistory';
import { EventObject } from '../core/Evented';

Expand All @@ -11,7 +21,7 @@ interface RouteWrapper {
route: Route;
segments: string[];
parent?: RouteWrapper;
type?: string;
type: MatchType;
params: Params;
}

Expand Down Expand Up @@ -155,7 +165,7 @@ export class Router extends Evented<{ nav: NavEvent; outlet: OutletEvent }> impl
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 = {} } = config[i];
let { path, outlet, children, defaultRoute = false, defaultParams = {}, title } = config[i];
let [parsedPath, queryParamString] = path.split('?');
let queryParams: string[] = [];
parsedPath = this._stripLeadingSlash(parsedPath);
Expand All @@ -166,6 +176,7 @@ export class Router extends Evented<{ nav: NavEvent; outlet: OutletEvent }> impl
outlet,
path: parsedPath,
segments,
title,
defaultParams: parentRoute ? { ...parentRoute.defaultParams, ...defaultParams } : defaultParams,
children: [],
fullPath: parentRoute ? `${parentRoute.fullPath}/${parsedPath}` : parsedPath,
Expand Down Expand Up @@ -236,14 +247,15 @@ export class Router extends Evented<{ nav: NavEvent; outlet: OutletEvent }> impl
route,
segments: [...segments],
parent: undefined,
params: {}
params: {},
type: 'index'
}));
let routeConfig: RouteWrapper | undefined;
let matchedRoutes: RouteWrapper[] = [];
while ((routeConfig = routeConfigs.pop())) {
const { route, parent, segments, params } = routeConfig;
let segmentIndex = 0;
let type = 'index';
let type: MatchType = 'index';
let paramIndex = 0;
let routeMatch = true;
if (segments.length < route.segments.length) {
Expand Down Expand Up @@ -285,7 +297,7 @@ export class Router extends Evented<{ nav: NavEvent; outlet: OutletEvent }> impl
}

let matchedOutletName: string | undefined = undefined;
let matchedRoute: any = matchedRoutes.reduce((match: any, matchedRoute: any) => {
let matchedRoute: RouteWrapper | undefined = matchedRoutes.reduce((match: any, matchedRoute: any) => {
if (!match) {
return matchedRoute;
}
Expand All @@ -300,8 +312,19 @@ export class Router extends Evented<{ nav: NavEvent; outlet: OutletEvent }> impl
matchedRoute.type = 'error';
}
matchedOutletName = matchedRoute.route.outlet;
const title = this._options.setDocumentTitle
? this._options.setDocumentTitle({
outlet: matchedOutletName,
title: matchedRoute.route.title,
params: matchedRoute.params,
queryParams: this._currentQueryParams
})
: matchedRoute.route.title;
if (title) {
global.document.title = title;
}
while (matchedRoute) {
let { type, params, parent, route } = matchedRoute;
let { type, params, route } = matchedRoute;
const matchedOutlet = {
id: route.outlet,
queryParams: this._currentQueryParams,
Expand All @@ -315,7 +338,7 @@ export class Router extends Evented<{ nav: NavEvent; outlet: OutletEvent }> impl
if (!previousMatchedOutlet || !matchingParams(previousMatchedOutlet, matchedOutlet)) {
this.emit({ type: 'outlet', outlet: matchedOutlet, action: 'enter' });
}
matchedRoute = parent;
matchedRoute = matchedRoute.parent;
}
} else {
this._matchedOutlets.errorOutlet = {
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 @@ -22,6 +22,7 @@ export interface Route {
fullQueryParams: string[];
defaultParams: Params;
score: number;
title?: string;
}

/**
Expand All @@ -33,6 +34,7 @@ export interface RouteConfig {
children?: RouteConfig[];
defaultParams?: Params;
defaultRoute?: boolean;
title?: string;
}

/**
Expand Down Expand Up @@ -159,6 +161,20 @@ export interface OnChangeFunction {
(path: string): void;
}

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

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

/**
* Options for a history provider
*/
Expand Down Expand Up @@ -200,4 +216,5 @@ export interface RouterOptions {
window?: Window;
base?: string;
HistoryManager?: HistoryConstructor;
setDocumentTitle?: SetDocumentTitle;
}
25 changes: 23 additions & 2 deletions 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: jsdomDescribe } = 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 @@ -156,7 +158,7 @@ const config = [
}
];

describe('Router', () => {
jsdomDescribe('Router', () => {
it('Navigates to current route if matches against a registered outlet', () => {
const router = new Router(routeConfig, { HistoryManager });
const context = router.getOutlet('home');
Expand Down Expand Up @@ -543,4 +545,23 @@ describe('Router', () => {
assert.strictEqual(historyManagerCount, 1);
assert.isTrue(initialNavEvent);
});

it('should set the title as defined in the routing config', () => {
const router = new Router([{ 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([{ outlet: 'foo', path: 'foo/{id}?{query}', title: 'foo' }], {
HistoryManager,
setDocumentTitle({ title, params, queryParams, outlet }) {
return `${title}-${outlet}-${params.id}-${queryParams.query}`;
}
});
router.setPath('/foo/id-value?query=queryValue');
assert.strictEqual(global.document.title, 'foo-foo-id-value-queryValue');
});
});