summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2017-02-27 23:30:12 +0300
committerAndrew Dolgov <[email protected]>2017-02-27 23:30:12 +0300
commit16d52ac49f7318620d3a84e172cf18917d70de1b (patch)
tree9a2e387a61d1e427a882557bc088ec6689fae7f9
parentc47556b617f0a351faeef53cd1aec67bf49229f1 (diff)
add basic offline mode search
-rw-r--r--js/offline.js160
-rw-r--r--offline.html16
2 files changed, 119 insertions, 57 deletions
diff --git a/js/offline.js b/js/offline.js
index 8a61417..0bcd447 100644
--- a/js/offline.js
+++ b/js/offline.js
@@ -1,3 +1,13 @@
+function offline_search(form) {
+ var query = $(".search_query").val();
+
+ localforage.setItem("epube.search-query", query).then(function() {
+ populate_list();
+ });
+
+ return false;
+}
+
function offline_remove2(elem) {
var bookId = elem.getAttribute("data-book-id");
@@ -30,80 +40,122 @@ function offline_clear() {
function populate_list() {
- var books = $("#books_container");
- books.html("");
+ localforage.getItem("epube.search-query").then(function(query) {
- localforage.iterate(function(value, key, i) {
- if (key.match(/epube-book\.\d{1,}$/)) {
+ if (query) query = query.toLowerCase();
- Promise.all([
- localforage.getItem(key),
- localforage.getItem(key + ".cover"),
- localforage.getItem(key + ".lastread"),
- localforage.getItem(key + ".book")
- ]).then(function(results) {
+ var books = $("#books_container");
+ books.html("");
- if (results[0] && results[3]) {
- var info = results[0];
- var cover = false;
+ localforage.iterate(function(value, key, i) {
+ if (key.match(/epube-book\.\d{1,}$/)) {
- if (results && results[1]) {
- cover = URL.createObjectURL(results[1]);
- }
+ Promise.all([
+ localforage.getItem(key),
+ localforage.getItem(key + ".cover"),
+ localforage.getItem(key + ".lastread"),
+ localforage.getItem(key + ".book")
+ ]).then(function(results) {
- var in_progress = false;
- var is_read = false;
+ if (results[0] && results[3]) {
+ var info = results[0];
- var lastread = results[2];
- if (lastread) {
+ if (query) {
+ var 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));
- in_progress = lastread.page > 0;
- is_read = lastread.total > 0 && lastread.total - lastread.page < 5;
- }
+ if (!match) return;
+ }
- var cell = "<div class='col-xs-6 col-sm-3 col-md-2 index_cell' id=\"cell-"+info.id+"\">";
- var cover_read = is_read ? "read" : "";
- var title_class = in_progress ? "in_progress" : "";
+ var cover = false;
- cell += "<div class=\"thumb "+cover_read+"\">";
- cell += "<a href=\"read.html?id="+info.epub_id+"&b="+info.id+"\"><img data-src=\"holder.js/120x180\"></a>";
+ if (results && results[1]) {
+ cover = URL.createObjectURL(results[1]);
+ }
- cell += "<div class=\"caption\">";
- cell += "<div><a class=\""+title_class+"\" href=\"read.html?id="+info.epub_id+"&b="+info.id+"\">" +
- info.title + "</a></div>";
- cell += "<div>" + info.author_sort + "</div>";
+ var in_progress = false;
+ var is_read = false;
- if (info.series_name) {
- cell += "<div>" + info.series_name + " [" + info.series_index + "]</div>";
- }
+ var lastread = results[2];
+ if (lastread) {
- cell += "</div>";
+ in_progress = lastread.page > 0;
+ is_read = lastread.total > 0 && lastread.total - lastread.page < 5;
+ }
- cell += "<div class=\"dropdown\" style=\"white-space : nowrap\">";
- cell += "<a href=\"#\" data-toggle=\"dropdown\" role=\"button\">" +
- "More..." + "<span class=\"caret\"></span></a>";
+ var cell = "<div class='col-xs-6 col-sm-3 col-md-2 index_cell' id=\"cell-"+info.id+"\">";
- cell += "<ul class=\"dropdown-menu\">";
- cell += "<li><a href=\"#\" data-book-id=\""+info.id+"\" onclick=\"offline_remove2(this)\">Remove offline data</a></li>";
- cell += "</ul>";
+ var cover_read = is_read ? "read" : "";
+ var title_class = in_progress ? "in_progress" : "";
- cell += "</div>";
+ cell += "<div class=\"thumb "+cover_read+"\">";
+ cell += "<a href=\"read.html?id="+info.epub_id+"&b="+info.id+"\"><img data-src=\"holder.js/120x180\"></a>";
- cell += "</div>";
- cell += "</div>";
+ cell += "<div class=\"caption\">";
+ cell += "<div><a class=\""+title_class+"\" href=\"read.html?id="+info.epub_id+"&b="+info.id+"\">" +
+ info.title + "</a></div>";
- var cell = $(cell);
+ cell += "<div><a href=\#\" class=\"author_link\">" + info.author_sort + "</a></div>";
- if (cover) {
- cell.find("img").attr("src", cover);
- }
+ if (info.series_name) {
+ cell += "<div><a href=\"\" class=\"series_link\">" +
+ info.series_name + " [" + info.series_index + "]</a></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_remove2(this)\">Remove offline data</a></li>";
+ cell += "</ul>";
+
+ cell += "</div>";
+
+ cell += "</div>";
+ cell += "</div>";
- books.append(cell);
+ var cell = $(cell);
+
+ if (cover) {
+ cell.find("img").attr("src", cover);
+
+ cell.find(".series_link")
+ .attr("title", info.series_name + " [" + info.series_index + "]")
+ .click(function() {
+
+ $(".search_query").val(info.series_name);
+ offline_search();
+
+ return false;
+ });
+
+ cell.find(".author_link")
+ .attr("title", info.author_sort)
+ .click(function() {
+
+ $(".search_query").val(info.author_sort);
+ offline_search();
+
+ return false;
+ });
+
+ }
+
+ books.append(cell);
+
+ Holder.run();
+ }
+ });
+ }
+ });
- Holder.run();
- }
- });
- }
});
+
}
+
diff --git a/offline.html b/offline.html
index 8b25d21..0062518 100644
--- a/offline.html
+++ b/offline.html
@@ -42,7 +42,13 @@
</ul>
<form onsubmit="return false;" class="navbar-form navbar-right">
- <button type="submit" onclick="offline_clear()" class="btn btn-default">Remove all</button>
+ <button type="submit" onclick="offline_clear()" class="btn btn-danger">Remove all</button>
+ </form>
+
+ <form onsubmit="return offline_search(this)" class="navbar-form navbar-right">
+ <input type="text" class="form-control search_query" name="query" class="form-control"
+ value="">
+ <button type="submit" class="btn btn-default">Search</button>
</form>
</div>
@@ -59,10 +65,14 @@
.then(function() {
console.log("service worker registered");
});
-
}
- populate_list();
+ localforage.getItem("epube.search-query").then(function(query) {
+
+ if (query) $(".search_query").val(query);
+
+ populate_list();
+ });
});
</script>