1 import {Component} from './component';
2 import {getLoading, htmlToDom} from '../services/dom';
4 export class PageComment extends Component {
8 this.commentId = this.$opts.commentId;
9 this.commentLocalId = this.$opts.commentLocalId;
10 this.commentParentId = this.$opts.commentParentId;
11 this.deletedText = this.$opts.deletedText;
12 this.updatedText = this.$opts.updatedText;
15 this.container = this.$el;
16 this.contentContainer = this.$refs.contentContainer;
17 this.form = this.$refs.form;
18 this.formCancel = this.$refs.formCancel;
19 this.editButton = this.$refs.editButton;
20 this.deleteButton = this.$refs.deleteButton;
21 this.replyButton = this.$refs.replyButton;
22 this.input = this.$refs.input;
24 this.setupListeners();
28 if (this.replyButton) {
29 this.replyButton.addEventListener('click', () => this.$emit('reply', {
30 id: this.commentLocalId,
31 element: this.container,
35 if (this.editButton) {
36 this.editButton.addEventListener('click', this.startEdit.bind(this));
37 this.form.addEventListener('submit', this.update.bind(this));
38 this.formCancel.addEventListener('click', () => this.toggleEditMode(false));
41 if (this.deleteButton) {
42 this.deleteButton.addEventListener('click', this.delete.bind(this));
46 toggleEditMode(show) {
47 this.contentContainer.toggleAttribute('hidden', show);
48 this.form.toggleAttribute('hidden', !show);
52 this.toggleEditMode(true);
53 const lineCount = this.$refs.input.value.split('\n').length;
54 this.$refs.input.style.height = `${(lineCount * 20) + 40}px`;
58 event.preventDefault();
59 const loading = this.showLoading();
60 this.form.toggleAttribute('hidden', true);
63 text: this.input.value,
64 parent_id: this.parentId || null,
68 const resp = await window.$http.put(`/comment/${this.commentId}`, reqData);
69 const newComment = htmlToDom(resp.data);
70 this.container.replaceWith(newComment);
71 window.$events.success(this.updatedText);
74 window.$events.showValidationErrors(err);
75 this.form.toggleAttribute('hidden', false);
83 await window.$http.delete(`/comment/${this.commentId}`);
84 this.container.closest('.comment-branch').remove();
85 window.$events.success(this.deletedText);
90 const loading = getLoading();
91 loading.classList.add('px-l');
92 this.container.append(loading);