8fcd75de32
closes https://github.com/tryghost/ghost/issues/10088 * added `updateDocumentTitle` action to base route and replace usage of `.send('collectTitleTokens, [])` * added `.titleToken()` method to editor route to add post title to document title * called `.send('updateDocumentTitle')` after saving post title in editor controller to keep document title in sync * updated editor controller test for latest ember-mocha and ember-test-helpers
60 lines
1.9 KiB
JavaScript
60 lines
1.9 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: {
|
|
updateDocumentTitle() {
|
|
this.send('collectTitleTokens', []);
|
|
},
|
|
|
|
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('updateDocumentTitle');
|
|
})
|
|
});
|
|
}
|