-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
80 lines (80 loc) · 2.87 KB
/
script.js
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
const comments = [
{ id: 1, message: "Comment 1", parent: 0 },
{ id: 2, message: "Comment 2", parent: 0 },
{ id: 3, message: "Comment 3", parent: 0 },
{ id: 4, message: "Reply 1", parent: 1 }
];
const commentsSection = document.querySelector("#comments");
document.querySelector("#main-button").addEventListener("click", (event) => {
comments.push({
id: comments.length + 1,
message: document.querySelector(`#main-input`).value,
parent: 0
});
drawLine({
id: comments.length + 1,
message: document.querySelector(`#main-input`).value,
parent: 0
});
document.querySelector(`#main-input`).value = "";
});
function drawLine(e) {
const commentLine = document.createElement("li");
commentLine.className = `comment-${e.id}`;
const commentText = document.createElement("p");
commentText.innerText = e.message;
commentLine.appendChild(commentText);
const commentReaction = document.createElement("div");
commentReaction.className = `reaction-${e.id}`;
const commentButton = document.createElement("button");
commentButton.innerText = "REPLY";
commentButton.addEventListener("click", (event) => {
const reaction = document.querySelector(`.reaction-${e.id}`);
reaction.removeChild(event.target);
const input = document.createElement("input");
input.type = "text";
input.className = `input-${e.id}`;
const submit = document.createElement("button");
submit.className = `submit-${e.id}`;
submit.innerText = "SUBMIT";
submit.addEventListener("click", (event2) => {
comments.push({
id: comments.length + 1,
message: document.querySelector(`.input-${e.id}`).value,
parent: e.id
});
drawLine({
id: comments.length + 1,
message: document.querySelector(`.input-${e.id}`).value,
parent: e.id
});
while (reaction.lastChild) {
reaction.lastChild.remove();
}
reaction.appendChild(event.target);
});
reaction.appendChild(input);
reaction.appendChild(submit);
input.focus();
});
commentReaction.appendChild(commentButton);
commentLine.appendChild(commentReaction);
const commentResponses = document.createElement("ul");
commentLine.appendChild(commentResponses);
if (e.parent === 0) commentsSection.appendChild(commentLine);
if (e.parent !== 0) {
const commentParent = document
.querySelector(`.comment-${e.parent}`)
.querySelector("ul");
commentParent?.appendChild(commentLine);
}
}
function drawComments() {
while (commentsSection.lastChild) {
commentsSection.lastChild.remove();
}
comments.map((e) => {
drawLine(e);
});
}
drawComments();