Ghost/ghost/admin/app/utils/document-title.js
Kevin Ansfield 7d95d42eda Fixed "Route#router intimate API" deprecation notices
closes https://github.com/TryGhost/Ghost/issues/9603
- don't add a `setTitle` method on the router and instead call `window.document.title` directly so that we don't have to access  `Route#router` (throws deprecation) or `Route#_router` (private and may break without notice)
2018-04-30 13:07:37 +01:00

56 lines
1.8 KiB
JavaScript

import Route from '@ember/routing/route';
import Router from '@ember/routing/router';
import {isArray as isEmberArray} from '@ember/array';
import {on} from '@ember/object/evented';
export default function () {
Route.reopen({
// `titleToken` can either be a static string or a function
// that accepts a model object and returns a string (or array
// of strings if there are multiple tokens).
titleToken: null,
// `title` can either be a static string or a function
// that accepts an array of tokens and returns a string
// that will be the document title. The `collectTitleTokens` action
// stops bubbling once a route is encountered that has a `title`
// defined.
title: null,
actions: {
collectTitleTokens(tokens) {
let {titleToken} = this;
let finalTitle;
if (typeof this.titleToken === 'function') {
titleToken = this.titleToken(this.currentModel);
}
if (isEmberArray(titleToken)) {
tokens.unshift(...titleToken);
} else if (titleToken) {
tokens.unshift(titleToken);
}
if (this.title) {
if (typeof this.title === 'function') {
finalTitle = this.title(tokens);
} else {
finalTitle = this.title;
}
window.document.title = finalTitle;
} else {
return true;
}
}
}
});
Router.reopen({
updateTitle: on('didTransition', function () {
this.send('collectTitleTokens', []);
})
});
}