/* AGI docs behaviour: theme, navigation drawer, code block tools, the "On this page" highlight and the search palette. */ (function () { "use strict"; var doc = document.documentElement; var body = document.body; var ROOT = body.getAttribute("data-root") || ""; var isMac = /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent); function $(sel, el) { return (el || document).querySelector(sel); } function $$(sel, el) { return Array.prototype.slice.call((el || document).querySelectorAll(sel)); } // ---------- toast ---------- var toastEl = null, toastTimer = 0; function toast(msg) { if (!toastEl) { toastEl = document.createElement("div"); toastEl.className = "toast"; toastEl.setAttribute("role", "status"); body.appendChild(toastEl); } toastEl.textContent = msg; toastEl.classList.add("show"); clearTimeout(toastTimer); toastTimer = setTimeout(function () { toastEl.classList.remove("show"); }, 1600); } window.AGIDocs = { toast: toast }; function copyText(text) { var done = function () { toast("Copied to the clipboard"); }; if (navigator.clipboard && window.isSecureContext) { navigator.clipboard.writeText(text).then(done, function () { fallbackCopy(text); done(); }); } else { fallbackCopy(text); done(); } } function fallbackCopy(text) { var ta = document.createElement("textarea"); ta.value = text; ta.style.position = "fixed"; ta.style.opacity = "0"; body.appendChild(ta); ta.select(); try { document.execCommand("copy"); } catch (e) { /* nothing more to try */ } body.removeChild(ta); } window.AGIDocs.copy = copyText; // ---------- theme ---------- var media = window.matchMedia("(prefers-color-scheme: dark)"); function applyTheme(pref) { doc.setAttribute("data-theme-pref", pref); doc.setAttribute("data-theme", pref === "auto" ? (media.matches ? "dark" : "light") : pref); $$("[data-theme-set]").forEach(function (b) { b.setAttribute("aria-checked", b.getAttribute("data-theme-set") === pref ? "true" : "false"); }); document.dispatchEvent(new CustomEvent("agi-theme")); } applyTheme(doc.getAttribute("data-theme-pref") || "auto"); media.addEventListener("change", function () { if (doc.getAttribute("data-theme-pref") === "auto") applyTheme("auto"); }); $$("[data-theme-set]").forEach(function (b) { b.addEventListener("click", function () { var pref = b.getAttribute("data-theme-set"); try { localStorage.setItem("aroz-theme", pref); } catch (e) { /* private mode */ } applyTheme(pref); closePops(); }); }); // ---------- pop menus ---------- function closePops(except) { $$("[data-pop].open").forEach(function (p) { if (p !== except) { p.classList.remove("open"); var b = p.querySelector("button"); if (b) b.setAttribute("aria-expanded", "false"); } }); } $$("[data-pop]").forEach(function (p) { var btn = p.querySelector("button"); btn.addEventListener("click", function (e) { e.stopPropagation(); var open = !p.classList.contains("open"); closePops(p); p.classList.toggle("open", open); btn.setAttribute("aria-expanded", open ? "true" : "false"); }); }); document.addEventListener("click", function () { closePops(); }); // ---------- header and drawer ---------- var top = $("#top"); if (top && doc.classList.contains("kind-home")) { var onScroll = function () { top.classList.toggle("scrolled", window.scrollY > 8); }; window.addEventListener("scroll", onScroll, { passive: true }); onScroll(); } var sideBtn = $(".side-btn"); function setSide(open) { doc.classList.toggle("side-open", open); if (sideBtn) sideBtn.setAttribute("aria-expanded", open ? "true" : "false"); } if (sideBtn) sideBtn.addEventListener("click", function () { setSide(!doc.classList.contains("side-open")); }); $$("[data-side-close]").forEach(function (el) { el.addEventListener("click", function () { setSide(false); }); }); var current = $(".side a[aria-current]"); if (current) current.scrollIntoView({ block: "center" }); $$("[data-kbd]").forEach(function (k) { k.textContent = isMac ? "⌘K" : "Ctrl K"; }); $$("[data-copy]").forEach(function (b) { b.addEventListener("click", function () { copyText(b.getAttribute("data-copy")); }); }); // ---------- code blocks ---------- function encodeCode(code) { var b = btoa(unescape(encodeURIComponent(code))); return b.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); } window.AGIDocs.encodeCode = encodeCode; var playIcon = ''; var copyIcon = ''; $$("pre > code").forEach(function (code) { var pre = code.parentNode; if (pre.closest(".pg")) return; var m = /language-(\w+)/.exec(code.className); var lang = m ? m[1] : ""; var src = code.textContent; if (window.AGIHL && lang) code.innerHTML = window.AGIHL.highlight(src, lang); var box = document.createElement("div"); box.className = "code-box"; pre.parentNode.insertBefore(box, pre); box.appendChild(pre); var tools = document.createElement("div"); tools.className = "code-tools"; var isJS = lang === "javascript" || lang === "js"; if (isJS) { var run = document.createElement("a"); run.className = "run"; run.href = ROOT + "playground.html#code=" + encodeCode(src); run.innerHTML = playIcon + "Run"; run.title = "Open this code in the playground"; tools.appendChild(run); } var cp = document.createElement("button"); cp.type = "button"; cp.innerHTML = copyIcon + "Copy"; cp.addEventListener("click", function () { copyText(src); }); tools.appendChild(cp); box.appendChild(tools); }); $$(".prose table").forEach(function (t) { if (t.parentNode.classList.contains("table-wrap")) return; var w = document.createElement("div"); w.className = "table-wrap"; t.parentNode.insertBefore(w, t); w.appendChild(t); }); // ---------- "On this page" ---------- var tocLinks = $$("[data-toc]"); if (tocLinks.length && "IntersectionObserver" in window) { var targets = tocLinks.map(function (a) { return document.getElementById(a.getAttribute("data-toc")); }).filter(Boolean); var visible = {}; var setOn = function (id) { tocLinks.forEach(function (a) { a.classList.toggle("on", a.getAttribute("data-toc") === id); }); var on = $(".toc a.on"); var box = $(".toc"); if (on && box) { var r = on.getBoundingClientRect(), br = box.getBoundingClientRect(); if (r.top < br.top + 40 || r.bottom > br.bottom - 40) box.scrollTop += r.top - br.top - br.height / 2; } }; var io = new IntersectionObserver(function (entries) { entries.forEach(function (e) { visible[e.target.id] = e.isIntersecting; }); for (var i = 0; i < targets.length; i++) { if (visible[targets[i].id]) { setOn(targets[i].id); return; } } }, { rootMargin: "-64px 0px -60% 0px" }); targets.forEach(function (t) { io.observe(t); }); } // Briefly outline the function card a link pointed to. function flashTarget() { var id = decodeURIComponent(location.hash.slice(1)); if (!id) return; var el = document.getElementById(id); if (!el) return; var card = el.classList.contains("fn") ? el : el.closest(".fn"); if (!card) return; card.classList.add("flash"); setTimeout(function () { card.classList.remove("flash"); }, 1400); } window.addEventListener("hashchange", flashTarget); flashTarget(); // ---------- search ---------- var search = $("#search"), q = $("#search-q"), res = $("#search-res"); var index = null, loading = false, hits = [], sel = 0, lastFocus = null; if (res) res.setAttribute("data-empty", "Type to search functions, libraries and guides."); function loadIndex() { if (index || loading) return; loading = true; fetch(ROOT + "search.json?v=" + body.getAttribute("data-ver")) .then(function (r) { return r.json(); }) .then(function (data) { index = data; loading = false; runSearch(); }) .catch(function () { loading = false; res.setAttribute("data-empty", "The search index could not be loaded."); }); } function openSearch() { if (!search) return; lastFocus = document.activeElement; search.hidden = false; body.classList.add("noscroll"); q.value = ""; runSearch(); loadIndex(); setTimeout(function () { q.focus(); }, 0); } function closeSearch() { if (!search || search.hidden) return; search.hidden = true; body.classList.remove("noscroll"); if (lastFocus && lastFocus.focus) lastFocus.focus(); } window.AGIDocs.openSearch = openSearch; function score(e, terms, whole) { var t = e.t.toLowerCase(); var short = t.slice(t.lastIndexOf(".") + 1); var s = 0; if (t === whole || short === whole) s = 100; else if (t.indexOf(whole) === 0 || short.indexOf(whole) === 0) s = 80; else if (t.indexOf(whole) >= 0) s = 60; else { var hay = (t + " " + e.s + " " + (e.d || "")).toLowerCase(); for (var i = 0; i < terms.length; i++) { if (hay.indexOf(terms[i]) < 0) return 0; } s = t.indexOf(terms[0]) >= 0 ? 35 : 15; } if (e.k === "page") s += 6; if (e.k === "fn") s += 3; return s - t.length / 100; } function mark(text, whole) { var i = text.toLowerCase().indexOf(whole); var E = window.AGIHL ? window.AGIHL.escape : function (x) { return x; }; if (!whole || i < 0) return E(text); return E(text.slice(0, i)) + "" + E(text.slice(i, i + whole.length)) + "" + E(text.slice(i + whole.length)); } function runSearch() { if (!res) return; var whole = q.value.trim().toLowerCase(); hits = []; if (whole && index) { var terms = whole.split(/\s+/); index.forEach(function (e) { var sc = score(e, terms, whole); if (sc > 0) hits.push({ e: e, s: sc }); }); hits.sort(function (a, b) { return b.s - a.s; }); hits = hits.slice(0, 40); } if (whole && index && !hits.length) res.setAttribute("data-empty", "Nothing matches “" + q.value.trim() + "”."); else if (!index && whole) res.setAttribute("data-empty", "Loading the index…"); else res.setAttribute("data-empty", "Type to search functions, libraries and guides."); sel = 0; res.innerHTML = hits.map(function (h, i) { var e = h.e; var k = e.k === "fn" ? "fn" : e.k === "section" ? "#" : "doc"; var label = e.k === "fn" ? "f" : e.k === "section" ? "#" : "¶"; return '