From d56fb62bd69f0be782b02ed2dcbe4c96b9d2ca0e Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 20 May 2018 18:46:06 +0300 Subject: use on-startup resource loading for iframe; and synchronouse switching for themes --- js/read.js | 10 +++------- read.html | 55 ++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/js/read.js b/js/read.js index 04b1e47..edbc2de 100644 --- a/js/read.js +++ b/js/read.js @@ -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]); }); } diff --git a/read.html b/read.html index 541e28a..7b09c64 100644 --- a/read.html +++ b/read.html @@ -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($("