1 import {Component} from './component';
2 import {getLoading, htmlToDom} from '../services/dom';
4 export class PageComments extends Component {
8 this.pageId = Number(this.$opts.pageId);
11 this.container = this.$refs.commentContainer;
12 this.commentCountBar = this.$refs.commentCountBar;
13 this.commentsTitle = this.$refs.commentsTitle;
14 this.addButtonContainer = this.$refs.addButtonContainer;
15 this.replyToRow = this.$refs.replyToRow;
16 this.formContainer = this.$refs.formContainer;
17 this.form = this.$refs.form;
18 this.formInput = this.$refs.formInput;
19 this.addCommentButton = this.$refs.addCommentButton;
20 this.hideFormButton = this.$refs.hideFormButton;
21 this.removeReplyToButton = this.$refs.removeReplyToButton;
24 this.createdText = this.$opts.createdText;
25 this.countText = this.$opts.countText;
30 this.setupListeners();
34 this.removeReplyToButton.addEventListener('click', this.removeReplyTo.bind(this));
35 this.hideFormButton.addEventListener('click', this.hideForm.bind(this));
36 this.addCommentButton.addEventListener('click', this.showForm.bind(this));
38 this.elem.addEventListener('page-comment-delete', () => {
43 this.elem.addEventListener('page-comment-reply', event => {
44 this.setReply(event.detail.id, event.detail.element);
48 this.form.addEventListener('submit', this.saveComment.bind(this));
53 event.preventDefault();
54 event.stopPropagation();
56 const loading = getLoading();
57 loading.classList.add('px-l');
58 this.form.after(loading);
59 this.form.toggleAttribute('hidden', true);
61 const text = this.formInput.value;
64 parent_id: this.parentId || null,
67 window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => {
68 const newElem = htmlToDom(resp.data);
69 this.formContainer.after(newElem);
70 window.$events.success(this.createdText);
74 this.form.toggleAttribute('hidden', false);
75 window.$events.showValidationErrors(err);
78 this.form.toggleAttribute('hidden', false);
83 const count = this.getCommentCount();
84 this.commentsTitle.textContent = window.trans_plural(this.countText, count, {count});
88 this.formInput.value = '';
90 this.container.append(this.formContainer);
94 this.formContainer.toggleAttribute('hidden', false);
95 this.addButtonContainer.toggleAttribute('hidden', true);
97 this.formInput.focus();
103 this.formContainer.toggleAttribute('hidden', true);
104 if (this.getCommentCount() > 0) {
105 this.elem.append(this.addButtonContainer);
107 this.commentCountBar.append(this.addButtonContainer);
109 this.addButtonContainer.toggleAttribute('hidden', false);
113 return this.container.querySelectorAll('[component="page-comment"]').length;
116 setReply(commentLocalId, commentElement) {
117 const targetFormLocation = commentElement.closest('.comment-branch').querySelector('.comment-branch-children');
119 targetFormLocation.append(this.formContainer);
120 this.formContainer.scrollIntoView({behavior: 'smooth', block: 'nearest'});
121 this.parentId = commentLocalId;
122 this.replyToRow.toggleAttribute('hidden', false);
123 const replyLink = this.replyToRow.querySelector('a');
124 replyLink.textContent = `#${this.parentId}`;
125 replyLink.href = `#comment${this.parentId}`;
129 this.parentId = null;
130 this.replyToRow.toggleAttribute('hidden', true);