diff options
author | Andrew Dolgov <[email protected]> | 2024-01-13 21:10:37 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2024-01-13 21:10:37 +0300 |
commit | d5cb80de13cf5fdb98895a1b983994c24e59048e (patch) | |
tree | e951491cbbc52cd8d9161bfd5101444251982ec8 /js | |
parent | 4cd967ec87da352ad2b792dc4b41995cdb5e2df4 (diff) |
more async/await; reduce debugging messages
Diffstat (limited to 'js')
-rw-r--r-- | js/reader.js | 128 |
1 files changed, 62 insertions, 66 deletions
diff --git a/js/reader.js b/js/reader.js index 00afabc..26a090d 100644 --- a/js/reader.js +++ b/js/reader.js @@ -129,8 +129,6 @@ const Reader = { $(".loading-message").html(`Unable to load book info.<br/><small>${e.responseText}</small>`); return; } - } else { - console.log('bookinfo already stored'); } console.log('trying to load book...'); @@ -159,9 +157,6 @@ const Reader = { $(".loading-message").html(`Unable to download book: ${book_resp.status}`); return; } - - } else { - console.log("local data already present for book"); } const fileReader = new FileReader(); @@ -265,17 +260,25 @@ const Reader = { } }, loadLocations: async function(book) { - console.log('init locations...'); - let locations = await localforage.getItem(Reader.cacheId("locations")); // legacy format is array of objects {cfi: ..., page: ...} if (locations && typeof locations[0] == "string") { console.log('loading local locations...'); - return book.locations.load(locations); - } else { - console.log("requesting locations..."); + + try { + await book.locations.load(locations); + return; + } catch (e) { + console.warn(e); + } + } + + if (App.isOnline()) { + console.log("downloading locations..."); + + $(".loading-message").html("Downloading locations…"); const resp = await fetch("backend.php?op=getpagination&id=" + $.urlParam("id")); @@ -289,22 +292,29 @@ const Reader = { await localforage.setItem(Reader.cacheId("locations"), locations); - return book.locations.load(locations); + try { + book.locations.load(locations); + + return; + } catch (e) { + console.warn(e); + } } } + } - $(".loading-message").html("Preparing locations…"); + $(".loading-message").html("Preparing locations…"); - locations = await book.locations.generate(100); + locations = await book.locations.generate(100); - $.post("backend.php", { op: "storepagination", id: $.urlParam("id"), - payload: JSON.stringify(locations), total: 100}); + $.post("backend.php", { op: "storepagination", id: $.urlParam("id"), + payload: JSON.stringify(locations), total: 100}); - await localforage.setItem(Reader.cacheId("locations"), locations); - } + await localforage.setItem(Reader.cacheId("locations"), locations); }, initRenditionHooks: function(book, rendition) { rendition.hooks.content.register(async (contents) => { + contents.on("linkClicked", (href) => { console.log('linkClicked', href); @@ -321,6 +331,8 @@ const Reader = { } }); + // get our stuff from resource loader and inject into iframe content + const base_url = window.location.href.match(/^.*\//)[0]; if (typeof EpubeApp != "undefined") { @@ -371,7 +383,6 @@ const Reader = { }, 150); Reader.applyTheme(); - Reader.resizeSideColumns(); try { @@ -406,15 +417,13 @@ const Reader = { if (book.locations.length() == 0) return; - localforage.getItem("epube.enable-column-hacks").then((enable) => { - if (enable && Reader.Page._moved_next >= 20) { - console.log('forcing re-render because of column hacks'); - - rendition.onResized($("#reader").width()); + const column_hacks = await localforage.getItem("epube.enable-column-hacks"); - Reader.Page._moved_next = 0; - } - }); + if (column_hacks && Reader.Page._moved_next >= 20) { + console.log('forcing re-render because of column hacks'); + rendition.onResized($("#reader").width()); + Reader.Page._moved_next = 0; + } const currentCfi = location.start.cfi; const currentPct = parseInt(book.locations.percentageFromCfi(currentCfi) * 100); @@ -456,8 +465,6 @@ const Reader = { }); }, initModals: function() { - console.log('initializing modals...'); - $('#settings-modal').on('shown.bs.modal', async function() { const last_read = await localforage.getItem(Reader.cacheId("lastread")); @@ -574,8 +581,6 @@ const Reader = { }); }, initToc: function(book) { - console.log('initializing toc modal...'); - function toc_loc_msg(href) { try { const cfiBase = book.spine.get(href).cfiBase; @@ -673,8 +678,6 @@ const Reader = { }); }, initStyleHooks: function(book) { - console.log('registering style hooks...'); - /* embedded styles may conflict with our font sizes, etc */ book.spine.hooks.content.register(function(doc/*, section */) { @@ -927,7 +930,6 @@ const Reader = { } }, generateTocBar: function(book, toc) { - $(".spacer") .html(""); @@ -959,7 +961,6 @@ const Reader = { $(".spacer").append($("<div class='toc-bar-entry current-position'>")); Reader.updateTocBarPosition(book, book.rendition.currentLocation()) - }, updateTocBarPosition: function(book, location) { const cur_pct = Math.round(location.start.location / book.locations.length() * 100); @@ -1087,28 +1088,24 @@ const Reader = { if (iframe && iframe.contentWindow.$) width += parseInt(iframe.contentWindow.$("body").css("padding-left")); - //console.log("resize columns, width=", width); - $("#left, #right").width(width); }, - markAsRead: function() { + markAsRead: async function() { if (confirm("Mark book as read?")) { const total = 100; const lastCfi = window.book.locations.cfiFromPercentage(1); const lastread_timestamp = new Date().getTime(); if (App.isOnline()) { - $.post("backend.php", { op: "storelastread", page: total, cfi: lastCfi, id: $.urlParam("id"), timestamp: lastread_timestamp }, function(data) { - $(".lastread_input").val(data.page + '%'); - }); + const data = await $.post("backend.php", { op: "storelastread", page: total, cfi: lastCfi, id: $.urlParam("id"), timestamp: lastread_timestamp }); + $(".lastread_input").val(data.page + '%'); } - localforage.setItem(Reader.cacheId("lastread"), + await localforage.setItem(Reader.cacheId("lastread"), {cfi: lastCfi, page: total, total: total, timestamp: lastread_timestamp}); - } }, - close: function() { + close: async function() { const location = window.book.rendition.currentLocation(); const currentCfi = location.start.cfi; @@ -1116,7 +1113,7 @@ const Reader = { const totalPages = 100; const lastread_timestamp = new Date().getTime(); - localforage.setItem(Reader.cacheId("lastread"), + await localforage.setItem(Reader.cacheId("lastread"), {cfi: currentCfi, page: currentPage, total: totalPages, timestamp: lastread_timestamp}); if (App.isOnline()) { @@ -1162,14 +1159,16 @@ const Reader = { else $(".header,.footer").fadeIn(); }, - lookupWord: function(word, callback) { + lookupWord: async function(word, callback) { word = word.replace(//g, ""); $(".dict_result").html('Loading, please wait...'); $("#dict-modal").modal('show'); - $.post("backend.php", {op: 'define', word: word}, function (data) { + try { + const data = await $.post("backend.php", {op: 'define', word: word}); + if (data) { $(".dict_result").html(data.result.join("<br/>")); @@ -1177,10 +1176,11 @@ const Reader = { if (callback) callback(); } - }).fail(function(res) { - console.warn(res); - $(".dict_result").html('Network error while looking up word: ' + res.statusText); - }); + } catch (e) { + console.error(e); + + $(".dict_result").html(`Network error while looking up word: ${e.responseText}.`); + } }, search: function() { const query = $(".search_input").val(); @@ -1351,33 +1351,29 @@ const Reader = { }, }, Settings: { - onThemeChanged: function(elem) { + onThemeChanged: async function(elem) { const theme = $(elem).val(); - localforage.setItem("epube.theme", theme).then(function() { - Reader.applyTheme(); - }); + await localforage.setItem("epube.theme", theme); + Reader.applyTheme(); }, - onLineHeightChanged: function(elem) { + onLineHeightChanged: async function(elem) { const height = $(elem).val(); - localforage.setItem("epube.lineHeight", height).then(function() { - Reader.applyStyles(); - }); + await localforage.setItem("epube.lineHeight", height); + Reader.applyStyles(); }, - onTextSizeChanged: function(elem) { + onTextSizeChanged: async function(elem) { const size = $(elem).val(); - localforage.setItem("epube.fontSize", size).then(function() { - Reader.applyStyles(); - }); + await localforage.setItem("epube.fontSize", size); + Reader.applyStyles(); }, - onFontChanged: function(elem) { + onFontChanged: async function(elem) { const font = $(elem).val(); - localforage.setItem("epube.fontFamily", font).then(function() { - Reader.applyStyles(); - }); + await localforage.setItem("epube.fontFamily", font); + Reader.applyStyles(); } } }; |