2014-10-28 17:29:42 +03:00
|
|
|
import AuthenticatedRoute from 'ghost/routes/authenticated';
|
2014-06-05 07:18:23 +04:00
|
|
|
import loadingIndicator from 'ghost/mixins/loading-indicator';
|
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
|
|
|
|
2014-10-28 17:29:42 +03:00
|
|
|
var PostsPostRoute = AuthenticatedRoute.extend(loadingIndicator, ShortcutsRoute, {
|
2014-03-03 00:12:06 +04:00
|
|
|
model: function (params) {
|
2014-06-17 23:17:08 +04:00
|
|
|
var self = this,
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-17 23:17:08 +04:00
|
|
|
post = this.store.getById('post', postId);
|
|
|
|
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
|
|
|
|
2014-11-21 21:07:30 +03:00
|
|
|
return self.store.find('post', query).then(function (records) {
|
|
|
|
var post = records.get('firstObject');
|
2014-06-17 23:17:08 +04:00
|
|
|
|
2014-11-21 21:07:30 +03:00
|
|
|
if (post) {
|
|
|
|
return post;
|
|
|
|
}
|
2014-07-30 20:44:49 +04:00
|
|
|
|
2014-11-21 21:07:30 +03:00
|
|
|
return self.replaceWith('posts.index');
|
|
|
|
});
|
|
|
|
},
|
2014-07-31 06:44:51 +04:00
|
|
|
|
2014-11-21 21:07:30 +03:00
|
|
|
afterModel: function (post) {
|
|
|
|
var self = this;
|
2014-07-30 20:44:49 +04:00
|
|
|
|
2014-11-21 21:07:30 +03: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 23:17:08 +04:00
|
|
|
});
|
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-07-31 06:33:05 +04:00
|
|
|
setupController: function (controller, model) {
|
|
|
|
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: {
|
|
|
|
openEditor: function () {
|
|
|
|
this.transitionTo('editor.edit', this.get('controller.model'));
|
2014-09-16 22:02:27 +04:00
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-09-16 22:02:27 +04:00
|
|
|
deletePost: function () {
|
|
|
|
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
|
|
|
});
|
2014-06-02 22:55:37 +04:00
|
|
|
|
2014-06-13 23:35:22 +04:00
|
|
|
export default PostsPostRoute;
|