diff options
author | Andrew Dolgov <[email protected]> | 2018-06-29 14:02:22 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2018-06-29 14:02:22 +0300 |
commit | 9d8ca2924f83b481162291ccebc2c48d9d538c54 (patch) | |
tree | 71fd765e72890542dca16fa4abcdddfbd36845ee | |
parent | 0d622ef4954bc9bb8743dc17f5af34637f69ffee (diff) |
some more updates
-rw-r--r-- | js/read.js | 46 | ||||
-rw-r--r-- | read.html | 199 |
2 files changed, 82 insertions, 163 deletions
@@ -185,27 +185,22 @@ function apply_styles() { 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"); */ + window.book.rendition.themes.default({ + html: { + 'font-size': fontSize, + 'font-family': fontFamily, + 'line-height': lineHeight, + } + }); + apply_theme(); }); } function clear_lastread() { if (confirm("Clear stored last read location?")) { - var total = book.locations.length(); + var total = window.book.locations.length(); if (navigator.onLine) { $.post("backend.php", { op: "storelastread", page: -1, cfi: "", id: $.urlParam("id") }, function(data) { @@ -221,8 +216,8 @@ function clear_lastread() { function mark_as_read() { if (confirm("Mark book as read?")) { - var total = book.locations.length(); - var lastCfi = book.locations.cfiFromPercentage(1); + var total = 100; + var lastCfi = window.book.locations.cfiFromPercentage(1); if (navigator.onLine) { $.post("backend.php", { op: "storelastread", page: total, cfi: lastCfi, id: $.urlParam("id") }, function(data) { @@ -237,11 +232,11 @@ function mark_as_read() { } function save_and_close() { - var location = book.rendition.currentLocation(); + var location = window.book.rendition.currentLocation(); - var currentPage = location.start.location; var currentCfi = location.start.cfi; - var totalPages = book.locations.length(); + var currentPage = parseInt(window.book.locations.percentageFromCfi(currentCfi) * 100); + var totalPages = 100; localforage.setItem(cacheId("lastread"), {cfi: currentCfi, page: currentPage, total: totalPages}); @@ -259,7 +254,7 @@ function save_and_close() { function change_theme(elem) { var theme = $(elem).val(); localforage.setItem("epube.theme", theme).then(function() { - apply_theme(); + apply_styles(); }); } @@ -269,16 +264,15 @@ function apply_theme() { var base_url = window.location.href.match(/^.*\//)[0]; - if (!theme) - theme = 'default'; - else - theme = theme.replace("/", ""); + if (!theme) theme = 'default'; var theme_url = base_url + "themes/" + theme + ".css"; $("#theme_css").attr("href", theme_url); - //$(book.renderer.doc).find("#theme_css").text(_res_data[theme_url]); + $.each(window.book.rendition.getContents(), function(i,c) { + $(c.document).find("#theme_css").text(_res_data[theme_url]) + }); }); } @@ -300,7 +294,7 @@ function search() { .attr('data-cfi', row.cfi) .attr('data-id', row.id) .click(function() { - window.book.gotoCfi(a.attr('data-cfi')); + window.book.rendition.display(a.attr('data-cfi')); }); list.append($("<li>").append(a)); @@ -414,31 +414,56 @@ _enable_fullscreen = enable; }); - /*EPUBJS.Hooks.register("beforeChapterDisplay").applyTheme = function(callback, renderer) { + var book = ePub({ + restore: false, + minSpreadWidth: 961, + }); - localforage.getItem("epube.theme").then(function(theme) { - var base_url = window.location.href.match(/^.*\//)[0]; + window.book = book; - if (!theme) - theme = 'default'; - else - theme = theme.replace("/", ""); + var rendition = book.renderTo("reader", {width: '100%', height: '100%'}); + var displayed = rendition.display(); - var theme_url = base_url + 'themes/' + theme + '.css'; + var fontSize; + var fontFamily; + var lineHeight; + var themeName; + + Promise.all([ + localforage.getItem("epube.fontSize"), + localforage.getItem("epube.fontFamily"), + localforage.getItem("epube.lineHeight"), + localforage.getItem("epube.theme") + ]).then(function(res) { + fontSize = res[0] ? res[0] + "px" : DEFAULT_FONT_SIZE + "px"; + fontFamily = res[1] ? res[1] : DEFAULT_FONT_FAMILY; + lineHeight = res[2] ? res[2] + "%" : DEFAULT_LINE_HEIGHT + "%"; + themeName = res[3] ? res[3] : 'default'; + + rendition.themes.default({ + html: { + 'font-size': fontSize, + 'font-family': fontFamily, + 'line-height': lineHeight, + } + }); - $(book.renderer.doc.head) - .append($("<style type='text/css' id='theme_css'>") - .text(_res_data[theme_url])); + }); - if (callback) callback(); - }); - } + rendition.hooks.content.register(function(contents) { - EPUBJS.Hooks.register("beforeChapterDisplay").initDict = function(callback, renderer) { + $("#reader iframe")[0].contentWindow.onwheel = function(event) { + if (event.deltaY > 0) { + next_page(); + } else if (event.deltaY < 0) { + prev_page(); + } + }; var base_url = window.location.href.match(/^.*\//)[0]; - var res_names = [ "lib/bootstrap/v3/js/jquery.js", "lib/jquery.mobile.custom.js", "js/reader.js", "js/dict.js" ]; - var doc = book.renderer.doc; + var res_names = [ "lib/bootstrap/v3/js/jquery.js", "lib/jquery.mobile.custom.js", + "js/reader.js", "js/dict.js" ]; + var doc = contents.document; for (var i = 0; i < res_names.length; i++) { @@ -450,74 +475,33 @@ doc.head.appendChild(elem); } - $(book.renderer.doc.head) + $(contents.document.head) .append($("<style type='text/css'>") .text(_res_data[base_url + 'css/reader.css'])); localforage.getItem("epube.disable-transitions").then(function(notransitions) { if (!notransitions) { - $(book.renderer.doc.head) + $(contents.document.head) .append($("<style type='text/css'>") .text(_res_data[base_url + 'css/transitions.css'])); - EPUBJS.Render.Iframe.prototype.setLeft = function(leftPos){ + // TODO: fix transitions somehow + + /*EPUBJS.Render.Iframe.prototype.setLeft = function(leftPos){ this.docEl.style[this.transform] = 'translate('+ (-leftPos) + 'px, 0)'; - } + }*/ } }); - if (callback) callback(); - }*/ - -/* EPUBJS.Renderer.prototype.onResized = function(e) { - this.trigger('renderer:beforeResize'); - - var width = $("#reader").width(); - var height = $("#reader").height(); - - this.resize(width, height, false); - }; */ - - if (_is_ios) { - var book = ePub({ - restore: false, - width: $("#reader").width(), - height: $("#reader").height(), - }); - } else { - var book = ePub({ - restore: false, - minSpreadWidth: 961, - }); - } - - window.book = book; + localforage.getItem("epube.theme").then(function(theme) { - var rendition = book.renderTo("reader", {width: '100%', height: '100%'}); - var displayed = rendition.display(); + if (!theme) theme = 'default'; - rendition.hooks.content.register(function(contents) { + var theme_url = base_url + 'themes/' + theme + '.css'; - var fontSize; - var fontFamily; - var lineHeight; - var themeName; - - Promise.all([ - localforage.getItem("epube.fontSize"), - localforage.getItem("epube.fontFamily"), - localforage.getItem("epube.lineHeight"), - localforage.getItem("epube.theme") - ]).then(function(res) { - fontSize = res[0] ? res[0] + "px" : DEFAULT_FONT_SIZE + "px"; - fontFamily = res[1] ? res[1] : DEFAULT_FONT_FAMILY; - lineHeight = res[2] ? res[2] + "%" : DEFAULT_LINE_HEIGHT + "%"; - themeName = res[3] ? res[3] : 'default'; - - contents.css("font-size", fontSize, 1); - contents.css("font-family", fontFamily, 1); - contents.css("line-height", lineHeight, 1); - contents.css("text-align", "justify", 1); + $(contents.document.head) + .append($("<style type='text/css' id='theme_css'>") + .text(_res_data[theme_url])); }); }); @@ -589,7 +573,6 @@ $('#dict-modal').on('shown.bs.modal', function() { $(".dict_result").scrollTop(0); - }) $(".dict_search_btn").on("click", function() { @@ -679,11 +662,12 @@ }); }) - book.on("renderer:chapterUnloaded", function() { + /*rendition.hooks.unloaded.register(function() { + console.log('unloaded'); + $(".loading").show(); $(".loading_message").html("Opening chapter..."); - - }); + });*/ book.on("renderer:chapterDisplayed", function() { $(".chapter").html(""); @@ -734,67 +718,6 @@ if (toc_entry) $(".chapter").html(toc_entry.label); }); - book.on("renderer:chapterDisplayed", function() { - $("#reader iframe")[0].contentWindow.onwheel = function(event) { - - if (event.deltaY > 0) { - next_page(); - } else if (event.deltaY < 0) { - prev_page(); - } - - }; - }); - - /* book.on("renderer:chapterDisplayed", function() { - - // variables defined above after reading from localforage - - $("#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"); - - }); */ - - 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); - });*/ - - /*book.ready.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(1020, 2400); - } - - }).catch(function() { - book.generatePagination(1020, 2400); - }); - } - }); - - }); */ - book.ready.then(function() { var meta = book.package.metadata; @@ -900,11 +823,13 @@ }); + rendition.on("keyup", hotkey_handler); + rendition.on('relocated', function(location) { - var currentPage = location.start.location; var currentCfi = location.start.cfi; - var totalPages = book.locations.length(); + var currentPage = parseInt(book.locations.percentageFromCfi(currentCfi) * 100); + var totalPages = 100; var pct = book.locations.percentageFromCfi(currentCfi); $("#cur_page").html(currentPage); |