szurubooru/client/js/controllers/post_controller.js

258 lines
9.2 KiB
JavaScript
Raw Normal View History

'use strict';
const router = require('../router.js');
const api = require('../api.js');
const misc = require('../util/misc.js');
const settings = require('../models/settings.js');
const Comment = require('../models/comment.js');
const Post = require('../models/post.js');
const PostList = require('../models/post_list.js');
const topNavigation = require('../models/top_navigation.js');
const PostView = require('../views/post_view.js');
const EmptyView = require('../views/empty_view.js');
2016-06-13 23:10:53 +02:00
class PostController {
constructor(id, editMode, ctx) {
topNavigation.activate('posts');
topNavigation.setTitle('Post #' + id.toString());
let parameters = ctx.parameters;
Promise.all([
Post.get(id),
PostList.getAround(
id, this._decorateSearchQuery(
parameters ? parameters.query : '')),
]).then(responses => {
const [post, aroundResponse] = responses;
// 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;
const url = editMode ?
'/post/' + id + '/edit' :
'/post/' + id;
router.replace(url, ctx.state, false);
}
this._post = post;
this._view = new PostView({
post: post,
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'),
parameters: parameters,
});
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));
}
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));
}
}, errorMessage => {
this._view = new EmptyView();
this._view.showError(errorMessage);
});
}
_decorateSearchQuery(text) {
const browsingSettings = settings.get();
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-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) {
this._view.sidebarControl.disableForm();
this._view.sidebarControl.clearMessages();
2016-07-03 13:46:49 +02:00
const post = e.detail.post;
if (e.detail.tags !== undefined) {
post.tags = e.detail.tags;
}
if (e.detail.safety !== undefined) {
post.safety = e.detail.safety;
}
if (e.detail.flags !== undefined) {
post.flags = e.detail.flags;
}
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;
}
if (e.detail.thumbnail !== undefined) {
post.thumbnail = e.detail.thumbnail;
}
2016-07-03 13:46:49 +02:00
post.save()
.then(() => {
this._view.sidebarControl.showSuccess('Post saved.');
this._view.sidebarControl.enableForm();
2016-07-03 13:46:49 +02:00
misc.disableExitConfirmation();
}, errorMessage => {
this._view.sidebarControl.showError(errorMessage);
this._view.sidebarControl.enableForm();
2016-07-03 13:46:49 +02:00
});
}
_evtPostChange(e) {
misc.enableExitConfirmation();
}
_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);
});
}
}
module.exports = router => {
router.enter('/post/:id/edit/:parameters?',
(ctx, next) => { misc.parseUrlParametersRoute(ctx, next); },
(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);
});
router.enter(
'/post/:id/:parameters?',
(ctx, next) => { misc.parseUrlParametersRoute(ctx, next); },
(ctx, next) => {
// 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);
});
};