(() => { const config = window.SOSTAVIKA_LANDING_CONFIG || {}; const localFallback = { scan: "index.html#scan", search: "index.html#search", privacy: "privacy.html", about: "about.html" }; const isLocalPreview = window.location.protocol === "file:" || window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1" || window.location.hostname === ""; const routeMap = { scan: isLocalPreview ? localFallback.scan : config.scanUrl || "/scan", search: isLocalPreview ? localFallback.search : config.searchUrl || "/search", privacy: isLocalPreview ? localFallback.privacy : config.privacyUrl || "/privacy", about: isLocalPreview ? localFallback.about : config.aboutUrl || "/about" }; const COOKIE_NAME = "sostavika_cookie_consent_v2"; const COOKIE_MAX_AGE = 60 * 60 * 24 * 180; const COOKIE_VALUES = new Set(["essential", "analytics"]); const getCookie = (name) => { const prefix = `${encodeURIComponent(name)}=`; const item = document.cookie.split("; ").find((entry) => entry.startsWith(prefix)); return item ? decodeURIComponent(item.slice(prefix.length)) : ""; }; const getConsent = () => { const value = getCookie(COOKIE_NAME); return COOKIE_VALUES.has(value) ? value : ""; }; let cookieConsent = getConsent(); const updateAnalyticsConsent = (value, mode = "update") => { const allowed = value === "analytics"; window.SOSTAVIKA_ANALYTICS_ALLOWED = allowed; if (typeof window.gtag === "function") { window.gtag("consent", mode, { analytics_storage: allowed ? "granted" : "denied", ad_storage: "denied", ad_user_data: "denied", ad_personalization: "denied" }); } window.dispatchEvent( new CustomEvent("sostavika:cookie-consent", { detail: { analytics: allowed, value: value || "unset" } }) ); }; updateAnalyticsConsent(cookieConsent, "default"); const setConsent = (value) => { if (!COOKIE_VALUES.has(value)) return; const secure = window.location.protocol === "https:" ? "; Secure" : ""; document.cookie = `${encodeURIComponent(COOKIE_NAME)}=${encodeURIComponent(value)}; Max-Age=${COOKIE_MAX_AGE}; Path=/; SameSite=Lax${secure}`; cookieConsent = value; updateAnalyticsConsent(cookieConsent); }; document.querySelectorAll("[data-landing-link]").forEach((link) => { const key = link.getAttribute("data-landing-link"); if (!routeMap[key]) return; const hash = link.hash || ""; link.setAttribute("href", `${routeMap[key]}${hash}`); }); document.querySelectorAll("[data-home-anchor]").forEach((link) => { const anchor = link.getAttribute("data-home-anchor"); const root = isLocalPreview ? "landing.html" : "/"; link.setAttribute("href", `${root}#${anchor}`); }); const trackEvent = (eventName, params = {}) => { if (!eventName || cookieConsent !== "analytics") return; if (typeof window.gtag === "function") { window.gtag("event", eventName, params); return; } window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: eventName, ...params }); }; const cookieUi = document.createElement("div"); cookieUi.innerHTML = `

Обязательные cookies нужны для работы сайта. Аналитика включается только с вашего согласия.

`; document.body.append(...cookieUi.children); const cookieBanner = document.querySelector("[data-cookie-banner]"); const cookieDialog = document.querySelector("[data-cookie-dialog]"); const analyticsToggle = document.querySelector("[data-cookie-analytics]"); let cookieReturnFocus = null; const hideCookieBanner = () => { if (cookieBanner) cookieBanner.hidden = true; }; const chooseConsent = (value) => { setConsent(value); hideCookieBanner(); if (cookieDialog && cookieDialog.open) cookieDialog.close(); }; const openCookieSettings = (opener) => { if (!cookieDialog) return; cookieReturnFocus = opener || document.activeElement; if (analyticsToggle) analyticsToggle.checked = cookieConsent === "analytics"; cookieDialog.showModal(); window.requestAnimationFrame(() => { if (analyticsToggle) analyticsToggle.focus(); }); }; document.querySelector("[data-cookie-essential]")?.addEventListener("click", () => chooseConsent("essential")); document.querySelector("[data-cookie-accept]")?.addEventListener("click", () => chooseConsent("analytics")); document.querySelector("[data-cookie-customize]")?.addEventListener("click", (event) => openCookieSettings(event.currentTarget)); document.querySelector("[data-cookie-save]")?.addEventListener("click", () => { chooseConsent(analyticsToggle && analyticsToggle.checked ? "analytics" : "essential"); }); document.querySelector("[data-cookie-close]")?.addEventListener("click", () => cookieDialog?.close()); document.querySelectorAll("[data-cookie-settings-open]").forEach((button) => { button.addEventListener("click", () => openCookieSettings(button)); }); if (cookieDialog) { cookieDialog.addEventListener("click", (event) => { if (event.target === cookieDialog) cookieDialog.close(); }); cookieDialog.addEventListener("close", () => { if (cookieReturnFocus && typeof cookieReturnFocus.focus === "function") cookieReturnFocus.focus(); cookieReturnFocus = null; }); } if (!cookieConsent && cookieBanner) cookieBanner.hidden = false; document.querySelectorAll("[data-analytics-event]").forEach((link) => { link.addEventListener("click", () => { trackEvent(link.getAttribute("data-analytics-event"), { link_url: link.href, link_text: link.textContent.trim() }); }); }); const skipLink = document.querySelector(".skip-link"); const main = document.querySelector("#main"); if (skipLink && main) { skipLink.addEventListener("click", (event) => { event.preventDefault(); main.scrollIntoView({ block: "start" }); window.history.replaceState(null, "", "#main"); window.requestAnimationFrame(() => { main.focus({ preventScroll: true }); }); }); } const menuToggle = document.querySelector(".menu-toggle"); const menu = document.querySelector("#mobile-menu"); const menuClose = menu ? menu.querySelector("[data-menu-close]") : null; let menuReturnFocus = null; const closeMenu = () => { if (!menu || !menu.open) return; menu.close(); }; if (menuToggle && menu) { menuToggle.addEventListener("click", () => { if (menu.open) { closeMenu(); return; } menuReturnFocus = document.activeElement; menuToggle.setAttribute("aria-expanded", "true"); document.body.classList.add("menu-open"); menu.showModal(); if (menuClose) menuClose.focus(); }); if (menuClose) menuClose.addEventListener("click", closeMenu); menu.addEventListener("click", (event) => { if (event.target === menu) closeMenu(); }); menu.addEventListener("keydown", (event) => { if (event.key !== "Escape") return; event.preventDefault(); closeMenu(); }); menu.addEventListener("close", () => { menuToggle.setAttribute("aria-expanded", "false"); document.body.classList.remove("menu-open"); if (menuReturnFocus && typeof menuReturnFocus.focus === "function") { menuReturnFocus.focus(); } menuReturnFocus = null; }); menu.querySelectorAll("a").forEach((link) => { link.addEventListener("click", closeMenu); }); window.addEventListener("resize", () => { if (window.innerWidth > 1024) closeMenu(); }); } })();