| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | Copyright (c) 2026 UtopiaIA |
| 3 | Elffuss Braid · braid.elffuss.com |
| 4 | The language toggle, the copy buttons, the faded edge of code blocks that |
| 5 | scroll (only those take keyboard focus), and the markers of the ratio |
| 6 | table. No dependencies, no requests. Without this file the page is |
| 7 | English and complete, every code block takes focus, and the toggle and |
| 8 | the copy buttons stay hidden. */ |
| 9 | (function () { |
| 10 | 'use strict'; |
| 11 | |
| 12 | var KEY = 'braid-lang'; |
| 13 | var root = document.documentElement; |
| 14 | // Each page's own titles: the English one is its <title>, which comes |
| 15 | // before this file in <head>; the Spanish one is data-title-es on <html>. |
| 16 | var TITLE = { |
| 17 | en: document.title, |
| 18 | es: root.getAttribute('data-title-es') || document.title |
| 19 | }; |
| 20 | var SAID = { |
| 21 | en: { copied: 'Copied', failed: 'Could not copy: the text is selected instead' }, |
| 22 | es: { copied: 'Copiado', failed: 'No se pudo copiar: el texto queda seleccionado' } |
| 23 | }; |
| 24 | |
| 25 | function valid(value) { |
| 26 | return value === 'en' || value === 'es' ? value : null; |
| 27 | } |
| 28 | function fromQuery() { |
| 29 | try { return valid(new URLSearchParams(window.location.search).get('lang')); } catch (e) { return null; } |
| 30 | } |
| 31 | function fromStore() { |
| 32 | try { return valid(window.localStorage.getItem(KEY)); } catch (e) { return null; } |
| 33 | } |
| 34 | function remember(value) { |
| 35 | try { window.localStorage.setItem(KEY, value); } catch (e) { /* private mode: fine */ } |
| 36 | } |
| 37 | function fromBrowser() { |
| 38 | var tag = (navigator.language || '').toLowerCase(); |
| 39 | return tag.indexOf('es') === 0 ? 'es' : null; |
| 40 | } |
| 41 | |
| 42 | // ?lang=, then the stored choice, then a Spanish browser, then English. |
| 43 | // Set before the body is parsed, so the other language never flashes. |
| 44 | var lang = fromQuery() || fromStore() || fromBrowser() || 'en'; |
| 45 | root.setAttribute('lang', lang); |
| 46 | |
| 47 | function each(selector, fn) { |
| 48 | var list = document.querySelectorAll(selector); |
| 49 | for (var i = 0; i < list.length; i++) fn(list[i]); |
| 50 | } |
| 51 | |
| 52 | // Boxes whose text keeps its lines and may scroll sideways. In the HTML |
| 53 | // each is a labelled region that takes keyboard focus; edges() keeps that |
| 54 | // only for the ones that do scroll at the current width. |
| 55 | var SCROLLERS = '.code pre, .term-cols pre, .table-wrap'; |
| 56 | var scrollers = []; |
| 57 | |
| 58 | function apply(value) { |
| 59 | lang = value; |
| 60 | root.setAttribute('lang', value); |
| 61 | document.title = TITLE[value]; |
| 62 | each('[data-set-lang]', function (button) { |
| 63 | button.setAttribute('aria-pressed', String(button.getAttribute('data-set-lang') === value)); |
| 64 | }); |
| 65 | each('[data-aria-' + value + ']', function (el) { |
| 66 | if (el.matches(SCROLLERS) && el.getAttribute('role') !== 'region') return; |
| 67 | el.setAttribute('aria-label', el.getAttribute('data-aria-' + value)); |
| 68 | }); |
| 69 | allEdges(); // the other language has other widths |
| 70 | } |
| 71 | |
| 72 | function keepQuery(value) { |
| 73 | try { |
| 74 | var url = new URL(window.location.href); |
| 75 | if (url.searchParams.has('lang')) { |
| 76 | url.searchParams.set('lang', value); |
| 77 | window.history.replaceState(null, '', url.toString()); |
| 78 | } |
| 79 | } catch (e) { /* old browser: the toggle still works */ } |
| 80 | } |
| 81 | |
| 82 | function say(text) { |
| 83 | var live = document.getElementById('live'); |
| 84 | if (!live) return; |
| 85 | live.textContent = ''; |
| 86 | window.setTimeout(function () { live.textContent = text; }, 40); |
| 87 | } |
| 88 | |
| 89 | function write(text) { |
| 90 | if (navigator.clipboard && window.isSecureContext) { |
| 91 | return navigator.clipboard.writeText(text); |
| 92 | } |
| 93 | return new Promise(function (resolve, reject) { |
| 94 | var area = document.createElement('textarea'); |
| 95 | area.value = text; |
| 96 | area.setAttribute('readonly', ''); |
| 97 | area.style.position = 'fixed'; |
| 98 | area.style.top = '0'; |
| 99 | area.style.left = '0'; |
| 100 | area.style.opacity = '0'; |
| 101 | document.body.appendChild(area); |
| 102 | area.select(); |
| 103 | var done = false; |
| 104 | try { done = document.execCommand('copy'); } catch (e) { done = false; } |
| 105 | document.body.removeChild(area); |
| 106 | if (done) resolve(); else reject(new Error('copy')); |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | function copy(button) { |
| 111 | var block = button.closest('.code'); |
| 112 | var code = block && block.querySelector('pre code'); |
| 113 | if (!code) return; |
| 114 | write(code.textContent).then(function () { |
| 115 | button.classList.add('copied'); |
| 116 | window.clearTimeout(button._braidTimer); |
| 117 | button._braidTimer = window.setTimeout(function () { button.classList.remove('copied'); }, 1800); |
| 118 | say(SAID[lang].copied); |
| 119 | }, function () { |
| 120 | var range = document.createRange(); |
| 121 | range.selectNodeContents(code); |
| 122 | var selection = window.getSelection(); |
| 123 | selection.removeAllRanges(); |
| 124 | selection.addRange(range); |
| 125 | say(SAID[lang].failed); |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | // A marker per row of the ratio table, drawn from the table's own cells |
| 130 | // on a log scale, with a line at 1. |
| 131 | function markers() { |
| 132 | var table = document.querySelector('#measured table.ratios'); |
| 133 | if (!table || !table.tBodies.length) return; |
| 134 | var found = []; |
| 135 | var rows = table.tBodies[0].rows; |
| 136 | for (var i = 0; i < rows.length; i++) { |
| 137 | var cell = rows[i].cells[1]; |
| 138 | var value = cell ? parseFloat(cell.textContent.replace(',', '.')) : NaN; |
| 139 | if (isFinite(value) && value > 0) found.push({ cell: cell, value: value }); |
| 140 | } |
| 141 | if (!found.length) return; |
| 142 | var values = found.map(function (f) { return f.value; }); |
| 143 | var lo = Math.log(Math.min(0.1, Math.min.apply(null, values) / 1.5)); |
| 144 | var hi = Math.log(Math.max(10, Math.max.apply(null, values) * 1.5)); |
| 145 | function at(v) { return (Math.log(v) - lo) / (hi - lo) * 100; } |
| 146 | var one = at(1); |
| 147 | found.forEach(function (f) { |
| 148 | var x = at(f.value); |
| 149 | var mark = document.createElement('span'); |
| 150 | mark.className = 'mk'; |
| 151 | mark.setAttribute('aria-hidden', 'true'); |
| 152 | var line = document.createElement('span'); |
| 153 | line.className = 'mk-one'; |
| 154 | line.style.left = one.toFixed(2) + '%'; |
| 155 | var bar = document.createElement('span'); |
| 156 | bar.className = 'mk-bar'; |
| 157 | bar.style.left = Math.min(x, one).toFixed(2) + '%'; |
| 158 | bar.style.width = Math.abs(x - one).toFixed(2) + '%'; |
| 159 | var dot = document.createElement('span'); |
| 160 | dot.className = 'mk-dot'; |
| 161 | dot.style.left = x.toFixed(2) + '%'; |
| 162 | mark.appendChild(bar); |
| 163 | mark.appendChild(line); |
| 164 | mark.appendChild(dot); |
| 165 | f.cell.appendChild(mark); |
| 166 | }); |
| 167 | var legend = document.getElementById('mk-legend'); |
| 168 | if (legend) legend.hidden = false; |
| 169 | } |
| 170 | |
| 171 | // A box that is wider than its text scrolls in it. Only then is it a |
| 172 | // region to tab to, named in the page's language; otherwise it is plain |
| 173 | // text, and keyboards and screen readers pass over it. The side that has |
| 174 | // more is marked, and site.css fades the text there. |
| 175 | function edges(el) { |
| 176 | var max = el.scrollWidth - el.clientWidth; |
| 177 | var scrolls = max > 1; |
| 178 | if (scrolls) { |
| 179 | el.setAttribute('tabindex', '0'); |
| 180 | el.setAttribute('role', 'region'); |
| 181 | var label = el.getAttribute('data-aria-' + lang); |
| 182 | if (label) el.setAttribute('aria-label', label); |
| 183 | } else if (el !== document.activeElement) { |
| 184 | el.removeAttribute('tabindex'); |
| 185 | el.removeAttribute('role'); |
| 186 | el.removeAttribute('aria-label'); |
| 187 | } |
| 188 | el.classList.toggle('more-right', scrolls && el.scrollLeft < max - 1); |
| 189 | el.classList.toggle('more-left', scrolls && el.scrollLeft > 1); |
| 190 | } |
| 191 | function allEdges() { |
| 192 | for (var i = 0; i < scrollers.length; i++) edges(scrollers[i]); |
| 193 | } |
| 194 | function watchEdges() { |
| 195 | scrollers = document.querySelectorAll(SCROLLERS); |
| 196 | for (var i = 0; i < scrollers.length; i++) { |
| 197 | scrollers[i].addEventListener('scroll', function () { edges(this); }, { passive: true }); |
| 198 | } |
| 199 | window.addEventListener('resize', allEdges); |
| 200 | document.addEventListener('toggle', allEdges, true); // a <details> opened |
| 201 | allEdges(); |
| 202 | } |
| 203 | |
| 204 | function start() { |
| 205 | apply(lang); |
| 206 | // Without this file these do nothing, so they only show up with it. |
| 207 | each('button.copy, .lang', function (el) { el.hidden = false; }); |
| 208 | document.addEventListener('click', function (event) { |
| 209 | var target = event.target; |
| 210 | if (!target || !target.closest) return; |
| 211 | var toggle = target.closest('[data-set-lang]'); |
| 212 | if (toggle) { |
| 213 | var value = valid(toggle.getAttribute('data-set-lang')); |
| 214 | if (value) { |
| 215 | apply(value); |
| 216 | remember(value); |
| 217 | keepQuery(value); |
| 218 | } |
| 219 | return; |
| 220 | } |
| 221 | var button = target.closest('button.copy'); |
| 222 | if (button) copy(button); |
| 223 | }); |
| 224 | markers(); |
| 225 | watchEdges(); |
| 226 | } |
| 227 | |
| 228 | if (document.readyState === 'loading') { |
| 229 | document.addEventListener('DOMContentLoaded', start); |
| 230 | } else { |
| 231 | start(); |
| 232 | } |
| 233 | })(); |