2016-06-30 13:21:47 +03:00
|
|
|
import Route from 'ember-route';
|
|
|
|
import Router from 'ember-router';
|
|
|
|
import {isEmberArray} from 'ember-array/utils';
|
|
|
|
import on from 'ember-evented/on';
|
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: {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.router.setTitle(finalTitle);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
Router.reopen({
|
|
|
|
updateTitle: on('didTransition', function () {
|
2014-11-25 23:56:08 +03:00
|
|
|
this.send('collectTitleTokens', []);
|
2015-09-03 14:06:50 +03:00
|
|
|
}),
|
2014-11-25 23:56:08 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
setTitle(title) {
|
2015-10-15 15:03:26 +03:00
|
|
|
window.document.title = title;
|
2014-11-25 23:56:08 +03:00
|
|
|
}
|
|
|
|
});
|
2015-08-19 14:55:40 +03:00
|
|
|
}
|