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
|
2019-02-26 06:29:57 +03:00
|
|
|
.queryRecord('setting', {type: 'blog,theme,private,members'})
|
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;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-19 19:57:56 +03:00
|
|
|
return settings.save().then((settings) => {
|
|
|
|
this.set('settledIcon', get(settings, 'icon'));
|
|
|
|
return settings;
|
|
|
|
});
|
2017-03-17 20:16:21 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
rollbackAttributes() {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.content.rollbackAttributes();
|
2018-03-09 17:42:27 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
changedAttributes() {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.content.changedAttributes();
|
2017-03-17 20:16:21 +03:00
|
|
|
}
|
|
|
|
});
|