homepage/res/index.js

99 lines
2.4 KiB
JavaScript
Raw Normal View History

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 peformRouteAction = (pathname) => {
const route = stripTrailingSlash(pathname);
switch (route) {
case '': {
2020-10-04 22:40:10 +03:00
A('#content-container').load('/pages/home.html');
break;
}
2020-10-25 16:20:23 +02:00
case '/upload': {
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-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-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-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-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
});