mirror of
https://github.com/McShelby/hugo-theme-relearn
synced 2025-04-30 09:26:33 +08:00
switcher: reset selection after browser back navigation #1065
This commit is contained in:
parent
3ccc8f777f
commit
7a2667a7d3
@ -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();
|
||||||
|
@ -45,29 +45,29 @@ 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) => {
|
||||||
if (!option) {
|
var option = select.options[`R-select-variant-${this.customvariantname}`];
|
||||||
option = document.createElement('option');
|
if (!option) {
|
||||||
option.id = 'R-select-variant-' + this.customvariantname;
|
option = document.createElement('option');
|
||||||
option.value = this.customvariantname;
|
option.id = `R-select-variant-${this.customvariantname}`;
|
||||||
option.text = this.customvariantname.replace(/-/g, ' ').replace(/\w\S*/g, function (w) {
|
option.value = this.customvariantname;
|
||||||
return w.replace(/^\w/g, function (c) {
|
option.text = this.customvariantname.replace(/-/g, ' ').replace(/\w\S*/g, function (w) {
|
||||||
return c.toUpperCase();
|
return w.replace(/^\w/g, function (c) {
|
||||||
|
return c.toUpperCase();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
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 () {
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
<i class="{{ $icon }}"></i>
|
<i class="{{ $icon }}"></i>
|
||||||
<span> </span>
|
<span> </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>
|
||||||
|
@ -8,10 +8,10 @@
|
|||||||
<span> </span>
|
<span> </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>
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
<span> </span>
|
<span> </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 := . }}
|
||||||
|
@ -10,12 +10,9 @@
|
|||||||
<span> </span>
|
<span> </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>
|
||||||
|
@ -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") ?? "";
|
||||||
|
@ -1 +1 @@
|
|||||||
7.5.0+a05b2124652293b1be3742f26aa3913861f956de
|
7.5.0+3ccc8f777f8dd35f750a00ca986f9db808548306
|
Loading…
x
Reference in New Issue
Block a user