summaryrefslogtreecommitdiff
path: root/js/index.js
blob: b3138b770c74f0a6026d2cd45c1120e6410b8d28 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'use strict';

/* global localforage */

let _dl_progress_timeout;

/* exported cache_refresh */
function cache_refresh(force) {
	if ('serviceWorker' in navigator) {
		localforage.getItem("epube.cache-timestamp").then(function(stamp) {
			const ts = parseInt(new Date().getTime()/1000);

			if (force || !stamp || ts - stamp > 3600 * 24 * 7) {
				console.log('asking worker to refresh cache');

				$(".dl-progress")
					.fadeIn()
					.html("Loading, please wait...");

				navigator.serviceWorker.controller.postMessage("refresh-cache");
				localforage.setItem("epube.cache-timestamp", ts);
			}

		});
	}

	return false;
}

/* exported toggle_fav */
function toggle_fav(elem) {
	const bookId = elem.getAttribute("data-book-id");

	if (elem.getAttribute("data-is-fav") == "0" || confirm("Remove favorite?")) {

		$.post("backend.php", {op: "togglefav", id: bookId}, function(data) {
			if (data) {
				let msg = "[Error]";

				if (data.status == 0) {
					msg = "Add to favorites";
				} else if (data.status == 1) {
					msg = "Remove from favorites";
				}

				$(elem).html(msg).attr('data-is-fav', data.status);

				/* global index_mode */
				if (index_mode == "favorites" && data.status == 0) {
					$("#cell-" + bookId).remove();
				}
			}
		});
	}

	return false;
}

/* global offline_remove */
function mark_offline(elem) {

	const bookId = elem.getAttribute("data-book-id");
	const cacheId = "epube-book." + bookId;

	localforage.getItem(cacheId).then(function(book) {
		if (book) {
			elem.onclick = function() {
				offline_remove(bookId, function() {
					mark_offline(elem);
				});
				return false;
			};

			elem.innerHTML = "Remove offline data";


		} else {
			elem.onclick = function() {
				offline_cache(bookId, function() {
					mark_offline(elem);
				});
				return false;
			};

			elem.innerHTML = "Make available offline";
		}
	});
}

/* exported mark_offline_books */
function mark_offline_books() {
	const elems = $(".offline_dropitem");

	$.each(elems, function (i, elem) {
		mark_offline(elem);
	});
}

/* exported show_covers */
function show_covers() {
	$("img[data-book-id]").each((i,e) => {
		e = $(e);

		if (e.attr('data-cover-link')) {
			const img = $("<img>")
				.on("load", function() {
					e.css("background-image", "url(" + e.attr('data-cover-link') + ")")
					.fadeIn();

					img.attr("src", null);
				})
				.attr("src", e.attr('data-cover-link'));
		} else {
			e.attr('src', 'holder.js/130x190?auto=yes').fadeIn();
		}
	});

	Holder.run();
}

function offline_cache(bookId, callback) {
	console.log("offline cache: " + bookId);

	$.post("backend.php", {op: "getinfo", id: bookId}, function(data) {

		if (data) {
			const cacheId = 'epube-book.' + bookId;

			localforage.setItem(cacheId, data).then(function(data) {

				console.log(cacheId + ' got data');

				const promises = [];

				promises.push(fetch('backend.php?op=download&id=' + data.epub_id, {credentials: 'same-origin'}).then(function(resp) {
					if (resp.status == 200) {
						console.log(cacheId + ' got book');

						callback();

						localforage.setItem(cacheId + '.book', resp.blob());
					}
				}));

				promises.push(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 + '.locations', JSON.parse(text));
						});
					}
				}));

				promises.push(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) {

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

					}));

				}

				Promise.all(promises).then(function() {
					$(".dl-progress")
						.show()
						.html("Finished downloading <b>" + data.title + "</b>");

					window.clearTimeout(_dl_progress_timeout);

					_dl_progress_timeout = window.setTimeout(function() {
						$(".dl-progress").fadeOut();
					}, 5*1000);
				});
			});
		}

	});
}

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

	$.post("backend.php", {op: 'getinfo', id: id}, 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;
}

/* exported offline_get_all */
function offline_get_all() {

	if (confirm("Download all books on this page?")) {

		$(".row > div").each(function (i, row) {
			const bookId = $(row).attr("id").replace("cell-", "");
			const dropitem = $(row).find(".offline_dropitem")[0];

			if (bookId) {

				const cacheId = 'epube-book.' + bookId;
				localforage.getItem(cacheId).then(function(book) {

					if (!book) {
						offline_cache(bookId, function() {
							mark_offline(dropitem);
						});
					}

				});

			}
		});
	}

}