| 1 | // The ship question on a project page (Paul, 2026-11-11): "Yes, it |
| 2 | // ships" and "No, main is not a release" apply at once. A tap posts in |
| 3 | // the background, the page is read again and the sentences, the pressed |
| 4 | // button and every form's revision are put back in place; "Saved" shows |
| 5 | // with Undo, which posts the answer from before the tap ("unknown" |
| 6 | // included) the same way. No reload. Without script each button posts |
| 7 | // and comes back to the page. |
| 8 | (function () { |
| 9 | "use strict"; |
| 10 | if (!window.fetch || !window.URLSearchParams || !window.DOMParser) return; |
| 11 | |
| 12 | document.addEventListener("submit", function (e) { |
| 13 | var form = e.target; |
| 14 | if (!form.matches || !form.matches("form[data-merge-ships-form]")) return; |
| 15 | e.preventDefault(); |
| 16 | var group = form.closest("[data-merge-ships]"); |
| 17 | var value = form.elements.main_ships.value, was = group.dataset.mergeShips; |
| 18 | if (value === was || group.hasAttribute("aria-busy")) return; |
| 19 | set(group, form.getAttribute("action"), value, was, function () { HTMLFormElement.prototype.submit.call(form); }); |
| 20 | }); |
| 21 | |
| 22 | // set posts value at the group's revision; was is what Undo puts back, |
| 23 | // and none after an Undo. |
| 24 | function set(group, action, value, was, failed) { |
| 25 | var rev = group.querySelector("input[name=revision]").value; |
| 26 | group.setAttribute("aria-busy", "true"); |
| 27 | fetch(action, { method: "POST", credentials: "same-origin", headers: { "Accept": "application/json" }, body: new URLSearchParams({ revision: rev, only: "main_ships", main_ships: value }) }) |
| 28 | .then(function (r) { |
| 29 | if (!r.ok) throw new Error("save"); |
| 30 | return fetch(location.pathname + location.search, { credentials: "same-origin", headers: { "Accept": "text/html" } }); |
| 31 | }) |
| 32 | .then(function (r) { |
| 33 | if (!r.ok) throw new Error("read"); |
| 34 | return r.text(); |
| 35 | }) |
| 36 | .then(function (html) { |
| 37 | var doc = new DOMParser().parseFromString(html, "text/html"); |
| 38 | refresh(group, doc); |
| 39 | group.removeAttribute("aria-busy"); |
| 40 | done(group, action, was, value); |
| 41 | }) |
| 42 | .catch(function () { |
| 43 | group.removeAttribute("aria-busy"); |
| 44 | failed(); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | // refresh takes the answer, the sentences and the revision from the |
| 49 | // page as it is now, leaving the Change form's own fields as they are. |
| 50 | function refresh(group, doc) { |
| 51 | var section = group.closest("[data-merge-policy]"); |
| 52 | var fresh = doc.querySelector("[data-merge-ships]"), sentences = doc.querySelector("[data-merge-sentences]"); |
| 53 | if (!fresh) throw new Error("gone"); |
| 54 | group.dataset.mergeShips = fresh.dataset.mergeShips; |
| 55 | group.querySelectorAll("[data-merge-ships-choice]").forEach(function (b) { |
| 56 | b.setAttribute("aria-pressed", b.dataset.mergeShipsChoice === fresh.dataset.mergeShips ? "true" : "false"); |
| 57 | }); |
| 58 | var mine = section.querySelector("[data-merge-sentences]"); |
| 59 | if (mine && sentences) mine.replaceWith(sentences); |
| 60 | var rev = fresh.querySelector("input[name=revision]").value; |
| 61 | section.querySelectorAll("input[name=revision]").forEach(function (i) { i.value = i.defaultValue = rev; }); |
| 62 | } |
| 63 | |
| 64 | // done shows the tick, and Undo while there is something to undo. |
| 65 | function done(group, action, was, value) { |
| 66 | var slot = group.querySelector("[data-merge-ships-done]"); |
| 67 | var template = group.querySelector("template[data-merge-ships-template]"); |
| 68 | if (!slot || !template) return; |
| 69 | slot.innerHTML = template.innerHTML; |
| 70 | var tick = slot.querySelector("[equipo-saved-tick]"), undo = slot.querySelector("[data-merge-ships-undo]"); |
| 71 | tick.setAttribute("data-shown", ""); |
| 72 | setTimeout(function () { tick.setAttribute("data-gone", ""); }, 2400); |
| 73 | if (!was) { |
| 74 | undo.remove(); |
| 75 | var back = group.querySelector('[data-merge-ships-choice="' + value + '"]') || group.querySelector("[data-merge-ships-choice]"); |
| 76 | if (back) back.focus(); |
| 77 | return; |
| 78 | } |
| 79 | undo.addEventListener("click", function () { |
| 80 | if (group.hasAttribute("aria-busy")) return; |
| 81 | set(group, action, was, "", function () { location.reload(); }); |
| 82 | }); |
| 83 | } |
| 84 | })(); |
| 85 | |