2014-03-11 20:23:32 +04:00
|
|
|
import AuthenticatedRoute from 'ghost/routes/authenticated';
|
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-06-19 23:44:44 +04:00
|
|
|
var EditorEditRoute = AuthenticatedRoute.extend(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);
|
|
|
|
|
|
|
|
if (!Number.isInteger(postId) || !Number.isFinite(postId) || postId <= 0) {
|
|
|
|
this.transitionTo('posts.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
|
|
|
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'));
|
|
|
|
|
|
|
|
model.get('tags').then(function (tags) {
|
|
|
|
// used to check if anything has changed in the editor
|
|
|
|
controller.set('previousTagNames', tags.mapBy('name'));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
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-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;
|