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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
<!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">
<select class="font_size form-control" onchange="apply_font_size(this)"></select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Line height</label>
<div class="col-sm-9">
<select class="line_height form-control" onchange="apply_line_height(this)"></select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label"></label>
<div class="col-sm-9">
<div class="checkbox">
<label>
<input class="transition_checkbox" onchange="toggle_transitions(this)"
type="checkbox"> Disable transitions (needs page reload)
</label>
</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="dict-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">Dictionary Lookup</h4>
</div>
<div class="modal-body">
<div class="dict_result"> </div>
</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="modal fade" id="search-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">Search</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" onsubmit="return false;">
<div class="form-group">
<label class="col-sm-4 control-label">Search (active chapter)</label>
<div class="col-sm-8">
<input type="search" class="form-control search_input">
</div>
</div>
<ol class="search_results"> </ol>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="search()">Search</button>
<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="toggle_night_mode()">
<span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
</button>
<button class="btn btn-default btn-xs"
data-toggle="modal" data-target="#search-modal">
<span class="glyphicon glyphicon-search" 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">?</span> / <span id="total_pages">?</span>
(<span id="page_pct">?</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;
const DEFAULT_FONT_SIZE = 16;
const DEFAULT_FONT_FAMILY = "Georgia";
const DEFAULT_LINE_HEIGHT = 140;
function cacheId(suffix) {
return "epube-book." + $.urlParam("b") + (suffix ? "." + suffix : "");
}
$(document).ready(function() {
apply_night_mode();
$(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;
EPUBJS.Hooks.register("beforeChapterDisplay").swipeDetection = function(callback, renderer) {
var baseUrl = window.location.href.match(/^.*\//)[0];
EPUBJS.core.addScripts([baseUrl + "lib/bootstrap/v3/js/jquery.js",
baseUrl + "lib/jquery.mobile.custom.js",
baseUrl + "js/swipes.js" ], null, renderer.doc.head);
localforage.getItem("epube.disable-transitions").then(function(disable) {
if (!disable) {
EPUBJS.core.addCss(baseUrl + "css/transitions.css", null, renderer.doc.head);
EPUBJS.Render.Iframe.prototype.setLeft = function(leftPos){
this.docEl.style[this.transform] = 'translate('+ (-leftPos) + 'px, 0)';
}
}
});
if (callback) callback();
}
var book = ePub({
restore: false,
});
Promise.all([
localforage.getItem("epube.fontSize"),
localforage.getItem("epube.fontFamily"),
localforage.getItem("epube.lineHeight")
]).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 + "%";
book.setStyle("fontSize", fontSize);
book.setStyle("fontFamily", fontFamily);
book.setStyle("lineHeight", lineHeight);
});
//styles: { fontSize: '24px', lineHeight: '130%', fontFamily: 'Georgia' }
window.book = book;
var rendered = book.renderTo("reader");
$('#settings-modal').on('shown.bs.modal', function() {
$.post("backend.php", { op: "getlastread", id: $.urlParam("id") }, function(data) {
$(".lastread_input").val(data.page);
});
localforage.getItem("epube.disable-transitions").then(function(disable) {
$(".transition_checkbox").attr("checked", disable);
});
localforage.getItem("epube.fontFamily").then(function(font) {
if (!font) font = DEFAULT_FONT_FAMILY;
$(".font_family").val(font);
});
localforage.getItem("epube.fontSize").then(function(size) {
if (!size) size = DEFAULT_FONT_SIZE;
var zoom = $(".font_size").html("");
for (var i = 10; i <= 32; i++) {
var opt = $("<option>").val(i).html(i + " px");
if (i == size) opt.attr("selected", "1");
zoom.append(opt);
}
});
localforage.getItem("epube.lineHeight").then(function(height) {
if (!height) height = DEFAULT_LINE_HEIGHT;
var zoom = $(".line_height").html("");
for (var i = 100; i <= 220; i += 10) {
var opt = $("<option>").val(i).html(i + "%");
if (i == height) opt.attr("selected", "1");
zoom.append(opt);
}
});
})
$('#dict-modal').on('shown.bs.modal', function() {
$(".dict_result").scrollTop(0);
})
$('#toc-modal').on('shown.bs.modal', function() {
book.getToc().then(function(toc) {
var list = $(".toc_list");
list.html("");
$.each(toc, function(i, row) {
// if anything fails here the toc entry is likely useless anyway (i.e. no cfi)
try {
var a = $("<a>")
.attr('href', '#')
.html(row.label + " <b>(Loc. " +
book.pagination.pageFromCfi(row.cfi) + ")</b>")
.attr('data-cfi', row.cfi)
.click(function() {
book.gotoCfi(a.attr('data-cfi'));
});
list.append($("<li>").append(a));
} catch (e) {
console.warn(e);
}
});
// well the toc didn't work out, might as well generate one
if (list.children().length <= 1) {
list.html("");
$.each(book.spine, function (i, row) {
var a = $("<a>")
.attr('href', '#')
.attr('title', row.url)
.html("Section " + (i+1) + " <b>(Loc. " +
book.pagination.pageFromCfi(row.cfi) + ")</b>")
.attr('data-cfi', row.cfi)
.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.onwheel = function(event) {
if (event.deltaY > 0) {
book.nextPage();
} else if (event.deltaY < 0) {
book.prevPage();
}
};
$("#reader iframe")[0].contentWindow.onmouseup = function(event) {
if (!navigator.onLine) return;
var wnd = this;
var sel = wnd.getSelection().toString().trim();
if (sel.match(/^\w+$/)) {
$.post("backend.php", {op: 'define', word: sel}, function(data) {
if (data) {
wnd.getSelection().removeAllRanges();
$(".dict_result").html(data.result.join("<br/>"));
$("#dict-modal").modal('show');
}
});
}
};
});
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(600, 1200);
}
}).catch(function() {
book.generatePagination(600, 1200);
});
}
});
});
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);
});
}
var curPage = book.pagination.pageFromCfi(book.getCurrentLocationCfi());
$("#total_pages").html(book.pagination.totalPages);
$("#cur_page").html(curPage);
if (book.pagination.totalPages > 0) {
var pct = parseInt(curPage / book.pagination.totalPages * 100);
$("#page_pct").html(pct + "%");
}
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);
var total = book.pagination.totalPages;
if (book.pagination.totalPages > 0) {
var pct = parseInt(location.anchorPage / book.pagination.totalPages * 100);
$("#page_pct").html(pct + "%");
}
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>
|