website/assets/js/page.js

53 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-08-06 09:51:41 +02:00
import Cookies from 'js-cookie';
import '/assets/scss/page.scss';
2021-08-06 09:51:41 +02:00
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');
2022-01-27 15:03:28 +01:00
let nineties = document.querySelector('.nineties-toggle');
let container = document.querySelector('.container');
container.style.height = (body.scrollHeight > window.outerHeight) ? 'auto' : body.scrollHeight - nineties.scrollHeight + 'px';
2021-08-05 13:51:44 +02:00
}
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');
}
})
}
2021-12-31 18:22:31 +01:00
const isExternalURL = (url) => {
if(url.startsWith('/')) return false;
return new URL(url).origin !== location.origin;
}