Ghost/ghost/admin/app/components/gh-psm-template-select.js
Kevin Ansfield 24df9781cc Custom Post Templates UI (#878)
refs https://github.com/TryGhost/Ghost/issues/9060

- add `{{gh-psm-template-select}}` component
  - allows selection of a custom template for a post if the active theme has custom templates
  - loads themes on render, only hitting the server if not already in the store
  - disables select if post slug matches a `post-*.hbs` or `page-*.hbs` template
- adds `customTemplate` attr to `Post` model
- adds `templates` attr to `Theme` model with CPs to pull out custom vs post/page override templates
- add `.gh-select.disabled` styles to make disabled selects look visually disabled
2017-10-10 14:26:19 +02:00

76 lines
2.1 KiB
JavaScript

import Component from '@ember/component';
import {computed} from '@ember/object';
import {inject as injectService} from '@ember/service';
import {isEmpty} from '@ember/utils';
import {task} from 'ember-concurrency';
export default Component.extend({
store: injectService(),
// public attributes
tagName: '',
post: null,
// internal properties
activeTheme: null,
// closure actions
onTemplateSelect() {},
// computed properties
customTemplates: computed('activeTheme.customTemplates.[]', function () {
let templates = this.get('activeTheme.customTemplates') || [];
let defaultTemplate = {
filename: '',
name: 'Default'
};
return isEmpty(templates) ? templates : [defaultTemplate, ...templates.sortBy('name')];
}),
matchedSlugTemplate: computed('post.{page,slug}', 'activeTheme.slugTemplates.[]', function () {
let slug = this.get('post.slug');
let type = this.get('post.page') ? 'page' : 'post';
let [matchedTemplate] = this.get('activeTheme.slugTemplates').filter(function (template) {
return template.for.includes(type) && template.slug === slug;
});
return matchedTemplate;
}),
selectedTemplate: computed('post.customTemplate', 'customTemplates.[]', function () {
let templates = this.get('customTemplates');
let filename = this.get('post.customTemplate');
return templates.findBy('filename', filename);
}),
// hooks
didInsertElement() {
this._super(...arguments);
this.get('loadActiveTheme').perform();
},
// tasks
loadActiveTheme: task(function* () {
let store = this.get('store');
let themes = yield store.peekAll('theme');
if (isEmpty(themes)) {
themes = yield store.findAll('theme');
}
let activeTheme = themes.filterBy('active', true).get('firstObject');
this.set('activeTheme', activeTheme);
}),
actions: {
selectTemplate(template) {
this.onTemplateSelect(template.filename);
}
}
});