| 1 | // mapa.js: Mapa's script layer (spec §6). Every screen works without it; |
| 2 | // this only adds, never replaces: |
| 3 | // - the editor: live preview, [[ autocomplete, link paste, and autosave |
| 4 | // through the person's draft (§5), with the two-tab version check and |
| 5 | // the commit after two minutes idle. Every write is a POST. |
| 6 | // - ⌘K: one box over titles, aliases and bodies. |
| 7 | // - the time zone offer: when the browser's zone differs from the one |
| 8 | // "today" uses, offer to switch. Never silently. |
| 9 | (function () { |
| 10 | "use strict"; |
| 11 | |
| 12 | var IDLE_COMMIT = 2 * 60 * 1000; |
| 13 | |
| 14 | // idleDelay is two minutes. A test may shorten it by setting |
| 15 | // data-mapa-idle (milliseconds) on <html>; nothing in the app does. |
| 16 | function idleDelay() { |
| 17 | var shorter = Number(document.documentElement.getAttribute("data-mapa-idle")); |
| 18 | return shorter > 0 ? shorter : IDLE_COMMIT; |
| 19 | } |
| 20 | var AUTOSAVE_DELAY = 1200; |
| 21 | var PREVIEW_DELAY = 300; |
| 22 | |
| 23 | function post(url, fields, extra) { |
| 24 | var body = new URLSearchParams(); |
| 25 | Object.keys(fields).forEach(function (key) { body.set(key, fields[key]); }); |
| 26 | var options = Object.assign({ |
| 27 | method: "POST", |
| 28 | body: body, |
| 29 | credentials: "same-origin", |
| 30 | headers: { "Accept": "application/json" } |
| 31 | }, extra || {}); |
| 32 | return fetch(url, options); |
| 33 | } |
| 34 | |
| 35 | function debounce(wait, run) { |
| 36 | var timer = null; |
| 37 | var wrapped = function () { |
| 38 | clearTimeout(timer); |
| 39 | timer = setTimeout(run, wait); |
| 40 | }; |
| 41 | wrapped.cancel = function () { clearTimeout(timer); }; |
| 42 | return wrapped; |
| 43 | } |
| 44 | |
| 45 | // ---- The editor ------------------------------------------------------- |
| 46 | |
| 47 | function Editor(form) { |
| 48 | this.form = form; |
| 49 | this.area = form.querySelector("textarea[mapa-editor]"); |
| 50 | this.revision = form.querySelector('input[name="revision"]'); |
| 51 | this.drafts = form.getAttribute("data-drafts") || ""; |
| 52 | this.version = form.getAttribute("data-version") || "0"; |
| 53 | this.versionField = form.querySelector('input[name="version"]'); |
| 54 | this.dirty = false; |
| 55 | this.stopped = false; |
| 56 | this.status = document.createElement("p"); |
| 57 | this.status.setAttribute("mapa-status", ""); |
| 58 | this.status.setAttribute("role", "status"); |
| 59 | this.status.setAttribute("aria-live", "polite"); |
| 60 | this.area.parentNode.appendChild(this.status); |
| 61 | this.preview = document.createElement("article"); |
| 62 | this.preview.setAttribute("mapa-body", ""); |
| 63 | this.preview.setAttribute("mapa-preview", ""); |
| 64 | this.preview.setAttribute("aria-label", "Preview"); |
| 65 | form.parentNode.insertBefore(this.preview, form.nextSibling); |
| 66 | this.suggest = new Suggest(this); |
| 67 | this.wire(); |
| 68 | this.renderPreview(); |
| 69 | } |
| 70 | |
| 71 | Editor.prototype.say = function (text) { this.status.textContent = text; }; |
| 72 | |
| 73 | // lead is estilo's save status in the bar's lead slot (level-up §3.1): |
| 74 | // saved, saving, retrying, failed, or idle. Its texts are on the |
| 75 | // element, so the words are the server's. |
| 76 | Editor.prototype.lead = function (state) { |
| 77 | var element = document.querySelector("[estilo-save-status]"); |
| 78 | if (!element) return; |
| 79 | element.setAttribute("data-state", state); |
| 80 | element.textContent = element.getAttribute("data-text-" + state) || ""; |
| 81 | }; |
| 82 | |
| 83 | // setVersion keeps the form's version in step with the draft, so a Save |
| 84 | // says which draft it saw (a stale tab's Save must not drop a newer one). |
| 85 | Editor.prototype.setVersion = function () { |
| 86 | if (this.versionField) this.versionField.value = this.version; |
| 87 | }; |
| 88 | |
| 89 | Editor.prototype.wire = function () { |
| 90 | var editor = this; |
| 91 | this.autosave = debounce(AUTOSAVE_DELAY, function () { editor.saveDraft(); }); |
| 92 | this.idle = function () { |
| 93 | clearTimeout(editor.idleTimer); |
| 94 | editor.idleTimer = setTimeout(function () { editor.commit(); }, idleDelay()); |
| 95 | }; |
| 96 | this.idle.cancel = function () { clearTimeout(editor.idleTimer); }; |
| 97 | this.previewSoon = debounce(PREVIEW_DELAY, function () { editor.renderPreview(); }); |
| 98 | this.area.addEventListener("input", function () { |
| 99 | editor.dirty = true; |
| 100 | editor.previewSoon(); |
| 101 | editor.suggest.update(); |
| 102 | if (editor.drafts && !editor.stopped) { |
| 103 | editor.say("Editing…"); |
| 104 | editor.autosave(); |
| 105 | } |
| 106 | }); |
| 107 | this.area.addEventListener("paste", function (event) { editor.paste(event); }); |
| 108 | this.area.addEventListener("keydown", function (event) { editor.keys(event); }); |
| 109 | this.form.addEventListener("submit", function () { |
| 110 | editor.autosave.cancel(); |
| 111 | editor.idle.cancel(); |
| 112 | editor.dirty = false; |
| 113 | }); |
| 114 | window.addEventListener("pagehide", function () { editor.flush(); }); |
| 115 | }; |
| 116 | |
| 117 | // keys: ⌘S or ⌘↩ saves; ⌘L is Insert → Link to a page (build note |
| 118 | // 81); the suggestion list takes arrows and Enter. |
| 119 | Editor.prototype.keys = function (event) { |
| 120 | if (this.suggest.keys(event)) return; |
| 121 | var command = event.metaKey || event.ctrlKey; |
| 122 | if (command && (event.key === "s" || event.key === "Enter")) { |
| 123 | event.preventDefault(); |
| 124 | this.form.requestSubmit(); |
| 125 | } else if (modKey(event) && !event.shiftKey && !event.altKey && event.key.toLowerCase() === "l") { |
| 126 | event.preventDefault(); |
| 127 | insert(this.area, "insert-link"); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | // modKey is ProseMirror's Mod: ⌘ on Apple's systems, Ctrl elsewhere. |
| 132 | // Ctrl+L on a Mac is the system's own, never Link to a page (review |
| 133 | // L3). |
| 134 | var APPLE = /Mac|iPhone|iPad|iPod/.test(navigator.platform || navigator.userAgent); |
| 135 | function modKey(event) { |
| 136 | return APPLE ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey; |
| 137 | } |
| 138 | |
| 139 | // paste: a URL pasted over selected text becomes a Markdown link. |
| 140 | Editor.prototype.paste = function (event) { |
| 141 | var text = (event.clipboardData || window.clipboardData).getData("text"); |
| 142 | var start = this.area.selectionStart, end = this.area.selectionEnd; |
| 143 | if (start === end || !/^https?:\/\/\S+$/.test(text.trim())) return; |
| 144 | event.preventDefault(); |
| 145 | var chosen = this.area.value.slice(start, end); |
| 146 | this.area.setRangeText("[" + chosen + "](" + text.trim() + ")", start, end, "end"); |
| 147 | this.area.dispatchEvent(new Event("input")); |
| 148 | }; |
| 149 | |
| 150 | Editor.prototype.renderPreview = function () { |
| 151 | var preview = this.preview; |
| 152 | post("/preview", { body: this.area.value }, { headers: {} }) |
| 153 | .then(function (response) { return response.ok ? response.text() : ""; }) |
| 154 | .then(function (html) { preview.innerHTML = html; }) |
| 155 | .catch(function () {}); |
| 156 | }; |
| 157 | |
| 158 | // saveDraft is one autosave. A stale tab is told, and loads the newer |
| 159 | // draft rather than overwriting it. |
| 160 | Editor.prototype.saveDraft = function () { |
| 161 | var editor = this; |
| 162 | if (!this.dirty || this.stopped) return; |
| 163 | this.dirty = false; |
| 164 | this.lead("saving"); |
| 165 | post(this.drafts, { |
| 166 | body: this.area.value, |
| 167 | base: this.revision ? this.revision.value : "0", |
| 168 | version: this.version |
| 169 | }).then(function (response) { |
| 170 | return response.json().then(function (reply) { |
| 171 | return { status: response.status, reply: reply }; |
| 172 | }); |
| 173 | }).then(function (answer) { |
| 174 | if (answer.status === 200) { |
| 175 | editor.version = answer.reply.version; |
| 176 | editor.setVersion(); |
| 177 | editor.say("Draft saved. It is committed when you press Save, or after two minutes."); |
| 178 | editor.lead("saved"); |
| 179 | editor.idle(); |
| 180 | } else if (answer.status === 409) { |
| 181 | editor.stale(answer.reply); |
| 182 | } else { |
| 183 | editor.say("Couldn't save a draft just now. Your text is still here."); |
| 184 | editor.lead("retrying"); |
| 185 | } |
| 186 | }).catch(function () { |
| 187 | editor.dirty = true; |
| 188 | editor.say("Offline? Your text is still here; Save when you are back."); |
| 189 | editor.lead("failed"); |
| 190 | }); |
| 191 | }; |
| 192 | |
| 193 | Editor.prototype.stale = function (reply) { |
| 194 | if (reply.committed) { |
| 195 | this.stopped = true; |
| 196 | this.say("This was saved in another tab. Reload to carry on from there."); |
| 197 | return; |
| 198 | } |
| 199 | this.area.value = reply.body; |
| 200 | this.version = reply.version; |
| 201 | this.setVersion(); |
| 202 | this.renderPreview(); |
| 203 | this.say("Another tab had newer text, so this tab now shows it."); |
| 204 | }; |
| 205 | |
| 206 | // commit is the two-minutes-idle Save (§5): a POST, by the open tab. |
| 207 | Editor.prototype.commit = function () { |
| 208 | var editor = this; |
| 209 | if (this.version === "0" || this.stopped) return; |
| 210 | post(this.drafts + "/save", { version: this.version }) |
| 211 | .then(function (response) { return response.json(); }) |
| 212 | .then(function (reply) { |
| 213 | if (!reply.saved) { |
| 214 | window.location.assign(reply.href); |
| 215 | return; |
| 216 | } |
| 217 | editor.version = "0"; |
| 218 | editor.setVersion(); |
| 219 | if (reply.merged) { |
| 220 | editor.merged(reply); |
| 221 | return; |
| 222 | } |
| 223 | if (editor.revision && reply.revision) editor.revision.value = reply.revision; |
| 224 | editor.say("Saved."); |
| 225 | }) |
| 226 | .catch(function () {}); |
| 227 | }; |
| 228 | |
| 229 | // merged: the commit took a teammate's edit in. The textarea does not |
| 230 | // hold it, so it must not be saved over the new revision. With nothing |
| 231 | // typed since, the editor takes the merged text and revision. With |
| 232 | // typing since, it keeps the revision it started from, so the next |
| 233 | // save merges again and keeps the teammate's blocks. |
| 234 | Editor.prototype.merged = function (reply) { |
| 235 | if (this.dirty) { |
| 236 | this.say("Saved, with a teammate's changes merged in. Keep typing: " + |
| 237 | "your next save merges them again."); |
| 238 | return; |
| 239 | } |
| 240 | this.area.value = reply.body; |
| 241 | if (this.revision && reply.revision) this.revision.value = reply.revision; |
| 242 | this.renderPreview(); |
| 243 | this.say("Saved, with a teammate's changes merged in. The text above " + |
| 244 | "now shows them."); |
| 245 | }; |
| 246 | |
| 247 | // flush keeps typing that has not reached the draft yet when the tab |
| 248 | // closes: the next visit offers it as Save or Discard. |
| 249 | Editor.prototype.flush = function () { |
| 250 | if (!this.dirty || !this.drafts || this.stopped) return; |
| 251 | post(this.drafts, { |
| 252 | body: this.area.value, |
| 253 | base: this.revision ? this.revision.value : "0", |
| 254 | version: this.version |
| 255 | }, { keepalive: true }).catch(function () {}); |
| 256 | }; |
| 257 | |
| 258 | // ---- [[ autocomplete ---------------------------------------------------- |
| 259 | |
| 260 | function Suggest(editor) { |
| 261 | this.editor = editor; |
| 262 | this.list = document.createElement("ul"); |
| 263 | this.list.setAttribute("mapa-suggest", ""); |
| 264 | this.list.setAttribute("role", "listbox"); |
| 265 | this.list.setAttribute("aria-label", "Pages to link"); |
| 266 | this.list.hidden = true; |
| 267 | editor.area.parentNode.insertBefore(this.list, editor.area.nextSibling); |
| 268 | this.items = []; |
| 269 | this.chosen = 0; |
| 270 | this.fetchSoon = debounce(30, this.fetch.bind(this)); |
| 271 | } |
| 272 | |
| 273 | // typed is the link text being typed after [[, or null. |
| 274 | Suggest.prototype.typed = function () { |
| 275 | var area = this.editor.area; |
| 276 | var before = area.value.slice(0, area.selectionStart); |
| 277 | var match = /\[\[([^\[\]\n|#]{0,80})$/.exec(before); |
| 278 | return match ? match[1] : null; |
| 279 | }; |
| 280 | |
| 281 | Suggest.prototype.update = function () { |
| 282 | if (this.typed() === null) { this.close(); return; } |
| 283 | this.fetchSoon(); |
| 284 | }; |
| 285 | |
| 286 | Suggest.prototype.fetch = function () { |
| 287 | var suggest = this, query = this.typed(); |
| 288 | if (query === null) { this.close(); return; } |
| 289 | fetch("/links/suggest?q=" + encodeURIComponent(query), { credentials: "same-origin" }) |
| 290 | .then(function (response) { return response.ok ? response.json() : { suggestions: [] }; }) |
| 291 | .then(function (reply) { suggest.show(reply.suggestions || []); }) |
| 292 | .catch(function () {}); |
| 293 | }; |
| 294 | |
| 295 | Suggest.prototype.show = function (found) { |
| 296 | var suggest = this; |
| 297 | this.items = found; |
| 298 | this.chosen = 0; |
| 299 | this.list.textContent = ""; |
| 300 | found.forEach(function (item, index) { |
| 301 | var row = document.createElement("li"); |
| 302 | row.setAttribute("role", "option"); |
| 303 | row.textContent = item.text + (item.pending ? " (not written yet)" : ""); |
| 304 | row.addEventListener("mousedown", function (event) { |
| 305 | event.preventDefault(); |
| 306 | suggest.accept(index); |
| 307 | }); |
| 308 | suggest.list.appendChild(row); |
| 309 | }); |
| 310 | this.list.hidden = found.length === 0; |
| 311 | this.mark(); |
| 312 | }; |
| 313 | |
| 314 | Suggest.prototype.mark = function () { |
| 315 | var rows = this.list.children; |
| 316 | for (var index = 0; index < rows.length; index++) { |
| 317 | rows[index].setAttribute("aria-selected", index === this.chosen ? "true" : "false"); |
| 318 | } |
| 319 | }; |
| 320 | |
| 321 | Suggest.prototype.close = function () { |
| 322 | this.list.hidden = true; |
| 323 | this.items = []; |
| 324 | }; |
| 325 | |
| 326 | Suggest.prototype.keys = function (event) { |
| 327 | if (this.list.hidden || this.items.length === 0) return false; |
| 328 | if (event.key === "ArrowDown" || event.key === "ArrowUp") { |
| 329 | var step = event.key === "ArrowDown" ? 1 : -1; |
| 330 | this.chosen = (this.chosen + step + this.items.length) % this.items.length; |
| 331 | this.mark(); |
| 332 | } else if (event.key === "Enter" || event.key === "Tab") { |
| 333 | this.accept(this.chosen); |
| 334 | } else if (event.key === "Escape") { |
| 335 | this.close(); |
| 336 | } else { |
| 337 | return false; |
| 338 | } |
| 339 | event.preventDefault(); |
| 340 | return true; |
| 341 | }; |
| 342 | |
| 343 | Suggest.prototype.accept = function (index) { |
| 344 | var area = this.editor.area, query = this.typed(); |
| 345 | if (query === null || !this.items[index]) return; |
| 346 | var end = area.selectionStart; |
| 347 | area.setRangeText(this.items[index].text + "]]", end - query.length, end, "end"); |
| 348 | this.close(); |
| 349 | area.dispatchEvent(new Event("input")); |
| 350 | area.focus(); |
| 351 | }; |
| 352 | |
| 353 | // ---- ⌘K ---------------------------------------------------------------- |
| 354 | // One box over titles, aliases, text and dates (level-up §4.6): a modal |
| 355 | // <dialog> whose input is a combobox over a grouped listbox. The answer |
| 356 | // is /search/suggest's JSON: Pages, In the text, Your notes, a typed |
| 357 | // date's row, and Create last. Snippets come with match ranges and are |
| 358 | // marked here from text nodes; nothing from the server is parsed as |
| 359 | // markup. |
| 360 | |
| 361 | // Lucide's outlines, as paths: the icons are the only colour (Calma). |
| 362 | var PALETTE_ICONS = { |
| 363 | search: ["M19 11a8 8 0 1 1-16 0 8 8 0 0 1 16 0Z", "m21 21-4.3-4.3"], |
| 364 | page: ["M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z", |
| 365 | "M14 2v4a2 2 0 0 0 2 2h4", "M10 9H8", "M16 13H8", "M16 17H8"], |
| 366 | daily: ["M8 2v4", "M16 2v4", "M3 10h18", |
| 367 | "M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2Z"], |
| 368 | scratch: ["M16 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V8Z", |
| 369 | "M15 3v4a2 2 0 0 0 2 2h4"], |
| 370 | create: ["M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z", |
| 371 | "M14 2v4a2 2 0 0 0 2 2h4", "M9 15h6", "M12 12v6"] |
| 372 | }; |
| 373 | |
| 374 | function paletteIcon(name) { |
| 375 | var space = "http://www.w3.org/2000/svg"; |
| 376 | var svg = document.createElementNS(space, "svg"); |
| 377 | svg.setAttribute("viewBox", "0 0 24 24"); |
| 378 | svg.setAttribute("aria-hidden", "true"); |
| 379 | svg.setAttribute("mapa-palette-svg", ""); |
| 380 | PALETTE_ICONS[name].forEach(function (shape) { |
| 381 | var path = document.createElementNS(space, "path"); |
| 382 | path.setAttribute("d", shape); |
| 383 | svg.appendChild(path); |
| 384 | }); |
| 385 | return svg; |
| 386 | } |
| 387 | |
| 388 | // lookIcon is a team page's own icon in a row's glyph cell (columns |
| 389 | // and icons spec §2.4), drawn as chips.mjs draws it, or null. |
| 390 | function lookIcon(look) { |
| 391 | if (look && look.emoji) { |
| 392 | return paletteElement("span", { "mapa-icon": "", "aria-hidden": "true" }, look.emoji); |
| 393 | } |
| 394 | if (look && /^[0-9a-z]{1,13}$/.test(look.image || "")) { |
| 395 | return paletteElement("img", { "mapa-icon": "", src: "/i/" + look.image, alt: "", |
| 396 | width: "16", height: "16", loading: "lazy", decoding: "async" }); |
| 397 | } |
| 398 | return null; |
| 399 | } |
| 400 | |
| 401 | function paletteElement(tag, attributes, text) { |
| 402 | var element = document.createElement(tag); |
| 403 | Object.keys(attributes || {}).forEach(function (name) { |
| 404 | element.setAttribute(name, attributes[name]); |
| 405 | }); |
| 406 | if (text) element.textContent = text; |
| 407 | return element; |
| 408 | } |
| 409 | |
| 410 | // markSnippet appends a snippet's text to parent, each range in a |
| 411 | // <mark>. Ranges are UTF-16 offsets, which is how a string slices. |
| 412 | function markSnippet(parent, snippet) { |
| 413 | var text = snippet.text, at = 0; |
| 414 | parent.appendChild(document.createTextNode("“")); |
| 415 | (snippet.marks || []).forEach(function (range) { |
| 416 | if (range[0] < at || range[1] > text.length) return; |
| 417 | parent.appendChild(document.createTextNode(text.slice(at, range[0]))); |
| 418 | parent.appendChild(paletteElement("mark", {}, text.slice(range[0], range[1]))); |
| 419 | at = range[1]; |
| 420 | }); |
| 421 | parent.appendChild(document.createTextNode(text.slice(at) + "”")); |
| 422 | } |
| 423 | |
| 424 | function Palette() { |
| 425 | var palette = this; |
| 426 | this.dialog = paletteElement("dialog", { "mapa-palette": "", "aria-label": "Search Mapa" }); |
| 427 | var box = paletteElement("div", { "mapa-palette-box": "" }); |
| 428 | box.appendChild(paletteIcon("search")); |
| 429 | this.input = paletteElement("input", { |
| 430 | type: "search", role: "combobox", "aria-label": "Search pages, or type a title to create one", |
| 431 | placeholder: "Search, or type a title to create one", "aria-expanded": "false", |
| 432 | "aria-controls": "mapa-palette-list", "aria-autocomplete": "list", |
| 433 | autocomplete: "off", spellcheck: "false", enterkeyhint: "go" |
| 434 | }); |
| 435 | box.appendChild(this.input); |
| 436 | var esc = paletteElement("button", { type: "button", "mapa-palette-esc": "", "aria-label": "Close" }, "esc"); |
| 437 | esc.addEventListener("click", function () { palette.dialog.close(); }); |
| 438 | box.appendChild(esc); |
| 439 | this.list = paletteElement("div", { |
| 440 | id: "mapa-palette-list", role: "listbox", "aria-label": "Results", "mapa-palette-results": "" |
| 441 | }); |
| 442 | this.count = paletteElement("div", { "mapa-sr": "", role: "status", "aria-live": "polite" }); |
| 443 | this.dialog.appendChild(box); |
| 444 | this.dialog.appendChild(this.list); |
| 445 | this.dialog.appendChild(this.foot()); |
| 446 | this.dialog.appendChild(this.count); |
| 447 | document.body.appendChild(this.dialog); |
| 448 | this.options = []; |
| 449 | this.chosen = 0; |
| 450 | this.create = ""; |
| 451 | // No debounce: an answer takes about a millisecond at 10,000 pages, |
| 452 | // and §6.1's budget is 100 ms from keystroke to paint. A stale |
| 453 | // answer is dropped when it arrives (fetch compares the query). |
| 454 | this.input.addEventListener("input", function () { palette.fetch(); }); |
| 455 | this.input.addEventListener("keydown", function (event) { palette.keys(event); }); |
| 456 | // Esc closes (the dialog's own cancel), as does a click outside it; |
| 457 | // either way focus goes back to what opened it. |
| 458 | this.dialog.addEventListener("click", function (event) { |
| 459 | if (event.target === palette.dialog) palette.dialog.close(); |
| 460 | }); |
| 461 | this.dialog.addEventListener("close", function () { |
| 462 | var opener = palette.opener; |
| 463 | palette.opener = null; |
| 464 | if (opener && opener.isConnected && opener.focus) opener.focus(); |
| 465 | }); |
| 466 | } |
| 467 | |
| 468 | // foot is the keyboard hints, hidden on phones by palette.css. |
| 469 | Palette.prototype.foot = function () { |
| 470 | var foot = paletteElement("div", { "mapa-palette-foot": "", "aria-hidden": "true" }); |
| 471 | [[["↑", "↓"], "to move"], [["↵"], "to open"], [["⌘", "↵"], "to create"], |
| 472 | [[], "Type a date, like “friday”, to open that day"]].forEach(function (hint) { |
| 473 | var item = paletteElement("span"); |
| 474 | hint[0].forEach(function (key) { item.appendChild(paletteElement("kbd", { "mapa-kbd": "" }, key)); }); |
| 475 | item.appendChild(document.createTextNode(hint[1])); |
| 476 | foot.appendChild(item); |
| 477 | }); |
| 478 | return foot; |
| 479 | }; |
| 480 | |
| 481 | Palette.prototype.open = function () { |
| 482 | if (this.dialog.open) { this.input.focus(); return; } |
| 483 | this.opener = document.activeElement; |
| 484 | this.input.value = ""; |
| 485 | this.render({}); |
| 486 | this.dialog.showModal(); |
| 487 | this.input.focus(); |
| 488 | }; |
| 489 | |
| 490 | Palette.prototype.fetch = function () { |
| 491 | var palette = this, query = this.input.value; |
| 492 | if (!query.trim()) { this.render({}); return; } |
| 493 | fetch("/search/suggest?q=" + encodeURIComponent(query), { credentials: "same-origin" }) |
| 494 | .then(function (response) { |
| 495 | if (response.redirected || !response.ok) { |
| 496 | window.location.assign("/search?q=" + encodeURIComponent(query)); |
| 497 | return null; |
| 498 | } |
| 499 | return response.json(); |
| 500 | }) |
| 501 | .then(function (reply) { |
| 502 | if (reply && palette.input.value === query) palette.render(reply); |
| 503 | }) |
| 504 | .catch(function () {}); |
| 505 | }; |
| 506 | |
| 507 | // render replaces the list with the reply's groups: the typed date |
| 508 | // first, then Pages, In the text and Your notes, and Create last. A |
| 509 | // date typed in numbers ("1.5", "12/11") might as well be a search, |
| 510 | // so when pages or text answer it too it goes after them, and Enter |
| 511 | // opens what was searched for (build note 85). |
| 512 | Palette.prototype.render = function (reply) { |
| 513 | var palette = this; |
| 514 | this.list.textContent = ""; |
| 515 | this.options = []; |
| 516 | this.create = reply.create ? "/new?title=" + encodeURIComponent(reply.create) : ""; |
| 517 | var later = reply.date && reply.date.numeric && |
| 518 | ((reply.pages || []).length + (reply.text || []).length) > 0; |
| 519 | var date = function () { |
| 520 | if (reply.date) palette.group("A date", null, [{ icon: "daily", main: reply.date.label, sub: "Your note", href: reply.date.href }]); |
| 521 | }; |
| 522 | if (!later) date(); |
| 523 | this.group("Pages", "", (reply.pages || []).map(function (hit) { |
| 524 | return hit.via ? { icon: "page", look: hit.look, main: hit.via, sub: "Old title of " + hit.label, href: hit.href } |
| 525 | : { icon: "page", look: hit.look, main: hit.label, sub: "Team page", href: hit.href }; |
| 526 | })); |
| 527 | this.group("In the text", "", (reply.text || []).map(function (hit) { |
| 528 | return { icon: "page", look: hit.look, main: hit.label, snippet: hit.snippet, href: hit.href }; |
| 529 | })); |
| 530 | if (later) date(); |
| 531 | this.group("Your notes", "Only you see these", (reply.notes || []).map(function (hit) { |
| 532 | var daily = hit.kind === "daily"; |
| 533 | return { icon: daily ? "daily" : "scratch", main: hit.label, snippet: hit.snippet, |
| 534 | sub: daily ? "Your daily note" : "Your scratch page", href: hit.href }; |
| 535 | })); |
| 536 | var found = this.options.length; |
| 537 | if (this.create) { |
| 538 | this.group("Create", null, [{ icon: "create", main: "Create “" + reply.create + "”", |
| 539 | sub: "A new team page with this title", href: this.create, dashed: true }]); |
| 540 | } |
| 541 | this.input.setAttribute("aria-expanded", this.options.length ? "true" : "false"); |
| 542 | this.chosen = 0; |
| 543 | this.choose(0, false); |
| 544 | this.announce(reply, found); |
| 545 | }; |
| 546 | |
| 547 | // group adds a labelled group of options, with a visible heading and |
| 548 | // its aside unless aside is null: the date and Create rows speak for |
| 549 | // themselves. |
| 550 | Palette.prototype.group = function (name, aside, rows) { |
| 551 | if (!rows.length) return; |
| 552 | var palette = this; |
| 553 | var group = paletteElement("div", { role: "group", "aria-label": name, "mapa-palette-group": "" }); |
| 554 | if (aside !== null) { |
| 555 | var head = paletteElement("div", { "mapa-palette-head": "", "aria-hidden": "true" }); |
| 556 | head.appendChild(paletteElement("span", {}, name)); |
| 557 | if (aside) head.appendChild(paletteElement("span", {}, aside)); |
| 558 | group.appendChild(head); |
| 559 | } |
| 560 | rows.forEach(function (row) { group.appendChild(palette.option(row)); }); |
| 561 | this.list.appendChild(group); |
| 562 | }; |
| 563 | |
| 564 | Palette.prototype.option = function (row) { |
| 565 | var palette = this, index = this.options.length; |
| 566 | var link = paletteElement("a", { |
| 567 | href: row.href, role: "option", id: "mapa-palette-option-" + index, |
| 568 | "aria-selected": "false", tabindex: "-1", "mapa-palette-option": "" |
| 569 | }); |
| 570 | var icon = paletteElement("span", row.dashed ? { "mapa-palette-ic": "dash" } : { "mapa-palette-ic": "" }); |
| 571 | icon.appendChild(lookIcon(row.look) || paletteIcon(row.icon)); |
| 572 | var main = paletteElement("span", { "mapa-palette-main": "" }); |
| 573 | main.appendChild(paletteElement("span", {}, row.main)); |
| 574 | var sub = paletteElement("small"); |
| 575 | if (row.snippet) markSnippet(sub, row.snippet); else sub.textContent = row.sub; |
| 576 | main.appendChild(sub); |
| 577 | link.appendChild(icon); |
| 578 | link.appendChild(main); |
| 579 | link.addEventListener("pointermove", function () { |
| 580 | if (palette.chosen !== index) palette.choose(index, false); |
| 581 | }); |
| 582 | this.options.push(link); |
| 583 | return link; |
| 584 | }; |
| 585 | |
| 586 | // choose makes option index the active one: aria-selected on it, and |
| 587 | // the input's aria-activedescendant naming it. |
| 588 | Palette.prototype.choose = function (index, scroll) { |
| 589 | var before = this.options[this.chosen]; |
| 590 | if (before) before.setAttribute("aria-selected", "false"); |
| 591 | this.chosen = index; |
| 592 | var now = this.options[index]; |
| 593 | if (!now) { this.input.removeAttribute("aria-activedescendant"); return; } |
| 594 | now.setAttribute("aria-selected", "true"); |
| 595 | this.input.setAttribute("aria-activedescendant", now.id); |
| 596 | if (scroll) now.scrollIntoView({ block: "nearest" }); |
| 597 | }; |
| 598 | |
| 599 | // announce tells a screen reader how many pages answer ("3 pages"). |
| 600 | Palette.prototype.announce = function (reply, found) { |
| 601 | var message = ""; |
| 602 | if (this.input.value.trim()) { |
| 603 | message = found === 1 ? "1 page" : (found || "No") + " pages"; |
| 604 | if (reply.create) message += ", or create “" + reply.create + "”"; |
| 605 | } |
| 606 | if (this.count.textContent !== message) this.count.textContent = message; |
| 607 | }; |
| 608 | |
| 609 | Palette.prototype.keys = function (event) { |
| 610 | var count = this.options.length; |
| 611 | if (event.key === "Escape") { |
| 612 | // At once: a search field's own Esc would only clear the text. |
| 613 | event.preventDefault(); |
| 614 | this.dialog.close(); |
| 615 | } else if (event.key === "ArrowDown" || event.key === "ArrowUp") { |
| 616 | event.preventDefault(); |
| 617 | if (count === 0) return; |
| 618 | var step = event.key === "ArrowDown" ? 1 : -1; |
| 619 | this.choose((this.chosen + step + count) % count, true); |
| 620 | } else if (event.key === "Enter") { |
| 621 | event.preventDefault(); |
| 622 | if ((event.metaKey || event.ctrlKey) && this.create) { |
| 623 | window.location.assign(this.create); |
| 624 | } else if (this.options[this.chosen]) { |
| 625 | window.location.assign(this.options[this.chosen].href); |
| 626 | } else if (this.input.value.trim()) { |
| 627 | window.location.assign("/search?q=" + encodeURIComponent(this.input.value)); |
| 628 | } |
| 629 | } |
| 630 | }; |
| 631 | |
| 632 | // ---- The time zone offer ------------------------------------------------- |
| 633 | |
| 634 | function offerZone() { |
| 635 | var zone = document.body.getAttribute("data-zone"); |
| 636 | var browser = ""; |
| 637 | try { browser = Intl.DateTimeFormat().resolvedOptions().timeZone || ""; } catch (error) {} |
| 638 | var key = "mapa-zone-declined:" + browser; |
| 639 | var main = document.querySelector("[mapa-doc]"); |
| 640 | if (!zone || !browser || browser === zone || !main) return; |
| 641 | try { if (localStorage.getItem(key)) return; } catch (error) {} |
| 642 | var offer = document.createElement("form"); |
| 643 | offer.method = "post"; |
| 644 | offer.action = "/settings"; |
| 645 | offer.setAttribute("mapa-unsaved", ""); |
| 646 | offer.setAttribute("mapa-zone-offer", ""); |
| 647 | var text = document.createElement("p"); |
| 648 | text.textContent = "Your browser's time zone is " + browser + |
| 649 | ", but “today” here uses " + zone + "."; |
| 650 | var field = document.createElement("input"); |
| 651 | field.type = "hidden"; field.name = "zone"; field.value = browser; |
| 652 | var back = document.createElement("input"); |
| 653 | back.type = "hidden"; back.name = "return"; back.value = location.pathname; |
| 654 | var use = document.createElement("button"); |
| 655 | use.type = "submit"; use.setAttribute("rst-btn", "primary"); |
| 656 | use.textContent = "Use " + browser; |
| 657 | var later = document.createElement("button"); |
| 658 | later.type = "button"; later.setAttribute("rst-btn", ""); |
| 659 | later.textContent = "Not now"; |
| 660 | later.addEventListener("click", function () { |
| 661 | try { localStorage.setItem(key, "1"); } catch (error) {} |
| 662 | offer.remove(); |
| 663 | }); |
| 664 | [text, field, back, use, later].forEach(function (part) { offer.appendChild(part); }); |
| 665 | main.insertBefore(offer, main.firstChild); |
| 666 | } |
| 667 | |
| 668 | // ---- The frame: the sidebar's fold, the menus' commands ------------------ |
| 669 | |
| 670 | // sidebarShown is what the sidebar shows now: the cookie's state, or |
| 671 | // what CSS chose by width when there is none (open from 80rem). |
| 672 | function sidebarShown(app) { |
| 673 | var state = app.getAttribute("data-side"); |
| 674 | if (state) return state; |
| 675 | return window.matchMedia("(min-width: 80rem)").matches ? "open" : "rail"; |
| 676 | } |
| 677 | |
| 678 | function setCookie(name, value) { |
| 679 | document.cookie = name + "=" + value + "; path=/; max-age=31536000; samesite=lax" + |
| 680 | (location.protocol === "https:" ? "; secure" : ""); |
| 681 | } |
| 682 | |
| 683 | // fold folds the sidebar to its rail, or opens it, in place: the same |
| 684 | // cookie the scriptless form sets, so the next page is drawn the same. |
| 685 | function fold() { |
| 686 | var app = document.querySelector("[mapa-app]"); |
| 687 | if (!app) return; |
| 688 | var next = sidebarShown(app) === "open" ? "rail" : "open"; |
| 689 | app.setAttribute("data-side", next); |
| 690 | setCookie("mapa-sidebar", next); |
| 691 | } |
| 692 | |
| 693 | // Markdown the Insert menu puts in at the caret, and whether it starts |
| 694 | // a line of its own. |
| 695 | var INSERTS = { |
| 696 | "insert-h1": ["# ", true], "insert-h2": ["## ", true], |
| 697 | "insert-h3": ["### ", true], "insert-list": ["- ", true], |
| 698 | "insert-numbered": ["1. ", true], "insert-check": ["- [ ] ", true], |
| 699 | "insert-quote": ["> ", true], "insert-code": ["```\n\n```", true], |
| 700 | "insert-rule": ["---", true], "insert-link": ["[[", false] |
| 701 | }; |
| 702 | |
| 703 | function today() { |
| 704 | var now = new Date(); |
| 705 | var pad = function (value) { return (value < 10 ? "0" : "") + value; }; |
| 706 | return now.getFullYear() + "-" + pad(now.getMonth() + 1) + "-" + pad(now.getDate()); |
| 707 | } |
| 708 | |
| 709 | // insert puts the Insert menu's Markdown in, and never over selected |
| 710 | // text (review H1: ⌘A then ⌘L, a habit from the address bar, once |
| 711 | // replaced the whole page with [[). Link to a page over selected words |
| 712 | // writes them as the link, [[words]], when they can be one; over any |
| 713 | // other selection it does nothing. With no selection it types [[, and |
| 714 | // the list opens. The other inserts go in at the selection's start. |
| 715 | function insert(area, name) { |
| 716 | var spec = name === "insert-date" ? ["[[" + today() + "]]", false] : INSERTS[name]; |
| 717 | if (!spec) return; |
| 718 | var at = area.selectionStart, end = area.selectionEnd; |
| 719 | var chosen = area.value.slice(at, end); |
| 720 | if (name === "insert-link" && chosen !== "") { |
| 721 | if (chosen.trim() === "" || /[\[\]\n\r|#]/.test(chosen)) { |
| 722 | area.focus(); |
| 723 | return; |
| 724 | } |
| 725 | var edges = /^(\s*)([^]*?)(\s*)$/.exec(chosen); |
| 726 | spec = [edges[1] + "[[" + edges[2] + "]]" + edges[3], false]; |
| 727 | } else { |
| 728 | end = at; |
| 729 | } |
| 730 | var text = spec[0]; |
| 731 | if (spec[1] && at > 0 && area.value.charAt(at - 1) !== "\n") text = "\n\n" + text; |
| 732 | area.focus(); |
| 733 | area.setRangeText(text, at, end, "end"); |
| 734 | area.dispatchEvent(new Event("input")); |
| 735 | } |
| 736 | |
| 737 | function runCommand(name) { |
| 738 | if (name === "toggle-sidebar") { fold(); return; } |
| 739 | var area = document.querySelector("textarea[mapa-editor]"); |
| 740 | if (!area) return; |
| 741 | if (name === "undo" || name === "redo") { |
| 742 | area.focus(); |
| 743 | document.execCommand(name); |
| 744 | } else if (name === "select-all") { |
| 745 | area.focus(); |
| 746 | area.select(); |
| 747 | } else { |
| 748 | insert(area, name); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | function bindCommands() { |
| 753 | var area = document.querySelector("textarea[mapa-editor]"); |
| 754 | document.querySelectorAll("[data-mapa-command]").forEach(function (control) { |
| 755 | var name = control.getAttribute("data-mapa-command"); |
| 756 | if (name === "toggle-links") return; // the panel's own (links.js) |
| 757 | if (name !== "toggle-sidebar" && !area) return; |
| 758 | control.disabled = false; |
| 759 | control.addEventListener("click", function (event) { |
| 760 | event.preventDefault(); |
| 761 | var menu = control.closest("details"); |
| 762 | if (menu) menu.open = false; |
| 763 | runCommand(name); |
| 764 | }); |
| 765 | }); |
| 766 | } |
| 767 | |
| 768 | // belowTheText: on the Markdown screen, a press on the page's empty |
| 769 | // area below the form (beside or under its preview, never on it) puts |
| 770 | // the caret at the textarea's end, as a press below the editor's last |
| 771 | // block does (build note 84). |
| 772 | function belowTheText() { |
| 773 | var area = document.querySelector("textarea[mapa-editor]"); |
| 774 | var main = area && area.closest("[mapa-main]"); |
| 775 | if (!main) return; |
| 776 | var column = main.querySelector("[mapa-doc]"); |
| 777 | main.addEventListener("mousedown", function (event) { |
| 778 | if (event.target !== main && event.target !== column) return; |
| 779 | if (event.button !== 0 || event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) return; |
| 780 | if (event.clientY < area.form.getBoundingClientRect().bottom) return; |
| 781 | event.preventDefault(); |
| 782 | area.focus({ preventScroll: true }); |
| 783 | area.setSelectionRange(area.value.length, area.value.length); |
| 784 | }); |
| 785 | } |
| 786 | |
| 787 | // ---- New team page --------------------------------------------------------- |
| 788 | // The sidebar's popover (level-up §4.7) needs no script: it opens, closes |
| 789 | // and takes focus by itself, and posts; a refusal is then the New page |
| 790 | // screen. This keeps a refusal in the popover, beside what was typed, |
| 791 | // holds the POST for HOLD_MS as busy.js holds a form (so busy.js leaves |
| 792 | // this one alone), and gives focus back to the button after Esc: WebKit |
| 793 | // does not focus a button on a click, so it would leave focus nowhere. |
| 794 | var HOLD_MS = 650; |
| 795 | |
| 796 | function newTeamPage() { |
| 797 | var pop = document.getElementById("mapa-new-page"); |
| 798 | var form = pop && pop.querySelector("form[mapa-new-page-form]"); |
| 799 | var opener = document.querySelector("[mapa-new-page-open]"); |
| 800 | if (!form || !opener || typeof pop.showPopover !== "function") return; |
| 801 | var field = form.querySelector('input[name="title"]'); |
| 802 | var said = form.querySelector("[mapa-new-page-error]"); |
| 803 | var button = form.querySelector('button[type="submit"]'); |
| 804 | var escaped = false, sending = false; |
| 805 | form.setAttribute("data-busy", "false"); |
| 806 | pop.addEventListener("keydown", function (event) { |
| 807 | if (event.key === "Escape") escaped = true; |
| 808 | }); |
| 809 | pop.addEventListener("toggle", function (event) { |
| 810 | if (event.newState === "open") { escaped = false; return; } |
| 811 | // WebKit still names the hidden field as focused here. |
| 812 | var here = document.activeElement; |
| 813 | if (escaped && (!here || here === document.body || pop.contains(here))) { |
| 814 | opener.focus({ preventScroll: true }); |
| 815 | } |
| 816 | }); |
| 817 | field.addEventListener("input", function () { say(""); }); |
| 818 | |
| 819 | // say shows a refusal under the field, or clears it. |
| 820 | function say(text, taken) { |
| 821 | said.textContent = text; |
| 822 | said.hidden = !text; |
| 823 | if (taken) { |
| 824 | var link = document.createElement("a"); |
| 825 | link.href = taken; |
| 826 | link.textContent = "Open the page with that title"; |
| 827 | said.append(" ", link); |
| 828 | } |
| 829 | if (text) { |
| 830 | field.setAttribute("aria-invalid", "true"); |
| 831 | field.setAttribute("aria-describedby", "mapa-new-page-error mapa-new-page-said"); |
| 832 | } else { |
| 833 | field.removeAttribute("aria-invalid"); |
| 834 | field.setAttribute("aria-describedby", "mapa-new-page-said"); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | function busy(on) { |
| 839 | sending = on; |
| 840 | button.disabled = on; |
| 841 | var spin = button.querySelector("[rst-spin]"); |
| 842 | if (on && !spin) { |
| 843 | spin = document.createElement("span"); |
| 844 | spin.setAttribute("rst-spin", ""); |
| 845 | spin.setAttribute("aria-hidden", "true"); |
| 846 | button.insertBefore(spin, button.firstChild); |
| 847 | } else if (!on && spin) { |
| 848 | spin.remove(); |
| 849 | } |
| 850 | if (on) button.setAttribute("aria-busy", "true"); else button.removeAttribute("aria-busy"); |
| 851 | } |
| 852 | |
| 853 | form.addEventListener("submit", function (event) { |
| 854 | event.preventDefault(); |
| 855 | if (sending) return; |
| 856 | busy(true); |
| 857 | var wait = new Promise(function (resolve) { setTimeout(resolve, HOLD_MS); }); |
| 858 | var sent = post(form.action, { title: field.value }).then(function (response) { |
| 859 | return response.json().then(function (reply) { return { ok: response.ok, reply: reply }; }); |
| 860 | }); |
| 861 | Promise.all([sent, wait]).then(function (both) { |
| 862 | var answer = both[0]; |
| 863 | if (answer.ok && answer.reply.href) { |
| 864 | window.location.assign(answer.reply.href); |
| 865 | return; |
| 866 | } |
| 867 | if (!answer.reply.error) throw new Error("no answer"); |
| 868 | busy(false); |
| 869 | say(answer.reply.error, answer.reply.taken); |
| 870 | field.focus(); |
| 871 | }).catch(function () { |
| 872 | busy(false); |
| 873 | say("The page could not be created just now. Try again."); |
| 874 | field.focus(); |
| 875 | }); |
| 876 | }); |
| 877 | } |
| 878 | |
| 879 | // ---- A sidebar row's ⋯ (build note 88) ------------------------------------ |
| 880 | // Opening it moves focus to its first row, and ↓ ↑ move between its |
| 881 | // rows; focus leaving it for another control closes it. Esc and a click |
| 882 | // outside are estilo's chrome.js, for every menu in the group, and Esc |
| 883 | // gives focus back to the ⋯. With scripts off it is a <details> still. |
| 884 | var ROW_MENU = "details[mapa-row-menu]"; |
| 885 | |
| 886 | function rowMenus() { |
| 887 | document.addEventListener("toggle", function (event) { |
| 888 | var menu = event.target; |
| 889 | if (!menu.matches || !menu.matches(ROW_MENU) || !menu.open) return; |
| 890 | var first = menu.querySelector("[estilo-menu-item]"); |
| 891 | if (first) first.focus({ preventScroll: true }); |
| 892 | }, true); |
| 893 | document.addEventListener("focusout", function (event) { |
| 894 | var menu = event.target.closest && event.target.closest(ROW_MENU + "[open]"); |
| 895 | var next = event.relatedTarget; |
| 896 | if (menu && next && !menu.contains(next)) menu.open = false; |
| 897 | }); |
| 898 | document.addEventListener("keydown", function (event) { |
| 899 | if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return; |
| 900 | var menu = event.target.closest && event.target.closest(ROW_MENU + "[open]"); |
| 901 | if (!menu) return; |
| 902 | var rows = Array.prototype.slice.call(menu.querySelectorAll("[estilo-menu-item]")); |
| 903 | var down = event.key === "ArrowDown", here = rows.indexOf(event.target); |
| 904 | var at = here < 0 ? (down ? 0 : rows.length - 1) : here + (down ? 1 : -1); |
| 905 | event.preventDefault(); |
| 906 | rows[(at + rows.length) % rows.length].focus(); |
| 907 | }); |
| 908 | } |
| 909 | |
| 910 | // ---- Pages inside (columns and icons spec §5.3) ------------------------- |
| 911 | // An arrow is a form that sets the mapa-open cookie and comes back. With |
| 912 | // script it is sent in the background and the sidebar the server draws |
| 913 | // for the page is put in place, so the rows open at once and the page |
| 914 | // and what was typed stay as they are. No busy rule: like the fold, it |
| 915 | // switches at once. |
| 916 | function sideArrows() { |
| 917 | var sent = 0; |
| 918 | // Toggles go one after another: each POST carries the cookie the |
| 919 | // last one set, so two rows opened quickly both stay open. |
| 920 | var queue = Promise.resolve(); |
| 921 | document.addEventListener("submit", function (event) { |
| 922 | var form = event.target.closest && event.target.closest("form[mapa-side-arrow-form]"); |
| 923 | if (!form) return; |
| 924 | event.preventDefault(); |
| 925 | var nav = form.closest("nav[mapa-side]"); |
| 926 | var id = form.querySelector('input[name="id"]').value; |
| 927 | // Which of this page's arrows it was: a page shown twice has two. |
| 928 | var same = nav ? arrowsFor(nav, id) : []; |
| 929 | var nth = Math.max(same.indexOf(form), 0); |
| 930 | var mine = ++sent; |
| 931 | var body = new URLSearchParams(new FormData(form)); |
| 932 | queue = queue.then(function () { |
| 933 | return fetch(form.action, { method: "POST", body: body, credentials: "same-origin" }); |
| 934 | }).then(function (response) { |
| 935 | // Only a page back where we are draws this sidebar. |
| 936 | if (!response.ok || new URL(response.url).pathname !== window.location.pathname) { |
| 937 | throw new Error("elsewhere"); |
| 938 | } |
| 939 | return response.text(); |
| 940 | }).then(function (html) { |
| 941 | if (mine !== sent) return; // a later toggle's reply is the one shown |
| 942 | var fresh = new DOMParser().parseFromString(html, "text/html").querySelector("nav[mapa-side]"); |
| 943 | if (!nav || !nav.isConnected || !fresh) { window.location.reload(); return; } |
| 944 | // Every row above the foot comes from the server's next page, so |
| 945 | // the open lists, their ids and the one aria-current all agree |
| 946 | // (build note 80); the foot keeps its listeners, and row menus |
| 947 | // and search are delegated. |
| 948 | var foot = nav.querySelector("[mapa-side-foot]"); |
| 949 | Array.prototype.slice.call(nav.children).forEach(function (child) { |
| 950 | if (child !== foot) child.remove(); |
| 951 | }); |
| 952 | Array.prototype.slice.call(fresh.children).forEach(function (child) { |
| 953 | if (!child.matches("[mapa-side-foot]")) nav.insertBefore(document.importNode(child, true), foot); |
| 954 | }); |
| 955 | // Focus comes back to the arrow only if it was not taken |
| 956 | // elsewhere while the reply was on its way. |
| 957 | var active = document.activeElement; |
| 958 | if (active && active !== document.body && !nav.contains(active)) return; |
| 959 | var again = arrowsFor(nav, id); |
| 960 | var button = (again[nth] || again[0] || nav).querySelector("[mapa-side-arrow]"); |
| 961 | if (button) button.focus({ preventScroll: true }); |
| 962 | }).catch(function () { |
| 963 | if (mine === sent) window.location.reload(); |
| 964 | }); // caught, so the queue goes on after a failure |
| 965 | }); |
| 966 | // → opens and ← closes, when the arrow has focus (§5.3). |
| 967 | document.addEventListener("keydown", function (event) { |
| 968 | var button = event.target.closest && event.target.closest("[mapa-side-arrow]"); |
| 969 | if (!button || event.altKey || event.ctrlKey || event.metaKey) return; |
| 970 | var open = button.getAttribute("aria-expanded") === "true"; |
| 971 | if ((event.key === "ArrowRight" && !open) || (event.key === "ArrowLeft" && open)) { |
| 972 | event.preventDefault(); |
| 973 | button.form.requestSubmit(button); |
| 974 | } |
| 975 | }); |
| 976 | } |
| 977 | |
| 978 | // arrowsFor is every arrow form in nav for one page, in document order. |
| 979 | function arrowsFor(nav, id) { |
| 980 | return Array.prototype.slice.call(nav.querySelectorAll("form[mapa-side-arrow-form]")).filter(function (form) { |
| 981 | return form.querySelector('input[name="id"]').value === id; |
| 982 | }); |
| 983 | } |
| 984 | |
| 985 | // ---- Start ---------------------------------------------------------------- |
| 986 | |
| 987 | function start() { |
| 988 | document.querySelectorAll("form[mapa-edit-form]").forEach(function (form) { |
| 989 | if (form.querySelector("textarea[mapa-editor]")) new Editor(form); |
| 990 | }); |
| 991 | var palette = null; |
| 992 | bindCommands(); |
| 993 | belowTheText(); |
| 994 | // Search in the sidebar and in the bar's Menu sheet opens ⌘K; |
| 995 | // without script they are links to the search page. |
| 996 | // Delegated: the sidebar's rows are put in place again when an |
| 997 | // arrow opens or closes a row (sideArrows). |
| 998 | document.addEventListener("click", function (event) { |
| 999 | var link = event.target.closest && event.target.closest('[estilo-bar] a[href="/search"], [mapa-side] a[href="/search"]'); |
| 1000 | if (!link || event.metaKey || event.ctrlKey || event.shiftKey) return; |
| 1001 | event.preventDefault(); |
| 1002 | palette = palette || new Palette(); |
| 1003 | palette.open(); |
| 1004 | }); |
| 1005 | document.addEventListener("keydown", function (event) { |
| 1006 | if ((event.metaKey || event.ctrlKey) && event.key === "k") { |
| 1007 | event.preventDefault(); |
| 1008 | palette = palette || new Palette(); |
| 1009 | palette.open(); |
| 1010 | } else if ((event.metaKey || event.ctrlKey) && event.key === "\\") { |
| 1011 | event.preventDefault(); |
| 1012 | fold(); |
| 1013 | } |
| 1014 | }); |
| 1015 | newTeamPage(); |
| 1016 | rowMenus(); |
| 1017 | sideArrows(); |
| 1018 | offerZone(); |
| 1019 | } |
| 1020 | |
| 1021 | if (document.readyState === "loading") { |
| 1022 | document.addEventListener("DOMContentLoaded", start); |
| 1023 | } else { |
| 1024 | start(); |
| 1025 | } |
| 1026 | })(); |
| 1027 | |