/* Progressive enhancement only: every page works without this file. */ (function () { "use strict"; /* The band's active tab: the longest link that prefixes the path. */ var p = location.pathname, best = null; document.querySelectorAll(".band a").forEach(function (a) { var h = a.getAttribute("href"); if (h !== "/" && (p === h || p.indexOf(h + "/") === 0) && (!best || h.length > best.getAttribute("href").length)) best = a; }); if (best) best.classList.add("on"); /* Off-the-shelf editors, loaded only where a page asks for them: CodeMirror 6 over textarea.code, EasyMDE over textarea.md. */ function load(src, css) { return new Promise(function (ok) { if (css) { var l = document.createElement("link"); l.rel = "stylesheet"; l.href = css; document.head.appendChild(l); } var s = document.createElement("script"); s.src = src; s.onload = ok; document.head.appendChild(s); }); } if (document.querySelector("textarea.code")) load("/static/vendor/editor.js"); var mds = document.querySelectorAll("textarea.md"); if (mds.length) load("/static/vendor/easymde.min.js", "/static/vendor/easymde.min.css").then(function () { var E = window.EasyMDE; function b(name, action, text, title) { return { name: name, action: action, text: text, title: title, className: "mde-btn" }; } var toolbar = [b("bold", E.toggleBold, "B", "Bold"), b("italic", E.toggleItalic, "I", "Italic"), b("heading", E.toggleHeadingSmaller, "H", "Heading"), "|", b("code", E.toggleCodeBlock, "", "Code"), b("quote", E.toggleBlockquote, "“", "Quote"), b("ul", E.toggleUnorderedList, "•", "List"), b("ol", E.toggleOrderedList, "1.", "Numbered list"), "|", b("link", E.drawLink, "link", "Link"), "|", b("preview", E.togglePreview, "preview", "Preview")]; mds.forEach(function (t) { new E({ element: t, spellChecker: false, status: false, forceSync: true, autoDownloadFontAwesome: false, toolbar: toolbar }); }); }); /* Theme switch in the header: dark, light, auto (follows the system); kept in the theme cookie. */ var toggle = document.querySelector("[data-theme-toggle]"); if (toggle) { var root = document.documentElement, order = ["dark", "light", "auto"], glyph = { dark: "◐", light: "☀", auto: "◑" }; var show = function () { var p = root.dataset.themePref || "dark"; toggle.title = "Theme: " + p + " (click to change)"; toggle.setAttribute("aria-label", toggle.title); toggle.firstElementChild.textContent = glyph[p]; }; show(); toggle.addEventListener("click", function () { var p = order[(order.indexOf(root.dataset.themePref || "dark") + 1) % order.length]; document.cookie = "theme=" + p + "; path=/; max-age=31536000; samesite=lax"; root.dataset.themePref = p; root.dataset.theme = p === "auto" ? (window.matchMedia && matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark") : p; show(); }); } /* Copy buttons. */ document.querySelectorAll("[data-copy]").forEach(function (b) { b.addEventListener("click", function () { navigator.clipboard.writeText(b.getAttribute("data-copy")).then(function () { var t = b.textContent; b.textContent = "copied"; setTimeout(function () { b.textContent = t; }, 1200); }); }); }); /* Tree rows: the last change per entry, fetched after the page paints. */ var tree = document.querySelector("table.tree[data-last]"); if (tree) { var paths = [].map.call(tree.querySelectorAll("tr[data-path]"), function (r) { return r.getAttribute("data-path"); }); if (paths.length) { fetch(tree.getAttribute("data-last") + "?paths=" + encodeURIComponent(paths.join(",")), { credentials: "same-origin" }) .then(function (r) { return r.json(); }) .then(function (rows) { rows.forEach(function (c) { var tr = tree.querySelector('tr[data-path="' + CSS.escape(c.path) + '"]'); if (!tr) return; var a = tr.querySelector("td.last a"), t = tr.querySelector("td.age"); if (a) { a.textContent = c.title; a.href = tree.getAttribute("data-change") + c.hash; } if (t) { t.textContent = c.ago; t.title = c.when; } }); }).catch(function () {}); } } /* Line anchors: #L12 or #L12-L20 highlight a range and clicking a number selects. */ function mark() { var m = location.hash.match(/^#L(\d+)(?:-L(\d+))?$/); document.querySelectorAll("tr.hl").forEach(function (r) { r.classList.remove("hl"); }); if (!m) return; var a = +m[1], b = +(m[2] || m[1]); for (var i = Math.min(a, b); i <= Math.max(a, b); i++) { var row = document.getElementById("L" + i); if (row) row.classList.add("hl"); } var first = document.getElementById("L" + Math.min(a, b)); if (first && !mark.done) { first.scrollIntoView({ block: "center" }); mark.done = true; } } window.addEventListener("hashchange", mark); mark(); document.querySelectorAll("table.code td.n").forEach(function (td) { td.addEventListener("click", function (e) { var n = td.parentNode.id; if (e.shiftKey && location.hash.match(/^#L(\d+)/)) location.hash = location.hash.replace(/^#(L\d+).*/, "$1-" + n); else location.hash = n; }); }); /* Keyboard: / focuses search; g c / g p / g i jump; ? shows the sheet. */ var pending = null; document.addEventListener("keydown", function (e) { if (e.target && /^(INPUT|TEXTAREA|SELECT)$/.test(e.target.tagName)) return; if (e.key === "/") { var s = document.querySelector("input[name=q]"); if (s) { e.preventDefault(); s.focus(); } return; } if (e.key === "?") { var k = document.getElementById("keys"); if (k) k.hidden = !k.hidden; return; } var repo = document.body.getAttribute("data-repo"); if (pending === "g" && repo) { var to = { c: "", p: "/proposals", i: "/issues", h: "/changes/" + (document.body.getAttribute("data-ref") || "main"), b: "/bookmarks", t: "/trust" }[e.key]; if (to !== undefined) location.href = repo + to; pending = null; return; } pending = e.key === "g" ? "g" : null; }); })();