2021-09-27 16:01:09 +03:00
|
|
|
import Service from '@ember/service';
|
2021-10-18 18:39:54 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
2021-09-27 16:01:09 +03:00
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency-decorators';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class CustomThemeSettingsServices extends Service {
|
|
|
|
@service store;
|
|
|
|
|
|
|
|
@tracked settings = [];
|
2021-10-14 16:15:11 +03:00
|
|
|
@tracked settingGroups = [];
|
|
|
|
|
2021-10-22 12:46:12 +03:00
|
|
|
_hasLoaded = false;
|
|
|
|
|
2021-10-14 16:15:11 +03:00
|
|
|
KNOWN_GROUPS = [{
|
|
|
|
key: 'homepage',
|
|
|
|
name: 'Homepage',
|
|
|
|
icon: 'house',
|
|
|
|
previewType: 'homepage'
|
|
|
|
}, {
|
|
|
|
key: 'post',
|
|
|
|
name: 'Post',
|
|
|
|
icon: 'post',
|
|
|
|
previewType: 'post'
|
|
|
|
}];
|
2021-09-27 16:01:09 +03:00
|
|
|
|
2021-09-28 18:12:01 +03:00
|
|
|
get isDirty() {
|
|
|
|
const dirtySetting = this.settings.find(setting => setting.hasDirtyAttributes);
|
|
|
|
return !!dirtySetting;
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:28:26 +03:00
|
|
|
get keyValueObject() {
|
|
|
|
const keyValue = {};
|
|
|
|
|
|
|
|
this.settings.forEach((setting) => {
|
|
|
|
keyValue[setting.key] = setting.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
return keyValue;
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:01:09 +03:00
|
|
|
load() {
|
|
|
|
return this.loadTask.perform();
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:46:12 +03:00
|
|
|
reload() {
|
|
|
|
this._hasLoaded = false;
|
|
|
|
|
|
|
|
return this.loadTask.perform();
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:01:09 +03:00
|
|
|
@task
|
|
|
|
*loadTask() {
|
2021-10-22 12:46:12 +03:00
|
|
|
if (this.hasLoaded) {
|
|
|
|
return this.settings;
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:01:09 +03:00
|
|
|
// unload stored settings and re-load from API so they always match active theme
|
|
|
|
// run is required here, see https://github.com/emberjs/data/issues/5447#issuecomment-845672812
|
|
|
|
run(() => this.store.unloadAll('custom-theme-setting'));
|
|
|
|
|
|
|
|
const settings = yield this.store.findAll('custom-theme-setting');
|
|
|
|
this.settings = settings;
|
2021-10-14 16:15:11 +03:00
|
|
|
this.settingGroups = this._buildSettingGroups(settings);
|
2021-09-27 16:01:09 +03:00
|
|
|
|
2021-10-22 12:46:12 +03:00
|
|
|
this._hasLoaded = true;
|
|
|
|
|
2021-09-27 16:01:09 +03:00
|
|
|
return this.settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
return this.saveTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*saveTask() {
|
2021-10-18 18:39:54 +03:00
|
|
|
if (isEmpty(this.settings)) {
|
|
|
|
return this.settings;
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:01:09 +03:00
|
|
|
// save all records in a single request to `/custom_theme_settings`
|
|
|
|
const listRecord = this.store.createRecord('custom-theme-setting-list', {customThemeSettings: this.settings});
|
|
|
|
yield listRecord.save();
|
|
|
|
|
2021-09-30 18:29:41 +03:00
|
|
|
// don't keep references to lists and their children around
|
2021-09-27 16:01:09 +03:00
|
|
|
this.store.unloadRecord(listRecord);
|
|
|
|
|
|
|
|
return this.settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
rollback() {
|
2021-10-19 18:22:16 +03:00
|
|
|
this.settings.forEach(setting => setting.rollbackAttributes());
|
2021-10-14 16:15:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_buildSettingGroups(settings) {
|
|
|
|
if (!settings || !settings.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupKeys = this.KNOWN_GROUPS.map(g => g.key);
|
|
|
|
const groups = [];
|
|
|
|
|
|
|
|
const siteWideSettings = settings.filter(setting => !groupKeys.includes(setting.group));
|
|
|
|
if (siteWideSettings.length) {
|
|
|
|
groups.push({
|
|
|
|
key: 'site-wide',
|
|
|
|
name: 'Site-wide',
|
|
|
|
icon: 'view-site',
|
|
|
|
settings: siteWideSettings
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.KNOWN_GROUPS.forEach((knownGroup) => {
|
|
|
|
const groupSettings = settings.filter(setting => setting.group === knownGroup.key);
|
|
|
|
|
|
|
|
if (groupSettings.length) {
|
2021-10-14 18:58:05 +03:00
|
|
|
groups.push(Object.assign({}, knownGroup, {settings: groupSettings}));
|
2021-10-14 16:15:11 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return groups;
|
2021-09-27 16:01:09 +03:00
|
|
|
}
|
|
|
|
}
|