2019-01-24 22:34:32 +03:00
|
|
|
import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
|
2019-10-10 15:36:33 +03:00
|
|
|
import CurrentUserSettings from 'ghost-admin/mixins/current-user-settings';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
|
|
|
export default AuthenticatedRoute.extend(CurrentUserSettings, {
|
|
|
|
router: service(),
|
|
|
|
|
2019-12-12 16:35:52 +03:00
|
|
|
_requiresBackgroundRefresh: true,
|
|
|
|
|
2019-10-10 15:36:33 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.router.on('routeWillChange', (transition) => {
|
|
|
|
this.showUnsavedChangesModal(transition);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeModel() {
|
|
|
|
this._super(...arguments);
|
|
|
|
return this.get('session.user')
|
|
|
|
.then(this.transitionAuthor());
|
|
|
|
},
|
2019-01-24 22:34:32 +03:00
|
|
|
|
2019-04-16 18:02:43 +03:00
|
|
|
model(params) {
|
2019-12-12 16:35:52 +03:00
|
|
|
this._requiresBackgroundRefresh = false;
|
|
|
|
|
|
|
|
if (params.member_id) {
|
|
|
|
return this.store.findRecord('member', params.member_id, {reload: true});
|
|
|
|
} else {
|
|
|
|
return this.store.createRecord('member');
|
|
|
|
}
|
2019-04-16 18:02:43 +03:00
|
|
|
},
|
|
|
|
|
2019-12-12 16:35:52 +03:00
|
|
|
setupController(controller, member) {
|
2019-04-16 18:02:43 +03:00
|
|
|
this._super(...arguments);
|
2019-12-12 16:35:52 +03:00
|
|
|
if (this._requiresBackgroundRefresh) {
|
|
|
|
controller.fetchMember.perform(member.get('id'));
|
2019-04-16 18:02:43 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2019-12-12 16:35:52 +03:00
|
|
|
// clean up newly created records and revert unsaved changes to existing
|
|
|
|
this.controller.member.rollbackAttributes();
|
|
|
|
|
|
|
|
this._requiresBackgroundRefresh = true;
|
2019-04-16 18:02:43 +03:00
|
|
|
},
|
2019-01-24 22:34:32 +03:00
|
|
|
|
2019-10-10 15:36:33 +03:00
|
|
|
actions: {
|
|
|
|
save() {
|
|
|
|
this.controller.send('save');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-24 22:34:32 +03:00
|
|
|
titleToken() {
|
|
|
|
return this.controller.get('member.name');
|
2019-10-10 15:36:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
showUnsavedChangesModal(transition) {
|
2019-12-12 16:35:52 +03:00
|
|
|
if (transition.from && transition.from.name === this.routeName && transition.targetName) {
|
2019-10-10 15:36:33 +03:00
|
|
|
let {controller} = this;
|
|
|
|
|
2019-12-12 16:35:52 +03:00
|
|
|
// member.changedAttributes is always true for new members but number of changed attrs is reliable
|
|
|
|
let isChanged = Object.keys(controller.member.changedAttributes()).length > 0;
|
|
|
|
|
|
|
|
if (!controller.member.isDeleted && isChanged) {
|
2019-10-10 15:36:33 +03:00
|
|
|
transition.abort();
|
|
|
|
controller.send('toggleUnsavedChangesModal', transition);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 22:34:32 +03:00
|
|
|
}
|
|
|
|
});
|