2014-06-19 23:44:44 +04:00
|
|
|
import base from 'ghost/mixins/editor-route-base';
|
2014-05-09 09:00:10 +04:00
|
|
|
|
2014-07-25 17:38:13 +04:00
|
|
|
var EditorEditRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin, base, {
|
2014-03-04 00:18:10 +04:00
|
|
|
classNames: ['editor'],
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-03-02 18:30:35 +04:00
|
|
|
model: function (params) {
|
2014-06-09 04:18:39 +04:00
|
|
|
var self = this,
|
|
|
|
post,
|
|
|
|
postId;
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2014-06-09 04:18:39 +04:00
|
|
|
postId = Number(params.post_id);
|
|
|
|
|
2014-07-02 18:09:04 +04:00
|
|
|
if (!_.isNumber(postId) || !_.isFinite(postId) || postId % 1 !== 0 || postId <= 0) {
|
|
|
|
return this.transitionTo('error404', 'editor/' + params.post_id);
|
2014-06-09 04:18:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
post = this.store.getById('post', postId);
|
2014-06-05 21:23:28 +04:00
|
|
|
|
|
|
|
if (post) {
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
|
2014-06-17 23:17:08 +04:00
|
|
|
return this.store.find('post', {
|
|
|
|
id: params.post_id,
|
|
|
|
status: 'all',
|
2014-06-27 06:35:25 +04:00
|
|
|
staticPages: 'all',
|
2014-06-05 21:23:28 +04:00
|
|
|
}).then(function (records) {
|
2014-06-09 04:18:39 +04:00
|
|
|
var post = records.get('firstObject');
|
|
|
|
|
|
|
|
if (post) {
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.transitionTo('posts.index');
|
2014-06-05 21:23:28 +04:00
|
|
|
});
|
2014-06-10 08:44:29 +04:00
|
|
|
},
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2014-06-10 08:44:29 +04:00
|
|
|
serialize: function (model) {
|
|
|
|
return {post_id: model.get('id')};
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setupController: function (controller, model) {
|
|
|
|
this._super(controller, model);
|
|
|
|
controller.set('scratch', model.get('markdown'));
|
2014-06-27 06:35:25 +04:00
|
|
|
// used to check if anything has changed in the editor
|
|
|
|
controller.set('previousTagNames', model.get('tags').mapBy('name'));
|
2014-06-28 07:01:56 +04:00
|
|
|
|
|
|
|
// attach model-related listeners created in editor-route-base
|
|
|
|
this.attachModelHooks(controller, model);
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
willTransition: function (transition) {
|
|
|
|
var controller = this.get('controller'),
|
|
|
|
isDirty = controller.get('isDirty'),
|
|
|
|
|
|
|
|
model = controller.get('model'),
|
|
|
|
isSaving = model.get('isSaving'),
|
2014-06-19 22:31:56 +04:00
|
|
|
isDeleted = model.get('isDeleted'),
|
|
|
|
modelIsDirty = model.get('isDirty');
|
2014-06-06 05:18:03 +04:00
|
|
|
|
|
|
|
// when `isDeleted && isSaving`, model is in-flight, being saved
|
2014-06-19 22:31:56 +04:00
|
|
|
// to the server. when `isDeleted && !isSaving && !modelIsDirty`,
|
|
|
|
// the record has already been deleted and the deletion persisted.
|
|
|
|
//
|
|
|
|
// in either case we can probably just transition now.
|
|
|
|
// in the former case the server will return the record, thereby updating it.
|
|
|
|
// @TODO: this will break if the model fails server-side validation.
|
|
|
|
if (!(isDeleted && isSaving) && !(isDeleted && !isSaving && !modelIsDirty) && isDirty) {
|
2014-06-06 05:18:03 +04:00
|
|
|
transition.abort();
|
|
|
|
this.send('openModal', 'leave-editor', [controller, transition]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// since the transition is now certain to complete..
|
|
|
|
window.onbeforeunload = null;
|
2014-06-28 07:01:56 +04:00
|
|
|
|
|
|
|
// remove model-related listeners created in editor-route-base
|
|
|
|
this.detachModelHooks(controller, model);
|
2014-06-06 05:18:03 +04:00
|
|
|
}
|
2014-03-02 18:30:35 +04:00
|
|
|
}
|
2014-03-04 00:18:10 +04:00
|
|
|
});
|
|
|
|
|
2014-06-10 08:44:29 +04:00
|
|
|
export default EditorEditRoute;
|