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