diff options
author | Andrew Dolgov <[email protected]> | 2018-05-20 18:46:06 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2018-05-20 18:46:06 +0300 |
commit | d56fb62bd69f0be782b02ed2dcbe4c96b9d2ca0e (patch) | |
tree | 2bbf814cb8c5a2d6b2c9932761cc6080c592faa4 | |
parent | 9599d67bcd04762b94f596edb013eee299046c92 (diff) |
use on-startup resource loading for iframe; and synchronouse switching for themes
-rw-r--r-- | js/read.js | 10 | ||||
-rw-r--r-- | read.html | 55 |
2 files changed, 51 insertions, 14 deletions
@@ -272,15 +272,11 @@ function apply_theme() { else theme = theme.replace("/", ""); - var themeUrl = baseUrl + "themes/" + theme + ".css"; + var theme_url = baseUrl + "themes/" + theme + ".css"; - $("#theme_css").attr("href", themeUrl); - //$(book.renderer.doc).find("#theme_css").attr('href', themeUrl); - - window.parent.$.get(baseUrl + 'themes/' + theme + '.css', function(data) { - $(book.renderer.doc).find("#theme_css").html(data); - }); + $("#theme_css").attr("href", theme_url); + $(book.renderer.doc).find("#theme_css").text(_res_data[theme_url]); }); } @@ -248,6 +248,7 @@ var _last_position_sync = 0; var _is_ios = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; + var _res_data = []; const DEFAULT_FONT_SIZE = 16; const DEFAULT_FONT_FAMILY = "Georgia"; @@ -258,6 +259,40 @@ } $(document).ready(function() { + var res_names = [ "lib/bootstrap/v3/js/jquery.js", "lib/jquery.mobile.custom.js", + "css/transitions.css", + "js/reader.js", "css/reader.css", "js/dict.js", + "themes/default.css", "themes/mocca.css", "themes/night.css", "themes/plan9.css" ]; + + for (var i = 0; i < res_names.length; i++) { + fetch(res_names[i], {credentials: 'same-origin'}).then(function(resp) { + resp.text().then(function(data) { + _res_data[resp.url] = data; + }); + }); + } + + check_resource_load(res_names, _res_data, 0); + }); + + function check_resource_load(res_names, res_data, attempt) { + console.log("check_resource_load", attempt, res_names.length, Object.keys(res_data).length); + + if (attempt == 5) { + $(".loading_message").html("Unable to load resources."); + return; + } + + if (res_names.length != Object.keys(res_data).length) { + window.setTimeout(function() { + check_resource_load(res_names, res_data, attempt+1); + }, 250); + } else { + init_reader(); + } + } + + function init_reader() { apply_theme(); $(window).on('online', function() { @@ -380,11 +415,17 @@ else theme = theme.replace("/", ""); - window.parent.$.get(baseUrl + 'themes/' + theme + '.css', function(data) { + /*window.parent.$.get(baseUrl + 'themes/' + theme + '.css', function(data) { $(book.renderer.doc.head) - .append($("<style type='text/css' id='theme_css' type='text/css'>") - .html(data)); - }); + .append($("<style type='text/css' id='theme_css'>") + .text(data)); + }); */ + + var theme_url = baseUrl + 'themes/' + theme + '.css'; + + $(book.renderer.doc.head) + .append($("<style type='text/css' id='theme_css'>") + .text(_res_data[theme_url])); if (callback) callback(); }); @@ -394,7 +435,7 @@ var baseUrl = window.location.href.match(/^.*\//)[0]; - EPUBJS.core.addScripts([baseUrl + "lib/bootstrap/v3/js/jquery.js", + /*EPUBJS.core.addScripts([baseUrl + "lib/bootstrap/v3/js/jquery.js", baseUrl + "lib/jquery.mobile.custom.js", baseUrl + "js/reader.js", baseUrl + "js/dict.js" ], null, renderer.doc.head); @@ -409,7 +450,7 @@ this.docEl.style[this.transform] = 'translate('+ (-leftPos) + 'px, 0)'; } } - }); + }); */ if (callback) callback(); } @@ -815,7 +856,7 @@ } }); - }); + } </script> </body> |