refactor: generalise post element generation
This commit is contained in:
parent
2755f7ebd3
commit
458ad097d8
115
main.js
115
main.js
|
@ -6,8 +6,7 @@ const postCountElem = document.getElementById("post-count");
|
||||||
const postTotalElem = document.getElementById("post-total");
|
const postTotalElem = document.getElementById("post-total");
|
||||||
const loader = document.getElementById("loader");
|
const loader = document.getElementById("loader");
|
||||||
|
|
||||||
// then we'll define a variable for how many posts we want to increase the
|
// define a variable for how many posts we want to increase the page by
|
||||||
// page by
|
|
||||||
const postIncrease = 9;
|
const postIncrease = 9;
|
||||||
// and define a value to determine which page we're on
|
// and define a value to determine which page we're on
|
||||||
let currentPage = 1;
|
let currentPage = 1;
|
||||||
|
@ -23,13 +22,15 @@ function getRandomColor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
class Post {
|
class Post {
|
||||||
|
// JSON post data
|
||||||
constructor(data) {
|
constructor(data) {
|
||||||
this.id = data.id;
|
this.id = data.id;
|
||||||
this.username = data.associatedUser;
|
this.username = data.associatedUser;
|
||||||
this.content = data.body;
|
this.content = data.body;
|
||||||
this.replyTo = data.replyTo;
|
this.replyTo = data.replyTo;
|
||||||
|
this.parentPost = null;
|
||||||
|
|
||||||
this.replies = []
|
this.replies = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
getIsReply() {
|
getIsReply() {
|
||||||
|
@ -38,64 +39,67 @@ class Post {
|
||||||
|
|
||||||
addReply(reply) {
|
addReply(reply) {
|
||||||
this.replies.push(reply);
|
this.replies.push(reply);
|
||||||
|
reply.parentPost = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPostLevel() {
|
||||||
|
let p = this.parentPost;
|
||||||
|
let depth = 0;
|
||||||
|
while (p != null) {
|
||||||
|
p = p.parentPost;
|
||||||
|
depth++;
|
||||||
|
}
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
|
||||||
|
getHeaderTag() {
|
||||||
|
const level = this.getPostLevel();
|
||||||
|
switch (level) {
|
||||||
|
case 0: return "h1";
|
||||||
|
case 1: return "h2";
|
||||||
|
case 2: return "h3";
|
||||||
|
case 3: return "h4";
|
||||||
|
default:
|
||||||
|
console.error(`${level} is not a supported post level`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getHeaderElement() {
|
||||||
|
const headerElem = document.createElement(this.getHeaderTag());
|
||||||
|
headerElem.innerHTML = this.username;
|
||||||
|
|
||||||
|
const elem = document.createElement("a");
|
||||||
|
elem.setAttribute("href", "#");
|
||||||
|
elem.addEventListener("click", () => updateUserProfile(this.username));
|
||||||
|
elem.appendChild(headerElem);
|
||||||
|
|
||||||
|
return elem;
|
||||||
|
}
|
||||||
|
|
||||||
|
getContentElement() {
|
||||||
|
const elem = document.createElement("p");
|
||||||
|
elem.innerHTML = this.content;
|
||||||
|
return elem;
|
||||||
}
|
}
|
||||||
|
|
||||||
getElement() {
|
getElement() {
|
||||||
const postElem = document.createElement("div");
|
const elem = document.createElement("div");
|
||||||
postElem.className = "block post";
|
|
||||||
postElem.style.backgroundColor = getRandomColor();
|
|
||||||
|
|
||||||
// add a header to the post
|
// display root posts as blocks, and comments as attached to their posts
|
||||||
const headerElem = document.createElement("h1");
|
if (this.getPostLevel() == 0) {
|
||||||
headerElem.innerHTML = this.username;
|
elem.className = "block post";
|
||||||
|
}
|
||||||
const profileLinkElem = document.createElement("a");
|
elem.style.backgroundColor = getRandomColor();
|
||||||
profileLinkElem.setAttribute("href", "#");
|
elem.appendChild(this.getHeaderElement());
|
||||||
profileLinkElem.addEventListener("click", () => updateUserProfile(this.username));
|
elem.appendChild(this.getContentElement());
|
||||||
profileLinkElem.appendChild(headerElem);
|
|
||||||
|
|
||||||
postElem.appendChild(profileLinkElem);
|
|
||||||
|
|
||||||
const contentElem = document.createElement("p");
|
|
||||||
contentElem.innerHTML = this.content;
|
|
||||||
postElem.appendChild(contentElem);
|
|
||||||
|
|
||||||
for (let i = 0; i < this.replies.length; i++) {
|
for (let i = 0; i < this.replies.length; i++) {
|
||||||
const reply = this.replies[i];
|
const reply = this.replies[i];
|
||||||
|
elem.appendChild(reply.getElement());
|
||||||
const commentElem = document.createElement("div");
|
|
||||||
commentElem.style.backgroundColor = getRandomColor();
|
|
||||||
|
|
||||||
const commentUserElem = document.createElement("h2");
|
|
||||||
commentUserElem.innerHTML = reply.username;
|
|
||||||
commentElem.appendChild(commentUserElem);
|
|
||||||
|
|
||||||
const commentContentElem = document.createElement("p");
|
|
||||||
commentContentElem.innerHTML = reply.content;
|
|
||||||
commentElem.appendChild(commentContentElem);
|
|
||||||
|
|
||||||
postElem.appendChild(commentElem);
|
|
||||||
|
|
||||||
// TODO: indent 2nd-level replies
|
|
||||||
for (let j = 0; j < reply.replies.length; j++) {
|
|
||||||
const replyReply = reply.replies[j];
|
|
||||||
|
|
||||||
const replyReplyElem = document.createElement("div");
|
|
||||||
replyReplyElem.style.backgroundColor = getRandomColor();
|
|
||||||
|
|
||||||
const replyReplyUserElem = document.createElement("h3");
|
|
||||||
replyReplyUserElem.innerHTML = replyReply.username;
|
|
||||||
replyReplyElem.appendChild(replyReplyUserElem);
|
|
||||||
|
|
||||||
const replyReplyContentElem = document.createElement("p");
|
|
||||||
replyReplyContentElem.innerHTML = replyReply.content;
|
|
||||||
replyReplyElem.appendChild(replyReplyContentElem);
|
|
||||||
|
|
||||||
postElem.appendChild(replyReplyElem);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return postElem;
|
return elem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,7 +130,9 @@ function addPosts(pageIdx) {
|
||||||
const rootPosts = getRootPosts();
|
const rootPosts = getRootPosts();
|
||||||
|
|
||||||
for (let i = startRange + 1; i <= endRange; i++) {
|
for (let i = startRange + 1; i <= endRange; i++) {
|
||||||
blockContainer.appendChild(rootPosts[i].getElement());
|
const post = rootPosts[i];
|
||||||
|
const elem = post.getElement();
|
||||||
|
blockContainer.appendChild(elem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,9 +251,8 @@ function init() {
|
||||||
|
|
||||||
console.log(`loaded ${users.length} users and ${postCount} posts`);
|
console.log(`loaded ${users.length} users and ${postCount} posts`);
|
||||||
|
|
||||||
// TODO: add write post block
|
// TODO: add user bio above write post button
|
||||||
addWritePostBlock();
|
addWritePostBlock();
|
||||||
// TODO: add bio
|
|
||||||
|
|
||||||
addPosts(currentPage);
|
addPosts(currentPage);
|
||||||
window.addEventListener("scroll", handleInfiniteScroll);
|
window.addEventListener("scroll", handleInfiniteScroll);
|
||||||
|
|
Loading…
Reference in New Issue