-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
234 lines (223 loc) · 7.68 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const data = {
currentUser: {
image: {
png: "./images/avatars/image-juliusomo.png",
webp: "./images/avatars/image-juliusomo.webp",
},
username: "juliusomo",
},
comments: [
{
parent: 0,
id: 1,
content:
"Impressive! Though it seems the drag feature could be improved. But overall it looks incredible. You've nailed the design and the responsiveness at various breakpoints works really well.",
createdAt: "1 month ago",
score: 12,
user: {
image: {
png: "./images/avatars/image-amyrobson.png",
webp: "./images/avatars/image-amyrobson.webp",
},
username: "amyrobson",
},
replies: [],
},
{
parent: 0,
id: 2,
content:
"Woah, your project looks awesome! How long have you been coding for? I'm still new, but think I want to dive into React as well soon. Perhaps you can give me an insight on where I can learn React? Thanks!",
createdAt: "2 weeks ago",
score: 5,
user: {
image: {
png: "./images/avatars/image-maxblagun.png",
webp: "./images/avatars/image-maxblagun.webp",
},
username: "maxblagun",
},
replies: [
{
parent: 2,
id: 1,
content:
"If you're still new, I'd recommend focusing on the fundamentals of HTML, CSS, and JS before considering React. It's very tempting to jump ahead but lay a solid foundation first.",
createdAt: "1 week ago",
score: 4,
replyingTo: "maxblagun",
user: {
image: {
png: "./images/avatars/image-ramsesmiron.png",
webp: "./images/avatars/image-ramsesmiron.webp",
},
username: "ramsesmiron",
},
},
{
parent: 2,
id: 1,
content:
"I couldn't agree more with this. Everything moves so fast and it always seems like everyone knows the newest library/framework. But the fundamentals are what stay constant.",
createdAt: "2 days ago",
score: 2,
replyingTo: "ramsesmiron",
user: {
image: {
png: "./images/avatars/image-juliusomo.png",
webp: "./images/avatars/image-juliusomo.webp",
},
username: "juliusomo",
},
},
],
},
],
};
function appendFrag(frag, parent) {
var children = [].slice.call(frag.childNodes, 0);
parent.appendChild(frag);
//console.log(children);
return children[1];
}
const addComment = (body, parentId, replyTo = undefined) => {
let commentParent =
parentId === 0
? data.comments
: data.comments.filter((c) => c.id == parentId)[0].replies;
let newComment = {
parent: parentId,
id:
commentParent.length == 0
? 1
: commentParent[commentParent.length - 1].id + 1,
content: body,
createdAt: "Now",
replyingTo: replyTo,
score: 0,
replies: parent == 0 ? [] : undefined,
user: data.currentUser,
};
commentParent.push(newComment);
initComments();
};
const deleteComment = (commentObject) => {
if (commentObject.parent == 0) {
data.comments = data.comments.filter((e) => e != commentObject);
} else {
data.comments.filter((e) => e.id === commentObject.parent)[0].replies =
data.comments
.filter((e) => e.id === commentObject.parent)[0]
.replies.filter((e) => e != commentObject);
}
initComments();
};
const promptDel = (commentObject) => {
const modalWrp = document.querySelector(".modal-wrp");
modalWrp.classList.remove("invisible");
modalWrp.querySelector(".yes").addEventListener("click", () => {
deleteComment(commentObject);
modalWrp.classList.add("invisible");
});
modalWrp.querySelector(".no").addEventListener("click", () => {
modalWrp.classList.add("invisible");
});
};
const spawnReplyInput = (parent, parentId, replyTo = undefined) => {
if (parent.querySelectorAll(".reply-input")) {
parent.querySelectorAll(".reply-input").forEach((e) => {
e.remove();
});
}
const inputTemplate = document.querySelector(".reply-input-template");
const inputNode = inputTemplate.content.cloneNode(true);
const addedInput = appendFrag(inputNode, parent);
addedInput.querySelector(".bu-primary").addEventListener("click", () => {
let commentBody = addedInput.querySelector(".cmnt-input").value;
if (commentBody.length == 0) return;
addComment(commentBody, parentId, replyTo);
});
};
const createCommentNode = (commentObject) => {
const commentTemplate = document.querySelector(".comment-template");
var commentNode = commentTemplate.content.cloneNode(true);
commentNode.querySelector(".usr-name").textContent =
commentObject.user.username;
commentNode.querySelector(".usr-img").src = commentObject.user.image.webp;
commentNode.querySelector(".score-number").textContent = commentObject.score;
commentNode.querySelector(".cmnt-at").textContent = commentObject.createdAt;
commentNode.querySelector(".c-body").textContent = commentObject.content;
if (commentObject.replyingTo)
commentNode.querySelector(".reply-to").textContent =
"@" + commentObject.replyingTo;
commentNode.querySelector(".score-plus").addEventListener("click", () => {
commentObject.score++;
initComments();
});
commentNode.querySelector(".score-minus").addEventListener("click", () => {
commentObject.score--;
if (commentObject.score < 0) commentObject.score = 0;
initComments();
});
if (commentObject.user.username == data.currentUser.username) {
commentNode.querySelector(".comment").classList.add("this-user");
commentNode.querySelector(".delete").addEventListener("click", () => {
promptDel(commentObject);
});
commentNode.querySelector(".edit").addEventListener("click", (e) => {
const path = e.path[3].querySelector(".c-body");
if (
path.getAttribute("contenteditable") == false ||
path.getAttribute("contenteditable") == null
) {
path.setAttribute("contenteditable", true);
path.focus()
} else {
path.removeAttribute("contenteditable");
}
});
return commentNode;
}
return commentNode;
};
const appendComment = (parentNode, commentNode, parentId) => {
const bu_reply = commentNode.querySelector(".reply");
// parentNode.appendChild(commentNode);
const appendedCmnt = appendFrag(commentNode, parentNode);
const replyTo = appendedCmnt.querySelector(".usr-name").textContent;
bu_reply.addEventListener("click", () => {
if (parentNode.classList.contains("replies")) {
spawnReplyInput(parentNode, parentId, replyTo);
} else {
//console.log(appendedCmnt.querySelector(".replies"));
spawnReplyInput(
appendedCmnt.querySelector(".replies"),
parentId,
replyTo
);
}
});
};
function initComments(
commentList = data.comments,
parent = document.querySelector(".comments-wrp")
) {
parent.innerHTML = "";
commentList.forEach((element) => {
var parentId = element.parent == 0 ? element.id : element.parent;
const comment_node = createCommentNode(element);
if (element.replies && element.replies.length > 0) {
initComments(element.replies, comment_node.querySelector(".replies"));
}
appendComment(parent, comment_node, parentId);
});
}
initComments();
const cmntInput = document.querySelector(".reply-input");
cmntInput.querySelector(".bu-primary").addEventListener("click", () => {
let commentBody = cmntInput.querySelector(".cmnt-input").value;
if (commentBody.length == 0) return;
addComment(commentBody, 0);
cmntInput.querySelector(".cmnt-input").value = "";
});
// addComment("Hello ! It works !!",0);