Skip to content

Commit ceb79ee

Browse files
committed
Swap install card in-place on version change
The header dropdown's fetch handler now fires a custom event after setting the cookie. A small listener flips the card wrapper and updates the doc link so the swap happens without a reload. Pure progressive enhancement.
1 parent 92fe4fc commit ceb79ee

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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. When switching to
7+
* the older variant, updates the "See Documentation" link to point at the
8+
* selected version's docs.
9+
*
10+
* Progressive enhancement: with JS disabled, the dropdown's form submits
11+
* normally and the server's POST + 302 + GET roundtrip swaps the card.
12+
*
13+
* Requires:
14+
* - Wrapper element [data-install-card-wrapper][data-active-variant]
15+
* around each install card render (see _install_card.html).
16+
* - data-doc-link-wrapper marker around the older-variant CTA link.
17+
* - The header JS to dispatch `boost:version-changed` with detail
18+
* { slug, label, isLatest, docUrl }.
19+
*/
20+
document.addEventListener("boost:version-changed", function (e) {
21+
var detail = e.detail || {};
22+
var isLatest = detail.isLatest === true;
23+
var docUrl = detail.docUrl || "";
24+
25+
document
26+
.querySelectorAll("[data-install-card-wrapper]")
27+
.forEach(function (wrapper) {
28+
wrapper.setAttribute(
29+
"data-active-variant",
30+
isLatest ? "latest" : "older"
31+
);
32+
if (!isLatest && docUrl) {
33+
var linkWrapper = wrapper.querySelector(
34+
"[data-install-card-doc-link-wrapper]"
35+
);
36+
var link = linkWrapper && linkWrapper.querySelector("a");
37+
if (link) link.setAttribute("href", docUrl);
38+
}
39+
});
40+
});

templates/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<script src="{% static 'js/code-block.js' %}" defer></script>
5050
<script src="{% static 'js/boost-gecko/main.CsWsx5SC.js' %}" defer></script>
5151
<script src="{% static 'js/install-card.js' %}" defer></script>
52+
<script src="{% static 'js/install-card-version-sync.js' %}" defer></script>
5253
{% else %}
5354
<script src="{% static 'js/boost-gecko/main.D1bdgn0u.js' %}" defer></script>
5455
{% endflag %}

templates/v3/includes/_header_v3.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@
148148
* Progressive enhancement for the cookie-mode version dropdown: send the
149149
* POST via fetch and update the UI in place, avoiding a full page reload.
150150
* Falls back to a normal form submit on any error.
151+
*
152+
* After a successful fetch, dispatch a `boost:version-changed` CustomEvent
153+
* on document so other components (e.g. the install card) can react without
154+
* a page reload. Detail: { slug, label, isLatest, docUrl }.
151155
*/
152156
document.addEventListener('submit', function (e) {
153157
var form = e.target;
@@ -161,6 +165,8 @@
161165

162166
var selectedValue = submitter.value;
163167
var newLabel = submitter.textContent.trim();
168+
var isLatest = submitter.dataset.isLatest === 'true';
169+
var docUrl = submitter.dataset.docUrl || '';
164170
var formData = new FormData(form);
165171
formData.set('version', selectedValue);
166172

@@ -189,6 +195,10 @@
189195
var toggle = menu.querySelector('.header__version-toggle');
190196
if (toggle) toggle.checked = false;
191197
});
198+
199+
document.dispatchEvent(new CustomEvent('boost:version-changed', {
200+
detail: { slug: selectedValue, label: newLabel, isLatest: isLatest, docUrl: docUrl }
201+
}));
192202
}).catch(function () {
193203
form.submit();
194204
});

0 commit comments

Comments
 (0)