homepage/res/index.js

157 lines
4.3 KiB
JavaScript
Raw Normal View History

const loadedScripts = [];
2020-10-04 23:56:12 +03:00
let selectedButtonId = 'button-home';
2020-10-04 23:56:12 +03:00
const changeSelected = (buttonId) => {
A(`#${selectedButtonId}`).removeClass('button-selected');
2020-10-04 23:56:12 +03:00
A(`#${buttonId}`).addClass('button-selected');
selectedButtonId = buttonId;
2020-10-04 23:56:12 +03:00
};
2020-10-25 16:20:23 +02:00
const stripTrailingSlash = (str) => {
return str.endsWith('/') ? str.slice(0, -1) : str;
};
const loadBlogScripts = async () => {
await loadScript('https://cdn.jsdelivr.net/npm/marked/marked.min.js');
await loadScript('/res/blog.js');
}
const peformRouteAction = async (pathname) => {
2020-10-25 16:20:23 +02:00
const route = stripTrailingSlash(pathname);
switch (route) {
case '': {
2020-10-04 22:40:10 +03:00
A('#content-container').load('/pages/home.html');
break;
}
case '/blog': {
A('meta[name="description"]').attr('content', "hippoz's blog");
await loadBlogScripts();
await A('#content-container').load('/pages/blog/blog.html');
blogPopulateArticleList('articlelist');
break;
}
2020-10-25 16:20:23 +02:00
case '/upload': {
A('meta[name="description"]').attr('content', "hippoz's upload server");
2020-10-05 00:00:20 +03:00
A('#content-container').load('/pages/upload.html');
2020-10-04 22:40:10 +03:00
break;
}
2020-10-25 16:20:23 +02:00
case '/git': {
2020-10-04 22:55:27 +03:00
window.location.href = 'https://git.hippoz.xyz/';
2020-10-04 22:40:10 +03:00
break;
}
2020-10-25 16:20:23 +02:00
case '/about': {
A('meta[name="description"]').attr('content', "hippoz's about page. i'm hippoz");
2020-10-04 22:48:57 +03:00
A('#content-container').load('/pages/about.html');
break;
}
2020-10-25 16:20:23 +02:00
case '/contact': {
A('meta[name="description"]').attr('content', "you can contact hippoz");
2020-10-04 22:48:57 +03:00
A('#content-container').load('/pages/contact.html');
break;
}
2020-10-25 16:20:23 +02:00
case '/404': {}
2020-10-04 23:56:12 +03:00
default: {
A('#content-container').html('<h2>page not found</h2><p>i think</p>');
2020-10-25 16:20:23 +02:00
break;
2020-10-04 23:56:12 +03:00
}
2020-10-04 22:40:10 +03:00
}
};
2020-10-25 16:20:23 +02:00
const buttonMap = {
'button-home': '/',
'button-blog': '/blog',
2020-10-25 16:20:23 +02:00
'button-file-server': '/upload',
'button-git-server': '/git',
'button-about-me': '/about',
'button-contact-me': '/contact',
'__NOT_FOUND': '/404'
}
const dynamicRoutesActions = {
'/article': async (arg) => {
await loadBlogScripts();
const desc = blogArticles[arg];
if (!desc) {
peformRouteAction('/404');
return;
}
changeSelected(getButtonFromPathname('/blog'));
blogVisitArticle(arg, desc);
}
};
2020-10-25 16:20:23 +02:00
const onButtonClick = (buttonId) => {
// The condition after && is kinda of a hack, the reason I do this is because I want users to be able to click the blog button and return to the article list to continue reading when they are already on an article.
if (selectedButtonId === buttonId && buttonId !== 'button-blog') {
2020-10-25 16:20:23 +02:00
return;
}
changeSelected(buttonId);
let route = buttonMap[buttonId];
if (!route) {
route = buttonMap['__NOT_FOUND'];
}
peformRouteAction(route);
history.pushState({ route }, '', route);
};
const getButtonFromPathname = (pathname) => {
let button = 'button-home';
2020-10-25 16:24:26 +02:00
for (const [k, v] of Object.entries(buttonMap)) {
2020-10-25 16:20:23 +02:00
if (v === pathname) {
button = k;
break;
}
}
return button;
}
const updateFromCurrentPathname = () => {
let pathname = window.location.pathname;
if (!pathname || pathname === '' || pathname === ' ') {
pathname = '/';
}
for (const [route, action] of Object.entries(dynamicRoutesActions)) {
if (pathname.startsWith(route)) {
2020-10-25 18:24:50 +02:00
const arg = stripTrailingSlash(pathname).split('/')[2];
action(arg);
break;
}
}
2020-10-25 16:20:23 +02:00
const buttonId = getButtonFromPathname(pathname);
changeSelected(buttonId);
2020-10-25 16:25:02 +02:00
peformRouteAction(pathname);
2020-10-25 16:20:23 +02:00
}
const loadScript = (src) => {
if (loadedScripts.includes(src)) {
return;
}
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.onload = () => {
loadedScripts.push(src);
resolve();
};
script.onerror = reject;
script.src = src;
document.head.appendChild(script);
});
}
2020-10-04 22:40:10 +03:00
A(() => {
A('.navigation-buttons .button-default').each((el) => {
A(el).on('click', () => {
onButtonClick(el.id);
});
});
updateFromCurrentPathname();
2020-10-04 22:40:10 +03:00
});