switcher: reset selection after browser back navigation #1065

This commit is contained in:
Sören Weber 2025-03-20 21:29:05 +01:00
parent 3ccc8f777f
commit 7a2667a7d3
No known key found for this signature in database
GPG Key ID: BEC6D55545451B6D
8 changed files with 65 additions and 30 deletions

View File

@ -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) { window.addEventListener('popstate', function (event) {
scrollToPositions(); scrollToPositions();
}); });
@ -1757,6 +1793,10 @@ function ready(fn) {
ready(function () { ready(function () {
initArrowVerticalNav(); initArrowVerticalNav();
initArrowHorizontalNav(); initArrowHorizontalNav();
handleHistoryClearer();
handleLanguageSwitcher();
handleVariantSwitcher();
handleVersionSwitcher();
initMermaid(); initMermaid();
initOpenapi(); initOpenapi();
initMenuScrollbar(); initMenuScrollbar();

View File

@ -45,14 +45,14 @@ var variants = {
addCustomVariantOption: function () { addCustomVariantOption: function () {
var customvariant = window.localStorage.getItem(window.relearn.absBaseUri + '/customvariant'); var customvariant = window.localStorage.getItem(window.relearn.absBaseUri + '/customvariant');
var select = document.querySelector('#R-select-variant'); if (!customvariant ) {
if (!customvariant || !select) {
return; return;
} }
var option = document.querySelector('#R-select-variant-' + this.customvariantname); document.querySelectorAll('.R-variantswitcher select').forEach((select) => {
var option = select.options[`R-select-variant-${this.customvariantname}`];
if (!option) { if (!option) {
option = document.createElement('option'); option = document.createElement('option');
option.id = 'R-select-variant-' + this.customvariantname; option.id = `R-select-variant-${this.customvariantname}`;
option.value = this.customvariantname; option.value = this.customvariantname;
option.text = this.customvariantname.replace(/-/g, ' ').replace(/\w\S*/g, function (w) { option.text = this.customvariantname.replace(/-/g, ' ').replace(/\w\S*/g, function (w) {
return w.replace(/^\w/g, function (c) { return w.replace(/^\w/g, function (c) {
@ -61,13 +61,13 @@ var variants = {
}); });
select.appendChild(option); select.appendChild(option);
} }
});
}, },
removeCustomVariantOption: function () { removeCustomVariantOption: function () {
var option = document.querySelector('#R-select-variant-' + this.customvariantname); document.querySelectorAll(`.R-variantswitcher option[value=${this.customvariantname}]`).forEach((option) => {
if (option) {
option.remove(); option.remove();
} });
}, },
addCustomVariantStyles: function () { addCustomVariantStyles: function () {

View File

@ -7,7 +7,7 @@
<i class="{{ $icon }}"></i> <i class="{{ $icon }}"></i>
<span>&nbsp;</span> <span>&nbsp;</span>
<div class="control-style"> <div class="control-style">
<button onclick="clearHistory();">{{ T "Clear-History" }}</button> <button>{{ T "Clear-History" }}</button>
</div> </div>
<div class="clear"></div> <div class="clear"></div>
</div> </div>

View File

@ -8,10 +8,10 @@
<span>&nbsp;</span> <span>&nbsp;</span>
<div class="control-style"> <div class="control-style">
<label class="a11y-only" for="R-select-language">{{ T "Language" }}</label> <label class="a11y-only" for="R-select-language">{{ T "Language" }}</label>
<select id="R-select-language" onchange="location = this.querySelector( `#R-select-language-${this.value}` ).dataset.url;"> <select id="R-select-language">
{{- $pageLang := .page.Language.Lang }} {{- $pageLang := .page.Language.Lang }}
{{- range .page.AllTranslations }} {{- range .page.AllTranslations }}
<option id="R-select-language-{{ .Language.Lang }}" value="{{ .Language.Lang }}" data-url="{{ partial "permalink.gotmpl" (dict "to" .) }}" lang="{{ .Language.LanguageCode }}"{{ if eq $pageLang .Language.Lang }} selected{{ end }}>{{ .Language.LanguageName }}</option> <option id="R-select-language-{{ .Language.Lang }}" value="{{ .Language.Lang }}" data-url="{{ partial "permalink.gotmpl" (dict "to" .) }}" lang="{{ .Language.LanguageCode }}"{{ if eq $pageLang .Language.Lang }} selected data-selected{{ end }}>{{ .Language.LanguageName }}</option>
{{- end }} {{- end }}
</select> </select>
</div> </div>

View File

@ -9,7 +9,7 @@
<span>&nbsp;</span> <span>&nbsp;</span>
<div class="control-style"> <div class="control-style">
<label class="a11y-only" for="R-select-variant">{{ T "Theme" }}</label> <label class="a11y-only" for="R-select-variant">{{ T "Theme" }}</label>
<select id="R-select-variant" onchange="window.relearn.changeVariant( this.value );"> <select id="R-select-variant">
{{- $firstvariant := true }} {{- $firstvariant := true }}
{{- range $themevariants }} {{- range $themevariants }}
{{- $themevariant := . }} {{- $themevariant := . }}

View File

@ -10,12 +10,9 @@
<span>&nbsp;</span> <span>&nbsp;</span>
<div class="control-style"> <div class="control-style">
<label class="a11y-only" for="R-select-version">{{ T "Version" }}</label> <label class="a11y-only" for="R-select-version">{{ T "Version" }}</label>
<select id="R-select-version" onchange="location = this.querySelector( `#R-select-version-${this.value}` ).dataset.abs == 'true' ? <select id="R-select-version">
this.querySelector( `#R-select-version-${this.value}` ).dataset.uri + window.relearn.path :
window.relearn.relBaseUri + this.querySelector( `#R-select-version-${this.value}` ).dataset.uri + window.relearn.path;"
>
{{- range $versions }} {{- range $versions }}
<option id="R-select-version-{{ .identifier | anchorize }}" value="{{ .identifier | anchorize }}" data-identifier="{{ .identifier }}" data-abs="{{ .isAbs }}" data-uri="{{ .baseURL }}"{{ if eq $pageVersion .identifier }} selected{{ end }}>{{ .title }}</option> <option id="R-select-version-{{ .identifier | anchorize }}" value="{{ .identifier | anchorize }}" data-identifier="{{ .identifier }}" data-abs="{{ .isAbs }}" data-uri="{{ .baseURL }}"{{ if eq $pageVersion .identifier }} selected data-selected{{ end }}>{{ .title }}</option>
{{- end }} {{- end }}
</select> </select>
</div> </div>

View File

@ -153,14 +153,12 @@
document.documentElement.dataset.rThemeVariant = variant; document.documentElement.dataset.rThemeVariant = variant;
if (oldVariant != variant) { if (oldVariant != variant) {
document.dispatchEvent( new CustomEvent('themeVariantLoaded', { detail: { variant, oldVariant } }) ); document.dispatchEvent( new CustomEvent('themeVariantLoaded', { detail: { variant, oldVariant } }) );
window.relearn.markVariant();
} }
} }
window.relearn.markVariant = function() { window.relearn.markVariant = function() {
var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant"); var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant");
var select = document.querySelector("#R-select-variant"); document.querySelectorAll(".R-variantswitcher select").forEach((select) => {select.value = variant;});
if (select) {
select.value = variant;
}
} }
window.relearn.initVariant = function() { window.relearn.initVariant = function() {
var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant") ?? ""; var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant") ?? "";

View File

@ -1 +1 @@
7.5.0+a05b2124652293b1be3742f26aa3913861f956de 7.5.0+3ccc8f777f8dd35f750a00ca986f9db808548306