2017-08-22 10:53:26 +03:00
|
|
|
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';
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2015-08-19 14:55:40 +03:00
|
|
|
export default function () {
|
2015-10-28 14:36:45 +03:00
|
|
|
Route.reopen({
|
2014-11-25 23:56:08 +03:00
|
|
|
// `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,
|
|
|
|
|
2015-11-30 21:23:47 +03:00
|
|
|
actions: {
|
2019-01-21 14:44:30 +03:00
|
|
|
updateDocumentTitle() {
|
|
|
|
this.send('collectTitleTokens', []);
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
collectTitleTokens(tokens) {
|
|
|
|
let {titleToken} = this;
|
|
|
|
let finalTitle;
|
2014-11-25 23:56:08 +03:00
|
|
|
|
|
|
|
if (typeof this.titleToken === 'function') {
|
|
|
|
titleToken = this.titleToken(this.currentModel);
|
|
|
|
}
|
|
|
|
|
2016-06-30 13:21:47 +03:00
|
|
|
if (isEmberArray(titleToken)) {
|
2016-04-08 18:00:16 +03:00
|
|
|
tokens.unshift(...titleToken);
|
2014-11-25 23:56:08 +03:00
|
|
|
} else if (titleToken) {
|
|
|
|
tokens.unshift(titleToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.title) {
|
|
|
|
if (typeof this.title === 'function') {
|
|
|
|
finalTitle = this.title(tokens);
|
|
|
|
} else {
|
|
|
|
finalTitle = this.title;
|
|
|
|
}
|
|
|
|
|
2018-04-30 15:07:37 +03:00
|
|
|
window.document.title = finalTitle;
|
2014-11-25 23:56:08 +03:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
Router.reopen({
|
|
|
|
updateTitle: on('didTransition', function () {
|
2019-01-21 14:44:30 +03:00
|
|
|
this.send('updateDocumentTitle');
|
2018-04-30 15:07:37 +03:00
|
|
|
})
|
2014-11-25 23:56:08 +03:00
|
|
|
});
|
2015-08-19 14:55:40 +03:00
|
|
|
}
|