2016-06-06 20:57:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-12 18:08:50 +02:00
|
|
|
const api = require('../api.js');
|
2016-06-12 20:11:43 +02:00
|
|
|
const router = require('../router.js');
|
2016-06-06 20:57:22 +02:00
|
|
|
const views = require('../util/views.js');
|
2016-06-11 21:37:19 +02:00
|
|
|
const keyboard = require('../util/keyboard.js');
|
2016-06-06 20:57:22 +02:00
|
|
|
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');
|
2016-06-11 17:41:28 +02:00
|
|
|
const CommentListControl = require('../controls/comment_list_control.js');
|
2016-06-12 18:08:50 +02:00
|
|
|
const CommentFormControl = require('../controls/comment_form_control.js');
|
2016-06-06 20:57:22 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const template = views.getTemplate('post');
|
2016-06-06 20:57:22 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
class PostView {
|
|
|
|
constructor(ctx) {
|
|
|
|
this._hostNode = document.getElementById('content-holder');
|
2016-06-06 20:57:22 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const sourceNode = template(ctx);
|
|
|
|
const postContainerNode = sourceNode.querySelector('.post-container');
|
|
|
|
const sidebarNode = sourceNode.querySelector('.sidebar');
|
|
|
|
views.replaceContent(this._hostNode, sourceNode);
|
2016-06-06 20:57:22 +02:00
|
|
|
|
|
|
|
const postViewNode = document.body.querySelector('.content-wrapper');
|
2016-06-13 22:34:39 +02:00
|
|
|
const topNavigationNode =
|
|
|
|
document.body.querySelector('#top-navigation');
|
2016-06-06 20:57:22 +02:00
|
|
|
|
|
|
|
const margin = (
|
2016-06-12 22:10:20 +02:00
|
|
|
postViewNode.getBoundingClientRect().top -
|
2016-06-13 22:34:39 +02:00
|
|
|
topNavigationNode.getBoundingClientRect().height);
|
2016-06-06 20:57:22 +02:00
|
|
|
|
|
|
|
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-06-06 20:57:22 +02:00
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
new PostNotesOverlayControl(
|
|
|
|
postContainerNode.querySelector('.post-overlay'),
|
|
|
|
ctx.post);
|
|
|
|
|
2016-06-12 18:08:50 +02:00
|
|
|
this._installSidebar(ctx);
|
|
|
|
this._installCommentForm(ctx);
|
|
|
|
this._installComments(ctx);
|
2016-06-11 17:41:28 +02:00
|
|
|
|
2016-06-11 21:37:19 +02:00
|
|
|
keyboard.bind('e', () => {
|
|
|
|
if (ctx.editMode) {
|
2016-06-12 20:11:43 +02:00
|
|
|
router.show('/post/' + ctx.post.id);
|
2016-06-11 21:37:19 +02:00
|
|
|
} else {
|
2016-06-12 20:11:43 +02:00
|
|
|
router.show('/post/' + ctx.post.id + '/edit');
|
2016-06-11 21:37:19 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
keyboard.bind(['a', 'left'], () => {
|
|
|
|
if (ctx.nextPostId) {
|
2016-06-12 20:11:43 +02:00
|
|
|
router.show('/post/' + ctx.nextPostId);
|
2016-06-11 21:37:19 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
keyboard.bind(['d', 'right'], () => {
|
|
|
|
if (ctx.prevPostId) {
|
2016-06-12 20:11:43 +02:00
|
|
|
router.show('/post/' + ctx.prevPostId);
|
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) {
|
|
|
|
new PostEditSidebarControl(
|
|
|
|
sidebarContainerNode, ctx.post, this._postContentControl);
|
|
|
|
} else {
|
|
|
|
new PostReadonlySidebarControl(
|
|
|
|
sidebarContainerNode, ctx.post, this._postContentControl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_installCommentForm(ctx) {
|
|
|
|
const commentFormContainer = document.querySelector(
|
|
|
|
'#content-holder .comment-form-container');
|
|
|
|
if (!commentFormContainer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._formControl = new CommentFormControl(
|
|
|
|
commentFormContainer,
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
onSave: text => {
|
|
|
|
return api.post('/comments', {
|
|
|
|
postId: ctx.post.id,
|
|
|
|
text: text,
|
|
|
|
}).then(response => {
|
|
|
|
ctx.post.comments.push(response);
|
|
|
|
this._formControl.setText('');
|
|
|
|
this._installComments(ctx);
|
|
|
|
}, response => {
|
|
|
|
this._formControl.showError(response.description);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
canCancel: false,
|
|
|
|
minHeight: 150,
|
|
|
|
});
|
|
|
|
this._formControl.enterEditMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
_installComments(ctx) {
|
|
|
|
const commentsContainerNode = document.querySelector(
|
|
|
|
'#content-holder .comments-container');
|
|
|
|
if (commentsContainerNode) {
|
|
|
|
new CommentListControl(commentsContainerNode, ctx.post.comments);
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 20:57:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostView;
|