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