Ghost/core/client/utils/document-title.js
Felix Rieseberg 7c4381c812 Dynamic Titles in Ghost Admin
- Every route can set a title token that is combined with the blog’s
title, resulting in titles like ‘Content - Test Blog’.
- Subroutes are supported (‘Settings - General - Test Blog’)
- The blog’s name is applied to and taken from the `config` object to
spare Ember a REST call via `store.find(‘settings’)`.
- Tests have been changed to test for the new titles.
- The initially proposed solution
(https://github.com/paddle8/ember-document-title) doesn’t play nice
with EAK, which is why I went with this solution
(https://gist.github.com/machty/8413411) by Ember.JS core dev @Machty.
2014-11-27 15:41:00 -08:00

61 lines
1.9 KiB
JavaScript

var documentTitle = function () {
Ember.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: function (tokens) {
var titleToken = this.titleToken,
finalTitle;
if (typeof this.titleToken === 'function') {
titleToken = this.titleToken(this.currentModel);
}
if (Ember.isArray(titleToken)) {
tokens.unshift.apply(this, titleToken);
} else if (titleToken) {
tokens.unshift(titleToken);
}
if (this.title) {
if (typeof this.title === 'function') {
finalTitle = this.title(tokens);
} else {
finalTitle = this.title;
}
this.router.setTitle(finalTitle);
} else {
return true;
}
}
}
});
Ember.Router.reopen({
updateTitle: function () {
this.send('collectTitleTokens', []);
}.on('didTransition'),
setTitle: function (title) {
if (Ember.testing) {
this._title = title;
} else {
window.document.title = title;
}
}
});
};
export default documentTitle;