From 17771625ba626b69a86cdc46508e4e58633c090d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Thu, 20 Mar 2025 22:59:41 +0100 Subject: [PATCH] version: fix multiple switchers #1050 --- assets/js/theme.js | 92 ++++++++++++++++++------------------ docs/assets/js/variant.js | 2 +- layouts/partials/version.txt | 2 +- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/assets/js/theme.js b/assets/js/theme.js index a0cd5fb5a1..ddf99457ec 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -1996,55 +1996,55 @@ function initVersionIndex(index) { return; } - var select = document.querySelector('#R-select-version'); - if (!select) { - return; - } + document.querySelectorAll('.R-versionswitcher select').forEach(function (select) { + var preSelectedOption = select.querySelector('[data-selected]')?.cloneNode(true); - // Remember the currently selected option - var selectedOption = null; - if (select.selectedIndex >= 0) { - selectedOption = select.options[select.selectedIndex].cloneNode(true); - } - - // Remove all existing options - while (select.firstChild) { - select.removeChild(select.firstChild); - } - - // Add all options from the index - index.forEach(function (version) { - // Create new option element - var option = document.createElement('option'); - option.id = 'R-select-version-' + version.value; - option.value = version.value; - option.dataset.abs = version.isAbs; - option.dataset.uri = version.baseURL; - option.dataset.identifier = version.identifier; - option.textContent = version.title; - - // Add the option to the select - select.appendChild(option); - }); - - // Re-select the previously selected option if it exists - if (selectedOption) { - for (var i = 0; i < select.options.length; i++) { - if (select.options[i].dataset.identifier === selectedOption.dataset.identifier) { - select.selectedIndex = i; - return; - } + var selectedOption = null; + if (select.selectedIndex >= 0) { + selectedOption = select.options[select.selectedIndex].cloneNode(true); } - // If the previously selected option doesn't exist, add it at the end - select.appendChild(selectedOption); - select.selectedIndex = select.options.length - 1; - return; - } else if (select.options.length > 0) { - // If there was no selection before, select the first option - select.selectedIndex = 0; - return; - } + // Remove all existing options + while (select.firstChild) { + select.removeChild(select.firstChild); + } + + // Add all options from the index + index.forEach(function (version) { + // Create new option element + var option = document.createElement('option'); + option.id = 'R-select-version-' + version.value; + option.value = version.value; + option.dataset.abs = version.isAbs; + option.dataset.uri = version.baseURL; + option.dataset.identifier = version.identifier; + option.textContent = version.title; + + // Add the option to the select + select.appendChild(option); + }); + + if (preSelectedOption) { + const option = select.querySelector(`option[value="${preSelectedOption.value}"]`); + if (!option) { + select.appendChild(preSelectedOption); + } else { + option.dataset.selected = ''; + } + } + if (selectedOption) { + // Re-select the previously selected option if it exists + const option = select.querySelector(`option[value="${selectedOption.value}"]`); + if (!option) { + select.appendChild(selectedOption); + } + select.value = selectedOption.value; + } else if (select.options.length > 0) { + // If there was no selection before, select the first option + select.selectedIndex = 0; + return; + } + }); } function initVersionJs() { diff --git a/docs/assets/js/variant.js b/docs/assets/js/variant.js index d823a6f568..559c4d4cf1 100644 --- a/docs/assets/js/variant.js +++ b/docs/assets/js/variant.js @@ -65,7 +65,7 @@ var variants = { }, removeCustomVariantOption: function () { - document.querySelectorAll(`.R-variantswitcher option[value=${this.customvariantname}]`).forEach((option) => { + document.querySelectorAll(`.R-variantswitcher option[value="${this.customvariantname}"]`).forEach((option) => { option.remove(); }); }, diff --git a/layouts/partials/version.txt b/layouts/partials/version.txt index 9a43d3fff5..789bca0947 100644 --- a/layouts/partials/version.txt +++ b/layouts/partials/version.txt @@ -1 +1 @@ -7.5.0+7a2667a7d3f4f14d5188e9df975ce6e3799e83c3 \ No newline at end of file +7.5.0+b5f8bcc4e397011d3dbec2ca215d641dd7a02689 \ No newline at end of file