]> BookStack Code Mirror - bookstack/blob - resources/js/components/page-comment.js
OIDC: Added testing of PKCE flow
[bookstack] / resources / js / components / page-comment.js
1 import {Component} from './component';
2 import {getLoading, htmlToDom} from '../services/dom';
3
4 export class PageComment extends Component {
5
6     setup() {
7         // Options
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;
13
14         // Element References
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;
23
24         this.setupListeners();
25     }
26
27     setupListeners() {
28         if (this.replyButton) {
29             this.replyButton.addEventListener('click', () => this.$emit('reply', {
30                 id: this.commentLocalId,
31                 element: this.container,
32             }));
33         }
34
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));
39         }
40
41         if (this.deleteButton) {
42             this.deleteButton.addEventListener('click', this.delete.bind(this));
43         }
44     }
45
46     toggleEditMode(show) {
47         this.contentContainer.toggleAttribute('hidden', show);
48         this.form.toggleAttribute('hidden', !show);
49     }
50
51     startEdit() {
52         this.toggleEditMode(true);
53         const lineCount = this.$refs.input.value.split('\n').length;
54         this.$refs.input.style.height = `${(lineCount * 20) + 40}px`;
55     }
56
57     async update(event) {
58         event.preventDefault();
59         const loading = this.showLoading();
60         this.form.toggleAttribute('hidden', true);
61
62         const reqData = {
63             text: this.input.value,
64             parent_id: this.parentId || null,
65         };
66
67         try {
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);
72         } catch (err) {
73             console.error(err);
74             window.$events.showValidationErrors(err);
75             this.form.toggleAttribute('hidden', false);
76             loading.remove();
77         }
78     }
79
80     async delete() {
81         this.showLoading();
82
83         await window.$http.delete(`/comment/${this.commentId}`);
84         this.container.closest('.comment-branch').remove();
85         window.$events.success(this.deletedText);
86         this.$emit('delete');
87     }
88
89     showLoading() {
90         const loading = getLoading();
91         loading.classList.add('px-l');
92         this.container.append(loading);
93         return loading;
94     }
95
96 }