From 80910996b07527f0941560bca5b598286a16ff9f Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 13 Mar 2018 15:36:55 +0300 Subject: react to device orientation change to preserve lastread position --- js/read.js | 43 +++++++++++++++++++++++++++++++++++++++++++ read.html | 29 +++-------------------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/js/read.js b/js/read.js index 61fa44e..b0b7cff 100644 --- a/js/read.js +++ b/js/read.js @@ -26,6 +26,33 @@ function toggle_ui() { $(".header,.footer").fadeIn(); } +function open_lastread() { + localforage.getItem(cacheId("lastread")).then(function(item) { + console.log('lr local', item); + + item = item || {}; + + if (item.cfi) book.gotoCfi(item.cfi); + + if (navigator.onLine) { + + $.post("backend.php", { op: "getlastread", id: $.urlParam("id") }, function(data) { + console.log('lr remote', data); + + if (navigator.onLine && data) { + localforage.setItem(cacheId("lastread"), + {cfi: data.cfi, page: data.page, total: data.total}); + + if (item.cfi != data.cfi && (!item.page || data.page > item.page)) + book.gotoCfi(data.cfi); + + } + }); + } + + }); +} + function next_page() { _store_position = 1; @@ -76,6 +103,22 @@ function hotkey_handler(e) { $(document).ready(function() { document.onkeydown = hotkey_handler; + $(window).on("orientationchange", function(evt) { + console.log("orientationchange"); + + $(".loading").show(); + $(".loading_message").html("Opening chapter..."); + + window.setTimeout(function() { + open_lastread(); + + window.setTimeout(function() { + $(".loading").hide(); + }, 500); + + }, 1000); + }); + $(window).on("mouseup", function(evt) { if (evt.button == 0) { diff --git a/read.html b/read.html index 7b48a3c..f37bb95 100644 --- a/read.html +++ b/read.html @@ -738,31 +738,6 @@ localforage.setItem(cacheId("pagination"), pageList); } - localforage.getItem(cacheId("lastread")).then(function(item) { - console.log('lr local', item); - - item = item || {}; - - if (item.cfi) book.gotoCfi(item.cfi); - - if (navigator.onLine) { - - $.post("backend.php", { op: "getlastread", id: $.urlParam("id") }, function(data) { - console.log('lr remote', data); - - if (navigator.onLine && data) { - localforage.setItem(cacheId("lastread"), - {cfi: data.cfi, page: data.page, total: data.total}); - - if (item.cfi != data.cfi && (!item.page || data.page > item.page)) - book.gotoCfi(data.cfi); - - } - }); - } - - }); - var curPage = book.pagination.pageFromCfi(book.getCurrentLocationCfi()); $("#total_pages").html(book.pagination.totalPages); @@ -773,9 +748,11 @@ $("#page_pct").html(pct + "%"); } + open_lastread(); + window.setTimeout(function() { $(".loading").hide(); - }, 1000); + }, 1500); $(".location").click(function() { var current = book.pagination.pageFromCfi(book.getCurrentLocationCfi()); -- cgit v1.2.3