From 17ff2a094dc0ca075ff1d57536da44593550bbc0 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 20 Mar 2022 20:28:43 +0400 Subject: [PATCH] Enabled home button in the iframe-based viewer --- static/viewer.html | 71 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/static/viewer.html b/static/viewer.html index e7ece5ea7..22e8599e7 100644 --- a/static/viewer.html +++ b/static/viewer.html @@ -27,13 +27,16 @@
+ + +
@@ -64,6 +67,63 @@ return `/content/${url}`; } + function getBookFromUserUrl(url) { + if ( url == '' ) { + return null; + } + + return url.split('/')[0]; + } + + let currentBook = null; + let currentBookTitle = null; + + const bookUIGroup = document.getElementById('kiwix_serve_taskbar_book_ui_group'); + const homeButton = document.getElementById('kiwix_serve_taskbar_home_button'); + + function gotoMainPageOfCurrentBook() { + location.hash = currentBook + '/'; + } + + function setCurrentBook(book, title) { + currentBook = book; + currentBookTitle = title; + homeButton.title = `Go to the main page of '${title}'`; + homeButton.setAttribute("aria-label", homeButton.title); + homeButton.innerHTML = ``; + bookUIGroup.style.display = 'inline'; + } + + function noCurrentBook() { + currentBook = null; + currentBookTitle = null; + bookUIGroup.style.display = 'none'; + } + + function updateCurrentBookIfNeeded(userUrl) { + const book = getBookFromUserUrl(userUrl); + if ( currentBook != book ) { + updateCurrentBook(book); + } + } + + function updateCurrentBook(book) { + if ( book == null ) { + noCurrentBook(); + } else { + fetch(`./raw/${book}/meta/Title`).then(async (resp) => { + if ( resp.ok ) { + setCurrentBook(book, await resp.text()); + } else { + noCurrentBook(); + } + }).catch((err) => { + console.log("Error fetching book title: " + err); + noCurrentBook(); + }); + } + } + function iframeUrl2UserUrl(url) { if ( url == '/skin/blank.html' ) { return ''; @@ -80,8 +140,9 @@ window.onresize = handle_visual_viewport_change; function handle_location_hash_change() { - const hash = window.location.hash; - const iframeContentUrl = userUrl2IframeUrl(hash.slice(1)); + const hash = window.location.hash.slice(1); + updateCurrentBookIfNeeded(hash); + const iframeContentUrl = userUrl2IframeUrl(hash); console.log("handle_location_hash_change: " + hash); if ( iframeContentUrl != cf.contentWindow.location.pathname ) { cf.contentWindow.location.replace(iframeContentUrl);