From d7b02e140e628fcb0ba838925ff0ad4237dbb6cd Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 21 Apr 2020 08:43:04 +0300 Subject: app: rename some offline-related methods --- js/app.js | 192 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 96 insertions(+), 96 deletions(-) (limited to 'js') diff --git a/js/app.js b/js/app.js index 885bfb2..2aacebf 100644 --- a/js/app.js +++ b/js/app.js @@ -112,101 +112,6 @@ const App = { return false; }, - offlineGetAll: function() { - if (confirm("Download all books on this page?")) { - - $(".row > div").each(function (i, row) { - const bookId = $(row).attr("id").replace("cell-", ""); - const dropitem = $(row).find(".offline_dropitem")[0]; - - if (bookId) { - - const cacheId = 'epube-book.' + bookId; - localforage.getItem(cacheId).then(function(book) { - - if (!book) { - App.offlineCache(bookId, function() { - App.Offline.mark(dropitem); - }); - } - - }); - - } - }); - } - }, - offlineCache: function(bookId, callback) { - console.log("offline cache: " + bookId); - - $.post("backend.php", {op: "getinfo", id: bookId}, function(data) { - - if (data) { - const cacheId = 'epube-book.' + bookId; - - localforage.setItem(cacheId, data).then(function(data) { - - console.log(cacheId + ' got data'); - - const promises = []; - - promises.push(fetch('backend.php?op=download&id=' + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { - if (resp.status == 200) { - console.log(cacheId + ' got book'); - - callback(); - - localforage.setItem(cacheId + '.book', resp.blob()); - } - })); - - promises.push(fetch("backend.php?op=getpagination&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { - if (resp.status == 200) { - console.log(cacheId + ' got pagination'); - - resp.text().then(function(text) { - localforage.setItem(cacheId + '.locations', JSON.parse(text)); - }); - } - })); - - promises.push(fetch("backend.php?op=getlastread&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { - if (resp.status == 200) { - console.log(cacheId + ' got lastread'); - resp.text().then(function(text) { - localforage.setItem(cacheId + '.lastread', JSON.parse(text)); - }); - } - })); - - if (data.has_cover) { - - promises.push(fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) { - - if (resp.status == 200) { - console.log(cacheId + ' got cover'); - localforage.setItem(cacheId + '.cover', resp.blob()); - } - - })); - - } - - Promise.all(promises).then(function() { - $(".dl-progress") - .show() - .html("Finished downloading " + data.title + ""); - - window.clearTimeout(App._dl_progress_timeout); - - App._dl_progress_timeout = window.setTimeout(function() { - $(".dl-progress").fadeOut(); - }, 5*1000); - }); - }); - } - }); - }, showCovers: function() { $("img[data-book-id]").each((i,e) => { e = $(e); @@ -362,7 +267,7 @@ const App = { } else { elem.onclick = function() { - App.offlineCache(bookId, function() { + App.Offline.get(bookId, function() { App.Offline.mark(elem); }); return false; @@ -542,4 +447,99 @@ const App = { }); } }, + getAll: function() { + if (confirm("Download all books on this page?")) { + + $(".row > div").each(function (i, row) { + const bookId = $(row).attr("id").replace("cell-", ""); + const dropitem = $(row).find(".offline_dropitem")[0]; + + if (bookId) { + + const cacheId = 'epube-book.' + bookId; + localforage.getItem(cacheId).then(function(book) { + + if (!book) { + App.Offline.get(bookId, function() { + App.Offline.mark(dropitem); + }); + } + + }); + + } + }); + } + }, + get: function(bookId, callback) { + console.log("offline cache: " + bookId); + + $.post("backend.php", {op: "getinfo", id: bookId}, function(data) { + + if (data) { + const cacheId = 'epube-book.' + bookId; + + localforage.setItem(cacheId, data).then(function(data) { + + console.log(cacheId + ' got data'); + + const promises = []; + + promises.push(fetch('backend.php?op=download&id=' + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { + if (resp.status == 200) { + console.log(cacheId + ' got book'); + + callback(); + + localforage.setItem(cacheId + '.book', resp.blob()); + } + })); + + promises.push(fetch("backend.php?op=getpagination&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { + if (resp.status == 200) { + console.log(cacheId + ' got pagination'); + + resp.text().then(function(text) { + localforage.setItem(cacheId + '.locations', JSON.parse(text)); + }); + } + })); + + promises.push(fetch("backend.php?op=getlastread&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { + if (resp.status == 200) { + console.log(cacheId + ' got lastread'); + resp.text().then(function(text) { + localforage.setItem(cacheId + '.lastread', JSON.parse(text)); + }); + } + })); + + if (data.has_cover) { + + promises.push(fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) { + + if (resp.status == 200) { + console.log(cacheId + ' got cover'); + localforage.setItem(cacheId + '.cover', resp.blob()); + } + + })); + + } + + Promise.all(promises).then(function() { + $(".dl-progress") + .show() + .html("Finished downloading " + data.title + ""); + + window.clearTimeout(App._dl_progress_timeout); + + App._dl_progress_timeout = window.setTimeout(function() { + $(".dl-progress").fadeOut(); + }, 5*1000); + }); + }); + } + }); + }, }; -- cgit v1.2.3