From 7a2667a7d3f4f14d5188e9df975ce6e3799e83c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Thu, 20 Mar 2025 21:29:05 +0100 Subject: [PATCH] switcher: reset selection after browser back navigation #1065 --- assets/js/theme.js | 40 +++++++++++++++++++ docs/assets/js/variant.js | 32 +++++++-------- .../sidebar/element/historyclearer.html | 2 +- .../sidebar/element/languageswitcher.html | 4 +- .../sidebar/element/variantswitcher.html | 2 +- .../sidebar/element/versionswitcher.html | 7 +--- layouts/partials/stylesheet.html | 6 +-- layouts/partials/version.txt | 2 +- 8 files changed, 65 insertions(+), 30 deletions(-) diff --git a/assets/js/theme.js b/assets/js/theme.js index b94f0b1d6f..a0cd5fb5a1 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -1444,6 +1444,42 @@ function scrollToPositions() { } } +function handleHistoryClearer() { + document.querySelectorAll('.R-historyclearer button').forEach(function (select) { + select.addEventListener('click', function (event) { + clearHistory(); + }); + }); +} + +function handleLanguageSwitcher() { + document.querySelectorAll('.R-languageswitcher select').forEach(function (select) { + select.addEventListener('change', function (event) { + const url = this.options[`R-select-language-${this.value}`].dataset.url; + this.value = this.querySelector('[data-selected]')?.value ?? select.value; + window.location = url; + }); + }); +} + +function handleVariantSwitcher() { + document.querySelectorAll('.R-variantswitcher select').forEach(function (select) { + select.addEventListener('change', function (event) { + window.relearn.changeVariant(this.value); + }); + }); +} + +function handleVersionSwitcher() { + document.querySelectorAll('.R-versionswitcher select').forEach(function (select) { + select.addEventListener('change', function (event) { + const url = (this.options[`R-select-version-${this.value}`].dataset.abs == 'true' ? '' : window.relearn.relBaseUri) + this.options[`R-select-version-${this.value}`].dataset.uri + window.relearn.path; + this.value = this.querySelector('[data-selected]')?.value ?? select.value; + window.location = url; + }); + }); +} + window.addEventListener('popstate', function (event) { scrollToPositions(); }); @@ -1757,6 +1793,10 @@ function ready(fn) { ready(function () { initArrowVerticalNav(); initArrowHorizontalNav(); + handleHistoryClearer(); + handleLanguageSwitcher(); + handleVariantSwitcher(); + handleVersionSwitcher(); initMermaid(); initOpenapi(); initMenuScrollbar(); diff --git a/docs/assets/js/variant.js b/docs/assets/js/variant.js index 7c4edb86c9..d823a6f568 100644 --- a/docs/assets/js/variant.js +++ b/docs/assets/js/variant.js @@ -45,29 +45,29 @@ var variants = { addCustomVariantOption: function () { var customvariant = window.localStorage.getItem(window.relearn.absBaseUri + '/customvariant'); - var select = document.querySelector('#R-select-variant'); - if (!customvariant || !select) { + if (!customvariant ) { return; } - var option = document.querySelector('#R-select-variant-' + this.customvariantname); - if (!option) { - option = document.createElement('option'); - option.id = 'R-select-variant-' + this.customvariantname; - option.value = this.customvariantname; - option.text = this.customvariantname.replace(/-/g, ' ').replace(/\w\S*/g, function (w) { - return w.replace(/^\w/g, function (c) { - return c.toUpperCase(); + document.querySelectorAll('.R-variantswitcher select').forEach((select) => { + var option = select.options[`R-select-variant-${this.customvariantname}`]; + if (!option) { + option = document.createElement('option'); + option.id = `R-select-variant-${this.customvariantname}`; + option.value = this.customvariantname; + option.text = this.customvariantname.replace(/-/g, ' ').replace(/\w\S*/g, function (w) { + return w.replace(/^\w/g, function (c) { + return c.toUpperCase(); + }); }); - }); - select.appendChild(option); - } + select.appendChild(option); + } + }); }, removeCustomVariantOption: function () { - var option = document.querySelector('#R-select-variant-' + this.customvariantname); - if (option) { + document.querySelectorAll(`.R-variantswitcher option[value=${this.customvariantname}]`).forEach((option) => { option.remove(); - } + }); }, addCustomVariantStyles: function () { diff --git a/layouts/partials/sidebar/element/historyclearer.html b/layouts/partials/sidebar/element/historyclearer.html index 0422663079..6cb6559954 100644 --- a/layouts/partials/sidebar/element/historyclearer.html +++ b/layouts/partials/sidebar/element/historyclearer.html @@ -7,7 +7,7 @@  
- +
diff --git a/layouts/partials/sidebar/element/languageswitcher.html b/layouts/partials/sidebar/element/languageswitcher.html index 9a1b57035d..f7d9d87b6a 100644 --- a/layouts/partials/sidebar/element/languageswitcher.html +++ b/layouts/partials/sidebar/element/languageswitcher.html @@ -8,10 +8,10 @@  
- {{- $pageLang := .page.Language.Lang }} {{- range .page.AllTranslations }} - + {{- end }}
diff --git a/layouts/partials/sidebar/element/variantswitcher.html b/layouts/partials/sidebar/element/variantswitcher.html index c82868bd68..398e8fb8e9 100644 --- a/layouts/partials/sidebar/element/variantswitcher.html +++ b/layouts/partials/sidebar/element/variantswitcher.html @@ -9,7 +9,7 @@  
- {{- $firstvariant := true }} {{- range $themevariants }} {{- $themevariant := . }} diff --git a/layouts/partials/sidebar/element/versionswitcher.html b/layouts/partials/sidebar/element/versionswitcher.html index 17c077f185..d49a3e9244 100644 --- a/layouts/partials/sidebar/element/versionswitcher.html +++ b/layouts/partials/sidebar/element/versionswitcher.html @@ -10,12 +10,9 @@  
- {{- range $versions }} - + {{- end }}
diff --git a/layouts/partials/stylesheet.html b/layouts/partials/stylesheet.html index ab13a1a2ab..a44d1b729b 100644 --- a/layouts/partials/stylesheet.html +++ b/layouts/partials/stylesheet.html @@ -153,14 +153,12 @@ document.documentElement.dataset.rThemeVariant = variant; if (oldVariant != variant) { document.dispatchEvent( new CustomEvent('themeVariantLoaded', { detail: { variant, oldVariant } }) ); + window.relearn.markVariant(); } } window.relearn.markVariant = function() { var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant"); - var select = document.querySelector("#R-select-variant"); - if (select) { - select.value = variant; - } + document.querySelectorAll(".R-variantswitcher select").forEach((select) => {select.value = variant;}); } window.relearn.initVariant = function() { var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant") ?? ""; diff --git a/layouts/partials/version.txt b/layouts/partials/version.txt index f012ce2d4a..fa88e6819c 100644 --- a/layouts/partials/version.txt +++ b/layouts/partials/version.txt @@ -1 +1 @@ -7.5.0+a05b2124652293b1be3742f26aa3913861f956de \ No newline at end of file +7.5.0+3ccc8f777f8dd35f750a00ca986f9db808548306 \ No newline at end of file