2018-11-09 16:17:35 -05:00
|
|
|
import MarkdownIt from "markdown-it";
|
2019-06-07 19:02:51 -04:00
|
|
|
import {scrollAndHighlightElement} from "../services/util";
|
|
|
|
|
2017-09-09 12:06:30 -04:00
|
|
|
const md = new MarkdownIt({ html: false });
|
2017-09-03 11:37:51 -04:00
|
|
|
|
|
|
|
class PageComments {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.pageId = Number(elem.getAttribute('page-id'));
|
2017-09-09 12:06:30 -04:00
|
|
|
this.editingComment = null;
|
|
|
|
this.parentId = null;
|
2017-09-03 11:37:51 -04:00
|
|
|
|
|
|
|
this.container = elem.querySelector('[comment-container]');
|
2017-09-09 12:06:30 -04:00
|
|
|
this.formContainer = elem.querySelector('[comment-form-container]');
|
|
|
|
|
|
|
|
if (this.formContainer) {
|
|
|
|
this.form = this.formContainer.querySelector('form');
|
|
|
|
this.formInput = this.form.querySelector('textarea');
|
|
|
|
this.form.addEventListener('submit', this.saveComment.bind(this));
|
|
|
|
}
|
2017-09-03 11:37:51 -04:00
|
|
|
|
|
|
|
this.elem.addEventListener('click', this.handleAction.bind(this));
|
|
|
|
this.elem.addEventListener('submit', this.updateComment.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
handleAction(event) {
|
|
|
|
let actionElem = event.target.closest('[action]');
|
2019-10-07 15:21:04 -04:00
|
|
|
|
2017-09-09 10:56:24 -04:00
|
|
|
if (event.target.matches('a[href^="#"]')) {
|
2019-06-07 19:02:51 -04:00
|
|
|
const id = event.target.href.split('#')[1];
|
|
|
|
scrollAndHighlightElement(document.querySelector('#' + id));
|
2017-09-09 10:56:24 -04:00
|
|
|
}
|
2019-10-07 15:21:04 -04:00
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
if (actionElem === null) return;
|
2017-09-09 10:56:24 -04:00
|
|
|
event.preventDefault();
|
2017-09-03 11:37:51 -04:00
|
|
|
|
|
|
|
let action = actionElem.getAttribute('action');
|
|
|
|
if (action === 'edit') this.editComment(actionElem.closest('[comment]'));
|
|
|
|
if (action === 'closeUpdateForm') this.closeUpdateForm();
|
|
|
|
if (action === 'delete') this.deleteComment(actionElem.closest('[comment]'));
|
|
|
|
if (action === 'addComment') this.showForm();
|
|
|
|
if (action === 'hideForm') this.hideForm();
|
2017-09-09 10:56:24 -04:00
|
|
|
if (action === 'reply') this.setReply(actionElem.closest('[comment]'));
|
|
|
|
if (action === 'remove-reply-to') this.removeReplyTo();
|
2017-09-03 11:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
closeUpdateForm() {
|
|
|
|
if (!this.editingComment) return;
|
|
|
|
this.editingComment.querySelector('[comment-content]').style.display = 'block';
|
|
|
|
this.editingComment.querySelector('[comment-edit-container]').style.display = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
editComment(commentElem) {
|
|
|
|
this.hideForm();
|
|
|
|
if (this.editingComment) this.closeUpdateForm();
|
|
|
|
commentElem.querySelector('[comment-content]').style.display = 'none';
|
|
|
|
commentElem.querySelector('[comment-edit-container]').style.display = 'block';
|
2017-09-09 12:06:30 -04:00
|
|
|
let textArea = commentElem.querySelector('[comment-edit-container] textarea');
|
|
|
|
let lineCount = textArea.value.split('\n').length;
|
2019-04-13 05:50:24 -04:00
|
|
|
textArea.style.height = ((lineCount * 20) + 40) + 'px';
|
2017-09-03 11:37:51 -04:00
|
|
|
this.editingComment = commentElem;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateComment(event) {
|
|
|
|
let form = event.target;
|
|
|
|
event.preventDefault();
|
|
|
|
let text = form.querySelector('textarea').value;
|
|
|
|
let reqData = {
|
|
|
|
text: text,
|
|
|
|
html: md.render(text),
|
2017-09-09 10:56:24 -04:00
|
|
|
parent_id: this.parentId || null,
|
2017-09-03 11:37:51 -04:00
|
|
|
};
|
2017-09-09 12:06:30 -04:00
|
|
|
this.showLoading(form);
|
2017-09-03 11:37:51 -04:00
|
|
|
let commentId = this.editingComment.getAttribute('comment');
|
|
|
|
window.$http.put(window.baseUrl(`/ajax/comment/${commentId}`), reqData).then(resp => {
|
|
|
|
let newComment = document.createElement('div');
|
|
|
|
newComment.innerHTML = resp.data;
|
|
|
|
this.editingComment.innerHTML = newComment.children[0].innerHTML;
|
|
|
|
window.$events.emit('success', window.trans('entities.comment_updated_success'));
|
2017-09-09 10:56:24 -04:00
|
|
|
window.components.init(this.editingComment);
|
2017-09-03 11:37:51 -04:00
|
|
|
this.closeUpdateForm();
|
|
|
|
this.editingComment = null;
|
2017-09-09 12:06:30 -04:00
|
|
|
this.hideLoading(form);
|
2017-09-03 11:37:51 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteComment(commentElem) {
|
|
|
|
let id = commentElem.getAttribute('comment');
|
2017-09-09 12:06:30 -04:00
|
|
|
this.showLoading(commentElem.querySelector('[comment-content]'));
|
2017-09-03 11:37:51 -04:00
|
|
|
window.$http.delete(window.baseUrl(`/ajax/comment/${id}`)).then(resp => {
|
|
|
|
commentElem.parentNode.removeChild(commentElem);
|
|
|
|
window.$events.emit('success', window.trans('entities.comment_deleted_success'));
|
|
|
|
this.updateCount();
|
2019-04-13 07:58:57 -04:00
|
|
|
this.hideForm();
|
2017-09-03 11:37:51 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
saveComment(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
let text = this.formInput.value;
|
|
|
|
let reqData = {
|
|
|
|
text: text,
|
|
|
|
html: md.render(text),
|
2017-09-09 10:56:24 -04:00
|
|
|
parent_id: this.parentId || null,
|
2017-09-03 11:37:51 -04:00
|
|
|
};
|
2017-09-09 12:06:30 -04:00
|
|
|
this.showLoading(this.form);
|
2017-09-03 11:37:51 -04:00
|
|
|
window.$http.post(window.baseUrl(`/ajax/page/${this.pageId}/comment`), reqData).then(resp => {
|
|
|
|
let newComment = document.createElement('div');
|
|
|
|
newComment.innerHTML = resp.data;
|
2017-09-09 10:56:24 -04:00
|
|
|
let newElem = newComment.children[0];
|
|
|
|
this.container.appendChild(newElem);
|
|
|
|
window.components.init(newElem);
|
2017-09-03 11:37:51 -04:00
|
|
|
window.$events.emit('success', window.trans('entities.comment_created_success'));
|
|
|
|
this.resetForm();
|
|
|
|
this.updateCount();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCount() {
|
|
|
|
let count = this.container.children.length;
|
|
|
|
this.elem.querySelector('[comments-title]').textContent = window.trans_choice('entities.comment_count', count, {count});
|
|
|
|
}
|
|
|
|
|
|
|
|
resetForm() {
|
|
|
|
this.formInput.value = '';
|
|
|
|
this.formContainer.appendChild(this.form);
|
|
|
|
this.hideForm();
|
2017-09-09 10:56:24 -04:00
|
|
|
this.removeReplyTo();
|
2017-09-09 12:06:30 -04:00
|
|
|
this.hideLoading(this.form);
|
2017-09-03 11:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
showForm() {
|
|
|
|
this.formContainer.style.display = 'block';
|
|
|
|
this.formContainer.parentNode.style.display = 'block';
|
2019-04-13 07:58:57 -04:00
|
|
|
this.elem.querySelector('[comment-add-button-container]').style.display = 'none';
|
2017-09-09 10:56:24 -04:00
|
|
|
this.formInput.focus();
|
2019-06-07 19:02:51 -04:00
|
|
|
this.formInput.scrollIntoView({behavior: "smooth"});
|
2017-09-03 11:37:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hideForm() {
|
|
|
|
this.formContainer.style.display = 'none';
|
|
|
|
this.formContainer.parentNode.style.display = 'none';
|
2019-04-13 07:58:57 -04:00
|
|
|
const addButtonContainer = this.elem.querySelector('[comment-add-button-container]');
|
|
|
|
if (this.getCommentCount() > 0) {
|
|
|
|
this.elem.appendChild(addButtonContainer)
|
|
|
|
} else {
|
|
|
|
const countBar = this.elem.querySelector('[comment-count-bar]');
|
|
|
|
countBar.appendChild(addButtonContainer);
|
|
|
|
}
|
|
|
|
addButtonContainer.style.display = 'block';
|
|
|
|
}
|
|
|
|
|
|
|
|
getCommentCount() {
|
|
|
|
return this.elem.querySelectorAll('.comment-box[comment]').length;
|
2017-09-03 11:37:51 -04:00
|
|
|
}
|
|
|
|
|
2017-09-09 10:56:24 -04:00
|
|
|
setReply(commentElem) {
|
2017-09-03 11:37:51 -04:00
|
|
|
this.showForm();
|
2017-09-09 10:56:24 -04:00
|
|
|
this.parentId = Number(commentElem.getAttribute('local-id'));
|
|
|
|
this.elem.querySelector('[comment-form-reply-to]').style.display = 'block';
|
|
|
|
let replyLink = this.elem.querySelector('[comment-form-reply-to] a');
|
|
|
|
replyLink.textContent = `#${this.parentId}`;
|
|
|
|
replyLink.href = `#comment${this.parentId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeReplyTo() {
|
|
|
|
this.parentId = null;
|
|
|
|
this.elem.querySelector('[comment-form-reply-to]').style.display = 'none';
|
2017-09-03 11:37:51 -04:00
|
|
|
}
|
|
|
|
|
2017-09-09 12:06:30 -04:00
|
|
|
showLoading(formElem) {
|
|
|
|
let groups = formElem.querySelectorAll('.form-group');
|
|
|
|
for (let i = 0, len = groups.length; i < len; i++) {
|
|
|
|
groups[i].style.display = 'none';
|
|
|
|
}
|
|
|
|
formElem.querySelector('.form-group.loading').style.display = 'block';
|
|
|
|
}
|
2017-09-03 11:37:51 -04:00
|
|
|
|
2017-09-09 12:06:30 -04:00
|
|
|
hideLoading(formElem) {
|
|
|
|
let groups = formElem.querySelectorAll('.form-group');
|
|
|
|
for (let i = 0, len = groups.length; i < len; i++) {
|
|
|
|
groups[i].style.display = 'block';
|
|
|
|
}
|
|
|
|
formElem.querySelector('.form-group.loading').style.display = 'none';
|
|
|
|
}
|
2017-09-03 11:37:51 -04:00
|
|
|
|
2017-09-09 12:06:30 -04:00
|
|
|
}
|
2017-09-03 11:37:51 -04:00
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default PageComments;
|