2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-07-07 22:13:58 +02:00
|
|
|
const router = require('../router.js');
|
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-19 19:16:40 +02:00
|
|
|
const PostList = require('../models/post_list.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-07-07 22:13:58 +02:00
|
|
|
constructor(id, editMode, ctx) {
|
2016-06-14 10:31:48 +02:00
|
|
|
topNavigation.activate('posts');
|
2016-07-13 21:50:07 +02:00
|
|
|
topNavigation.setTitle('Post #' + id.toString());
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-07-07 22:13:58 +02:00
|
|
|
let parameters = ctx.parameters;
|
2016-06-06 20:57:22 +02:00
|
|
|
Promise.all([
|
2016-06-13 23:28:55 +02:00
|
|
|
Post.get(id),
|
2016-06-19 19:16:40 +02:00
|
|
|
PostList.getAround(
|
|
|
|
id, this._decorateSearchQuery(
|
2016-07-07 21:18:35 +02:00
|
|
|
parameters ? parameters.query : '')),
|
2016-06-06 20:57:22 +02:00
|
|
|
]).then(responses => {
|
2016-06-13 23:28:55 +02:00
|
|
|
const [post, aroundResponse] = responses;
|
2016-07-07 22:13:58 +02:00
|
|
|
|
|
|
|
// remove junk from query, but save it into history so that it can
|
|
|
|
// be still accessed after history navigation / page refresh
|
|
|
|
if (parameters.query) {
|
|
|
|
ctx.state.parameters = parameters;
|
2016-07-26 23:13:07 +02:00
|
|
|
const url = editMode ?
|
|
|
|
'/post/' + id + '/edit' :
|
|
|
|
'/post/' + id;
|
|
|
|
router.replace(url, ctx.state, false);
|
2016-07-07 22:13:58 +02:00
|
|
|
}
|
|
|
|
|
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-07-07 21:18:35 +02:00
|
|
|
parameters: parameters,
|
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));
|
2016-06-29 18:54:49 +02:00
|
|
|
this._view.sidebarControl.addEventListener(
|
|
|
|
'fitModeChange', e => this._evtFitModeChange(e));
|
2016-07-03 13:46:49 +02:00
|
|
|
this._view.sidebarControl.addEventListener(
|
|
|
|
'change', e => this._evtPostChange(e));
|
|
|
|
this._view.sidebarControl.addEventListener(
|
|
|
|
'submit', e => this._evtPostEdit(e));
|
2016-08-02 10:37:56 +02:00
|
|
|
this._view.sidebarControl.addEventListener(
|
|
|
|
'feature', e => this._evtPostFeature(e));
|
2016-08-02 11:05:40 +02:00
|
|
|
this._view.sidebarControl.addEventListener(
|
|
|
|
'delete', e => this._evtPostDelete(e));
|
2016-06-17 20:25:44 +02:00
|
|
|
}
|
|
|
|
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-19 19:16:40 +02:00
|
|
|
}, errorMessage => {
|
2016-06-14 10:31:48 +02:00
|
|
|
this._view = new EmptyView();
|
2016-06-19 19:16:40 +02:00
|
|
|
this._view.showError(errorMessage);
|
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
|
|
|
|
2016-06-29 18:54:49 +02:00
|
|
|
_evtFitModeChange(e) {
|
|
|
|
const browsingSettings = settings.get();
|
|
|
|
browsingSettings.fitMode = e.detail.mode;
|
|
|
|
settings.save(browsingSettings);
|
|
|
|
}
|
|
|
|
|
2016-08-02 10:37:56 +02:00
|
|
|
_evtPostFeature(e) {
|
|
|
|
this._view.sidebarControl.disableForm();
|
|
|
|
this._view.sidebarControl.clearMessages();
|
|
|
|
e.detail.post.feature()
|
|
|
|
.then(() => {
|
|
|
|
this._view.sidebarControl.showSuccess('Post featured.');
|
|
|
|
this._view.sidebarControl.enableForm();
|
|
|
|
}, errorMessage => {
|
|
|
|
this._view.sidebarControl.showError(errorMessage);
|
|
|
|
this._view.sidebarControl.enableForm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-02 11:05:40 +02:00
|
|
|
_evtPostDelete(e) {
|
|
|
|
this._view.sidebarControl.disableForm();
|
|
|
|
this._view.sidebarControl.clearMessages();
|
|
|
|
e.detail.post.delete()
|
|
|
|
.then(() => {
|
|
|
|
misc.disableExitConfirmation();
|
|
|
|
const ctx = router.show('/posts');
|
|
|
|
ctx.controller.showSuccess('Post deleted.');
|
|
|
|
}, errorMessage => {
|
|
|
|
this._view.sidebarControl.showError(errorMessage);
|
|
|
|
this._view.sidebarControl.enableForm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-03 13:46:49 +02:00
|
|
|
_evtPostEdit(e) {
|
2016-07-30 16:13:07 +02:00
|
|
|
this._view.sidebarControl.disableForm();
|
2016-07-31 23:55:22 +02:00
|
|
|
this._view.sidebarControl.clearMessages();
|
2016-07-03 13:46:49 +02:00
|
|
|
const post = e.detail.post;
|
2016-07-26 19:58:26 +02:00
|
|
|
if (e.detail.tags !== undefined) {
|
|
|
|
post.tags = e.detail.tags;
|
|
|
|
}
|
|
|
|
if (e.detail.safety !== undefined) {
|
|
|
|
post.safety = e.detail.safety;
|
|
|
|
}
|
2016-07-26 20:37:55 +02:00
|
|
|
if (e.detail.flags !== undefined) {
|
|
|
|
post.flags = e.detail.flags;
|
|
|
|
}
|
2016-07-26 19:58:26 +02:00
|
|
|
if (e.detail.relations !== undefined) {
|
|
|
|
post.relations = e.detail.relations;
|
|
|
|
}
|
2016-07-27 22:27:33 +02:00
|
|
|
if (e.detail.content !== undefined) {
|
|
|
|
post.content = e.detail.content;
|
|
|
|
}
|
2016-07-28 19:28:48 +02:00
|
|
|
if (e.detail.thumbnail !== undefined) {
|
|
|
|
post.thumbnail = e.detail.thumbnail;
|
|
|
|
}
|
2016-07-03 13:46:49 +02:00
|
|
|
post.save()
|
|
|
|
.then(() => {
|
2016-07-30 16:13:07 +02:00
|
|
|
this._view.sidebarControl.showSuccess('Post saved.');
|
|
|
|
this._view.sidebarControl.enableForm();
|
2016-07-03 13:46:49 +02:00
|
|
|
misc.disableExitConfirmation();
|
|
|
|
}, errorMessage => {
|
2016-07-30 16:13:07 +02:00
|
|
|
this._view.sidebarControl.showError(errorMessage);
|
|
|
|
this._view.sidebarControl.enableForm();
|
2016-07-03 13:46:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtPostChange(e) {
|
|
|
|
misc.enableExitConfirmation();
|
|
|
|
}
|
|
|
|
|
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-07-07 21:18:35 +02:00
|
|
|
router.enter('/post/:id/edit/:parameters?',
|
|
|
|
(ctx, next) => { misc.parseUrlParametersRoute(ctx, next); },
|
2016-06-18 11:39:54 +02:00
|
|
|
(ctx, next) => {
|
2016-07-17 19:00:30 +02:00
|
|
|
// restore parameters from history state
|
|
|
|
if (ctx.state.parameters) {
|
|
|
|
Object.assign(ctx.parameters, ctx.state.parameters);
|
|
|
|
}
|
|
|
|
ctx.controller = new PostController(ctx.parameters.id, true, ctx);
|
2016-06-18 11:39:54 +02:00
|
|
|
});
|
|
|
|
router.enter(
|
2016-07-07 21:18:35 +02:00
|
|
|
'/post/:id/:parameters?',
|
|
|
|
(ctx, next) => { misc.parseUrlParametersRoute(ctx, next); },
|
2016-06-18 11:39:54 +02:00
|
|
|
(ctx, next) => {
|
2016-07-07 22:13:58 +02:00
|
|
|
// restore parameters from history state
|
|
|
|
if (ctx.state.parameters) {
|
|
|
|
Object.assign(ctx.parameters, ctx.state.parameters);
|
|
|
|
}
|
|
|
|
ctx.controller = new PostController(ctx.parameters.id, false, ctx);
|
2016-06-18 11:39:54 +02:00
|
|
|
});
|
2016-06-14 10:31:48 +02:00
|
|
|
};
|