2017-03-17 20:16:21 +03:00
|
|
|
import Ember from 'ember';
|
|
|
|
import RSVP from 'rsvp';
|
2017-05-29 21:50:03 +03:00
|
|
|
import Service from 'ember-service';
|
2017-03-17 20:16:21 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2017-04-19 19:57:56 +03:00
|
|
|
import get from 'ember-metal/get';
|
2017-05-29 21:50:03 +03:00
|
|
|
import injectService from 'ember-service/inject';
|
2017-03-17 20:16:21 +03:00
|
|
|
|
|
|
|
// ember-cli-shims doesn't export _ProxyMixin
|
|
|
|
const {_ProxyMixin} = Ember;
|
|
|
|
|
|
|
|
export default Service.extend(_ProxyMixin, ValidationEngine, {
|
|
|
|
store: injectService(),
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
this._loadingPromise = this.get('store')
|
|
|
|
.queryRecord('setting', {type: 'blog,theme,private'})
|
|
|
|
.then((settings) => {
|
|
|
|
this._loadingPromise = null;
|
|
|
|
return settings;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._loadingPromise;
|
|
|
|
},
|
|
|
|
|
|
|
|
fetch() {
|
|
|
|
if (!this.get('content')) {
|
|
|
|
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() {
|
|
|
|
let settings = this.get('content');
|
|
|
|
|
|
|
|
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() {
|
|
|
|
return this.get('content').rollbackAttributes();
|
|
|
|
}
|
|
|
|
});
|