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';
|
|
|
|
import {alias} from '@ember/object/computed';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-05-11 13:37:35 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2017-01-20 12:33:54 +03:00
|
|
|
|
|
|
|
export default Controller.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
notifications: service(),
|
|
|
|
settings: service(),
|
2017-01-20 12:33:54 +03:00
|
|
|
|
2017-10-31 18:27:25 +03:00
|
|
|
leaveSettingsTransition: null,
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
ampSettings: alias('settings.amp'),
|
2017-01-20 12:33:54 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
actions: {
|
|
|
|
update(value) {
|
2018-01-11 01:57:43 +03:00
|
|
|
this.set('ampSettings', value);
|
2017-05-18 13:48:37 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
save() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.save.perform();
|
2017-10-31 18:27:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleLeaveSettingsModal(transition) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let leaveTransition = this.leaveSettingsTransition;
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!transition && this.showLeaveSettingsModal) {
|
2017-10-31 18:27:25 +03:00
|
|
|
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
|
2020-05-11 13:37:35 +03:00
|
|
|
if (this.save.isRunning) {
|
|
|
|
return this.save.last.then(() => {
|
2017-10-31 18:27:25 +03:00
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showLeaveSettingsModal', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveSettings() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let transition = this.leaveSettingsTransition;
|
|
|
|
let settings = this.settings;
|
2017-10-31 18:27:25 +03:00
|
|
|
|
|
|
|
if (!transition) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
2017-10-31 18:27:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-11 01:57:43 +03:00
|
|
|
// roll back changes on settings model
|
2017-10-31 18:27:25 +03:00
|
|
|
settings.rollbackAttributes();
|
|
|
|
|
|
|
|
return transition.retry();
|
2017-01-20 12:33:54 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
2020-05-11 13:37:35 +03:00
|
|
|
save: task(function* () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let amp = this.ampSettings;
|
|
|
|
let settings = this.settings;
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
settings.set('amp', amp);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return yield settings.save();
|
|
|
|
} catch (error) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error);
|
2018-01-11 20:43:23 +03:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}).drop()
|
2017-01-20 12:33:54 +03:00
|
|
|
});
|