blob: 45dad248007b2f57f48a6cdc3a37ed788b22e4b7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
/* @license
magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
@license-end */
import { BaseController } from "./base_controller.js";
export class RepliesController extends BaseController {
constructor() {
super();
}
async init() {
this.comment_id = 0;
this.reply_form = await this.getReplyForm();
this.initializeButtons();
}
initializeButtons() {
this.listenButtons("replies-button", this.renderReplies.bind(this));
this.listenButtons("new-reply-button", this.showReplyForm.bind(this));
}
async submit(event) {
await super.submit(event, `${this.comments_server_host}/api/comments/${this.comment_id}/replies`);
}
listenButtons(class_name, func) {
const buttons = document.getElementsByClassName(class_name);
for (let button of buttons)
button.addEventListener("click", func);
}
showReplyForm(event) {
const replies_section = event.target.parentElement.parentElement.childNodes[3];
const form = replies_section.querySelector(".reply-form");
if (form == null) {
replies_section.appendChild(this.reply_form);
this.setCommentId(replies_section);
this.reply_form.addEventListener("submit", this.submit.bind(this));
} else
form.remove();
}
renderReplies(event) {
/* The div to contain the comment's replies. From the element id
we can get the comment's id. */
const comment_replies_section = event.target.parentElement.parentElement.querySelector(".comment-replies");
const present_replies = comment_replies_section.querySelector(".replies-section");
this.setCommentId(comment_replies_section);
if (present_replies != null) {
present_replies.remove();
return;
}
this.get(`/api/comments/${this.comment_id}/replies`).then((value) => {
const replies = value?.getElementById(`replies-section-${this.comment_id}`);
if (replies != null)
comment_replies_section.appendChild(replies);
});
}
renderSubmitResponse(response_document) {
const comment = document.getElementById(`comment-${this.comment_id}`);
const comment_replies = comment.querySelector(".comment-replies");
const present_replies = comment_replies.querySelector(".replies-section");
const new_comment_replies = this.htmlFromResponse(response_document).getElementById(`replies-section-${this.comment_id}`)
if(present_replies != null)
present_replies.replaceWith(new_comment_replies);
else
comment_replies.appendChild(new_comment_replies);
this.initializeButtons();
}
setCommentId(replies_section) {
this.comment_id = /\d/.exec(replies_section.id)[0];
}
async getReplyForm() {
var form = await this.get("/api/replies/new");
return form.getElementsByClassName("reply-form")[0];
}
}
|