/* ArozOS website behaviour: theme, language memory, menus and the scroll effects. Edit it in scripts/sitegen/static/ and run the generator. */ (function () { "use strict"; var d = document.documentElement; var reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches; var $$ = function (sel, root) { return Array.prototype.slice.call((root || document).querySelectorAll(sel)); }; var store = { get: function (k) { try { return localStorage.getItem(k); } catch (e) { return null; } }, set: function (k, v) { try { localStorage.setItem(k, v); } catch (e) { /* private mode */ } } }; var clamp = function (v) { return v < 0 ? 0 : v > 1 ? 1 : v; }; /* ---------- Theme: light, dark or follow the system ---------- */ var dark = window.matchMedia("(prefers-color-scheme: dark)"); function applyTheme(pref) { d.setAttribute("data-theme-pref", pref); d.setAttribute("data-theme", pref === "auto" ? (dark.matches ? "dark" : "light") : pref); $$("[data-theme-set]").forEach(function (b) { b.setAttribute("aria-checked", b.getAttribute("data-theme-set") === pref ? "true" : "false"); }); } applyTheme(d.getAttribute("data-theme-pref") || "auto"); var onSystemChange = function () { if ((d.getAttribute("data-theme-pref") || "auto") === "auto") applyTheme("auto"); }; if (dark.addEventListener) dark.addEventListener("change", onSystemChange); else if (dark.addListener) dark.addListener(onSystemChange); /* ---------- Menus ---------- */ var nav = document.getElementById("nav"); var menuBtn = nav && nav.querySelector(".menu-btn"); var pops = $$("[data-pop]"); function closePops() { pops.forEach(function (p) { p.classList.remove("open"); var b = p.querySelector("button"); if (b) b.setAttribute("aria-expanded", "false"); }); } function setSheet(open) { if (!nav || !menuBtn) return; nav.classList.toggle("open", open); menuBtn.setAttribute("aria-expanded", open ? "true" : "false"); document.body.classList.toggle("noscroll", open); } pops.forEach(function (p) { var btn = p.querySelector("button"); btn.addEventListener("click", function (e) { e.stopPropagation(); var open = !p.classList.contains("open"); closePops(); if (open) { p.classList.add("open"); btn.setAttribute("aria-expanded", "true"); } }); }); if (menuBtn) { menuBtn.addEventListener("click", function () { setSheet(!nav.classList.contains("open")); }); } document.addEventListener("click", function (e) { var t = e.target.closest ? e.target : e.target.parentElement; var themeBtn = t && t.closest("[data-theme-set]"); if (themeBtn) { var pref = themeBtn.getAttribute("data-theme-set"); store.set("aroz-theme", pref); applyTheme(pref); closePops(); return; } var langLink = t && t.closest("[data-lang]"); if (langLink) { store.set("aroz-lang", langLink.getAttribute("data-lang")); if (location.hash) langLink.setAttribute("href", langLink.getAttribute("href") + location.hash); return; } if (!(t && t.closest("[data-pop]"))) closePops(); }); document.addEventListener("keydown", function (e) { if (e.key === "Escape") { closePops(); setSheet(false); } }); window.addEventListener("resize", function () { if (window.innerWidth > 1000) setSheet(false); }); /* ---------- Reveal on scroll ---------- */ var reveal = $$(".rv"); if ("IntersectionObserver" in window && !reduce) { var io = new IntersectionObserver(function (entries) { entries.forEach(function (en) { if (!en.isIntersecting) return; en.target.classList.add("in"); io.unobserve(en.target); $$("[data-count]", en.target).forEach(countUp); }); }, { rootMargin: "0px 0px -8% 0px", threshold: 0.12 }); reveal.forEach(function (el) { io.observe(el); }); } else { reveal.forEach(function (el) { el.classList.add("in"); }); } /* Count a statistic up from zero, keeping any prefix and suffix. */ function countUp(el) { if (reduce || el.dataset.done) return; el.dataset.done = "1"; var m = /^(\D*)(\d+)(.*)$/.exec(el.textContent.trim()); if (!m) return; var target = parseInt(m[2], 10); if (target < 2) return; var start = null; var step = function (ts) { if (start === null) start = ts; var p = Math.min(1, (ts - start) / 1300); var eased = 1 - Math.pow(1 - p, 3); el.textContent = m[1] + Math.round(target * eased) + m[3]; if (p < 1) requestAnimationFrame(step); }; el.textContent = m[1] + "0" + m[3]; requestAnimationFrame(step); } /* ---------- Scroll-driven effects ---------- */ var scaled = $$("[data-scale]"); var shows = $$(".showcase").map(function (s) { return { track: s.querySelector(".sc-track"), imgs: $$(".sc-img", s), caps: $$(".sc-cap", s), dots: $$(".sc-dots span", s), n: $$(".sc-cap", s).length, at: 0 }; }); /* Split statement text into words (or single CJK characters) so they can light up one by one. */ var cjk = "⺀-鿿豈-﫿＀-￯ -〿"; var tokenRe = new RegExp("[" + cjk + "][、。,.:;!?」』]?|[^\\s" + cjk + "]+|\\s+", "g"); var lits = $$("[data-lit]").map(function (el) { var walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT); var nodes = []; var n; while ((n = walker.nextNode())) nodes.push(n); nodes.forEach(function (t) { var frag = document.createDocumentFragment(); (t.nodeValue.match(tokenRe) || []).forEach(function (tok) { if (/^\s+$/.test(tok)) { frag.appendChild(document.createTextNode(tok)); return; } var s = document.createElement("span"); s.className = "w"; s.textContent = tok; frag.appendChild(s); }); t.parentNode.replaceChild(frag, t); }); return { el: el, words: $$(".w", el), lit: -1 }; }); function setStep(sh, i) { if (i === sh.at) return; sh.at = i; [sh.imgs, sh.caps, sh.dots].forEach(function (list) { list.forEach(function (el, j) { el.classList.toggle("on", j === i); }); }); } var ticking = false; function update() { ticking = false; var vh = window.innerHeight; var navH = nav ? nav.offsetHeight : 0; if (nav) nav.classList.toggle("scrolled", window.scrollY > 4); scaled.forEach(function (el) { var r = el.getBoundingClientRect(); if (r.bottom < -100 || r.top > vh + 100) return; var p = el.getAttribute("data-scale") === "hero" ? clamp(window.scrollY / (vh * 0.55)) : clamp((vh - r.top) / (vh * 0.7)); el.style.setProperty("--p", reduce ? 1 : p.toFixed(3)); }); shows.forEach(function (sh) { if (!sh.track || sh.n < 2) return; var r = sh.track.getBoundingClientRect(); var room = r.height - (vh - navH); var p = room > 0 ? clamp((navH - r.top) / room) : 0; setStep(sh, Math.min(sh.n - 1, Math.floor(p * sh.n))); }); lits.forEach(function (l) { var r = l.el.getBoundingClientRect(); var p = reduce ? 1 : clamp((vh * 0.88 - r.top) / (r.height + vh * 0.38)); var count = Math.round(p * l.words.length); if (count === l.lit) return; l.lit = count; l.words.forEach(function (w, i) { w.classList.toggle("on", i < count); }); }); } function onScroll() { if (!ticking) { ticking = true; requestAnimationFrame(update); } } window.addEventListener("scroll", onScroll, { passive: true }); window.addEventListener("resize", onScroll); update(); /* ---------- Copy buttons ---------- */ $$("[data-copy]").forEach(function (btn) { btn.addEventListener("click", function () { var code = btn.parentNode.querySelector("code"); var text = code ? code.textContent : ""; var done = function () { btn.classList.add("done"); setTimeout(function () { btn.classList.remove("done"); }, 1600); }; if (navigator.clipboard && window.isSecureContext) { navigator.clipboard.writeText(text).then(done, function () {}); } else { var r = document.createRange(); r.selectNodeContents(code); var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(r); try { document.execCommand("copy"); done(); } catch (e) { /* user can copy the selection */ } } }); }); /* ---------- Footer year ---------- */ $$("[data-year]").forEach(function (el) { el.textContent = String(new Date().getFullYear()); }); })();