oliverbooth.dev/src/ts/app.ts

119 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-08-10 22:47:28 +01:00
import API from "./API";
2023-08-10 14:19:11 +01:00
import UI from "./UI";
2023-08-10 22:47:28 +01:00
import Input from "./Input";
2023-08-11 17:15:21 +01:00
import Author from "./Author";
import BlogPost from "./BlogPost";
declare const Handlebars: any;
declare const Prism: any;
2023-08-04 12:55:16 +01:00
(() => {
Prism.languages.extend('markup', {});
Prism.languages.hex = {
'number': {
2023-08-16 15:17:05 +01: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 02:08:48 +01: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 22:47:28 +01: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 14:19:11 +01:00
const blogPostContainer = UI.blogPostContainer;
if (blogPostContainer) {
2023-08-11 17:15:21 +01: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 17:15:21 +01: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 04:57:17 +01:00
document.body.appendChild(UI.createDisqusCounterScript());
const spinner = document.querySelector("#blog-loading-spinner");
if (spinner) {
spinner.classList.add("removed");
2023-08-11 02:08:48 +01:00
setTimeout(() => spinner.remove(), 1000);
}
});
}
2023-08-10 15:15:06 +01:00
UI.updateUI();
2024-02-19 22:44:28 +00:00
setInterval(() => {
const countdown = document.querySelector("#usa-countdown p");
const start = new Date().getTime();
const end = Date.UTC(2024, 2, 7, 13, 20);
const diff = end - start;
let days = Math.floor(diff / (1000 * 60 * 60 * 24));
let hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((diff % (1000 * 60)) / 1000);
if (days < 0) days = 0
if (hours < 0) hours = 0;
if (minutes < 0) minutes = 0;
if (seconds < 0) seconds = 0;
countdown.innerHTML = `${days.toString().padStart(2, '0')} : ${hours.toString().padStart(2, '0')} : ${minutes.toString().padStart(2, '0')} : ${seconds.toString().padStart(2, '0')}`;
}, 1000);
2023-08-04 12:55:16 +01:00
})();