From 51dd4fdecd75977a7638212a5bfeb2082e45b1a5 Mon Sep 17 00:00:00 2001 From: Michele Redolfi Date: Thu, 13 Jun 2013 12:39:52 +0200 Subject: [PATCH] load_page idx now starts from 1; pages_loading move to Viewer instance; fixes; pdf2htmlEX.js.in executable bit removed; --- share/pdf2htmlEX.js.in | 69 ++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 29 deletions(-) mode change 100755 => 100644 share/pdf2htmlEX.js.in diff --git a/share/pdf2htmlEX.js.in b/share/pdf2htmlEX.js.in old mode 100755 new mode 100644 index 3eb2e05..fe4d92e --- a/share/pdf2htmlEX.js.in +++ b/share/pdf2htmlEX.js.in @@ -18,9 +18,7 @@ var pdf2htmlEX = (function(){ link : '@CSS_LINK_CN@', __dummy__ : 'no comma' }; - var PAGES_TO_PRELOAD = 3; - - var pages_loading = {}; + var DEFAULT_PAGES_TO_PRELOAD = 3; var pdf2htmlEX = new Object(); @@ -132,6 +130,8 @@ var pdf2htmlEX = (function(){ this.container_id = config['container_id']; this.sidebar_id = config['sidebar_id']; this.outline_id = config['outline_id']; + this.pages_to_preload = config['pages_to_preload'] || DEFAULT_PAGES_TO_PRELOAD; + this.pages_loading = {}; this.init_before_loading_content(); var _ = this; @@ -163,7 +163,7 @@ var pdf2htmlEX = (function(){ var _ = this; this.container.scroll(function(){ _.schedule_render(); }); - this.zoom_fixer(); + //this.zoom_fixer(); // handle links this.container.add(this.outline).on('click', '.'+CSS_CLASS_NAMES['link'], this, this.link_handler); @@ -171,7 +171,7 @@ var pdf2htmlEX = (function(){ this.render(); // Trigger documentLoaded event - $(this.container).trigger("documentLoaded"); + this.container.trigger("documentLoaded"); }, find_pages : function() { var new_pages = new Array(); @@ -183,15 +183,18 @@ var pdf2htmlEX = (function(){ } this.pages = new_pages; }, - load_page : function(idx, pages_to_preload, successCallback) { - if (this.pages[idx+1].loaded) + load_page : function(idx, pages_to_preload, successCallback, errorCallback) { + if (idx >= this.pages.length) + return; // Page does not exist + + if (this.pages[idx].loaded) return; // Page is loaded - if (pages_loading[idx]) + if (this.pages_loading[idx]) return; // Page is already loading - var page_no_hex = (idx+1).toString(16); - var $pf = $(this.container).find('#' + CSS_CLASS_NAMES['page_frame'] + page_no_hex); + var page_no_hex = idx.toString(16); + var $pf = this.container.find('#' + CSS_CLASS_NAMES['page_frame'] + page_no_hex); if($pf.length == 0) return; // Page does not exist @@ -199,34 +202,42 @@ var pdf2htmlEX = (function(){ var url = $pf.data('page-url'); if (url && url.length > 0) { - pages_loading[idx] = true; // Set semaphore + this.pages_loading[idx] = true; // Set semaphore $.ajax({ url: url, dataType: 'text' }).done(function(data){ - _.pages[idx+1].p.parent().replaceWith(data); // pages index starts from 1 + _.pages[idx].p.parent().replaceWith(data); - var $new_pf = $(_.container).find('#' + CSS_CLASS_NAMES['page_frame'] + page_no_hex); - _.pages[idx+1] = new Page($new_pf, _.container); - _.pages[idx+1].rescale(_.scale); + var $new_pf = _.container.find('#' + CSS_CLASS_NAMES['page_frame'] + page_no_hex); + _.pages[idx] = new Page($new_pf, _.container); + _.pages[idx].rescale(_.scale); _.schedule_render(); // Event fired when page is loaded. Please notice that it's not necessarily rendered (see event pageShown). - _.container.trigger("pageLoaded", idx+1); + _.container.trigger("pageLoaded", idx); // disable background image dragging $new_pf.find('.'+CSS_CLASS_NAMES['background_image']).on('dragstart', function(e){return false;}); // Reset loading token - pages_loading[idx] = undefined; + delete _.pages_loading[idx]; if (successCallback) successCallback(); - }); + } + ).fail(function(jqXHR, textStatus, errorThrown){ + console.error('error loading page ' + idx + ': ' + textStatus); + + // Reset loading token + delete _.pages_loading[idx]; + + if (errorCallback) errorCallback(); + }); } // Concurrent prefetch of the next pages if (pages_to_preload === undefined) - pages_to_preload = PAGES_TO_PRELOAD; + pages_to_preload = this.pages_to_preload; if (--pages_to_preload > 0) _.load_page(idx+1, pages_to_preload); @@ -261,7 +272,7 @@ var pdf2htmlEX = (function(){ p.show(); this.container.trigger("pageShown", i); } else - this.load_page(p.n - 1); // load_page index starts from 0 + this.load_page(p.n); } else { p.hide(); } @@ -394,21 +405,21 @@ var pdf2htmlEX = (function(){ } if(ok) { - if (target_page.loaded) { + var transform_and_scroll = function() { pos = transform(target_page.ctm, pos); - if(upside_down) { + if(upside_down) { pos[1] = target_page.height() - pos[1]; } _.scroll_to(detail[0], pos); + } + + if (target_page.loaded) { + transform_and_scroll(); } else { // Scroll to the exact position once loaded. - _.load_page(target_page.n - 1, 1, function() { // load_page index starts from 0 - target_page = _.pages[target_page.n]; - pos = transform(target_page.ctm, pos); - if(upside_down) { - pos[1] = target_page.height() - pos[1]; - } - _.scroll_to(detail[0], pos); + _.load_page(target_page.n, 1, function() { + target_page = _.pages[target_page.n]; // Refresh reference + transform_and_scroll(); }); // In the meantime page gets loaded, scroll approximately position for maximum responsiveness.