2017-10-10 15:26:19 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {isEmpty} from '@ember/utils';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-10-10 15:26:19 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
|
2017-10-30 12:38:01 +03:00
|
|
|
store: service(),
|
2017-10-10 15:26:19 +03:00
|
|
|
|
|
|
|
// 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');
|
2019-02-22 06:17:33 +03:00
|
|
|
let type = this.post.constructor.modelName;
|
2017-10-10 15:26:19 +03:00
|
|
|
|
|
|
|
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 () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let templates = this.customTemplates;
|
2017-10-10 15:26:19 +03:00
|
|
|
let filename = this.get('post.customTemplate');
|
|
|
|
|
|
|
|
return templates.findBy('filename', filename);
|
|
|
|
}),
|
|
|
|
|
|
|
|
// hooks
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
2019-03-06 16:53:54 +03:00
|
|
|
this.loadActiveTheme.perform();
|
2017-10-10 15:26:19 +03:00
|
|
|
},
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
actions: {
|
|
|
|
selectTemplate(template) {
|
|
|
|
this.onTemplateSelect(template.filename);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-10 15:26:19 +03:00
|
|
|
// tasks
|
|
|
|
loadActiveTheme: task(function* () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let store = this.store;
|
2017-10-10 15:26:19 +03:00
|
|
|
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);
|
2018-01-11 20:43:23 +03:00
|
|
|
})
|
2017-10-10 15:26:19 +03:00
|
|
|
});
|