Navigation history tracks UI language changes

This commit is contained in:
Veloman Yunkan 2023-01-20 17:30:34 +04:00
parent 6c2f229d31
commit ca65dd9000
2 changed files with 35 additions and 4 deletions

View File

@ -211,6 +211,7 @@ function handle_location_hash_change() {
}
updateSearchBoxForLocationChange();
previousScrollTop = Infinity;
history.replaceState(viewerState, null);
}
function handle_content_url_change() {
@ -220,8 +221,7 @@ function handle_content_url_change() {
const iframeContentUrl = iframeLocation.pathname;
const iframeContentQuery = iframeLocation.search;
const newHash = iframeUrl2UserUrl(iframeContentUrl, iframeContentQuery);
const viewerURL = location.origin + location.pathname + location.search;
window.location.replace(viewerURL + '#' + newHash);
history.replaceState(viewerState, null, makeURL(location.search, newHash));
updateCurrentBookIfNeeded(newHash);
};
@ -403,6 +403,16 @@ function setupSuggestions() {
});
}
function makeURL(search, hash) {
let url = location.origin + location.pathname;
if (search != "") {
url += (search[0] == '?' ? search : '?' + search);
}
url += (hash[0] == '#' ? hash : '#' + hash);
return url;
}
function initUILanguageSelector() {
const languageSelector = document.getElementById("ui_language");
for (const lang of uiLanguages ) {
@ -413,12 +423,32 @@ function initUILanguageSelector() {
}
}
function updateUILanguageSelector(userLang) {
console.log(`updateUILanguageSelector(${userLang})`);
const languageSelector = document.getElementById("ui_language");
for (const opt of languageSelector.children ) {
if ( opt.value == userLang ) {
opt.selected = true;
}
}
}
function handle_history_state_change(event) {
console.log(`handle_history_state_change`);
if ( event.state ) {
viewerState = event.state;
updateUILanguageSelector(viewerState.uiLanguage);
setUserLanguage(viewerState.uiLanguage, updateUIText);
}
}
function changeUILanguage() {
const s = document.getElementById("ui_language");
const lang = s.options[s.selectedIndex].value;
viewerState.uiLanguage = lang;
setUserLanguage(lang, () => {
updateUIText();
history.pushState(viewerState, null);
});
}
@ -474,6 +504,7 @@ function finishViewerSetupOnceTranslationsAreLoaded()
handle_location_hash_change();
window.onhashchange = handle_location_hash_change;
window.onpopstate = handle_history_state_change;
viewerSetupComplete = true;
}

View File

@ -73,7 +73,7 @@ const ResourceCollection resources200Compressible{
{ DYNAMIC_CONTENT, "/ROOT/skin/taskbar.css" },
{ STATIC_CONTENT, "/ROOT/skin/taskbar.css?cacheid=2cbac34b" },
{ DYNAMIC_CONTENT, "/ROOT/skin/viewer.js" },
{ STATIC_CONTENT, "/ROOT/skin/viewer.js?cacheid=eca8fdbe" },
{ STATIC_CONTENT, "/ROOT/skin/viewer.js?cacheid=4f0e70d6" },
{ DYNAMIC_CONTENT, "/ROOT/skin/fonts/Poppins.ttf" },
{ STATIC_CONTENT, "/ROOT/skin/fonts/Poppins.ttf?cacheid=af705837" },
{ DYNAMIC_CONTENT, "/ROOT/skin/fonts/Roboto.ttf" },
@ -302,7 +302,7 @@ R"EXPECTEDRESULT( <link type="text/css" href="./skin/taskbar.css?cacheid=2cba
<link type="text/css" href="./skin/css/autoComplete.css?cacheid=08951e06" rel="Stylesheet" />
<script type="module" src="./skin/i18n.js?cacheid=eb41f5ce" defer></script>
<script type="text/javascript" src="./skin/languages.js?cacheid=fe100348" defer></script>
<script type="text/javascript" src="./skin/viewer.js?cacheid=eca8fdbe" defer></script>
<script type="text/javascript" src="./skin/viewer.js?cacheid=4f0e70d6" defer></script>
<script type="text/javascript" src="./skin/autoComplete.min.js?cacheid=1191aaaf"></script>
const blankPageUrl = root + "/skin/blank.html?cacheid=6b1fa032";
<label for="kiwix_button_show_toggle"><img src="./skin/caret.png?cacheid=22b942b4" alt=""></label>