summaryrefslogtreecommitdiff
path: root/js/read.js
blob: 196550420fdf0accf1a071609697f11d55d2801e (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
var _store_position = 0;
var _enable_fullscreen = 0;
var _has_touch = 0;

function request_fullscreen() {
	if (_enable_fullscreen)
		document.documentElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
}

function disable_fullscreen() {
	document.webkitExitFullscreen();
}

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 next_page() {
	_store_position = 1;

	window.book.nextPage();

	show_ui(false);
	request_fullscreen();
}

function prev_page() {
	window.book.prevPage();

	show_ui(false);
	request_fullscreen();
}

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

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

$(document).ready(function() {
	document.onkeydown = hotkey_handler;

	$(window).on("mouseup", function(evt) {
		if (evt.button == 0) {

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

			var doc = document.documentElement;
			var margin_x = 64;
			var margin_y_top = 48;
			var margin_y_bottom = 48;

			//console.log(event.clientY + " " + doc.clientHeight);

			if (evt.clientY < margin_y_top || evt.clientY >= doc.clientHeight - margin_y_bottom) {
				return;
			}

			if (evt.clientX >= doc.clientWidth - margin_x) {
				console.log("RIGHT SIDE");
				next_page();
			} else if (evt.clientX <= margin_x) {
				console.log("LEFT SIDE");
				prev_page();
			}
		}
	});

});

function apply_line_height(elem) {
	var height = elem[elem.selectedIndex].value;

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

function apply_font(elem) {
	var font = elem[elem.selectedIndex].value;

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

}

function apply_font_size(elem) {
	var size = elem[elem.selectedIndex].value;

	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) {
		var fontSize = res[0] ? res[0] + "px" : DEFAULT_FONT_SIZE + "px";
		var fontFamily = res[1] ? res[1] : DEFAULT_FONT_FAMILY;
		var lineHeight = res[2] ? res[2] + "%" : DEFAULT_LINE_HEIGHT + "%";
		var themeName = res[3] ? res[3] : false;

		book.setStyle("fontSize", fontSize);
		book.setStyle("fontFamily", fontFamily);
		book.setStyle("lineHeight", lineHeight);
		book.setStyle("textAlign", "justify");

/*		$("#reader iframe").contents().find("p")
			.css("background", "")
			.css("color", "")
			.css("background-color", "")
			.css("font-family", fontFamily)
			.css("font-size", fontSize)
			.css("line-height", lineHeight)
			.css("text-align", "justify"); */

	});

}

function clear_lastread() {
	if (confirm("Clear stored last read location?")) {
		var total = window.book.pagination.totalPages;

		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?")) {
		var total = window.book.pagination.totalPages;
		var lastCfi = book.pagination.cfiFromPage(total);

		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() {
	var currentPage = book.pagination.pageFromCfi(book.getCurrentLocationCfi());
	var currentCfi = book.getCurrentLocationCfi();
	var totalPages = book.pagination.totalPages;

	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(data) {
				window.location = "index.php";
			});
	} else {
		window.location = "index.php";
	}
}

function change_theme(elem) {
	var theme = $(elem).val();
	localforage.setItem("epube.theme", theme).then(function() {
		apply_theme();
	});
}

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

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

		if (!theme) theme = 'default';

		var themeUrl = baseUrl + "/themes/" + theme + ".css";

		$("#theme_css").attr("href", themeUrl);
		$(book.renderer.doc).find("#theme_css").attr('href', themeUrl);

	});
}

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

	list.html("");

	if (query) {
		var results = window.book.currentChapter.find(query);

		$.each(results, function (i, row) {
			var a = $("<a>")
				.attr('href', '#')
				.html(row.excerpt +
					" <b>(Loc.&nbsp;" + window.book.pagination.pageFromCfi(row.cfi) + ")</b>")
				.attr('data-cfi', row.cfi)
				.attr('data-id', row.id)
				.click(function() {
						window.book.gotoCfi(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();
		}
	});
}