actuel-inactuel/assets/js/script.js

209 lines
5.8 KiB
JavaScript
Raw Normal View History

2024-04-07 09:57:55 +02:00
const verticalUnit = getUnit("--unit--vertical");
function getUnit(id) {
const remFactor = 16;
2024-04-09 08:38:29 +02:00
const rawUnit =
getComputedStyle(document.documentElement).getPropertyValue(id) || "1.7rem";
2024-04-07 09:57:55 +02:00
const remUnit = parseFloat(rawUnit);
const pxUnit = remUnit * remFactor;
return pxUnit;
}
2024-12-02 16:39:38 +01:00
// Throttle found here : https://gist.github.com/ionurboz/51b505ee3281cd713747b4a84d69f434
function throttle(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function () {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
2024-04-07 09:57:55 +02:00
return function () {
2024-12-02 16:39:38 +01:00
var now = Date.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
2024-04-07 09:57:55 +02:00
}
2024-12-02 16:39:38 +01:00
return result;
2024-04-07 09:57:55 +02:00
};
}
2024-01-26 13:42:00 +01:00
2024-04-06 10:08:45 +02:00
function setWindowHeightFactor() {
const windowHeight = window.innerHeight;
const min = 650;
const delta = windowHeight - min;
const factor = roundToNearestHalf(delta / 300) + 1;
2024-04-06 10:08:45 +02:00
2024-04-10 14:55:34 +02:00
document
.querySelector(":root")
.style.setProperty("--window-height-factor", factor);
2024-04-06 10:08:45 +02:00
}
function roundToNearestHalf(num) {
const round = Math.round(num * 2) / 2;
return Math.max(round, 0);
2024-04-06 10:08:45 +02:00
}
2024-04-07 09:57:55 +02:00
function toggleLogoState() {
const scrollY = window.scrollY || window.pageYOffset;
if (scrollY > 10) {
document.querySelector("#main-header").classList.add("minimized");
} else {
document.querySelector("#main-header").classList.remove("minimized");
2024-01-25 17:05:19 +01:00
}
2024-04-07 09:57:55 +02:00
}
2024-12-02 16:39:38 +01:00
function toggleFooterState() {
if (scrollY > 90) {
2025-02-18 17:11:09 +01:00
document.querySelector(".open-nav-wrapper").classList.remove("hidden");
2024-12-02 16:39:38 +01:00
} else {
2025-02-18 17:11:09 +01:00
document.querySelector(".open-nav-wrapper").classList.add("hidden");
2024-12-02 16:39:38 +01:00
}
}
2024-01-25 17:05:19 +01:00
2024-04-10 17:23:32 +02:00
function fixFootNotes() {
const footnotes = document.querySelectorAll('a[href^="#sdfootnote"]');
footnotes.forEach((footnote) => {
const href = footnote.href;
2025-02-03 14:06:04 +01:00
2024-04-10 17:23:32 +02:00
footnote.classList.add("footnote");
2025-02-03 14:06:04 +01:00
2024-04-10 17:23:32 +02:00
if (href.includes("sym")) {
footnote.id = footnote.hash.replace("sym", "anc").replace("#", "");
2025-02-03 14:06:04 +01:00
} else if (href.includes("anc")) {
2024-04-10 17:23:32 +02:00
footnote.id = footnote.hash.replace("anc", "sym").replace("#", "");
}
});
}
2024-06-20 10:33:53 +02:00
function removeAccents(str) {
const from = "áäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđ߯a·/_,:;";
const to = "aaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------";
for (let i = 0, l = from.length; i < l; i++) {
str = str.replace(new RegExp(from.charAt(i), "g"), to.charAt(i));
}
return str;
}
function slugify(str) {
return removeAccents(str.toLowerCase());
}
2024-10-25 18:24:19 +02:00
function subscribe(event) {
event.preventDefault();
2025-01-26 14:44:21 +01:00
const emailInput = document.querySelector("#subscribe-form input");
2024-10-25 18:24:19 +02:00
2025-01-26 14:44:21 +01:00
if (emailInput.value.toLowerCase().match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)) {
2024-10-25 18:24:19 +02:00
const header = {
method: "POST",
2025-01-26 14:44:21 +01:00
body: JSON.stringify(emailInput.value),
2024-10-25 18:24:19 +02:00
};
2025-01-26 14:44:21 +01:00
fetch("/subscribe.json", header)
.then((res) => res.json())
.then((data) => {
const formNode = emailInput.parentNode.parentNode;
formNode.outerHTML = "<p>" + data.message + "</p>";
});
2024-10-25 18:24:19 +02:00
} else {
2025-01-26 14:44:21 +01:00
emailInput.value = "E-mail invalide. Recommencez.";
2024-10-25 18:24:19 +02:00
}
}
2024-11-26 13:21:42 +01:00
const panelNav = document.querySelector(".panel");
const navOverlay = document.querySelector("#nav-overlay");
const openNavBtn = document.querySelector("button.open-nav");
const closeNavBtn = document.querySelector(".panel-close");
function closeNav() {
panelNav.classList.remove("panel--visible");
navOverlay.classList.remove("nav-overlay--visible");
document.body.classList.remove("no-scroll");
}
2024-04-07 09:57:55 +02:00
document.addEventListener("DOMContentLoaded", () => {
2025-02-04 09:33:15 +01:00
ragadjust("h1, h2, h4, h5", ["all"]);
2024-03-12 17:41:59 +01:00
window.window.scrollTo({
top: 0,
});
2024-12-02 16:39:38 +01:00
const handleScroll = throttle(() => {
2024-04-09 08:38:29 +02:00
toggleLogoState();
2024-12-02 16:39:38 +01:00
if (window.innerWidth <= 680) {
toggleFooterState();
}
}, 100);
window.addEventListener("scroll", handleScroll);
2024-04-09 07:24:22 +02:00
2024-04-07 09:57:55 +02:00
setWindowHeightFactor();
2024-04-15 16:01:13 +02:00
window.addEventListener("resize", () => {
setWindowHeightFactor();
});
2024-04-07 09:57:55 +02:00
2024-04-10 17:23:32 +02:00
fixFootNotes();
2024-04-15 13:43:57 +02:00
window.addEventListener("keyup", (event) => {
if (event.key === "Escape") {
2024-11-26 13:21:42 +01:00
closeNav();
2024-04-15 13:43:57 +02:00
}
});
document.querySelectorAll(".panel").forEach((panel) => {
panel.addEventListener("click", (event) => {
event.stopPropagation();
});
});
2024-11-26 09:53:05 +01:00
const navSortBtns = document.querySelectorAll("nav .sort-btn");
const navSections = document.querySelectorAll(
".panel__all-texts, .panel__collection"
);
navSortBtns.forEach((sortBtn) => {
sortBtn.addEventListener("click", () => {
navSortBtns.forEach((btn) => btn.classList.remove("active"));
sortBtn.classList.add("active");
const sections = {
"sort-btn--all": ".panel__all-texts",
"sort-btn--years": ".panel__collection--years",
"sort-btn--categories": ".panel__collection--categories",
};
navSections.forEach((navSection) => navSection.classList.add("hidden"));
Object.keys(sections).forEach((key) => {
if (sortBtn.classList.contains(key)) {
document.querySelector(sections[key]).classList.remove("hidden");
}
});
});
});
openNavBtn.addEventListener("click", () => {
panelNav.classList.add("panel--visible");
2024-11-26 10:19:13 +01:00
navOverlay.classList.add("nav-overlay--visible");
document.body.classList.add("no-scroll");
2024-11-26 09:53:05 +01:00
});
closeNavBtn.addEventListener("click", () => {
2024-11-26 13:21:42 +01:00
closeNav();
2024-11-26 10:19:13 +01:00
});
navOverlay.addEventListener("click", () => {
2024-11-26 13:21:42 +01:00
closeNav();
2024-11-26 09:53:05 +01:00
});
2024-01-25 18:34:32 +01:00
});