// side.js: folds the side to its rail, or opens it, in place (estilo // side). Optional: without it the fold's form posts the new state and the // next page is drawn folded. It sets the same cookie the form does // (data-estilo-side-cookie on the fold's form), so the next page is drawn // the same. Mapa's, copied, with focus kept and the rail's tooltips // dismissable (Astra's review of contra-joins-ring). // // A page with no frame binds nothing. A side drawn with no fold // (Side.Fold nil) has nothing to fold, so ⌘\ is left alone, but its // rail's tooltips still dismiss on Escape. (function () { "use strict"; var ITEM = "[estilo-side-item]"; // shown is what the side shows now: the frame's state, or what CSS // chose by width when there is none (open from 80rem). function shown(frame) { var state = (frame.getAttribute("estilo-side-frame") || "").split(/\s+/); if (state.indexOf("open") >= 0) return "open"; if (state.indexOf("rail") >= 0) return "rail"; return window.matchMedia("(min-width: 80rem)").matches ? "open" : "rail"; } // returnTo focuses a menu's title; in a menubar with a roving tab stop // the stop moves with it, as chrome.js does on Escape. function returnTo(title) { title.focus(); var bar = title.closest("[estilo-menubar]"); if (!bar) return; var titles = Array.prototype.slice.call(bar.querySelectorAll("[estilo-menubar-title]")); if (!titles.some(function (t) { return t.hasAttribute("tabindex"); })) return; titles.forEach(function (t) { t.tabIndex = t === title ? 0 : -1; }); } // replacement is the control that takes focus from active when the side // turns to next, or null when active stays on screen. The fold's two // buttons swap; a place drawn twice (estilo-side-holds) swaps for its // other copy; a page row, which the rail hides, gives way to the lit // place that holds the page, or else to the fold. function replacement(side, form, active, next) { if (!active || !side.contains(active)) return null; var other = next === "rail" ? "open" : "rail"; if (active.hasAttribute("estilo-side-fold-to")) { return form.querySelector('[estilo-side-fold-to="' + other + '"]'); } var holds = active.getAttribute("estilo-side-holds"); if (holds && holds !== next) { var href = active.getAttribute("href"); return Array.prototype.filter.call(side.querySelectorAll('[estilo-side-holds="' + next + '"]'), function (copy) { return copy.getAttribute("href") === href; })[0] || null; } if (next === "rail" && active.hasAttribute("estilo-side-page")) { return side.querySelector('[estilo-side-holds="rail"]') || form.querySelector('[estilo-side-fold-to="open"]'); } return null; } // tooltips lets Escape dismiss the rail's tooltip, on the focused row // and the hovered one, leaving focus where it is. It stays dismissed // until a new hover or focus. Bound wherever there is a side, folded // or not: the width's rail has tooltips too. function tooltips(side) { document.addEventListener("keydown", function (event) { if (event.key !== "Escape" || event.defaultPrevented) return; var active = document.activeElement; side.querySelectorAll(ITEM).forEach(function (item) { if (item === active || item.matches(":hover")) item.setAttribute("data-estilo-side-tip", "off"); }); }); side.querySelectorAll(ITEM).forEach(function (item) { var again = function () { item.removeAttribute("data-estilo-side-tip"); }; item.addEventListener("pointerenter", again); item.addEventListener("focus", again); }); } function start() { var frame = document.querySelector("[estilo-side-frame]"); var side = frame && frame.querySelector("[estilo-side]"); if (!side) return; tooltips(side); // Folding needs the fold (Side.Fold); without it nothing folds and // the shortcut is left alone. var form = side.querySelector("form[estilo-side-fold]"); if (!form) return; // fold turns the side; from is the control that asked, if any. function fold(from) { var active = document.activeElement; var next = shown(frame) === "open" ? "rail" : "open"; var target = replacement(side, form, active, next); frame.setAttribute("estilo-side-frame", next); var cookie = form.getAttribute("data-estilo-side-cookie"); if (cookie) { document.cookie = cookie + "=" + next + "; path=/; max-age=31536000; samesite=lax" + (location.protocol === "https:" ? "; secure" : ""); } var menu = from && from.closest("details"); if (menu) { // A menu row: the menu closes and focus goes back to its title. menu.open = false; var title = menu.querySelector("summary"); if (title) returnTo(title); } else if (target) { target.focus(); } } // The fold's own buttons, and any control an app marks (a View menu // row): each folds in place instead of posting. document.querySelectorAll("[data-estilo-side-fold]").forEach(function (control) { control.disabled = false; control.addEventListener("click", function (event) { event.preventDefault(); fold(control); }); }); document.addEventListener("keydown", function (event) { if ((event.metaKey || event.ctrlKey) && event.key === "\\") { event.preventDefault(); fold(null); } }); } if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", start); } else { start(); } })();