| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- /*
- 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 = '<svg class="i" viewBox="0 0 24 24" aria-hidden="true" fill="currentColor"><path d="M8 5.6v12.8c0 .8.9 1.3 1.6.9l10.2-6.4c.6-.4.6-1.3 0-1.7L9.6 4.8C8.9 4.3 8 4.8 8 5.6z"/></svg>';
- var copyIcon = '<svg class="i" viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="11" height="11" rx="2.5"/><path d="M5.5 15H5a1.5 1.5 0 0 1-1.5-1.5V5A1.5 1.5 0 0 1 5 3.5h8.5A1.5 1.5 0 0 1 15 5v.5"/></svg>';
- $$("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 + "<span>Run</span>";
- run.title = "Open this code in the playground";
- tools.appendChild(run);
- }
- var cp = document.createElement("button");
- cp.type = "button";
- cp.innerHTML = copyIcon + "<span>Copy</span>";
- 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)) + "<mark>" + E(text.slice(i, i + whole.length)) + "</mark>" + 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 '<li role="option"><a href="' + ROOT + e.u + '" data-i="' + i + '" aria-selected="' + (i === 0) + '">' +
- '<span class="k ' + k + '">' + label + "</span>" +
- '<span class="t' + (e.k === "fn" ? " mono" : "") + '">' + mark(e.t, whole) + "</span>" +
- '<span class="s">' + mark(e.s + (e.d ? " · " + e.d : ""), "") + "</span></a></li>";
- }).join("");
- }
- function move(d) {
- var items = $$("a", res);
- if (!items.length) return;
- sel = (sel + d + items.length) % items.length;
- items.forEach(function (a, i) { a.setAttribute("aria-selected", i === sel ? "true" : "false"); });
- items[sel].scrollIntoView({ block: "nearest" });
- }
- if (search) {
- q.addEventListener("input", runSearch);
- q.addEventListener("keydown", function (e) {
- if (e.key === "ArrowDown") { e.preventDefault(); move(1); }
- else if (e.key === "ArrowUp") { e.preventDefault(); move(-1); }
- else if (e.key === "Enter") {
- var a = $$("a", res)[sel];
- if (a) { e.preventDefault(); closeSearch(); location.href = a.href; }
- }
- });
- search.addEventListener("click", function (e) {
- if (e.target === search) closeSearch();
- var a = e.target.closest && e.target.closest("a");
- if (a) closeSearch();
- });
- $$("[data-search-open]").forEach(function (b) {
- b.addEventListener("click", function (e) { e.stopPropagation(); openSearch(); });
- });
- }
- document.addEventListener("keydown", function (e) {
- var typing = /INPUT|TEXTAREA|SELECT/.test((e.target && e.target.tagName) || "") || (e.target && e.target.isContentEditable);
- if ((e.ctrlKey || e.metaKey) && (e.key === "k" || e.key === "K")) {
- e.preventDefault();
- if (search && search.hidden) openSearch(); else closeSearch();
- } else if (e.key === "/" && !typing && search && search.hidden) {
- e.preventDefault();
- openSearch();
- } else if (e.key === "Escape") {
- closeSearch();
- closePops();
- setSide(false);
- }
- });
- })();
|