1 import {scrollAndHighlightElement} from "../services/util";
2 import {Component} from "./component";
3 import {htmlToDom} from "../services/dom";
5 export class PageComments extends Component {
9 this.pageId = Number(this.$opts.pageId);
12 this.container = this.$refs.commentContainer;
13 this.formContainer = this.$refs.formContainer;
14 this.commentCountBar = this.$refs.commentCountBar;
15 this.addButtonContainer = this.$refs.addButtonContainer;
16 this.replyToRow = this.$refs.replyToRow;
19 this.updatedText = this.$opts.updatedText;
20 this.deletedText = this.$opts.deletedText;
21 this.createdText = this.$opts.createdText;
22 this.countText = this.$opts.countText;
25 this.editingComment = null;
28 if (this.formContainer) {
29 this.form = this.formContainer.querySelector('form');
30 this.formInput = this.form.querySelector('textarea');
31 this.form.addEventListener('submit', this.saveComment.bind(this));
34 this.elem.addEventListener('click', this.handleAction.bind(this));
35 this.elem.addEventListener('submit', this.updateComment.bind(this));
39 let actionElem = event.target.closest('[action]');
41 if (event.target.matches('a[href^="#"]')) {
42 const id = event.target.href.split('#')[1];
43 scrollAndHighlightElement(document.querySelector('#' + id));
46 if (actionElem === null) return;
47 event.preventDefault();
49 const action = actionElem.getAttribute('action');
50 const comment = actionElem.closest('[comment]');
51 if (action === 'edit') this.editComment(comment);
52 if (action === 'closeUpdateForm') this.closeUpdateForm();
53 if (action === 'delete') this.deleteComment(comment);
54 if (action === 'addComment') this.showForm();
55 if (action === 'hideForm') this.hideForm();
56 if (action === 'reply') this.setReply(comment);
57 if (action === 'remove-reply-to') this.removeReplyTo();
61 if (!this.editingComment) return;
62 this.editingComment.querySelector('[comment-content]').style.display = 'block';
63 this.editingComment.querySelector('[comment-edit-container]').style.display = 'none';
66 editComment(commentElem) {
68 if (this.editingComment) this.closeUpdateForm();
69 commentElem.querySelector('[comment-content]').style.display = 'none';
70 commentElem.querySelector('[comment-edit-container]').style.display = 'block';
71 let textArea = commentElem.querySelector('[comment-edit-container] textarea');
72 let lineCount = textArea.value.split('\n').length;
73 textArea.style.height = ((lineCount * 20) + 40) + 'px';
74 this.editingComment = commentElem;
77 updateComment(event) {
78 let form = event.target;
79 event.preventDefault();
80 let text = form.querySelector('textarea').value;
83 parent_id: this.parentId || null,
85 this.showLoading(form);
86 let commentId = this.editingComment.getAttribute('comment');
87 window.$http.put(`/comment/${commentId}`, reqData).then(resp => {
88 let newComment = document.createElement('div');
89 newComment.innerHTML = resp.data;
90 this.editingComment.innerHTML = newComment.children[0].innerHTML;
91 window.$events.success(this.updatedText);
92 window.$components.init(this.editingComment);
93 this.closeUpdateForm();
94 this.editingComment = null;
95 }).catch(window.$events.showValidationErrors).then(() => {
96 this.hideLoading(form);
100 deleteComment(commentElem) {
101 let id = commentElem.getAttribute('comment');
102 this.showLoading(commentElem.querySelector('[comment-content]'));
103 window.$http.delete(`/comment/${id}`).then(resp => {
104 commentElem.parentNode.removeChild(commentElem);
105 window.$events.success(this.deletedText);
112 event.preventDefault();
113 event.stopPropagation();
114 let text = this.formInput.value;
117 parent_id: this.parentId || null,
119 this.showLoading(this.form);
120 window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => {
121 const newElem = htmlToDom(resp.data);
122 this.container.appendChild(newElem);
123 window.$components.init(newElem);
124 window.$events.success(this.createdText);
128 window.$events.showValidationErrors(err);
129 this.hideLoading(this.form);
134 let count = this.container.children.length;
135 this.elem.querySelector('[comments-title]').textContent = window.trans_plural(this.countText, count, {count});
139 this.formInput.value = '';
140 this.formContainer.appendChild(this.form);
142 this.removeReplyTo();
143 this.hideLoading(this.form);
147 this.formContainer.style.display = 'block';
148 this.formContainer.parentNode.style.display = 'block';
149 this.addButtonContainer.style.display = 'none';
150 this.formInput.focus();
151 this.formInput.scrollIntoView({behavior: "smooth"});
155 this.formContainer.style.display = 'none';
156 this.formContainer.parentNode.style.display = 'none';
157 if (this.getCommentCount() > 0) {
158 this.elem.appendChild(this.addButtonContainer)
160 this.commentCountBar.appendChild(this.addButtonContainer);
162 this.addButtonContainer.style.display = 'block';
166 return this.elem.querySelectorAll('.comment-box[comment]').length;
169 setReply(commentElem) {
171 this.parentId = Number(commentElem.getAttribute('local-id'));
172 this.replyToRow.style.display = 'block';
173 const replyLink = this.replyToRow.querySelector('a');
174 replyLink.textContent = `#${this.parentId}`;
175 replyLink.href = `#comment${this.parentId}`;
179 this.parentId = null;
180 this.replyToRow.style.display = 'none';
183 showLoading(formElem) {
184 const groups = formElem.querySelectorAll('.form-group');
185 for (let group of groups) {
186 group.style.display = 'none';
188 formElem.querySelector('.form-group.loading').style.display = 'block';
191 hideLoading(formElem) {
192 const groups = formElem.querySelectorAll('.form-group');
193 for (let group of groups) {
194 group.style.display = 'block';
196 formElem.querySelector('.form-group.loading').style.display = 'none';