ldc-nature-eleveurs/resources/ts/fo/base/common.ts
2024-12-06 10:43:27 +01:00

173 lines
6.6 KiB
TypeScript

import Swal from "sweetalert2";
document.addEventListener("DOMContentLoaded", function () {
let windoOffset = 0;
const offsetLimit = 150;
const arrow = document.querySelectorAll(".arrow") as NodeListOf<HTMLAnchorElement>;
const arrowUp = document.querySelector(".arrow-up") as HTMLAnchorElement|null;
const arrowDown = document.querySelector(".arrow-down") as HTMLAnchorElement|null;
const demarche = document.querySelector("#demarche") as HTMLDivElement|null;
const engagements = document.querySelector("#engagements") as HTMLDivElement|null;
const elevages = document.querySelector("#elevages") as HTMLDivElement|null;
const data = document.querySelector("#data") as HTMLDivElement|null;
const footer = document.querySelector("#footer") as HTMLDivElement|null;
const navBg = document.querySelector("#navigation div") as HTMLDivElement|null;
const navLogo = document.querySelector("#navigation img") as HTMLImageElement|null;
const menu = document.querySelector("#navigation ul") as HTMLUListElement|null;
const menuBtnOpen = document.querySelector(".nav-btn-open") as HTMLImageElement|null;
const menuBtnClose = document.querySelector(".nav-btn-close") as HTMLImageElement|null;
const menuTitle = document.querySelectorAll("#navigation ul li") as NodeListOf<HTMLLIElement>;
const footerMent = document.querySelector("#footer .mentions") as HTMLLIElement|null;
const footerPoli = document.querySelector("#footer .politique") as HTMLLIElement|null;
const cnPoli = document.querySelector("#cn-wrapper .politique") as HTMLLIElement|null;
bindEvent();
arrowChangeLink();
function windowOffset() {
windoOffset = window.pageYOffset;
}
function arrowChangeLink() {
windowOffset();
if (demarche !== null && windoOffset < demarche.offsetTop - offsetLimit) {
arrowLinkToDemarche();
} else if (
engagements !== null &&
windoOffset <
engagements.offsetTop - offsetLimit
) {
arrowLinkToEngagements();
} else if (elevages != null && windoOffset < elevages.offsetTop - offsetLimit) {
arrowLinkToElevages();
} else if (data != null && windoOffset < data.offsetTop - offsetLimit) {
arrowLinkToData();
} else if (footer != null && windoOffset < footer.offsetTop - offsetLimit) {
arrowLinkToFooter();
}
}
function checkLogoReduced() {
windowOffset();
if (windoOffset > 0) {
navLogo?.classList.add("img-reduced");
navBg?.classList.remove("background");
navBg?.classList.add("plain-background");
} else {
navLogo?.classList.remove("img-reduced");
navBg?.classList.add("background");
navBg?.classList.remove("plain-background");
}
}
function bindEvent() {
for (let i = 0; i < arrow.length; i++) {
arrow[i].addEventListener("click", arrowChangeLink);
}
window.addEventListener("scroll", checkLogoReduced);
menuBtnOpen?.addEventListener("click", menuOpen);
menuBtnClose?.addEventListener("click", menuClose);
for (let i = 0; i < menuTitle.length; i++) {
menuTitle[i].addEventListener("click", menuClose);
}
footerMent?.addEventListener("click", popinMentions);
footerPoli?.addEventListener("click", popinPolitique);
cnPoli?.addEventListener("click", popinPolitique);
}
function popinMentions() {
Swal.fire({
template: "#popin-mention",
showCloseButton: true,
width: "50rem",
confirmButtonColor: "var(--yellow)",
});
}
function popinPolitique () {
Swal.fire({
template: "#popin-politique",
showCloseButton: true,
width: "50rem",
confirmButtonColor: "var(--yellow)",
});
}
function menuOpen() {
menu?.classList.add("menu-activated");
navLogo?.classList.remove("img-reduced");
menuBtnClose?.classList.remove("vishidden");
}
function menuClose() {
menuBtnClose?.classList.add("vishidden");
menu?.classList.remove("menu-activated");
navLogo?.classList.remove("img-reduced");
}
function arrowLinkToDemarche() {
if (arrowUp !== null && arrowDown !== null) {
arrowUp.href = "#";
arrowDown.href = "#demarche";
}
}
function arrowLinkToEngagements() {
if (arrowUp !== null && arrowDown !== null) {
arrowUp.href = "#";
arrowDown.href = "#engagements";
}
}
function arrowLinkToElevages() {
if (arrowUp !== null && arrowDown !== null) {
arrowUp.href = "#demarche";
arrowDown.href = "#elevages";
}
}
function arrowLinkToData() {
if (arrowUp !== null && arrowDown !== null) {
arrowUp.href = "#engagements";
arrowDown.href = "#data";
}
}
function arrowLinkToFooter() {
if (arrowUp !== null && arrowDown !== null) {
arrowUp.href = "#elevages";
arrowDown.href = "#footer";
}
}
// * Video frames.
const videoFrameList = document.querySelectorAll(
".video-frame"
) as NodeListOf<HTMLLinkElement>;
for (const videoLink of videoFrameList) {
if (!videoLink.dataset.type) {
throw new Error("Missing dataset type");
}
if (!videoLink.dataset.frame) {
throw new Error("Missing dataset frame");
}
// @ts-ignore
window.__CN_VIDEO = window.__CN_VIDEO ?? {};
const newIframe = document.createElement("iframe");
newIframe.frameBorder = "0";
newIframe.allow = "fullscreen";
newIframe.width = "1920px";
newIframe.height = "1080px";
newIframe.setAttribute("allowfullscreen", "allowfullscreen");
// @ts-ignore
const instance = new FsLightbox();
instance.props.sources = [newIframe];
setTimeout(() => {
videoLink.addEventListener("click", (e) => {
e.preventDefault();
// * This comes fslightbox lib
// @ts-ignore
instance.open();
setTimeout(function () {
// @ts-ignore
newIframe.src = window.__CN_VIDEO[videoLink.dataset.type]
? String(videoLink.href)
: String(videoLink.dataset.frame);
}, 50);
return false;
});
}, 300);
}
});