2014-10-28 17:29:42 +03:00
|
|
|
import AuthenticatedRoute from 'ghost/routes/authenticated';
|
2014-06-21 18:44:53 +04:00
|
|
|
import ShortcutsRoute from 'ghost/mixins/shortcuts-route';
|
2014-08-01 00:29:35 +04:00
|
|
|
import isNumber from 'ghost/utils/isNumber';
|
|
|
|
import isFinite from 'ghost/utils/isFinite';
|
2014-06-13 23:35:22 +04:00
|
|
|
|
2015-08-19 14:55:40 +03:00
|
|
|
export default AuthenticatedRoute.extend(ShortcutsRoute, {
|
2015-10-28 14:36:45 +03:00
|
|
|
model(params) {
|
|
|
|
let post,
|
2014-07-30 20:44:49 +04:00
|
|
|
postId,
|
2014-11-21 21:07:30 +03:00
|
|
|
query;
|
2014-05-24 07:25:20 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
2014-06-17 23:17:08 +04:00
|
|
|
postId = Number(params.post_id);
|
|
|
|
|
2014-10-25 01:09:50 +04:00
|
|
|
if (!isNumber(postId) || !isFinite(postId) || postId % 1 !== 0 || postId <= 0) {
|
2014-07-02 18:09:04 +04:00
|
|
|
return this.transitionTo('error404', params.post_id);
|
2014-05-24 07:25:20 +04:00
|
|
|
}
|
2015-10-28 14:36:45 +03:00
|
|
|
/* jscs:enable requireCamelCaseOrUpperCaseIdentifiers */
|
2014-05-24 07:25:20 +04:00
|
|
|
|
2015-09-03 14:06:50 +03:00
|
|
|
post = this.store.peekRecord('post', postId);
|
2014-06-17 23:17:08 +04:00
|
|
|
if (post) {
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
|
2014-11-21 21:07:30 +03:00
|
|
|
query = {
|
2014-07-30 20:44:49 +04:00
|
|
|
id: postId,
|
2014-06-17 23:17:08 +04:00
|
|
|
status: 'all',
|
2014-07-30 20:44:49 +04:00
|
|
|
staticPages: 'all'
|
|
|
|
};
|
2014-06-17 23:17:08 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return this.store.queryRecord('post', query).then((post) => {
|
2014-11-21 21:07:30 +03:00
|
|
|
if (post) {
|
|
|
|
return post;
|
|
|
|
}
|
2014-07-30 20:44:49 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return this.replaceRoute('posts.index');
|
2014-11-21 21:07:30 +03:00
|
|
|
});
|
|
|
|
},
|
2014-07-31 06:44:51 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
afterModel(post) {
|
|
|
|
return this.get('session.user').then((user) => {
|
2014-11-21 21:07:30 +03:00
|
|
|
if (user.get('isAuthor') && !post.isAuthoredByUser(user)) {
|
2015-10-28 14:36:45 +03:00
|
|
|
return this.replaceRoute('posts.index');
|
2014-11-21 21:07:30 +03:00
|
|
|
}
|
2014-06-17 23:17:08 +04:00
|
|
|
});
|
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
setupController(controller, model) {
|
2014-07-31 06:33:05 +04:00
|
|
|
this._super(controller, model);
|
2014-07-31 12:29:05 +04:00
|
|
|
|
2014-07-31 06:33:05 +04:00
|
|
|
this.controllerFor('posts').set('currentPost', model);
|
|
|
|
},
|
2014-07-31 12:29:05 +04:00
|
|
|
|
2014-06-21 18:44:53 +04:00
|
|
|
shortcuts: {
|
2014-09-21 20:31:40 +04:00
|
|
|
'enter, o': 'openEditor',
|
2014-09-16 22:02:27 +04:00
|
|
|
'command+backspace, ctrl+backspace': 'deletePost'
|
2014-06-21 18:44:53 +04:00
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-06-21 18:44:53 +04:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
openEditor(post) {
|
2015-08-31 12:56:57 +03:00
|
|
|
post = post || this.get('controller.model');
|
|
|
|
|
2015-06-13 17:34:09 +03:00
|
|
|
if (!post) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.transitionTo('editor.edit', post.get('id'));
|
2014-09-16 22:02:27 +04:00
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
deletePost() {
|
2014-09-16 22:02:27 +04:00
|
|
|
this.send('openModal', 'delete-post', this.get('controller.model'));
|
2014-06-21 18:44:53 +04:00
|
|
|
}
|
|
|
|
}
|
2014-03-04 00:18:10 +04:00
|
|
|
});
|