'use strict'; /* global localforage, Holder */ /* exported offline_search */ function offline_search() { const query = $(".search_query").val(); localforage.setItem("epube.search-query", query).then(function() { populate_list(); }); return false; } /* exported offline_remove2 */ function offline_remove2(elem) { const bookId = elem.getAttribute("data-book-id"); /* global offline_remove */ return offline_remove(bookId, function() { $("#cell-" + bookId).remove(); }); } /* exported offline_clear */ function offline_clear() { if (confirm("Remove all downloaded books?")) { const promises = []; localforage.iterate(function(value, key/*, i*/) { if (key.match("epube-book")) { promises.push(localforage.removeItem(key)); } }); Promise.all(promises).then(function() { window.setTimeout(function() { populate_list(); }, 500); }); } } function populate_list() { let query = $.urlParam("query"); if (query) query = query.toLowerCase(); const books = $("#books_container"); books.html(""); localforage.iterate(function(value, key/*, i*/) { if (key.match(/epube-book\.\d{1,}$/)) { Promise.all([ localforage.getItem(key), localforage.getItem(key + ".cover"), localforage.getItem(key + ".lastread"), localforage.getItem(key + ".book") ]).then(function(results) { if (results[0] && results[3]) { const info = results[0]; if (query) { const match = (info.series_name && info.series_name.toLowerCase().match(query)) || (info.title && info.title.toLowerCase().match(query)) || (info.author_sort && info.author_sort.toLowerCase().match(query)); if (!match) return; } let cover = false; if (results && results[1]) { cover = URL.createObjectURL(results[1]); } let in_progress = false; let is_read = false; const lastread = results[2]; if (lastread) { in_progress = lastread.page > 0; is_read = lastread.total > 0 && lastread.total - lastread.page < 5; } const thumb_class = is_read ? "read" : ""; const title_class = in_progress ? "in_progress" : ""; const series_link = info.series_name ? `
` : ""; const cell = $(`