oliverbooth.dev/src/ts/app.ts

138 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-08-10 21:47:28 +00:00
import API from "./API";
2023-08-10 13:19:11 +00:00
import UI from "./UI";
2023-08-10 21:47:28 +00:00
import Input from "./Input";
2023-08-11 16:15:21 +00:00
import Author from "./Author";
import BlogPost from "./BlogPost";
declare const Handlebars: any;
declare const Prism: any;
2023-08-04 11:55:16 +00:00
(() => {
Prism.languages.extend('markup', {});
Prism.languages.hex = {
'number': {
2023-08-16 14:17:05 +00:00
pattern: /(?:[a-fA-F0-9]{3}){1,2}\b/i,
lookbehind: true
}
};
Prism.languages.binary = {
'number': {
pattern: /[10]+/i,
lookbehind: true
}
};
Prism.languages.insertBefore('custom', 'tag', {
'mark': {
pattern: /<\/?mark(?:\s+\w+(?:=(?:"[^"]*"|'[^']*'|[^\s'">=]+))?\s*|\s*)\/?>/,
greedy: true
}
});
2023-08-11 01:08:48 +00:00
Handlebars.registerHelper("urlEncode", encodeURIComponent);
function getQueryVariable(variable: string): string {
const query = window.location.search.substring(1);
const vars = query.split("&");
for (const element of vars) {
const pair = element.split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return null;
}
2023-08-10 21:47:28 +00:00
Input.registerShortcut(Input.KONAMI_CODE, () => {
window.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "_blank");
});
const blogPost = UI.blogPost;
if (blogPost) {
const id = blogPost.dataset.blogId;
API.getBlogPost(id).then((post) => {
blogPost.innerHTML = post.content;
UI.updateUI(blogPost);
});
}
2023-08-10 13:19:11 +00:00
const blogPostContainer = UI.blogPostContainer;
if (blogPostContainer) {
2023-08-11 16:15:21 +00:00
const authors = [];
const template = Handlebars.compile(UI.blogPostTemplate.innerHTML);
API.getBlogPostCount().then(async (count) => {
for (let i = 0; i <= count / 10; i++) {
let posts: BlogPost[];
const tag = getQueryVariable("tag");
if (tag !== null && tag !== "") {
posts = await API.getBlogPostsByTag(decodeURIComponent(tag), i);
} else {
posts = await API.getBlogPosts(i);
}
for (const post of posts) {
2023-08-11 16:15:21 +00:00
let author: Author;
if (authors[post.authorId]) {
author = authors[post.authorId];
} else {
author = await API.getAuthor(post.authorId);
authors[post.authorId] = author;
}
const card = UI.createBlogPostCard(template, post, author);
blogPostContainer.appendChild(card);
UI.updateUI(card);
}
}
2023-08-10 03:57:17 +00:00
document.body.appendChild(UI.createDisqusCounterScript());
const spinner = document.querySelector("#blog-loading-spinner");
if (spinner) {
spinner.classList.add("removed");
2023-08-11 01:08:48 +00:00
setTimeout(() => spinner.remove(), 1000);
}
});
}
2023-08-10 14:15:06 +00:00
UI.updateUI();
2024-02-19 22:44:28 +00:00
const usaCountdown = document.getElementById("usa-countdown");
if (usaCountdown) {
usaCountdown.addEventListener("click", () => window.location.href = "/blog/2024/02/19/the-american");
UI.updateUsaCountdown(usaCountdown);
setInterval(() => UI.updateUsaCountdown(usaCountdown), 1000);
}
let avatarType = 0;
const headshot = document.getElementById("index-headshot") as HTMLImageElement;
headshot.addEventListener("click", (ev: MouseEvent) => {
if (avatarType === 0) {
headshot.classList.add("headshot-spin", "headshot-spin-start");
setTimeout(() => {
headshot.classList.remove("headshot-spin-start");
headshot.src = "/img/avatar_512x512.jpg"
headshot.classList.add("headshot-spin", "headshot-spin-end");
setTimeout(() => {
headshot.classList.remove("headshot-spin", "headshot-spin-end");
avatarType = 1;
}, 800);
}, 400);
} else if (avatarType === 1) {
headshot.classList.add("headshot-spin", "headshot-spin-start");
setTimeout(() => {
headshot.classList.remove("headshot-spin-start");
headshot.src = "/img/headshot_512x512_2023.jpg"
headshot.classList.add("headshot-spin", "headshot-spin-end");
setTimeout(() => {
headshot.classList.remove("headshot-spin", "headshot-spin-end");
avatarType = 0;
}, 800);
}, 400);
}
});
2023-08-04 11:55:16 +00:00
})();