site.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ArozOS website behaviour: theme, language memory, menus and the scroll
  3. effects. Edit it in scripts/sitegen/static/ and run the generator.
  4. */
  5. (function () {
  6. "use strict";
  7. var d = document.documentElement;
  8. var reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
  9. var $$ = function (sel, root) { return Array.prototype.slice.call((root || document).querySelectorAll(sel)); };
  10. var store = {
  11. get: function (k) { try { return localStorage.getItem(k); } catch (e) { return null; } },
  12. set: function (k, v) { try { localStorage.setItem(k, v); } catch (e) { /* private mode */ } }
  13. };
  14. var clamp = function (v) { return v < 0 ? 0 : v > 1 ? 1 : v; };
  15. /* ---------- Theme: light, dark or follow the system ---------- */
  16. var dark = window.matchMedia("(prefers-color-scheme: dark)");
  17. function applyTheme(pref) {
  18. d.setAttribute("data-theme-pref", pref);
  19. d.setAttribute("data-theme", pref === "auto" ? (dark.matches ? "dark" : "light") : pref);
  20. $$("[data-theme-set]").forEach(function (b) {
  21. b.setAttribute("aria-checked", b.getAttribute("data-theme-set") === pref ? "true" : "false");
  22. });
  23. }
  24. applyTheme(d.getAttribute("data-theme-pref") || "auto");
  25. var onSystemChange = function () {
  26. if ((d.getAttribute("data-theme-pref") || "auto") === "auto") applyTheme("auto");
  27. };
  28. if (dark.addEventListener) dark.addEventListener("change", onSystemChange);
  29. else if (dark.addListener) dark.addListener(onSystemChange);
  30. /* ---------- Menus ---------- */
  31. var nav = document.getElementById("nav");
  32. var menuBtn = nav && nav.querySelector(".menu-btn");
  33. var pops = $$("[data-pop]");
  34. function closePops() {
  35. pops.forEach(function (p) {
  36. p.classList.remove("open");
  37. var b = p.querySelector("button");
  38. if (b) b.setAttribute("aria-expanded", "false");
  39. });
  40. }
  41. function setSheet(open) {
  42. if (!nav || !menuBtn) return;
  43. nav.classList.toggle("open", open);
  44. menuBtn.setAttribute("aria-expanded", open ? "true" : "false");
  45. document.body.classList.toggle("noscroll", open);
  46. }
  47. pops.forEach(function (p) {
  48. var btn = p.querySelector("button");
  49. btn.addEventListener("click", function (e) {
  50. e.stopPropagation();
  51. var open = !p.classList.contains("open");
  52. closePops();
  53. if (open) {
  54. p.classList.add("open");
  55. btn.setAttribute("aria-expanded", "true");
  56. }
  57. });
  58. });
  59. if (menuBtn) {
  60. menuBtn.addEventListener("click", function () { setSheet(!nav.classList.contains("open")); });
  61. }
  62. document.addEventListener("click", function (e) {
  63. var t = e.target.closest ? e.target : e.target.parentElement;
  64. var themeBtn = t && t.closest("[data-theme-set]");
  65. if (themeBtn) {
  66. var pref = themeBtn.getAttribute("data-theme-set");
  67. store.set("aroz-theme", pref);
  68. applyTheme(pref);
  69. closePops();
  70. return;
  71. }
  72. var langLink = t && t.closest("[data-lang]");
  73. if (langLink) {
  74. store.set("aroz-lang", langLink.getAttribute("data-lang"));
  75. if (location.hash) langLink.setAttribute("href", langLink.getAttribute("href") + location.hash);
  76. return;
  77. }
  78. if (!(t && t.closest("[data-pop]"))) closePops();
  79. });
  80. document.addEventListener("keydown", function (e) {
  81. if (e.key === "Escape") { closePops(); setSheet(false); }
  82. });
  83. window.addEventListener("resize", function () {
  84. if (window.innerWidth > 1000) setSheet(false);
  85. });
  86. /* ---------- Reveal on scroll ---------- */
  87. var reveal = $$(".rv");
  88. if ("IntersectionObserver" in window && !reduce) {
  89. var io = new IntersectionObserver(function (entries) {
  90. entries.forEach(function (en) {
  91. if (!en.isIntersecting) return;
  92. en.target.classList.add("in");
  93. io.unobserve(en.target);
  94. $$("[data-count]", en.target).forEach(countUp);
  95. });
  96. }, { rootMargin: "0px 0px -8% 0px", threshold: 0.12 });
  97. reveal.forEach(function (el) { io.observe(el); });
  98. } else {
  99. reveal.forEach(function (el) { el.classList.add("in"); });
  100. }
  101. /* Count a statistic up from zero, keeping any prefix and suffix. */
  102. function countUp(el) {
  103. if (reduce || el.dataset.done) return;
  104. el.dataset.done = "1";
  105. var m = /^(\D*)(\d+)(.*)$/.exec(el.textContent.trim());
  106. if (!m) return;
  107. var target = parseInt(m[2], 10);
  108. if (target < 2) return;
  109. var start = null;
  110. var step = function (ts) {
  111. if (start === null) start = ts;
  112. var p = Math.min(1, (ts - start) / 1300);
  113. var eased = 1 - Math.pow(1 - p, 3);
  114. el.textContent = m[1] + Math.round(target * eased) + m[3];
  115. if (p < 1) requestAnimationFrame(step);
  116. };
  117. el.textContent = m[1] + "0" + m[3];
  118. requestAnimationFrame(step);
  119. }
  120. /* ---------- Scroll-driven effects ---------- */
  121. var scaled = $$("[data-scale]");
  122. var shows = $$(".showcase").map(function (s) {
  123. return {
  124. track: s.querySelector(".sc-track"),
  125. imgs: $$(".sc-img", s),
  126. caps: $$(".sc-cap", s),
  127. dots: $$(".sc-dots span", s),
  128. n: $$(".sc-cap", s).length,
  129. at: 0
  130. };
  131. });
  132. /* Split statement text into words (or single CJK characters) so they can
  133. light up one by one. */
  134. var cjk = "⺀-鿿豈-﫿＀-￯ -〿";
  135. var tokenRe = new RegExp("[" + cjk + "][、。,.:;!?」』]?|[^\\s" + cjk + "]+|\\s+", "g");
  136. var lits = $$("[data-lit]").map(function (el) {
  137. var walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
  138. var nodes = [];
  139. var n;
  140. while ((n = walker.nextNode())) nodes.push(n);
  141. nodes.forEach(function (t) {
  142. var frag = document.createDocumentFragment();
  143. (t.nodeValue.match(tokenRe) || []).forEach(function (tok) {
  144. if (/^\s+$/.test(tok)) { frag.appendChild(document.createTextNode(tok)); return; }
  145. var s = document.createElement("span");
  146. s.className = "w";
  147. s.textContent = tok;
  148. frag.appendChild(s);
  149. });
  150. t.parentNode.replaceChild(frag, t);
  151. });
  152. return { el: el, words: $$(".w", el), lit: -1 };
  153. });
  154. function setStep(sh, i) {
  155. if (i === sh.at) return;
  156. sh.at = i;
  157. [sh.imgs, sh.caps, sh.dots].forEach(function (list) {
  158. list.forEach(function (el, j) { el.classList.toggle("on", j === i); });
  159. });
  160. }
  161. var ticking = false;
  162. function update() {
  163. ticking = false;
  164. var vh = window.innerHeight;
  165. var navH = nav ? nav.offsetHeight : 0;
  166. if (nav) nav.classList.toggle("scrolled", window.scrollY > 4);
  167. scaled.forEach(function (el) {
  168. var r = el.getBoundingClientRect();
  169. if (r.bottom < -100 || r.top > vh + 100) return;
  170. var p = el.getAttribute("data-scale") === "hero"
  171. ? clamp(window.scrollY / (vh * 0.55))
  172. : clamp((vh - r.top) / (vh * 0.7));
  173. el.style.setProperty("--p", reduce ? 1 : p.toFixed(3));
  174. });
  175. shows.forEach(function (sh) {
  176. if (!sh.track || sh.n < 2) return;
  177. var r = sh.track.getBoundingClientRect();
  178. var room = r.height - (vh - navH);
  179. var p = room > 0 ? clamp((navH - r.top) / room) : 0;
  180. setStep(sh, Math.min(sh.n - 1, Math.floor(p * sh.n)));
  181. });
  182. lits.forEach(function (l) {
  183. var r = l.el.getBoundingClientRect();
  184. var p = reduce ? 1 : clamp((vh * 0.88 - r.top) / (r.height + vh * 0.38));
  185. var count = Math.round(p * l.words.length);
  186. if (count === l.lit) return;
  187. l.lit = count;
  188. l.words.forEach(function (w, i) { w.classList.toggle("on", i < count); });
  189. });
  190. }
  191. function onScroll() {
  192. if (!ticking) {
  193. ticking = true;
  194. requestAnimationFrame(update);
  195. }
  196. }
  197. window.addEventListener("scroll", onScroll, { passive: true });
  198. window.addEventListener("resize", onScroll);
  199. update();
  200. /* ---------- Copy buttons ---------- */
  201. $$("[data-copy]").forEach(function (btn) {
  202. btn.addEventListener("click", function () {
  203. var code = btn.parentNode.querySelector("code");
  204. var text = code ? code.textContent : "";
  205. var done = function () {
  206. btn.classList.add("done");
  207. setTimeout(function () { btn.classList.remove("done"); }, 1600);
  208. };
  209. if (navigator.clipboard && window.isSecureContext) {
  210. navigator.clipboard.writeText(text).then(done, function () {});
  211. } else {
  212. var r = document.createRange();
  213. r.selectNodeContents(code);
  214. var sel = window.getSelection();
  215. sel.removeAllRanges();
  216. sel.addRange(r);
  217. try { document.execCommand("copy"); done(); } catch (e) { /* user can copy the selection */ }
  218. }
  219. });
  220. });
  221. /* ---------- Footer year ---------- */
  222. $$("[data-year]").forEach(function (el) { el.textContent = String(new Date().getFullYear()); });
  223. })();