2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2017-09-20 13:19:48 +03:00
|
|
|
import {alias} from '@ember/object/computed';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-08-02 10:05:59 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
|
|
|
|
|
|
|
export default Controller.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
notifications: service(),
|
|
|
|
settings: service(),
|
2017-08-02 10:05:59 +03:00
|
|
|
|
2017-10-31 18:27:25 +03:00
|
|
|
dirtyAttributes: null,
|
|
|
|
rollbackValue: null,
|
|
|
|
leaveSettingsTransition: null,
|
2017-08-02 10:05:59 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
unsplashSettings: alias('settings.unsplash'),
|
2017-08-02 10:05:59 +03:00
|
|
|
|
|
|
|
actions: {
|
|
|
|
save() {
|
|
|
|
this.get('save').perform();
|
|
|
|
},
|
|
|
|
|
|
|
|
update(value) {
|
2017-10-31 18:27:25 +03:00
|
|
|
if (!this.get('dirtyAttributes')) {
|
2018-01-11 01:57:43 +03:00
|
|
|
this.set('rollbackValue', this.get('unsplashSettings.isActive'));
|
2017-10-31 18:27:25 +03:00
|
|
|
}
|
2018-01-11 01:57:43 +03:00
|
|
|
this.set('unsplashSettings.isActive', value);
|
2017-10-31 18:27:25 +03:00
|
|
|
this.set('dirtyAttributes', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleLeaveSettingsModal(transition) {
|
|
|
|
let leaveTransition = this.get('leaveSettingsTransition');
|
|
|
|
|
|
|
|
if (!transition && this.get('showLeaveSettingsModal')) {
|
|
|
|
this.set('leaveSettingsTransition', null);
|
|
|
|
this.set('showLeaveSettingsModal', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('leaveSettingsTransition', transition);
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.get('save.isRunning')) {
|
|
|
|
return this.get('save.last').then(() => {
|
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showLeaveSettingsModal', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveSettings() {
|
|
|
|
let transition = this.get('leaveSettingsTransition');
|
|
|
|
|
|
|
|
if (!transition) {
|
|
|
|
this.get('notifications').showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// roll back changes on model props
|
2018-01-11 01:57:43 +03:00
|
|
|
this.set('unsplashSettings.isActive', this.get('rollbackValue'));
|
2017-10-31 18:27:25 +03:00
|
|
|
this.set('dirtyAttributes', false);
|
|
|
|
this.set('rollbackValue', null);
|
|
|
|
|
|
|
|
return transition.retry();
|
2017-08-02 10:05:59 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
save: task(function* () {
|
|
|
|
let unsplash = this.get('unsplashSettings');
|
|
|
|
let settings = this.get('settings');
|
|
|
|
|
|
|
|
try {
|
|
|
|
settings.set('unsplash', unsplash);
|
|
|
|
this.set('dirtyAttributes', false);
|
|
|
|
this.set('rollbackValue', null);
|
|
|
|
return yield settings.save();
|
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
this.get('notifications').showAPIError(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop()
|
2017-08-02 10:05:59 +03:00
|
|
|
});
|