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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="lib/bootstrap/v3/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="lib/bootstrap/v3/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="lib/bootstrap/v3/js/jquery.js"></script>
<script src="lib/bootstrap/v3/js/bootstrap.min.js"></script>
<script src="lib/zip.min.js"></script>
<script src="lib/localforage.min.js"></script>
<script src="lib/epub.js"></script>
<script src="lib/smartimages.js"></script>
<script src="js/read.js"></script>
<script src="js/common.js"></script>
<link id="favicon" rel="shortcut icon" type="image/png" href="img/favicon.png" />
<link type="text/css" rel="stylesheet" media="screen" href="css/read.css" />
</head>
<body>
<div class="modal fade" id="settings-modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Settings</h4>
</div>
<div class="modal-body">
<form name="settings-form" onsubmit="return false" class="form-horizontal">
<div class="form-group">
<label class="col-sm-3 control-label">Font</label>
<div class="col-sm-9">
<select class="font_family form-control" onchange="apply_font(this)">
<option>Arial</option>
<option>Times New Roman</option>
<option>Georgia</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Text size</label>
<div class="col-sm-9">
<div class="btn-group" role="group" aria-label="...">
<button type="button" onclick="zoom(2)" class="btn btn-default">+</button>
<button type="button" onclick="zoom(-2)" class="btn btn-default">-</button>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Line margins</label>
<div class="col-sm-9">
<div class="btn-group" role="group" aria-label="...">
<button type="button" onclick="lmargin(20)" class="btn btn-default">+</button>
<button type="button" onclick="lmargin(-20)" class="btn btn-default">-</button>
</div>
</div>
</div>
<hr/>
<div class="form-group">
<label class="col-sm-3 control-label">Last location</label>
<div class="col-sm-9">
<div class="input-group">
<input type="numeric" disabled="disabled" class="form-control lastread_input">
<span class="input-group-btn">
<button class="btn btn-danger" type="button" onclick="clear_lastread()">Clear</button>
</span>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label"></label>
<div class="col-sm-9">
<button class="btn btn-primary" type="button" onclick="mark_as_read()">Mark as read</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="toc-modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Table of Contents</h4>
</div>
<div class="modal-body">
<ul class="toc_list list-unstyled"> </ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="header">
<span>
<a href="#" onclick="save_and_close()">« Exit</a>
<span class="title"></span>
</span>
<span class="toolbar">
<button class="btn btn-default btn-xs"
data-toggle="modal" data-target="#toc-modal">
<span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
</button>
<button class="btn btn-default btn-xs"
data-toggle="modal" data-target="#settings-modal">
<span class="glyphicon glyphicon-wrench" aria-hidden="true"></span>
</button>
<button class="btn btn-default btn-xs" onclick="invert()">
<span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
</button>
<button class="btn btn-default btn-xs hidden-xs" onclick="zoom(2)">
<span class="glyphicon glyphicon-zoom-in" aria-hidden="true"></span>
</button>
<button class="btn btn-default btn-xs hidden-xs" onclick="zoom(-2)">
<span class="glyphicon glyphicon-zoom-out" aria-hidden="true"></span>
</button>
</span>
</div>
<div class="footer">
<div class="notice bg-warning text-warning" style="display : none"></div>
<div class="location">
Loc: <span id="cur_page">-1</span> / <span id="total_pages">-1</span>
</div>
</div>
<div id="reader"></div>
<div class="loading">
<div class="loading_message">
Opening book...
</div>
</div>
<script>
var _pagination_stored = 0;
var _last_position_sync = 0;
$.urlParam = function(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
function cacheId(suffix) {
return "epube-book." + $.urlParam("b") + (suffix ? "." + suffix : "");
}
$(document).ready(function() {
$(window).on('online', function() {
console.log("we're online, storing lastread");
var currentPage = book.pagination.pageFromCfi(book.getCurrentLocationCfi());
var currentCfi = book.getCurrentLocationCfi();
$.post("backend.php", { op: "storelastread", id: $.urlParam("id"), page: currentPage,
cfi: currentCfi }, function(data) {
if (data.cfi) {
_last_position_sync = new Date().getTime()/1000;
}
});
});
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('worker.js')
.then(function() {
console.log("service worker registered");
});
}
localforage.getItem(cacheId("book")).then(function(item) {
if (item) {
console.log("loading from local storage");
var fileReader = new FileReader();
fileReader.onload = function() {
book.open(this.result);
};
fileReader.readAsArrayBuffer(item);
} else {
console.log("loading from network");
if (navigator.onLine) {
var book_url = "backend.php?op=download&id=" + $.urlParam("id");
RSVP.on('error', function(error) {
if ($(".loading").is(":visible")) {
$(".loading_message").html("Unable to load book.");
}
console.log(error);
});
fetch(book_url, {credentials: 'same-origin'}).then(function(resp) {
if (resp.status == 200) {
var bookId = $.urlParam("b");
// let's store this for later
localforage.setItem(cacheId('book'), resp.clone().blob());
// if there's no base information cached yet, let's do that too
localforage.getItem(cacheId()).then(function(info) {
if (!info) {
$.post("backend.php", {op: "getinfo", id: bookId }, function(data) {
if (data) {
localforage.setItem(cacheId(), data);
if (data.has_cover) {
fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) {
if (resp.status == 200) {
localforage.setItem(cacheId('cover'), resp.blob());
}
});
}
}
});
}
});
resp.blob().then(function(blob) {
var fileReader = new FileReader();
fileReader.onload = function() {
book.open(this.result);
};
fileReader.readAsArrayBuffer(blob);
});
} else {
$(".loading_message").html("Unable to download book: " + resp.status + ".");
}
});
} else {
$(".loading_message").html("This book is not available offline.");
}
}
});
init_taps();
document.onkeydown = hotkey_handler;
var book = ePub({
restore: false,
});
var settings = {
fontSize: localStorage['epube:fontSize'] ? localStorage['epube:fontSize'] : '16px',
fontFamily: localStorage['epube:fontFamily'] ? localStorage['epube:fontFamily'] : 'Georgia',
lineHeight: localStorage['epube:lineHeight'] ? localStorage['epube:lineHeight'] : '140%' };
book.setStyle("fontSize", settings.fontSize + "px");
book.setStyle("fontFamily", settings.fontFamily);
book.setStyle("lineHeight", settings.lineHeight + "%");
//styles: { fontSize: '24px', lineHeight: '130%', fontFamily: 'Georgia' }
window.book = book;
apply_night_mode();
var rendered = book.renderTo("reader");
$('#settings-modal').on('shown.bs.modal', function() {
var font = book.settings.styles.fontFamily;
$("select.font_family").val(font);
$.post("backend.php", { op: "getlastread", id: $.urlParam("id") }, function(data) {
$(".lastread_input").val(data.page);
});
})
$('#toc-modal').on('shown.bs.modal', function() {
book.getToc().then(function(toc) {
var list = $(".toc_list");
list.html("");
$.each(toc, function(i, row) {
var a = $("<a>")
.attr('href', '#')
.html(row.label)
.attr('data-cfi', row.cfi)
.attr('data-id', row.id)
.click(function() {
book.gotoCfi(a.attr('data-cfi'));
});
list.append($("<li>").append(a));
});
});
})
book.on("renderer:chapterUnloaded", function() {
$(".notice").html("Loading...").show();
});
book.on("renderer:chapterDisplayed", function() {
$(".notice").hide();
});
book.on("renderer:chapterDisplayed", function() {
$("#reader iframe")[0].contentWindow.onmousewheel = function(event) {
if (event.deltaY > 0) {
book.nextPage();
} else if (event.deltaY < 0) {
book.prevPage();
}
};
});
book.on("renderer:keydown", hotkey_handler);
book.getMetadata().then(function(meta){
document.title = meta.bookTitle + " – " + meta.creator;
$(".title").html("<b>" + meta.bookTitle + "</b> - " + meta.creator);
});
rendered.then(function() {
localforage.getItem(cacheId("pagination")).then(function(pageList) {
if (pageList && book.loadPagination(pageList).length > 0) {
_pagination_stored = 1;
} else {
var url = "backend.php?op=getpagination&id=" + encodeURIComponent($.urlParam("id"));
EPUBJS.core.request(url).then(function(pageList) {
console.log("pagination: requesting remote: ");
if (book.loadPagination(pageList).length > 0) {
localforage.setItem(cacheId("pagination"), JSON.parse(pageList));
_pagination_stored = 1;
} else {
book.generatePagination();
}
}).catch(function() {
book.generatePagination();
});
}
});
});
book.pageListReady.then(function(pageList) {
if (!_pagination_stored) {
if (navigator.onLine) {
$.post("backend.php", { op: "storepagination", id: $.urlParam("id"),
payload: JSON.stringify(pageList), total: book.pagination.totalPages });
}
localforage.setItem(cacheId("pagination"), pageList);
}
if (navigator.onLine) {
$.post("backend.php", { op: "getlastread", id: $.urlParam("id") }, function(data) {
console.log(data);
if (navigator.onLine && data) {
localforage.setItem(cacheId("lastread"),
{cfi: data.cfi, page: data.page, total: data.total});
book.gotoCfi(data.cfi);
} else {
localforage.getItem(cacheId("lastread")).then(function(item) {
if (item) book.gotoCfi(item.cfi);
});
}
});
} else {
localforage.getItem(cacheId("lastread")).then(function(item) {
if (item) book.gotoCfi(item.cfi);
});
}
$("#total_pages").html(book.pagination.totalPages);
$("#cur_page").html(book.pagination.pageFromCfi(book.getCurrentLocationCfi()));
window.setTimeout(function() {
$(".loading").hide();
}, 1000);
$(".location").click(function() {
var current = book.pagination.pageFromCfi(book.getCurrentLocationCfi());
var total = book.pagination.totalPages;
var page = prompt("Jump to location [1-" + total + "]", current);
if (page) {
book.gotoPage(page);
}
});
});
book.on('book:pageChanged', function(location) {
//console.log(location);
$(".loading").hide();
$("#cur_page").html(location.anchorPage);
if (_store_position && new Date().getTime()/1000 - _last_position_sync > 60) {
console.log("storing lastread");
var currentCfi = book.getCurrentLocationCfi();
var currentPage = location.anchorPage;
var totalPages = book.pagination.totalPages;
if (navigator.onLine) {
$.post("backend.php", { op: "storelastread", id: $.urlParam("id"), page: currentPage,
cfi: currentCfi }, function(data) {
if (data.cfi) {
_last_position_sync = new Date().getTime()/1000;
}
});
_store_position = 0;
} else {
_last_position_sync = 0;
}
localforage.setItem(cacheId("lastread"),
{cfi: currentCfi, page: currentPage, total: totalPages});
}
});
});
</script>
</body>
</html>
|