Ghost/ghost/admin/app/components/gh-psm-template-select.js
Kevin Ansfield 352c4af1d7 Refactored usage of .get('property') with es5 getters
no issue
- ran [es5-getter-ember-codemod](https://github.com/rondale-sc/es5-getter-ember-codemod)
- [es5 getters RFC](https://github.com/emberjs/rfcs/blob/master/text/0281-es5-getters.md)
- updates the majority of `object.get('property')` with `object.property` with exceptions:
  - `.get('nested.property')` - it's not possible to determine if this is relying on "safe" path chaining for when `nested` doesn't exist
  - `.get('config.x')` and `.get('settings.x')` - both our `config` and `settings` services are proxy objects which do not support es5 getters
- this PR is not exhaustive, there are still a number of places where `.get('service.foo')` and similar could be replaced but it gets us a long way there in a quick and automated fashion
2019-03-06 13:54:14 +00:00

76 lines
2.0 KiB
JavaScript

import Component from '@ember/component';
import {computed} from '@ember/object';
import {isEmpty} from '@ember/utils';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default Component.extend({
store: service(),
// 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.post.constructor.modelName;
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.customTemplates;
let filename = this.get('post.customTemplate');
return templates.findBy('filename', filename);
}),
// hooks
didInsertElement() {
this._super(...arguments);
this.loadActiveTheme.perform();
},
actions: {
selectTemplate(template) {
this.onTemplateSelect(template.filename);
}
},
// tasks
loadActiveTheme: task(function* () {
let store = this.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);
})
});