website/assets/js/page.js

51 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-08-06 09:51:41 +02:00
import Cookies from 'js-cookie';
2021-08-05 13:51:44 +02:00
document.addEventListener("readystatechange", event => {
if(event.target.readyState === 'complete') {
initOverflow();
initExternalLinks();
2021-08-06 09:51:41 +02:00
initNinetiesToggle();
2021-08-05 13:51:44 +02:00
}
});
function initOverflow() {
let body = document.querySelector('body');
if (body.scrollHeight > window.outerHeight) {
body.style.height = 'auto';
}
}
2021-08-06 09:51:41 +02:00
function initNinetiesToggle() {
2021-08-06 10:41:12 +02:00
let nineties = document.querySelector('.nineties-toggle');
let container = document.querySelector('.container');
nineties.addEventListener('click', function() {
2021-08-06 09:51:41 +02:00
let body = document.querySelector('body');
if (body.classList.contains('nineties')) {
body.classList.remove('nineties');
Cookies.remove('nineties');
} else {
body.classList.add('nineties');
Cookies.set('nineties', true, { sameSite: 'strict' });
}
})
2021-08-06 10:41:12 +02:00
if(
nineties.getBoundingClientRect().left < container.getBoundingClientRect().right &&
nineties.getBoundingClientRect().bottom > container.getBoundingClientRect().top
) {
nineties.style.position = 'initial';
nineties.style.textAlign = 'right';
}
2021-08-06 09:51:41 +02:00
}
2021-08-05 13:51:44 +02:00
function initExternalLinks() {
document.querySelectorAll('a').forEach(elem => {
if(isExternalURL(elem.getAttribute('href'))) {
elem.setAttribute('target', '_blank');
}
})
}
const isExternalURL = (url) => new URL(url).origin !== location.origin;