mirror of
https://github.com/McShelby/hugo-theme-relearn
synced 2025-04-30 11:29:05 +08:00
version: fix multiple switchers #1050
This commit is contained in:
parent
b5f8bcc4e3
commit
17771625ba
@ -1996,12 +1996,9 @@ function initVersionIndex(index) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var select = document.querySelector('#R-select-version');
|
document.querySelectorAll('.R-versionswitcher select').forEach(function (select) {
|
||||||
if (!select) {
|
var preSelectedOption = select.querySelector('[data-selected]')?.cloneNode(true);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remember the currently selected option
|
|
||||||
var selectedOption = null;
|
var selectedOption = null;
|
||||||
if (select.selectedIndex >= 0) {
|
if (select.selectedIndex >= 0) {
|
||||||
selectedOption = select.options[select.selectedIndex].cloneNode(true);
|
selectedOption = select.options[select.selectedIndex].cloneNode(true);
|
||||||
@ -2027,24 +2024,27 @@ function initVersionIndex(index) {
|
|||||||
select.appendChild(option);
|
select.appendChild(option);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Re-select the previously selected option if it exists
|
if (preSelectedOption) {
|
||||||
|
const option = select.querySelector(`option[value="${preSelectedOption.value}"]`);
|
||||||
|
if (!option) {
|
||||||
|
select.appendChild(preSelectedOption);
|
||||||
|
} else {
|
||||||
|
option.dataset.selected = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
if (selectedOption) {
|
if (selectedOption) {
|
||||||
for (var i = 0; i < select.options.length; i++) {
|
// Re-select the previously selected option if it exists
|
||||||
if (select.options[i].dataset.identifier === selectedOption.dataset.identifier) {
|
const option = select.querySelector(`option[value="${selectedOption.value}"]`);
|
||||||
select.selectedIndex = i;
|
if (!option) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the previously selected option doesn't exist, add it at the end
|
|
||||||
select.appendChild(selectedOption);
|
select.appendChild(selectedOption);
|
||||||
select.selectedIndex = select.options.length - 1;
|
}
|
||||||
return;
|
select.value = selectedOption.value;
|
||||||
} else if (select.options.length > 0) {
|
} else if (select.options.length > 0) {
|
||||||
// If there was no selection before, select the first option
|
// If there was no selection before, select the first option
|
||||||
select.selectedIndex = 0;
|
select.selectedIndex = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function initVersionJs() {
|
function initVersionJs() {
|
||||||
|
@ -65,7 +65,7 @@ var variants = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
removeCustomVariantOption: function () {
|
removeCustomVariantOption: function () {
|
||||||
document.querySelectorAll(`.R-variantswitcher option[value=${this.customvariantname}]`).forEach((option) => {
|
document.querySelectorAll(`.R-variantswitcher option[value="${this.customvariantname}"]`).forEach((option) => {
|
||||||
option.remove();
|
option.remove();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -1 +1 @@
|
|||||||
7.5.0+7a2667a7d3f4f14d5188e9df975ce6e3799e83c3
|
7.5.0+b5f8bcc4e397011d3dbec2ca215d641dd7a02689
|
Loading…
x
Reference in New Issue
Block a user