summaryrefslogtreecommitdiff
path: root/js/read.js
blob: f82fc8de54ce2923b106f2cb7145316e221b9ea1 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
'use strict';

/* global localforage, book, cacheId */

function toggle_fullscreen() {
	const element = document.documentElement;
	const isFullscreen = document.webkitIsFullScreen || document.mozFullScreen || false;

	element.requestFullScreen = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen ||
		function () { return false; };

	document.cancelFullScreen = document.cancelFullScreen || document.webkitCancelFullScreen || document.mozCancelFullScreen ||
		function () { return false; };

	isFullscreen ? document.cancelFullScreen() : element.requestFullScreen();
}

function show_ui(show) {
	if (show)
		$(".header,.footer").fadeIn();
	else
		$(".header,.footer").fadeOut();
}

function toggle_ui() {
	if ($(".header").is(":visible"))
		$(".header,.footer").fadeOut();
	else
		$(".header,.footer").fadeIn();
}

function open_lastread() {
	localforage.getItem(cacheId("lastread")).then(function(item) {
		console.log('lr local', item);

		item = item || {};

		// CFI missing or w/e
		try {

			// this is ridiculous tbh
			if (item.cfi) book.rendition.display(item.cfi).then(() => {
				book.rendition.display(item.cfi);
			});

		} catch (e) {
			console.warn(e);
		}

		if (navigator.onLine) {

			$.post("backend.php", { op: "getlastread", id: $.urlParam("id") }, function(data) {
				console.log('lr remote', data);

				if (navigator.onLine && data) {
					localforage.setItem(cacheId("lastread"),
						{cfi: data.cfi, page: data.page, total: data.total});

					try {
						if (item.cfi != data.cfi && (!item.page || data.page >= item.page))
							console.log('using remote lastread...');

							book.rendition.display(data.cfi).then(() => {
								book.rendition.display(data.cfi);
							});
					} catch (e) {
						console.warn(e);
					}

				}
			})
			.fail(function(e) {
				if (e && e.status == 401) {
					window.location = "index.php";
				}
			});
		}

	});
}

function next_page() {
	_store_position = 1;

	window.book.rendition.next();

	localforage.getItem("epube.keep-ui-visible").then(function(keep) {
		if (!keep) show_ui(false);
	});
}

function prev_page() {
	window.book.rendition.prev();

	localforage.getItem("epube.keep-ui-visible").then(function(keep) {
		if (!keep) show_ui(false);
	});
}

function hotkey_handler(e) {
	try {
		//console.log('K3:' + e.which, e);

		if ($(".modal").is(":visible"))
			return;

		// right or space
		if (e.which == 39 || e.which == 32) {
			e.preventDefault();
			next_page();
		}

		// left
		if (e.which == 37) {
			e.preventDefault();
			prev_page();
		}

		// esc
		if (e.which == 27) {
			e.preventDefault();
			show_ui(true);
		}
	} catch (e) {
		console.warn(e);
	}
}

function resize_side_columns() {
	let width = $("#reader").position().left;
	const iframe = $("#reader iframe")[0];

	if (iframe && iframe.contentWindow.$)
		width += parseInt(iframe.contentWindow.$("body").css("padding-left"));

	$("#left, #right").width(width);
}

$(document).ready(function() {
	$(document).on("keyup", function(e) {
		hotkey_handler(e);
	});

	$("#left").on("mouseup", function() {
		prev_page();
	});

	$("#right").on("mouseup", function() {
		next_page();
	});

});

function apply_line_height(elem) {
	const height = $(elem).val();

	localforage.setItem("epube.lineHeight", height).then(function() {
		apply_styles();
	});
}

function apply_font(elem) {
	const font = $(elem).val();

	localforage.setItem("epube.fontFamily", font).then(function() {
		apply_styles();
	});

}

function apply_font_size(elem) {
	const size = $(elem).val();

	localforage.setItem("epube.fontSize", size).then(function() {
		apply_styles();
	});
}

function apply_styles() {

	Promise.all([
		localforage.getItem("epube.fontSize"),
		localforage.getItem("epube.fontFamily"),
		localforage.getItem("epube.lineHeight"),
		localforage.getItem("epube.theme")
	]).then(function(res) {
		const fontSize = res[0] ? res[0] + "px" : DEFAULT_FONT_SIZE + "px";
		const fontFamily = res[1] ? res[1] : DEFAULT_FONT_FAMILY;
		const lineHeight = res[2] ? res[2] + "%" : DEFAULT_LINE_HEIGHT + "%";
		//const themeName = res[3] ? res[3] : false;

		$.each(window.book.rendition.getContents(), function(i, c) {
			c.css("font-size", fontSize);
			c.css("font-family", fontFamily);
			c.css("line-height", lineHeight);
		});

		apply_theme();
	});

}

function clear_lastread() {
	if (confirm("Clear stored last read location?")) {
		const total = window.book.locations.length();

		if (navigator.onLine) {
			$.post("backend.php", { op: "storelastread", page: -1, cfi: "", id: $.urlParam("id") }, function(data) {
				$(".lastread_input").val(data.page + '%');
			});
		}

		localforage.setItem(cacheId("lastread"),
			{cfi: "", page: 0, total: total});

	}
}

function mark_as_read() {
	if (confirm("Mark book as read?")) {
		const total = 100;
		const lastCfi = window.book.locations.cfiFromPercentage(1);

		if (navigator.onLine) {
			$.post("backend.php", { op: "storelastread", page: total, cfi: lastCfi, id: $.urlParam("id") }, function(data) {
				$(".lastread_input").val(data.page + '%');
			});
		}

		localforage.setItem(cacheId("lastread"),
			{cfi: lastCfi, page: total, total: total});

	}
}

function save_and_close() {
	const location = window.book.rendition.currentLocation();

	const currentCfi = location.start.cfi;
	const currentPage = parseInt(window.book.locations.percentageFromCfi(currentCfi) * 100);
	const totalPages = 100;

	localforage.setItem(cacheId("lastread"),
		{cfi: currentCfi, page: currentPage, total: totalPages});

	if (navigator.onLine) {
		$.post("backend.php", { op: "storelastread", id: $.urlParam("id"), page: currentPage,
			cfi: currentCfi }, function() {
				window.location = $.urlParam("rt") ? "index.php?mode=" + $.urlParam("rt") : "index.php";
			})
			.fail(function() {
				window.location = "index.php";
			});
	} else {
		window.location = "index.php";
	}
}

function change_theme(elem) {
	const theme = $(elem).val();

	localforage.setItem("epube.theme", theme).then(function() {
		apply_styles();
	});
}

function apply_theme() {
	localforage.getItem("epube.theme").then(function(theme) {
		console.log('theme', theme);

		const base_url = window.location.href.match(/^.*\//)[0];

		if (!theme) theme = 'default';
		const theme_url = base_url + "themes/" + theme + ".css";

		$("#theme_css").attr("href", theme_url);

		$.each(window.book.rendition.getContents(), function(i,c) {
			$(c.document).find("#theme_css").text(_res_data[theme_url])
		});

	});
}

function search() {
	const query = $(".search_input").val();
	const list = $(".search_results");

	list.html("");

	if (query) {

		/* eslint-disable prefer-spread */
		Promise.all(
			book.spine.spineItems.map(
				(item) => item.load(book.load.bind(book))
				.then(item.find.bind(item, query))
				.finally(item.unload.bind(item)))
			)
		.then((results) => Promise.resolve([].concat.apply([], results)))
		.then(function(results) {
			$.each(results, function (i, row) {
				const a = $("<a>")
					.attr('href', '#')
					.html("<b class='pull-right'>" + window.book.locations.locationFromCfi(row.cfi) + "</b>" + row.excerpt)
					.attr('data-cfi', row.cfi)
					.attr('data-id', row.id)
					.click(function() {
							window.book.rendition.display(a.attr('data-cfi'));
					});

				list.append($("<li>").append(a));
			});
		});
	}
}

function dict_lookup(word, callback) {
	$.post("backend.php", {op: 'define', word: word}, function(data) {
		if (data) {

			$(".dict_result").html(data.result.join("<br/>"));
			$(".dict_query").val(word);
			$("#dict-modal").modal('show');

			if (callback) callback();
		}
	});
}

function open_previous_location(elem) {
	const cfi = $(elem).attr("data-location-cfi");

	if (cfi) {
		window.book.rendition.display(cfi);
	}

	$(elem).fadeOut();
}