|
| 1 | +/** |
| 2 | + * Install Card — In-place version sync |
| 3 | + * |
| 4 | + * Listens for the `boost:version-changed` CustomEvent dispatched by the header |
| 5 | + * version-dropdown JS (see _header_v3.html) and swaps the install card between |
| 6 | + * its "latest" and "older" variants without a page reload. |
| 7 | + * |
| 8 | + * Progressive enhancement: with JS disabled, the dropdown's form submits |
| 9 | + * normally and the server's POST + 302 + GET roundtrip swaps the card. |
| 10 | + * |
| 11 | + * Requires: |
| 12 | + * - Wrapper element [data-install-card-wrapper][data-active-variant] |
| 13 | + * around each install card render (see _install_card.html). |
| 14 | + * - The header JS to dispatch `boost:version-changed` with detail |
| 15 | + * { slug, label, isLatest }. |
| 16 | + */ |
| 17 | + |
| 18 | +function _getOrCreateLiveRegion() { |
| 19 | + var el = document.getElementById("install-card-live"); |
| 20 | + if (el) return el; |
| 21 | + el = document.createElement("span"); |
| 22 | + el.id = "install-card-live"; |
| 23 | + el.setAttribute("aria-live", "polite"); |
| 24 | + el.style.cssText = |
| 25 | + "position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;"; |
| 26 | + document.body.appendChild(el); |
| 27 | + return el; |
| 28 | +} |
| 29 | + |
| 30 | +document.addEventListener("boost:version-changed", function (e) { |
| 31 | + var detail = e.detail || {}; |
| 32 | + var isLatest = detail.isLatest === true; |
| 33 | + var label = detail.label || ""; |
| 34 | + |
| 35 | + document |
| 36 | + .querySelectorAll("[data-install-card-wrapper]") |
| 37 | + .forEach(function (wrapper) { |
| 38 | + var prev = wrapper.getAttribute("data-active-variant"); |
| 39 | + var next = isLatest ? "latest" : "older"; |
| 40 | + |
| 41 | + wrapper.setAttribute("data-active-variant", next); |
| 42 | + |
| 43 | + if (prev !== next) { |
| 44 | + var visible = isLatest |
| 45 | + ? wrapper.querySelector(".install-card:not(.install-card--older)") |
| 46 | + : wrapper.querySelector(".install-card--older"); |
| 47 | + if (visible) { |
| 48 | + visible.classList.remove("install-card--entering"); |
| 49 | + void visible.offsetWidth; |
| 50 | + visible.classList.add("install-card--entering"); |
| 51 | + visible.addEventListener( |
| 52 | + "animationend", |
| 53 | + function () { |
| 54 | + visible.classList.remove("install-card--entering"); |
| 55 | + }, |
| 56 | + { once: true } |
| 57 | + ); |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + var liveRegion = _getOrCreateLiveRegion(); |
| 63 | + liveRegion.textContent = isLatest |
| 64 | + ? "Showing install steps for the latest version." |
| 65 | + : "Showing documentation link for Boost " + label + "."; |
| 66 | +}); |
0 commit comments