oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1// side.js: folds the side to its rail, or opens it, in place (estilo
2// side). Optional: without it the fold's form posts the new state and the
3// next page is drawn folded. It sets the same cookie the form does
4// (data-estilo-side-cookie on the fold's form), so the next page is drawn
5// the same. Mapa's, copied, with focus kept and the rail's tooltips
6// dismissable (Astra's review of contra-joins-ring).
7//
8// A page with no frame binds nothing. A side drawn with no fold
9// (Side.Fold nil) has nothing to fold, so ⌘\ is left alone, but its
10// rail's tooltips still dismiss on Escape.
11(function () {
12 "use strict";
13
14 var ITEM = "[estilo-side-item]";
15
16 // shown is what the side shows now: the frame's state, or what CSS
17 // chose by width when there is none (open from 80rem).
18 function shown(frame) {
19 var state = (frame.getAttribute("estilo-side-frame") || "").split(/\s+/);
20 if (state.indexOf("open") >= 0) return "open";
21 if (state.indexOf("rail") >= 0) return "rail";
22 return window.matchMedia("(min-width: 80rem)").matches ? "open" : "rail";
23 }
24
25 // returnTo focuses a menu's title; in a menubar with a roving tab stop
26 // the stop moves with it, as chrome.js does on Escape.
27 function returnTo(title) {
28 title.focus();
29 var bar = title.closest("[estilo-menubar]");
30 if (!bar) return;
31 var titles = Array.prototype.slice.call(bar.querySelectorAll("[estilo-menubar-title]"));
32 if (!titles.some(function (t) { return t.hasAttribute("tabindex"); })) return;
33 titles.forEach(function (t) { t.tabIndex = t === title ? 0 : -1; });
34 }
35
36 // replacement is the control that takes focus from active when the side
37 // turns to next, or null when active stays on screen. The fold's two
38 // buttons swap; a place drawn twice (estilo-side-holds) swaps for its
39 // other copy; a page row, which the rail hides, gives way to the lit
40 // place that holds the page, or else to the fold.
41 function replacement(side, form, active, next) {
42 if (!active || !side.contains(active)) return null;
43 var other = next === "rail" ? "open" : "rail";
44 if (active.hasAttribute("estilo-side-fold-to")) {
45 return form.querySelector('[estilo-side-fold-to="' + other + '"]');
46 }
47 var holds = active.getAttribute("estilo-side-holds");
48 if (holds && holds !== next) {
49 var href = active.getAttribute("href");
50 return Array.prototype.filter.call(side.querySelectorAll('[estilo-side-holds="' + next + '"]'),
51 function (copy) { return copy.getAttribute("href") === href; })[0] || null;
52 }
53 if (next === "rail" && active.hasAttribute("estilo-side-page")) {
54 return side.querySelector('[estilo-side-holds="rail"]') ||
55 form.querySelector('[estilo-side-fold-to="open"]');
56 }
57 return null;
58 }
59
60 // tooltips lets Escape dismiss the rail's tooltip, on the focused row
61 // and the hovered one, leaving focus where it is. It stays dismissed
62 // until a new hover or focus. Bound wherever there is a side, folded
63 // or not: the width's rail has tooltips too.
64 function tooltips(side) {
65 document.addEventListener("keydown", function (event) {
66 if (event.key !== "Escape" || event.defaultPrevented) return;
67 var active = document.activeElement;
68 side.querySelectorAll(ITEM).forEach(function (item) {
69 if (item === active || item.matches(":hover")) item.setAttribute("data-estilo-side-tip", "off");
70 });
71 });
72 side.querySelectorAll(ITEM).forEach(function (item) {
73 var again = function () { item.removeAttribute("data-estilo-side-tip"); };
74 item.addEventListener("pointerenter", again);
75 item.addEventListener("focus", again);
76 });
77 }
78
79 function start() {
80 var frame = document.querySelector("[estilo-side-frame]");
81 var side = frame && frame.querySelector("[estilo-side]");
82 if (!side) return;
83 tooltips(side);
84 // Folding needs the fold (Side.Fold); without it nothing folds and
85 // the shortcut is left alone.
86 var form = side.querySelector("form[estilo-side-fold]");
87 if (!form) return;
88
89 // fold turns the side; from is the control that asked, if any.
90 function fold(from) {
91 var active = document.activeElement;
92 var next = shown(frame) === "open" ? "rail" : "open";
93 var target = replacement(side, form, active, next);
94 frame.setAttribute("estilo-side-frame", next);
95 var cookie = form.getAttribute("data-estilo-side-cookie");
96 if (cookie) {
97 document.cookie = cookie + "=" + next + "; path=/; max-age=31536000; samesite=lax" +
98 (location.protocol === "https:" ? "; secure" : "");
99 }
100 var menu = from && from.closest("details");
101 if (menu) {
102 // A menu row: the menu closes and focus goes back to its title.
103 menu.open = false;
104 var title = menu.querySelector("summary");
105 if (title) returnTo(title);
106 } else if (target) {
107 target.focus();
108 }
109 }
110
111 // The fold's own buttons, and any control an app marks (a View menu
112 // row): each folds in place instead of posting.
113 document.querySelectorAll("[data-estilo-side-fold]").forEach(function (control) {
114 control.disabled = false;
115 control.addEventListener("click", function (event) {
116 event.preventDefault();
117 fold(control);
118 });
119 });
120
121 document.addEventListener("keydown", function (event) {
122 if ((event.metaKey || event.ctrlKey) && event.key === "\\") {
123 event.preventDefault();
124 fold(null);
125 }
126 });
127 }
128
129 if (document.readyState === "loading") {
130 document.addEventListener("DOMContentLoaded", start);
131 } else {
132 start();
133 }
134})();
135