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-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';
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
export default Controller.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
notifications: service(),
|
2018-01-11 01:57:43 +03:00
|
|
|
settings: service(),
|
2014-07-31 23:36:20 +04:00
|
|
|
|
2017-05-18 13:48:37 +03:00
|
|
|
actions: {
|
|
|
|
save() {
|
2020-05-11 13:37:35 +03:00
|
|
|
this.saveTask.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
|
2021-02-11 18:39:45 +03:00
|
|
|
if (this.saveTask.isRunning) {
|
|
|
|
return this.saveTask.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 props
|
2017-10-31 18:27:25 +03:00
|
|
|
settings.rollbackAttributes();
|
|
|
|
|
|
|
|
return transition.retry();
|
2017-05-18 13:48:37 +03:00
|
|
|
}
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
2020-04-06 13:47:28 +03:00
|
|
|
saveTask: task(function* () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let notifications = this.notifications;
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
try {
|
2019-03-06 16:53:54 +03:00
|
|
|
return yield this.settings.save();
|
2018-01-11 20:43:23 +03:00
|
|
|
} catch (error) {
|
|
|
|
notifications.showAPIError(error, {key: 'code-injection.save'});
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
})
|
2014-07-31 23:36:20 +04:00
|
|
|
});
|