2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-02 00:07:51 +02:00
|
|
|
const api = require('../api.js');
|
2016-06-17 20:25:44 +02:00
|
|
|
const misc = require('../util/misc.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
const settings = require('../models/settings.js');
|
2016-06-17 20:25:44 +02:00
|
|
|
const Comment = require('../models/comment.js');
|
2016-06-13 23:28:55 +02:00
|
|
|
const Post = require('../models/post.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
const topNavigation = require('../models/top_navigation.js');
|
2016-06-06 20:57:22 +02:00
|
|
|
const PostView = require('../views/post_view.js');
|
2016-05-29 12:21:25 +02:00
|
|
|
const EmptyView = require('../views/empty_view.js');
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-06-13 23:10:53 +02:00
|
|
|
class PostController {
|
2016-06-18 11:39:54 +02:00
|
|
|
constructor(id, editMode, searchQuery) {
|
2016-06-14 10:31:48 +02:00
|
|
|
topNavigation.activate('posts');
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-06-06 20:57:22 +02:00
|
|
|
Promise.all([
|
2016-06-13 23:28:55 +02:00
|
|
|
Post.get(id),
|
2016-06-12 22:10:20 +02:00
|
|
|
api.get(`/post/${id}/around?fields=id&query=` +
|
2016-06-18 11:39:54 +02:00
|
|
|
this._decorateSearchQuery(
|
|
|
|
searchQuery ? searchQuery.text : '')),
|
2016-06-06 20:57:22 +02:00
|
|
|
]).then(responses => {
|
2016-06-13 23:28:55 +02:00
|
|
|
const [post, aroundResponse] = responses;
|
2016-06-17 20:25:44 +02:00
|
|
|
this._post = post;
|
2016-06-14 10:31:48 +02:00
|
|
|
this._view = new PostView({
|
2016-06-13 23:28:55 +02:00
|
|
|
post: post,
|
2016-06-06 20:57:22 +02:00
|
|
|
editMode: editMode,
|
|
|
|
nextPostId: aroundResponse.next ? aroundResponse.next.id : null,
|
|
|
|
prevPostId: aroundResponse.prev ? aroundResponse.prev.id : null,
|
2016-06-11 09:59:29 +02:00
|
|
|
canEditPosts: api.hasPrivilege('posts:edit'),
|
2016-06-12 18:08:50 +02:00
|
|
|
canListComments: api.hasPrivilege('comments:list'),
|
|
|
|
canCreateComments: api.hasPrivilege('comments:create'),
|
2016-06-18 11:39:54 +02:00
|
|
|
searchQuery: searchQuery,
|
2016-06-06 20:57:22 +02:00
|
|
|
});
|
2016-06-17 20:25:44 +02:00
|
|
|
if (this._view.sidebarControl) {
|
|
|
|
this._view.sidebarControl.addEventListener(
|
|
|
|
'favorite', e => this._evtFavoritePost(e));
|
|
|
|
this._view.sidebarControl.addEventListener(
|
|
|
|
'unfavorite', e => this._evtUnfavoritePost(e));
|
|
|
|
this._view.sidebarControl.addEventListener(
|
|
|
|
'score', e => this._evtScorePost(e));
|
|
|
|
}
|
|
|
|
if (this._view.commentFormControl) {
|
|
|
|
this._view.commentFormControl.addEventListener(
|
|
|
|
'change', e => this._evtCommentChange(e));
|
|
|
|
this._view.commentFormControl.addEventListener(
|
|
|
|
'submit', e => this._evtCreateComment(e));
|
|
|
|
}
|
|
|
|
if (this._view.commentListControl) {
|
|
|
|
this._view.commentListControl.addEventListener(
|
|
|
|
'change', e => this._evtUpdateComment(e));
|
|
|
|
this._view.commentListControl.addEventListener(
|
|
|
|
'score', e => this._evtScoreComment(e));
|
|
|
|
this._view.commentListControl.addEventListener(
|
|
|
|
'delete', e => this._evtDeleteComment(e));
|
|
|
|
}
|
2016-06-06 20:57:22 +02:00
|
|
|
}, response => {
|
2016-06-14 10:31:48 +02:00
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError(response.description);
|
2016-06-06 20:57:22 +02:00
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 20:57:22 +02:00
|
|
|
_decorateSearchQuery(text) {
|
2016-06-14 10:31:48 +02:00
|
|
|
const browsingSettings = settings.get();
|
2016-06-06 20:57:22 +02:00
|
|
|
let disabledSafety = [];
|
|
|
|
for (let key of Object.keys(browsingSettings.listPosts)) {
|
|
|
|
if (browsingSettings.listPosts[key] === false) {
|
|
|
|
disabledSafety.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (disabledSafety.length) {
|
|
|
|
text = `-rating:${disabledSafety.join(',')} ${text}`;
|
|
|
|
}
|
|
|
|
return text.trim();
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-06-17 20:25:44 +02:00
|
|
|
|
|
|
|
_evtCommentChange(e) {
|
|
|
|
misc.enableExitConfirmation();
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtCreateComment(e) {
|
|
|
|
// TODO: disable form
|
|
|
|
const comment = Comment.create(this._post.id);
|
|
|
|
comment.text = e.detail.text;
|
|
|
|
comment.save()
|
|
|
|
.then(() => {
|
|
|
|
this._post.comments.add(comment);
|
|
|
|
this._view.commentFormControl.setText('');
|
|
|
|
// TODO: enable form
|
|
|
|
misc.disableExitConfirmation();
|
|
|
|
}, errorMessage => {
|
|
|
|
this._view.commentFormControl.showError(errorMessage);
|
|
|
|
// TODO: enable form
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtUpdateComment(e) {
|
|
|
|
// TODO: disable form
|
|
|
|
e.detail.comment.text = e.detail.text;
|
|
|
|
e.detail.comment.save()
|
|
|
|
.catch(errorMessage => {
|
|
|
|
e.detail.target.showError(errorMessage);
|
|
|
|
// TODO: enable form
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtScoreComment(e) {
|
|
|
|
e.detail.comment.setScore(e.detail.score)
|
|
|
|
.catch(errorMessage => {
|
|
|
|
window.alert(errorMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtDeleteComment(e) {
|
|
|
|
e.detail.comment.delete()
|
|
|
|
.catch(errorMessage => {
|
|
|
|
window.alert(errorMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtScorePost(e) {
|
|
|
|
e.detail.post.setScore(e.detail.score)
|
|
|
|
.catch(errorMessage => {
|
|
|
|
window.alert(errorMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtFavoritePost(e) {
|
|
|
|
e.detail.post.addToFavorites()
|
|
|
|
.catch(errorMessage => {
|
|
|
|
window.alert(errorMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtUnfavoritePost(e) {
|
|
|
|
e.detail.post.removeFromFavorites()
|
|
|
|
.catch(errorMessage => {
|
|
|
|
window.alert(errorMessage);
|
|
|
|
});
|
|
|
|
}
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
module.exports = router => {
|
2016-06-18 11:39:54 +02:00
|
|
|
router.enter('/post/:id/edit/:query?',
|
|
|
|
(ctx, next) => { misc.parseSearchQueryRoute(ctx, next); },
|
|
|
|
(ctx, next) => {
|
|
|
|
ctx.controller = new PostController(
|
|
|
|
ctx.params.id, true, ctx.searchQuery);
|
|
|
|
});
|
|
|
|
router.enter(
|
|
|
|
'/post/:id/:query?',
|
|
|
|
(ctx, next) => { misc.parseSearchQueryRoute(ctx, next); },
|
|
|
|
(ctx, next) => {
|
|
|
|
ctx.controller = new PostController(
|
|
|
|
ctx.params.id, false, ctx.searchQuery);
|
|
|
|
});
|
2016-06-14 10:31:48 +02:00
|
|
|
};
|