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.formReplyLink = this.$refs.formReplyLink;
20 this.addCommentButton = this.$refs.addCommentButton;
21 this.hideFormButton = this.$refs.hideFormButton;
22 this.removeReplyToButton = this.$refs.removeReplyToButton;
25 this.createdText = this.$opts.createdText;
26 this.countText = this.$opts.countText;
30 this.formReplyText = this.formReplyLink.textContent;
32 this.setupListeners();
36 this.removeReplyToButton.addEventListener('click', this.removeReplyTo.bind(this));
37 this.hideFormButton.addEventListener('click', this.hideForm.bind(this));
38 this.addCommentButton.addEventListener('click', this.showForm.bind(this));
40 this.elem.addEventListener('page-comment-delete', () => {
45 this.elem.addEventListener('page-comment-reply', event => {
46 this.setReply(event.detail.id, event.detail.element);
50 this.form.addEventListener('submit', this.saveComment.bind(this));
55 event.preventDefault();
56 event.stopPropagation();
58 const loading = getLoading();
59 loading.classList.add('px-l');
60 this.form.after(loading);
61 this.form.toggleAttribute('hidden', true);
63 const text = this.formInput.value;
66 parent_id: this.parentId || null,
69 window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => {
70 const newElem = htmlToDom(resp.data);
71 this.formContainer.after(newElem);
72 window.$events.success(this.createdText);
76 this.form.toggleAttribute('hidden', false);
77 window.$events.showValidationErrors(err);
80 this.form.toggleAttribute('hidden', false);
85 const count = this.getCommentCount();
86 this.commentsTitle.textContent = window.trans_plural(this.countText, count, {count});
90 this.formInput.value = '';
92 this.replyToRow.toggleAttribute('hidden', true);
93 this.container.append(this.formContainer);
97 this.formContainer.toggleAttribute('hidden', false);
98 this.addButtonContainer.toggleAttribute('hidden', true);
99 this.formContainer.scrollIntoView({behavior: 'smooth', block: 'nearest'});
101 this.formInput.focus();
107 this.formContainer.toggleAttribute('hidden', true);
108 if (this.getCommentCount() > 0) {
109 this.elem.append(this.addButtonContainer);
111 this.commentCountBar.append(this.addButtonContainer);
113 this.addButtonContainer.toggleAttribute('hidden', false);
117 return this.container.querySelectorAll('[component="page-comment"]').length;
120 setReply(commentLocalId, commentElement) {
121 const targetFormLocation = commentElement.closest('.comment-branch').querySelector('.comment-branch-children');
122 targetFormLocation.append(this.formContainer);
124 this.parentId = commentLocalId;
125 this.replyToRow.toggleAttribute('hidden', false);
126 const replyLink = this.replyToRow.querySelector('a');
127 replyLink.textContent = this.formReplyText.replace('1234', this.parentId);
128 replyLink.href = `#comment${this.parentId}`;
132 this.parentId = null;
133 this.replyToRow.toggleAttribute('hidden', true);
134 this.container.append(this.formContainer);