From ecc201dbf0e2d8674ca2ecf529526e93a1f6e348 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 20 Jan 2021 16:50:40 +0300 Subject: better promise chain for startup, fix some minor bugs --- js/reader.js | 934 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 485 insertions(+), 449 deletions(-) (limited to 'js/reader.js') diff --git a/js/reader.js b/js/reader.js index aa05703..b8549c5 100644 --- a/js/reader.js +++ b/js/reader.js @@ -28,7 +28,7 @@ const Reader = { onOfflineModeChanged: function(offline) { console.log('onOfflineModeChanged', offline); - if (!offline) { + if (!offline && window.book) { const book = window.book; console.log("we're online, storing lastread"); @@ -66,631 +66,666 @@ const Reader = { Reader.applyTheme(); - localforage.getItem(Reader.cacheId("book")).then(function(item) { + return localforage.getItem(Reader.cacheId()).then(function(item) { + if (!item) { + console.log('requesting bookinfo...') - // ios doesn't work with FileReader for whatever reason - if (/*!_is_ios &&*/ item) { + return new Promise((resolve, reject) => { + const bookId = $.urlParam("b"); - console.log("loading from local storage"); + $.post("backend.php", {op: "getinfo", id: bookId }).success(function(data) { + if (data) { + if (data.has_cover) { + fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) { + if (resp.status == 200) { + localforage.setItem(Reader.cacheId('cover'), resp.blob()); + } + }); + } + + return localforage.setItem(Reader.cacheId(), data).then(function() { + console.log('bookinfo saved'); + resolve(); + }) + } - const fileReader = new FileReader(); + reject(new Error("unable to load book info: blank")); - fileReader.onload = function() { - try { - book.open(this.result); - } catch (e) { - $(".loading_message").html("Unable to load book (local)."); - console.log(e); - } - }; + }).error(function(xhr) { + $(".loading_message").html("Unable to load book info.
" + xhr.status + ""); - fileReader.readAsArrayBuffer(item); + reject(new Error("unable to load book info: " + e)); + }); + }); } else { + console.log('bookinfo already stored'); + } + }).then(function() { - console.log("loading from network"); + console.log('trying to load book...'); - if (App.isOnline()) { - const book_url = "backend.php?op=download&id=" + $.urlParam("id"); + localforage.getItem(Reader.cacheId("book")).then(function(item) { - $(".loading_message").html("Downloading..."); + if (item) { - fetch(book_url, {credentials: 'same-origin'}).then(function(resp) { + console.log("loading from local storage"); - if (resp.status == 200) { - const bookId = $.urlParam("b"); + return new Promise(function (resolve, reject) { - resp.blob().then(function(blob) { + const fileReader = new FileReader(); - // if there's no base information cached yet, let's do that too - localforage.getItem(Reader.cacheId()).then(function(info) { - if (!info) { - $.post("backend.php", {op: "getinfo", id: bookId }, function(data) { - if (data) { - localforage.setItem(Reader.cacheId(), data); + fileReader.onload = function() { + try { + return book.open(this.result).then(function() { + resolve(); + }) + } catch (e) { + $(".loading_message").html("Unable to load book (local)."); + console.log(e); - if (data.has_cover) { - fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) { - if (resp.status == 200) { - localforage.setItem(Reader.cacheId('cover'), resp.blob()); - } - }); - } - } - }); - } - }); + reject(new Error("Unable to load book (local):" + e)); + } + }; - const fileReader = new FileReader(); + fileReader.readAsArrayBuffer(item); + }); - fileReader.onload = function() { - book.open(this.result).then(() => { + } else { - // let's store this for later - localforage.setItem(Reader.cacheId('book'), blob); + console.log("loading from network"); - }).catch((e) => { - $(".loading_message").html("Unable to open book.
" + e + ""); - }); - }; + if (App.isOnline()) { + const book_url = "backend.php?op=download&id=" + $.urlParam("id"); - fileReader.onerror = function(e) { - console.log('filereader error', e); - $(".loading_message").html("Unable to open book.
" + e + ""); - }; + $(".loading_message").html("Downloading..."); - fileReader.readAsArrayBuffer(blob); + return fetch(book_url, {credentials: 'same-origin'}).then(function(resp) { - }).catch((e) => { - console.log('blob error', e); - $(".loading_message").html("Unable to download book.
" + e + ""); - }); - } else { - $(".loading_message").html("Unable to download book: " + resp.status + "."); - } - }).catch(function(e) { - console.warn(e); + if (resp.status == 200) { + return resp.blob().then(function(blob) { - if ($(".loading").is(":visible")) { - $(".loading_message").html("Unable to load book (remote).
" + e + ""); - } - }); + return new Promise(function(resolve, reject) { - } else { - $(".loading_message").html("This book is not available offline."); - } - } - }); + const fileReader = new FileReader(); - /* global ePub */ - const book = ePub(); - window.book = book; + fileReader.onload = function() { + book.open(this.result).then(function() { - const rendition = book.renderTo("reader", { - width: '100%', - height: '100%', - minSpreadWidth: 961 - }); + // let's store this for later + localforage.setItem(Reader.cacheId('book'), blob).then(function() { + resolve(); + }) - localforage.getItem("epube.enable-hyphens").then(function(enable_hyphens) { - if (enable_hyphens) { - /* global hyphenationPatternsEnUs, createHyphenator */ - Reader.hyphenateHTML = createHyphenator(hyphenationPatternsEnUs, { html: true }); - } - - Reader.applyStyles(true); + }).catch((e) => { + $(".loading_message").html("Unable to open book.
" + e + ""); - /* rendition.hooks.content.register(function() { - Reader.applyStyles(); - }); */ + reject(new Error("Unable to open book: " + e)); + }); + }; - rendition.display().then(function() { - console.log("book displayed"); - }); + fileReader.onerror = function(e) { + console.log('filereader error', e); + $(".loading_message").html("Unable to open book.
" + e + ""); - }); + reject(new Error("Unable to open book: " + e)); + }; - rendition.hooks.content.register(function(contents) { + fileReader.readAsArrayBuffer(blob); + }); - contents.on("linkClicked", function(href) { - console.log('linkClicked', href); + }).catch((e) => { + console.log('blob error', e); + $(".loading_message").html("Unable to download book.
" + e + ""); + }); + } else { + $(".loading_message").html("Unable to download book: " + resp.status + "."); + } + }).catch(function(e) { + console.warn(e); - if (href.indexOf("://") == -1) { - $(".prev_location_btn") - .attr("data-location-cfi", book.rendition.currentLocation().start.cfi) - .show(); + if ($(".loading").is(":visible")) { + $(".loading_message").html("Unable to load book (remote).
" + e + ""); + } + }); - window.setTimeout(function() { - Reader.showUI(true); - }, 50); + } else { + $(".loading_message").html("This book is not available offline."); + } } - }); - const base_url = window.location.href.match(/^.*\//)[0]; - const res_names = [ "dist/app-libs.min.js", "dist/reader_iframe.min.js" ]; - const doc = contents.document; - - for (let i = 0; i < res_names.length; i++) { + /* global ePub */ + const book = ePub(); + window.book = book; - // we need to create script element with proper context, that is inside the iframe - const elem = doc.createElement("script"); - elem.type = 'text/javascript'; - elem.text = Reader.Loader._res_data[base_url + res_names[i]]; + const rendition = book.renderTo("reader", { + width: '100%', + height: '100%', + minSpreadWidth: 961 + }); - doc.head.appendChild(elem); - } + localforage.getItem("epube.enable-hyphens").then(function(enable_hyphens) { + if (enable_hyphens) { + /* global hyphenationPatternsEnUs, createHyphenator */ + Reader.hyphenateHTML = createHyphenator(hyphenationPatternsEnUs, { html: true }); + } - $(contents.document.head) - .append($("