'use strict';
/* global localforage, EpubeApp */
const DEFAULT_FONT_SIZE = 16;
const DEFAULT_FONT_FAMILY = "Georgia";
const DEFAULT_LINE_HEIGHT = 140;
const Reader = {
init: function() {
$(document).on("keyup", function(e) {
Reader.hotkeyHandler(e);
});
$("#left").on("mouseup", function() {
Reader.Page.prev();
});
$("#right").on("mouseup", function() {
Reader.Page.next();
});
Reader.Loader.init();
},
initSecondStage: function() {
if (typeof EpubeApp != "undefined") {
EpubeApp.setPage("PAGE_READER");
}
Reader.applyTheme();
/* global Cookie */
if (Cookie.get("is-epube-app") == "true") {
$("body").addClass("is-epube-app");
}
$(window).on('online', function() {
console.log("we're online, storing lastread");
const currentCfi = book.rendition.currentLocation().start.cfi;
const currentPage = parseInt(book.locations.percentageFromCfi(currentCfi) * 100);
$.post("backend.php", { op: "storelastread", id: $.urlParam("id"), page: currentPage,
cfi: currentCfi }, function(data) {
if (data.cfi) {
Reader.Page._last_position_sync = new Date().getTime()/1000;
}
})
.fail(function(e) {
if (e && e.status == 401) {
window.location = "index.php";
}
});
});
localforage.getItem(Reader.cacheId("book")).then(function(item) {
// ios doesn't work with FileReader for whatever reason
if (/*!_is_ios &&*/ item) {
console.log("loading from local storage");
const fileReader = new FileReader();
fileReader.onload = function() {
try {
book.open(this.result);
} catch (e) {
$(".loading_message").html("Unable to load book (local).");
console.log(e);
}
};
fileReader.readAsArrayBuffer(item);
} else {
console.log("loading from network");
if (navigator.onLine) {
const book_url = "backend.php?op=download&id=" + $.urlParam("id");
$(".loading_message").html("Downloading...");
fetch(book_url, {credentials: 'same-origin'}).then(function(resp) {
if (resp.status == 200) {
const bookId = $.urlParam("b");
resp.blob().then(function(blob) {
// if there's no base information cached yet, let's do that too
localforage.getItem(Reader.cacheId()).then(function(info) {
if (!info) {
$.post("backend.php", {op: "getinfo", id: bookId }, function(data) {
if (data) {
localforage.setItem(Reader.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(Reader.cacheId('cover'), resp.blob());
}
});
}
}
});
}
});
const fileReader = new FileReader();
fileReader.onload = function() {
book.open(this.result).then(() => {
// let's store this for later
localforage.setItem(Reader.cacheId('book'), blob);
}).catch((e) => {
$(".loading_message").html("Unable to open book.
" + e + "");
});
};
fileReader.onerror = function(e) {
console.log('filereader error', e);
$(".loading_message").html("Unable to open book.
" + e + "");
};
fileReader.readAsArrayBuffer(blob);
}).catch((e) => {
console.log('blob error', e);
$(".loading_message").html("Unable to download book.
" + e + "");
});
} else {
$(".loading_message").html("Unable to download book: " + resp.status + ".");
}
}).catch(function(e) {
console.warn(e);
if ($(".loading").is(":visible")) {
$(".loading_message").html("Unable to load book (remote).
" + e + "");
}
});
} else {
$(".loading_message").html("This book is not available offline.");
}
}
});
const book = ePub();
window.book = book;
const rendition = book.renderTo("reader", {
width: '100%',
height: '100%',
minSpreadWidth: 961
});
rendition.hooks.content.register(function() {
Reader.applyStyles();
});
rendition.display().then(function() {
console.log("book displayed");
});
rendition.hooks.content.register(function(contents) {
contents.on("linkClicked", function(href) {
console.log('linkClicked', href);
if (href.indexOf("://") == -1) {
$(".prev_location_btn")
.attr("data-location-cfi", book.rendition.currentLocation().start.cfi)
.show();
window.setTimeout(function() {
show_ui(true);
}, 50);
}
});
const base_url = window.location.href.match(/^.*\//)[0];
const res_names = [ "lib/bootstrap/v3/js/jquery.js", "lib/jquery.mobile-events.min.js",
"js/reader_iframe.js", "js/dict.js" ];
const doc = contents.document;
for (let i = 0; i < res_names.length; i++) {
// we need to create script element with proper context, that is inside the iframe
const elem = doc.createElement("script");
elem.type = 'text/javascript';
elem.text = Reader.Loader._res_data[base_url + res_names[i]];
doc.head.appendChild(elem);
}
$(contents.document.head)
.append($("