singularity/main.js

205 lines
5.8 KiB
JavaScript
Raw Normal View History

2024-07-11 21:13:17 +02:00
// first, let's get all the elements we'll need from our DOM
2024-07-11 22:19:51 +02:00
const postContainer = document.getElementById("post-container");
const postCountElem = document.getElementById("post-count");
const postTotalElem = document.getElementById("post-total");
2024-07-11 21:13:17 +02:00
const loader = document.getElementById("loader");
2024-07-11 22:19:51 +02:00
// we'll need a value for the max numaer of posts to be added to the page
const postLimit = 99;
// then we'll define a variable for how many posts we want to increase the
2024-07-11 21:13:17 +02:00
// page by
2024-07-11 22:19:51 +02:00
const postIncrease = 9;
2024-07-11 21:13:17 +02:00
// how many times can we increase the content until we reach the max limit?
2024-07-11 22:19:51 +02:00
const pageCount = Math.ceil(postLimit / postIncrease);
2024-07-11 21:13:17 +02:00
// and define a value to determine which page we're on
let currentPage = 1;
2024-07-11 22:19:51 +02:00
postTotalElem.innerHTML = postLimit;
2024-07-11 21:13:17 +02:00
function getRandomColor() {
const h = Math.floor(Math.random() * 360);
return `hsl(${h}deg, 90%, 85%)`;
}
2024-07-11 22:19:51 +02:00
function getUsername() {
const usernames = [
"xXPu55y5l4y3r69Xx",
"Keef Farmer",
2024-07-13 02:45:17 +02:00
"Alan",
"mongle boy",
"girlthing",
"doggle poggle",
"catlicker",
"350 million for the nhs",
"thoughts were a mistake",
"410,757,864,530 FRIENDS MADE"
2024-07-11 22:19:51 +02:00
];
const r = Math.floor(Math.random() * usernames.length);
return usernames[r];
}
function getContent() {
return "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
}
class Post {
constructor() {
this.username = getUsername();
this.content = getContent();
}
getElement() {
const postElem = document.createElement("div");
postElem.className = "post";
postElem.style.backgroundColor = getRandomColor();
// add a header to the post
const headerElem = document.createElement("h1");
headerElem.innerHTML = this.username;
2024-07-11 23:53:45 +02:00
const profileLinkElem = document.createElement("a");
profileLinkElem.setAttribute("href", "#");
profileLinkElem.addEventListener("click", () => updateUserProfile(this.username));
profileLinkElem.appendChild(headerElem);
postElem.appendChild(profileLinkElem);
2024-07-11 22:19:51 +02:00
const contentElem = document.createElement("p");
contentElem.innerHTML = this.content;
postElem.appendChild(contentElem);
2024-07-13 02:45:55 +02:00
// add a random number of comments to the post
const numComments = Math.random() * 5;
for (let i = 0; i < numComments; i++) {
const commentElem = document.createElement("div");
commentElem.style.backgroundColor = getRandomColor();
const commentUserElem = document.createElement("h2");
commentUserElem.innerHTML = getUsername();
commentElem.appendChild(commentUserElem);
const commentContentElem = document.createElement("p");
commentContentElem.innerHTML = `comment ${i}`;
commentElem.appendChild(commentContentElem);
postElem.appendChild(commentElem);
}
2024-07-11 22:19:51 +02:00
return postElem;
}
}
2024-07-11 23:53:45 +02:00
var userProfile = null;
class UserProfile {
constructor(username) {
this.username = username;
this.bio = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
this.elem = document.getElementById("profile");
this.nameElem = document.getElementById("profile-name");
this.descElem = document.getElementById("profile-description");
}
show() {
this.nameElem.innerHTML = this.username;
this.descElem.innerHTML = this.bio;
this.elem.style.visibility = "visible";
}
hide() {
this.descElem.innerHTML = "";
this.nameElem.innerHTML = "";
this.elem.style.visibility = "hidden";
}
}
function goToHome() {
updateUserProfile(null);
}
function updateUserProfile(username) {
if (userProfile != null) {
userProfile.hide();
userProfile = null;
}
if (!username)
return;
userProfile = new UserProfile(username);
userProfile.show();
}
2024-07-11 22:19:51 +02:00
function createPost() {
const post = new Post();
postContainer.appendChild(post.getElement());
2024-07-11 21:13:17 +02:00
}
2024-07-11 22:19:51 +02:00
function addPosts(pageIdx) {
2024-07-11 21:13:17 +02:00
currentPage = pageIdx;
2024-07-11 22:19:51 +02:00
const startRange = (pageIdx - 1) * postIncrease;
2024-07-11 21:13:17 +02:00
const endRange = currentPage == pageCount
2024-07-11 22:19:51 +02:00
? postLimit
: pageIdx * postIncrease;
2024-07-11 21:13:17 +02:00
2024-07-11 22:19:51 +02:00
postCountElem.innerHTML = endRange;
2024-07-11 21:13:17 +02:00
for (let i = startRange + 1; i <= endRange; i++) {
2024-07-11 22:19:51 +02:00
createPost();
2024-07-11 21:13:17 +02:00
}
}
function handleInfiniteScroll() {
throttle(() => {
const endOfPage = window.innerHeight + window.pageYOffset >= document.body.offsetHeight;
if (endOfPage) {
2024-07-11 22:19:51 +02:00
addPosts(currentPage + 1);
2024-07-11 21:13:17 +02:00
}
if (currentPage === pageCount) {
removeInfiniteScroll();
}
}, 1000);
}
2024-07-11 22:19:51 +02:00
// limit how often we try to load new posts to maintain browser performance
2024-07-11 21:13:17 +02:00
var throttleTimer;
function throttle(callback, time) {
if (throttleTimer) return;
throttleTimer = true;
setTimeout(() => {
callback();
throttleTimer = false;
}, time);
}
function removeInfiniteScroll() {
loader.remove();
window.removeEventListener("scroll", handleInfiniteScroll);
}
2024-07-11 22:19:51 +02:00
addPosts(currentPage);
2024-07-11 21:13:17 +02:00
window.addEventListener("scroll", handleInfiniteScroll);
// TODO: define the limit of the content to be loaded on the page
// TODO: detect when the user has reached the end of the content container
// TODO: load more content once the end of the container has been reached
// TODO: if there's no more content to be loaded, stop the infinite scroll