summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2017-02-26 13:12:33 +0300
committerAndrew Dolgov <[email protected]>2017-02-26 13:12:33 +0300
commite01f57a3d98943954372110dc75e8d731fbacc10 (patch)
tree2d57e3a2368984f03507f2650f0b94273dced625 /js
parentb8ae4b31c129031e89e4c7fb9801d6761513dd73 (diff)
further offline improvements
Diffstat (limited to 'js')
-rw-r--r--js/index.js92
-rw-r--r--js/offline.js63
-rw-r--r--js/read.js10
3 files changed, 144 insertions, 21 deletions
diff --git a/js/index.js b/js/index.js
index e76be90..95615a2 100644
--- a/js/index.js
+++ b/js/index.js
@@ -1,13 +1,91 @@
-function offline_cache(elem) {
- try {
+function mark_offline_books() {
+ var elems = $(".offline_dropitem");
+
+ $.each(elems, function (i, elem) {
var bookId = elem.getAttribute("data-book-id");
+ var cacheId = "epube-book." + bookId;
+
+ localforage.getItem(cacheId).then(function(book) {
+ if (book) {
+
+ elem.onclick = function() {
+ offline_remove(elem, function() {
+ mark_offline_books();
+ });
+ };
+
+ elem.innerHTML = "Remove offline data";
+
+
+ } else {
+ elem.onclick = function() {
+ offline_cache(bookId, function() {
+ mark_offline_books();
+ });
+ };
+
+ elem.innerHTML = "Make available offline";
+ }
+ });
+ });
+}
+
+function offline_cache(bookId, callback) {
+ console.log("offline cache: " + bookId);
+
+ $.post("backend.php", {op: "getinfo", id: bookId}, function(data) {
+
+ if (data) {
+ var cacheId = 'epube-book.' + bookId;
+
+ localforage.setItem(cacheId, data).then(function(data) {
+
+ console.log(cacheId + ' got data');
+
+ fetch('getbook/' + data.epub_id + ".epub", {credentials: 'same-origin'}).then(function(resp) {
+ if (resp.status == 200) {
+ console.log(cacheId + ' got book');
+
+ callback();
+
+ localforage.setItem(cacheId + '.book', resp.blob());
+ }
+ });
+
+ 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 + '.pagination', JSON.parse(text));
+ });
+ }
+ });
+
+ 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) {
+
+ 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());
+ }
- console.log(bookId);
+ });
+ }
- return false;
+ });
+ }
- } catch (e) {
- console.warn(e);
- }
+ });
}
diff --git a/js/offline.js b/js/offline.js
index d9bed0b..d1c49d6 100644
--- a/js/offline.js
+++ b/js/offline.js
@@ -1,23 +1,64 @@
-var CACHE_NAME = "epube-test";
-
function populate_list() {
var books = $("#books_container");
+ books.html("");
- window.caches.open(CACHE_NAME).then(function(cache) {
- cache.keys().then(function(items) {
+ localforage.iterate(function(value, key, i) {
+ if (key.match(/epube-book\.\d{1,}$/)) {
- $.each(items, function(i, req) {
+ Promise.all([
+ localforage.getItem(key),
+ localforage.getItem(key + ".cover")
+ ]).then(function(results) {
- if (req.url.match(/\.epub/)) {
- console.log(req.url);
+ var info = results[0];
+ if (info) {
+ var cover = false;
- }
+ if (results && results[1]) {
+ cover = URL.createObjectURL(results[1]);
+ }
- });
+ var cell = "<div class='col-xs-6 col-sm-3 col-md-2 index_cell'>";
- });
- });
+ cell += "<div class=\"thumb\">";
+ cell += "<a href=\"read.html?id="+info.epub_id+"&b="+info.id+"\"><img data-src=\"holder.js/120x180\"></a>";
+
+ cell += "<div class=\"caption\">";
+ cell += "<div><a href=\"read.html?id="+info.epub_id+"&b="+info.id+"\">" + info.title + "</a></div>";
+ cell += "<div>" + info.author_sort + "</div>";
+
+ if (info.series_name) {
+ cell += "<div>" + info.series_name + " [" + info.series_index + "]</div>";
+ }
+
+ cell += "</div>";
+
+ cell += "<div class=\"dropdown\" style=\"white-space : nowrap\">";
+ cell += "<a href=\"#\" data-toggle=\"dropdown\" role=\"button\">" +
+ "More..." + "<span class=\"caret\"></span></a>";
+
+ cell += "<ul class=\"dropdown-menu\">";
+ cell += "<li><a href=\"#\" data-book-id=\""+info.id+"\" onclick=\"offline_remove(this)\">Remove download</a></li>";
+ cell += "</ul>";
+ cell += "</div>";
+
+ cell += "</div>";
+ cell += "</div>";
+
+ var cell = $(cell);
+
+ if (cover) {
+ cell.find("img").attr("src", cover);
+ }
+
+ books.append(cell);
+
+ Holder.run();
+ }
+ });
+ }
+ });
}
diff --git a/js/read.js b/js/read.js
index 9039826..dc968f4 100644
--- a/js/read.js
+++ b/js/read.js
@@ -111,10 +111,14 @@ function mark_as_read() {
function save_and_close() {
if (navigator.onLine) {
- var curPage = book.pagination.pageFromCfi(book.getCurrentLocationCfi());
+ var currentPage = book.pagination.pageFromCfi(book.getCurrentLocationCfi());
+ var currentCfi = book.getCurrentLocationCfi();
- $.post("backend.php", { op: "storelastread", id: $.urlParam("id"), page: curPage,
- cfi: book.getCurrentLocationCfi() }, function(data) {
+ localforage.setItem("epube-book." + $.urlParam("b") + ".lastread",
+ {cfi: currentCfi, page: currentPage});
+
+ $.post("backend.php", { op: "storelastread", id: $.urlParam("id"), page: currentPage,
+ cfi: currentCfi }, function(data) {
window.location = "index.php";
});
} else {