szurubooru/client/js/views/post_main_view.js

122 lines
4.1 KiB
JavaScript
Raw Normal View History

'use strict';
const router = require('../router.js');
const views = require('../util/views.js');
const uri = require('../util/uri.js');
2016-06-11 21:37:19 +02:00
const keyboard = require('../util/keyboard.js');
const PostContentControl = require('../controls/post_content_control.js');
2016-06-12 22:10:20 +02:00
const PostNotesOverlayControl =
require('../controls/post_notes_overlay_control.js');
const PostReadonlySidebarControl =
require('../controls/post_readonly_sidebar_control.js');
const PostEditSidebarControl =
require('../controls/post_edit_sidebar_control.js');
const CommentControl = require('../controls/comment_control.js');
const CommentListControl = require('../controls/comment_list_control.js');
2016-10-22 10:03:38 +02:00
const template = views.getTemplate('post-main');
2016-10-22 10:03:38 +02:00
class PostMainView {
constructor(ctx) {
this._hostNode = document.getElementById('content-holder');
const sourceNode = template(ctx);
const postContainerNode = sourceNode.querySelector('.post-container');
const sidebarNode = sourceNode.querySelector('.sidebar');
views.replaceContent(this._hostNode, sourceNode);
2016-07-13 17:18:28 +02:00
views.syncScrollPosition();
const postViewNode = document.body.querySelector('.content-wrapper');
2016-06-13 22:34:39 +02:00
const topNavigationNode =
document.body.querySelector('#top-navigation');
const margin = (
2016-06-12 22:10:20 +02:00
postViewNode.getBoundingClientRect().top -
2016-06-13 22:34:39 +02:00
topNavigationNode.getBoundingClientRect().height);
this._postContentControl = new PostContentControl(
postContainerNode,
ctx.post,
() => {
return [
2016-06-12 22:10:20 +02:00
window.innerWidth -
postContainerNode.getBoundingClientRect().left -
margin,
window.innerHeight -
2016-06-13 22:34:39 +02:00
topNavigationNode.getBoundingClientRect().height -
2016-06-12 22:10:20 +02:00
margin * 2,
];
});
2016-07-22 13:27:52 +02:00
this._postNotesOverlayControl = new PostNotesOverlayControl(
postContainerNode.querySelector('.post-overlay'),
ctx.post);
if (ctx.post.type === 'video' || ctx.post.type === 'flash') {
this._postContentControl.disableOverlay();
}
2016-06-12 18:08:50 +02:00
this._installSidebar(ctx);
this._installCommentForm();
this._installComments(ctx.post.comments);
2016-06-11 21:37:19 +02:00
keyboard.bind('e', () => {
if (ctx.editMode) {
router.show(uri.formatClientLink('post', ctx.post.id));
2016-06-11 21:37:19 +02:00
} else {
router.show(uri.formatClientLink('post', ctx.post.id, 'edit'));
2016-06-11 21:37:19 +02:00
}
});
keyboard.bind(['a', 'left'], () => {
if (ctx.prevPostId) {
router.show(uri.formatClientLink('post', ctx.prevPostId));
2016-06-11 21:37:19 +02:00
}
});
keyboard.bind(['d', 'right'], () => {
if (ctx.nextPostId) {
router.show(uri.formatClientLink('post', ctx.nextPostId));
2016-06-11 21:37:19 +02:00
}
});
}
2016-06-12 18:08:50 +02:00
_installSidebar(ctx) {
const sidebarContainerNode = document.querySelector(
'#content-holder .sidebar-container');
if (ctx.editMode) {
this.sidebarControl = new PostEditSidebarControl(
2016-07-22 13:27:52 +02:00
sidebarContainerNode,
ctx.post,
this._postContentControl,
this._postNotesOverlayControl);
2016-06-12 18:08:50 +02:00
} else {
this.sidebarControl = new PostReadonlySidebarControl(
2016-06-12 18:08:50 +02:00
sidebarContainerNode, ctx.post, this._postContentControl);
}
}
_installCommentForm() {
2016-06-12 18:08:50 +02:00
const commentFormContainer = document.querySelector(
'#content-holder .comment-form-container');
if (!commentFormContainer) {
return;
}
this.commentControl = new CommentControl(
commentFormContainer, null, true);
2016-06-12 18:08:50 +02:00
}
_installComments(comments) {
2016-06-12 18:08:50 +02:00
const commentsContainerNode = document.querySelector(
'#content-holder .comments-container');
if (!commentsContainerNode) {
return;
2016-06-12 18:08:50 +02:00
}
this.commentListControl = new CommentListControl(
commentsContainerNode, comments);
2016-06-12 18:08:50 +02:00
}
}
2016-10-22 10:03:38 +02:00
module.exports = PostMainView;