| 1 | // history.js: the History panel in place (level-up spec §4.5). Without |
| 2 | // it, History is the page /p/{id}/history, the panel drawn beside the |
| 3 | // page, and every control in it is a link or a form. This only adds: |
| 4 | // - History in the bar (or the File menu) fetches the panel with |
| 5 | // ?panel=1 and shows it beside the page, focus in it; |
| 6 | // - its close button and Esc take it away and give focus back to what |
| 7 | // opened it (History, or the menu it was chosen from); "Show older" pages through it in place; |
| 8 | // - on the history routes, where the server drew the panel, Esc goes |
| 9 | // back to the page, as the close button does. |
| 10 | (function () { |
| 11 | "use strict"; |
| 12 | |
| 13 | var opener = null; |
| 14 | |
| 15 | function frame() { return document.querySelector("[mapa-app]"); } |
| 16 | function panel() { return document.querySelector("aside[mapa-history]"); } |
| 17 | |
| 18 | // isHistoryLink is a link to a page's History (/p/{id}/history, nothing |
| 19 | // more), from a screen where the server has not drawn the panel |
| 20 | // already: the page itself, or a daily note at /d/{date}. |
| 21 | function isHistoryLink(link) { |
| 22 | if (!link || link.origin !== location.origin || link.search !== "") return false; |
| 23 | var drawn = panel(); |
| 24 | if (drawn && !drawn.hasAttribute("data-mapa-in-place")) return false; |
| 25 | return /^\/p\/[^/]+\/history$/.test(link.pathname); |
| 26 | } |
| 27 | |
| 28 | function historyAction() { |
| 29 | var links = document.querySelectorAll("[estilo-bar-actions] a[href]"); |
| 30 | for (var index = 0; index < links.length; index++) { |
| 31 | if (isHistoryLink(links[index])) return links[index]; |
| 32 | } |
| 33 | return null; |
| 34 | } |
| 35 | |
| 36 | function fragmentURL(href) { |
| 37 | var url = new URL(href, location.href); |
| 38 | url.searchParams.set("panel", "1"); |
| 39 | return url.toString(); |
| 40 | } |
| 41 | |
| 42 | // show fetches the panel at href and puts it beside the page. A reply |
| 43 | // that is not the panel (a full page, an error) is left to the browser. |
| 44 | function show(href) { |
| 45 | fetch(fragmentURL(href), { |
| 46 | credentials: "same-origin", |
| 47 | headers: { "Accept": "text/html" } |
| 48 | }).then(function (response) { |
| 49 | if (!response.ok) throw new Error("history " + response.status); |
| 50 | return response.text(); |
| 51 | }).then(function (markup) { |
| 52 | var holder = document.createElement("template"); |
| 53 | holder.innerHTML = markup; |
| 54 | var aside = holder.content.querySelector("aside[mapa-history]"); |
| 55 | if (!aside) throw new Error("no panel"); |
| 56 | aside.setAttribute("data-mapa-in-place", ""); |
| 57 | var old = panel(); |
| 58 | if (old) old.replaceWith(aside); else frame().appendChild(aside); |
| 59 | frame().setAttribute("data-history", "list"); |
| 60 | aside.querySelector("#mapa-history-title").focus(); |
| 61 | }).catch(function () { location.href = href; }); |
| 62 | } |
| 63 | |
| 64 | function close() { |
| 65 | var aside = panel(); |
| 66 | if (aside) aside.remove(); |
| 67 | frame().removeAttribute("data-history"); |
| 68 | var back = opener || historyAction(); |
| 69 | opener = null; |
| 70 | if (back) back.focus(); |
| 71 | } |
| 72 | |
| 73 | function closeMenus(from) { |
| 74 | var menu = from.closest("details[open]"); |
| 75 | if (menu) menu.removeAttribute("open"); |
| 76 | } |
| 77 | |
| 78 | function plainClick(event) { |
| 79 | return event.button === 0 && !event.metaKey && !event.ctrlKey && |
| 80 | !event.shiftKey && !event.altKey; |
| 81 | } |
| 82 | |
| 83 | function clicked(event) { |
| 84 | if (!plainClick(event)) return; |
| 85 | var link = event.target.closest("a[href]"); |
| 86 | if (!link) return; |
| 87 | var aside = panel(); |
| 88 | var inPlace = aside && aside.hasAttribute("data-mapa-in-place") && |
| 89 | aside.contains(link); |
| 90 | if (inPlace && link.hasAttribute("data-mapa-history-close")) { |
| 91 | event.preventDefault(); |
| 92 | close(); |
| 93 | } else if (inPlace && link.hasAttribute("data-mapa-history-older")) { |
| 94 | event.preventDefault(); |
| 95 | show(link.href); |
| 96 | } else if (isHistoryLink(link)) { |
| 97 | event.preventDefault(); |
| 98 | var menu = link.closest("details"); |
| 99 | opener = menu ? menu.querySelector("summary") : link; |
| 100 | closeMenus(link); |
| 101 | show(link.href); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function keyed(event) { |
| 106 | var aside = panel(); |
| 107 | if (event.key !== "Escape" || event.defaultPrevented || !aside) return; |
| 108 | if (document.querySelector("details[open], dialog[open]")) return; |
| 109 | event.preventDefault(); |
| 110 | if (aside.hasAttribute("data-mapa-in-place")) { |
| 111 | close(); |
| 112 | return; |
| 113 | } |
| 114 | var back = aside.querySelector("[data-mapa-history-close]"); |
| 115 | if (back) location.href = back.href; |
| 116 | } |
| 117 | |
| 118 | document.addEventListener("click", clicked); |
| 119 | document.addEventListener("keydown", keyed); |
| 120 | })(); |
| 121 | |