Ghost/ghost/admin/app/mixins/editor-base-route.js
Kevin Ansfield b4cdc85a59 "400 Version Mismatch" error handling
refs https://github.com/TryGhost/Ghost/issues/6949

Handle version mismatch errors by:
- displaying an alert asking the user to copy any data and refresh
- disabling navigation so that unsaved data is not accidentally lost

Detailed changes:
- add `error` action to application route for global route-based error handling
- remove 404-handler mixin, move logic into app route error handler
- update `.catch` in validation-engine so that promises are rejected with the
  original error objects
- add `VersionMismatchError` and `isVersionMismatchError` to ajax service
- add `upgrade-status` service
  - has a method to trigger the alert and toggle the "upgrade required" mode
  - is injected into all routes by default so that it can be checked before
    transitioning
- add `Route` override
  - updates the `willTransition` hook to check the `upgrade-status` service
    and abort the transition if we're in "upgrade required" mode
- update notifications `showAPIError` method to handle version mismatch errors
- update any areas where we were catching ajax errors manually so that the
  version mismatch error handling is obeyed
- fix redirect tests in editor acceptance test
- fix mirage's handling of 404s for unknown posts in get post requests
- adjust alert z-index to to appear above modal backgrounds
2016-07-08 14:56:26 +01:00

151 lines
5.3 KiB
JavaScript

import $ from 'jquery';
import Mixin from 'ember-metal/mixin';
import RSVP from 'rsvp';
import run from 'ember-runloop';
import ShortcutsRoute from 'ghost-admin/mixins/shortcuts-route';
import styleBody from 'ghost-admin/mixins/style-body';
import ctrlOrCmd from 'ghost-admin/utils/ctrl-or-cmd';
let generalShortcuts = {};
generalShortcuts[`${ctrlOrCmd}+alt+p`] = 'publish';
generalShortcuts['alt+shift+z'] = 'toggleZenMode';
export default Mixin.create(styleBody, ShortcutsRoute, {
classNames: ['editor'],
shortcuts: generalShortcuts,
actions: {
save() {
let selectedElement = $(document.activeElement);
if (selectedElement.is('input[type="text"]')) {
selectedElement.trigger('focusout');
}
run.scheduleOnce('actions', this, function () {
this.get('controller').send('save');
});
},
publish() {
let controller = this.get('controller');
controller.send('setSaveType', 'publish');
controller.send('save');
},
toggleZenMode() {
$('body').toggleClass('zen');
},
willTransition(transition) {
let controller = this.get('controller');
let scratch = controller.get('model.scratch');
let controllerIsDirty = controller.get('hasDirtyAttributes');
let model = controller.get('model');
let state = model.getProperties('isDeleted', 'isSaving', 'hasDirtyAttributes', 'isNew');
let deletedWithoutChanges,
fromNewToEdit;
if (this.get('upgradeStatus.isRequired')) {
return this._super(...arguments);
}
// if a save is in-flight we don't know whether or not it's safe to leave
// so we abort the transition and retry after the save has completed.
if (state.isSaving) {
transition.abort();
return run.later(this, function () {
RSVP.resolve(controller.get('lastPromise')).then(() => {
transition.retry();
});
}, 100);
}
fromNewToEdit = this.get('routeName') === 'editor.new' &&
transition.targetName === 'editor.edit' &&
transition.intent.contexts &&
transition.intent.contexts[0] &&
transition.intent.contexts[0].id === model.get('id');
deletedWithoutChanges = state.isDeleted &&
(state.isSaving || !state.hasDirtyAttributes);
if (!fromNewToEdit && !deletedWithoutChanges && controllerIsDirty) {
transition.abort();
controller.send('toggleLeaveEditorModal', transition);
return;
}
// The controller may hold model state that will be lost in the transition,
// so we need to apply it now.
if (fromNewToEdit && controllerIsDirty) {
if (scratch !== model.get('markdown')) {
model.set('markdown', scratch);
}
}
if (state.isNew) {
model.deleteRecord();
}
// since the transition is now certain to complete..
window.onbeforeunload = null;
// remove model-related listeners created in editor-base-route
this.detachModelHooks(controller, model);
}
},
renderTemplate(controller, model) {
this._super(controller, model);
this.render('post-settings-menu', {
model,
into: 'application',
outlet: 'settings-menu'
});
},
attachModelHooks(controller, model) {
// this will allow us to track when the model is saved and update the controller
// so that we can be sure controller.hasDirtyAttributes is correct, without having to update the
// controller on each instance of `model.save()`.
//
// another reason we can't do this on `model.save().then()` is because the post-settings-menu
// also saves the model, and passing messages is difficult because we have two
// types of editor controllers, and the PSM also exists on the posts.post route.
//
// The reason we can't just keep this functionality in the editor controller is
// because we need to remove these handlers on `willTransition` in the editor route.
model.on('didCreate', controller, controller.get('modelSaved'));
model.on('didUpdate', controller, controller.get('modelSaved'));
},
detachModelHooks(controller, model) {
model.off('didCreate', controller, controller.get('modelSaved'));
model.off('didUpdate', controller, controller.get('modelSaved'));
},
setupController(controller, model) {
let tags = model.get('tags');
model.set('scratch', model.get('markdown'));
model.set('titleScratch', model.get('title'));
this._super(...arguments);
if (tags) {
// used to check if anything has changed in the editor
controller.set('previousTagNames', tags.mapBy('name'));
} else {
controller.set('previousTagNames', []);
}
// attach model-related listeners created in editor-base-route
this.attachModelHooks(controller, model);
}
});