summaryrefslogtreecommitdiff
path: root/js/offline.js
blob: 6fd3ddd1f67eac38b7ddeb1058ccd4e9f4c64e0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';

/* global localforage, Holder */

function offline_search() {
	const query = $(".search_query").val();

	localforage.setItem("epube.search-query", query).then(function() {
		populate_list();
	});

	return false;
}

function offline_remove2(elem) {
	const bookId = elem.getAttribute("data-book-id");

	return offline_remove(bookId, function() {
		$("#cell-" + bookId).remove();
	});
}

function offline_clear() {

	if (confirm("Remove all offline data?")) {

		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;
					}

					let cell = "<div class='col-xs-6 col-sm-3 col-md-2 index_cell' id=\"cell-"+info.id+"\">";

					const cover_read = is_read ? "read" : "";
					const title_class = in_progress ? "in_progress" : "";

					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 class=\"caption\">";
					cell += "<div><a class=\""+title_class+"\" href=\"read.html?id="+info.epub_id+"&b="+info.id+"\">" +
						info.title + "</a></div>";

					cell += "<div><a href=\"#\" class=\"author_link\">" + info.author_sort + "</a></div>";

					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=\"return show_summary(this)\">Summary</a></li>";
					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>";

					cell = $(cell);

					if (cover) {

						cell.find("img").attr("src", cover);

						cell.find(".series_link")
							.attr("title", info.series_name + " [" + info.series_index  + "]")
							.attr("href", "offline.html?query=" + encodeURIComponent(info.series_name));

						cell.find(".author_link")
							.attr("title", info.author_sort)
							.attr("href", "offline.html?query=" + encodeURIComponent(info.author_sort));

					}

					books.append(cell);

					Holder.run();
				}
			});
		}
	});

}

function show_summary(elem) {
	const bookId = elem.getAttribute("data-book-id");

	localforage.getItem("epube-book." + bookId).then(function(data) {

		const comment = data.comment ? data.comment : 'No description available';

		$("#summary-modal .modal-title").html(data.title);
		$("#summary-modal .book-summary").html(comment);

		$("#summary-modal").modal();

	});

	return false;
}