2020-10-07 19:15:33 +03:00
|
|
|
let selectedButtonId = 'button-home';
|
2020-10-25 17:43:06 +02:00
|
|
|
const loadedScripts = [];
|
2020-10-04 23:56:12 +03:00
|
|
|
|
|
|
|
const changeSelected = (buttonId) => {
|
2020-10-07 19:15:33 +03:00
|
|
|
A(`#${selectedButtonId}`).removeClass('button-selected');
|
2020-10-04 23:56:12 +03:00
|
|
|
A(`#${buttonId}`).addClass('button-selected');
|
2020-10-07 19:15:33 +03:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-10-25 17:43:06 +02:00
|
|
|
const peformRouteAction = async (pathname) => {
|
2020-10-25 16:20:23 +02:00
|
|
|
const route = stripTrailingSlash(pathname);
|
|
|
|
|
|
|
|
switch (route) {
|
2020-10-25 16:26:58 +02:00
|
|
|
case '': {
|
2020-10-04 22:40:10 +03:00
|
|
|
A('#content-container').load('/pages/home.html');
|
|
|
|
break;
|
|
|
|
}
|
2020-10-25 17:43:06 +02:00
|
|
|
case '/blog': {
|
|
|
|
A('meta[name="description"]').attr('content', "hippoz's blog");
|
|
|
|
await loadScript('https://cdn.jsdelivr.net/npm/marked/marked.min.js');
|
|
|
|
await loadScript('/res/blog.js');
|
|
|
|
A('#content-container').load('/pages/blog/blog.html');
|
|
|
|
break;
|
|
|
|
}
|
2020-10-25 16:20:23 +02:00
|
|
|
case '/upload': {
|
2020-10-25 17:43:06 +02:00
|
|
|
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': {
|
2020-10-25 17:43:06 +02:00
|
|
|
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': {
|
2020-10-25 17:43:06 +02:00
|
|
|
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: {
|
2020-10-24 20:04:40 +03:00
|
|
|
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': '/',
|
2020-10-25 17:43:06 +02:00
|
|
|
'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 onButtonClick = (buttonId) => {
|
|
|
|
if (selectedButtonId === buttonId) {
|
|
|
|
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 = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
const buttonId = getButtonFromPathname(pathname);
|
|
|
|
changeSelected(buttonId);
|
2020-10-25 16:25:02 +02:00
|
|
|
peformRouteAction(pathname);
|
2020-10-25 16:20:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:43:06 +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);
|
|
|
|
});
|
2020-10-25 16:23:23 +02:00
|
|
|
});
|
|
|
|
updateFromCurrentPathname();
|
2020-10-04 22:40:10 +03:00
|
|
|
});
|