summaryrefslogtreecommitdiff
path: root/js/offline.js
blob: c47c7b660380bd3dad03ffe8f28e60610bbef7af (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
'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 ? `<div><a class="series_link" href="#">${info.series_name + " [" + info.series_index + "]"}</a></div>` : "";

					const cell = $(`<div class="col-xs-6 col-sm-3 col-md-2 index_cell" id="cell-${info.id}">
							<div class="thumb ${thumb_class}">
								<a href="read.html?id=${info.epub_id}&b=${info.id}">
									<img data-src="holder.js/120x180">
								</a>
								<div class="caption">
									<div><a class="${title_class}" href="read.html?id=${info.epub_id}&b=${info.id}">${info.title}</a></div>
									<div><a class="author_link" href="#">${info.author_sort}</a></div>
									${series_link}
								</div>
								<div class="dropdown" style="white-space : nowrap">
									<a href="#" data-toggle="dropdown" role="button">More...<span class="caret"></span></a>
									<ul class="dropdown-menu">
										<li><a href="#" data-book-id="${info.id}" onclick="return show_summary(this)">Summary</a></li>
										<li><a href="#" data-book-id="${info.id}" onclick="offline_remove2(this)">Remove offline data</a></li>
									</ul>
								</div>
							</div>
						</div>`);

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

}

/* exported show_summary */
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;
}