2017-03-17 20:16:21 +03:00
|
|
|
import Ember from 'ember';
|
|
|
|
import RSVP from 'rsvp';
|
2017-10-30 12:38:01 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2017-03-17 20:16:21 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {get} from '@ember/object';
|
2017-03-17 20:16:21 +03:00
|
|
|
|
|
|
|
// ember-cli-shims doesn't export _ProxyMixin
|
|
|
|
const {_ProxyMixin} = Ember;
|
|
|
|
|
|
|
|
export default Service.extend(_ProxyMixin, ValidationEngine, {
|
2017-10-30 12:38:01 +03:00
|
|
|
store: service(),
|
2017-03-17 20:16:21 +03:00
|
|
|
|
|
|
|
// will be set to the single Settings model, it's a reference so any later
|
|
|
|
// changes to the settings object in the store will be reflected
|
|
|
|
content: null,
|
|
|
|
|
|
|
|
validationType: 'setting',
|
|
|
|
_loadingPromise: null,
|
|
|
|
|
2017-04-19 19:57:56 +03:00
|
|
|
// this is an odd case where we only want to react to changes that we get
|
|
|
|
// back from the API rather than local updates
|
|
|
|
settledIcon: '',
|
|
|
|
|
2017-03-17 20:16:21 +03:00
|
|
|
// the settings API endpoint is a little weird as it's singular and we have
|
|
|
|
// to pass in all types - if we ever fetch settings without all types then
|
|
|
|
// save we have problems with the missing settings being removed or reset
|
|
|
|
_loadSettings() {
|
|
|
|
if (!this._loadingPromise) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this._loadingPromise = this.store
|
2021-05-07 12:02:19 +03:00
|
|
|
.queryRecord('setting', {group: 'site,theme,private,members,portal,newsletter,email,amp,labs,slack,unsplash,views,firstpromoter,oauth,editor'})
|
2017-03-17 20:16:21 +03:00
|
|
|
.then((settings) => {
|
|
|
|
this._loadingPromise = null;
|
|
|
|
return settings;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._loadingPromise;
|
|
|
|
},
|
|
|
|
|
|
|
|
fetch() {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!this.content) {
|
2017-03-17 20:16:21 +03:00
|
|
|
return this.reload();
|
|
|
|
} else {
|
|
|
|
return RSVP.resolve(this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
reload() {
|
|
|
|
return this._loadSettings().then((settings) => {
|
|
|
|
this.set('content', settings);
|
2017-04-19 19:57:56 +03:00
|
|
|
this.set('settledIcon', get(settings, 'icon'));
|
2017-03-17 20:16:21 +03:00
|
|
|
return this;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-06-04 23:30:06 +03:00
|
|
|
async save() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let settings = this.content;
|
2017-03-17 20:16:21 +03:00
|
|
|
|
|
|
|
if (!settings) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-04 23:30:06 +03:00
|
|
|
await settings.save();
|
|
|
|
this.set('settledIcon', settings.icon);
|
|
|
|
return settings;
|
2017-03-17 20:16:21 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
rollbackAttributes() {
|
2021-01-20 01:31:59 +03:00
|
|
|
return this.content?.rollbackAttributes();
|
2018-03-09 17:42:27 +03:00
|
|
|
},
|
2020-02-24 12:08:47 +03:00
|
|
|
|
2018-03-09 17:42:27 +03:00
|
|
|
changedAttributes() {
|
2021-01-20 01:31:59 +03:00
|
|
|
return this.content?.changedAttributes();
|
2017-03-17 20:16:21 +03:00
|
|
|
}
|
|
|
|
});
|