2023-08-10 04:56:12 +01:00
|
|
|
import API from "./API";
|
|
|
|
import TimeUtility from "./TimeUtility";
|
2023-08-10 14:19:11 +01:00
|
|
|
import UI from "./UI";
|
2023-08-10 04:56:12 +01:00
|
|
|
|
2023-08-10 14:36:51 +01:00
|
|
|
declare const Handlebars: any;
|
2023-08-04 12:55:16 +01:00
|
|
|
|
|
|
|
(() => {
|
2023-08-10 14:19:11 +01:00
|
|
|
const blogPostContainer = UI.blogPostContainer;
|
2023-08-10 04:56:12 +01:00
|
|
|
if (blogPostContainer) {
|
2023-08-10 14:36:51 +01:00
|
|
|
const template = Handlebars.compile(UI.blogPostTemplate.innerHTML);
|
2023-08-10 04:56:12 +01:00
|
|
|
API.getBlogPostCount().then(async (count) => {
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
const posts = await API.getBlogPosts(i, 5);
|
|
|
|
for (const post of posts) {
|
|
|
|
const author = await API.getAuthor(post.authorId);
|
|
|
|
|
|
|
|
const card = document.createElement("div") as HTMLDivElement;
|
|
|
|
card.classList.add("card");
|
|
|
|
card.classList.add("blog-card");
|
|
|
|
card.classList.add("animate__animated");
|
|
|
|
card.classList.add("animate__fadeIn");
|
|
|
|
card.style.marginBottom = "50px";
|
|
|
|
|
2023-08-10 14:36:51 +01:00
|
|
|
const body = template({
|
|
|
|
post: {
|
|
|
|
title: post.title,
|
|
|
|
excerpt: post.excerpt,
|
|
|
|
url: post.url,
|
|
|
|
date: TimeUtility.formatRelativeTimestamp(post.published),
|
|
|
|
date_humanized: `${post.updated ? "Updated" : "Published"} ${post.humanizedTimestamp}`,
|
|
|
|
enable_comments: post.commentsEnabled,
|
|
|
|
disqus_identifier: post.identifier,
|
|
|
|
trimmed: post.trimmed,
|
|
|
|
},
|
|
|
|
author: {
|
|
|
|
name: author.name,
|
|
|
|
avatar: `https://gravatar.com/avatar/${author.avatarHash}?s=28`,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
card.innerHTML = body.trim();
|
2023-08-10 04:56:12 +01:00
|
|
|
|
|
|
|
blogPostContainer.appendChild(card);
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 4;
|
|
|
|
}
|
2023-08-10 04:57:17 +01:00
|
|
|
|
2023-08-10 15:15:06 +01:00
|
|
|
UI.updateUI();
|
|
|
|
|
2023-08-10 14:32:56 +01:00
|
|
|
const spinner = document.querySelector("#blog-loading-spinner");
|
2023-08-10 04:56:12 +01:00
|
|
|
if (spinner) {
|
|
|
|
spinner.classList.add("removed");
|
|
|
|
setTimeout(() => spinner.remove(), 1100);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:15:06 +01:00
|
|
|
UI.updateUI();
|
2023-08-08 02:10:17 +01:00
|
|
|
|
2023-08-10 01:49:09 +01:00
|
|
|
const timestamps = document.querySelectorAll("span[data-timestamp][data-format]");
|
|
|
|
timestamps.forEach((timestamp) => {
|
|
|
|
const seconds = parseInt(timestamp.getAttribute("data-timestamp"));
|
|
|
|
const format = timestamp.getAttribute("data-format");
|
|
|
|
const date = new Date(seconds * 1000);
|
|
|
|
|
|
|
|
const shortTimeString = date.toLocaleTimeString([], {hour: "2-digit", minute: "2-digit"});
|
|
|
|
const shortDateString = date.toLocaleDateString([], {day: "2-digit", month: "2-digit", year: "numeric"});
|
|
|
|
const longTimeString = date.toLocaleTimeString([], {hour: "2-digit", minute: "2-digit", second: "2-digit"});
|
|
|
|
const longDateString = date.toLocaleDateString([], {day: "numeric", month: "long", year: "numeric"});
|
|
|
|
const weekday = date.toLocaleString([], {weekday: "long"});
|
|
|
|
timestamp.setAttribute("title", `${weekday}, ${longDateString} ${shortTimeString}`);
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case "t":
|
|
|
|
timestamp.textContent = shortTimeString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "T":
|
|
|
|
timestamp.textContent = longTimeString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "d":
|
|
|
|
timestamp.textContent = shortDateString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "D":
|
|
|
|
timestamp.textContent = longDateString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "f":
|
|
|
|
timestamp.textContent = `${longDateString} at ${shortTimeString}`
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "F":
|
|
|
|
timestamp.textContent = `${weekday}, ${longDateString} at ${shortTimeString}`
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "R":
|
|
|
|
setInterval(() => {
|
2023-08-10 04:57:17 +01:00
|
|
|
timestamp.textContent = TimeUtility.formatRelativeTimestamp(date);
|
2023-08-10 01:49:09 +01:00
|
|
|
}, 1000);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2023-08-04 12:55:16 +01:00
|
|
|
})();
|