1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-05 01:28:39 +00:00

load_page idx now starts from 1; pages_loading move to Viewer instance; fixes; pdf2htmlEX.js.in executable bit removed;

This commit is contained in:
Michele Redolfi 2013-06-13 12:39:52 +02:00
parent d5ade42d5a
commit 51dd4fdecd

69
share/pdf2htmlEX.js.in Executable file → Normal file
View File

@ -18,9 +18,7 @@ var pdf2htmlEX = (function(){
link : '@CSS_LINK_CN@', link : '@CSS_LINK_CN@',
__dummy__ : 'no comma' __dummy__ : 'no comma'
}; };
var PAGES_TO_PRELOAD = 3; var DEFAULT_PAGES_TO_PRELOAD = 3;
var pages_loading = {};
var pdf2htmlEX = new Object(); var pdf2htmlEX = new Object();
@ -132,6 +130,8 @@ var pdf2htmlEX = (function(){
this.container_id = config['container_id']; this.container_id = config['container_id'];
this.sidebar_id = config['sidebar_id']; this.sidebar_id = config['sidebar_id'];
this.outline_id = config['outline_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(); this.init_before_loading_content();
var _ = this; var _ = this;
@ -163,7 +163,7 @@ var pdf2htmlEX = (function(){
var _ = this; var _ = this;
this.container.scroll(function(){ _.schedule_render(); }); this.container.scroll(function(){ _.schedule_render(); });
this.zoom_fixer(); //this.zoom_fixer();
// handle links // handle links
this.container.add(this.outline).on('click', '.'+CSS_CLASS_NAMES['link'], this, this.link_handler); this.container.add(this.outline).on('click', '.'+CSS_CLASS_NAMES['link'], this, this.link_handler);
@ -171,7 +171,7 @@ var pdf2htmlEX = (function(){
this.render(); this.render();
// Trigger documentLoaded event // Trigger documentLoaded event
$(this.container).trigger("documentLoaded"); this.container.trigger("documentLoaded");
}, },
find_pages : function() { find_pages : function() {
var new_pages = new Array(); var new_pages = new Array();
@ -183,15 +183,18 @@ var pdf2htmlEX = (function(){
} }
this.pages = new_pages; this.pages = new_pages;
}, },
load_page : function(idx, pages_to_preload, successCallback) { load_page : function(idx, pages_to_preload, successCallback, errorCallback) {
if (this.pages[idx+1].loaded) if (idx >= this.pages.length)
return; // Page does not exist
if (this.pages[idx].loaded)
return; // Page is loaded return; // Page is loaded
if (pages_loading[idx]) if (this.pages_loading[idx])
return; // Page is already loading return; // Page is already loading
var page_no_hex = (idx+1).toString(16); var page_no_hex = idx.toString(16);
var $pf = $(this.container).find('#' + CSS_CLASS_NAMES['page_frame'] + page_no_hex); var $pf = this.container.find('#' + CSS_CLASS_NAMES['page_frame'] + page_no_hex);
if($pf.length == 0) if($pf.length == 0)
return; // Page does not exist return; // Page does not exist
@ -199,34 +202,42 @@ var pdf2htmlEX = (function(){
var url = $pf.data('page-url'); var url = $pf.data('page-url');
if (url && url.length > 0) { if (url && url.length > 0) {
pages_loading[idx] = true; // Set semaphore this.pages_loading[idx] = true; // Set semaphore
$.ajax({ $.ajax({
url: url, url: url,
dataType: 'text' dataType: 'text'
}).done(function(data){ }).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); var $new_pf = _.container.find('#' + CSS_CLASS_NAMES['page_frame'] + page_no_hex);
_.pages[idx+1] = new Page($new_pf, _.container); _.pages[idx] = new Page($new_pf, _.container);
_.pages[idx+1].rescale(_.scale); _.pages[idx].rescale(_.scale);
_.schedule_render(); _.schedule_render();
// Event fired when page is loaded. Please notice that it's not necessarily rendered (see event pageShown). // 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 // disable background image dragging
$new_pf.find('.'+CSS_CLASS_NAMES['background_image']).on('dragstart', function(e){return false;}); $new_pf.find('.'+CSS_CLASS_NAMES['background_image']).on('dragstart', function(e){return false;});
// Reset loading token // Reset loading token
pages_loading[idx] = undefined; delete _.pages_loading[idx];
if (successCallback) successCallback(); 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 // Concurrent prefetch of the next pages
if (pages_to_preload === undefined) if (pages_to_preload === undefined)
pages_to_preload = PAGES_TO_PRELOAD; pages_to_preload = this.pages_to_preload;
if (--pages_to_preload > 0) if (--pages_to_preload > 0)
_.load_page(idx+1, pages_to_preload); _.load_page(idx+1, pages_to_preload);
@ -261,7 +272,7 @@ var pdf2htmlEX = (function(){
p.show(); p.show();
this.container.trigger("pageShown", i); this.container.trigger("pageShown", i);
} else } else
this.load_page(p.n - 1); // load_page index starts from 0 this.load_page(p.n);
} else { } else {
p.hide(); p.hide();
} }
@ -394,21 +405,21 @@ var pdf2htmlEX = (function(){
} }
if(ok) { if(ok) {
if (target_page.loaded) { var transform_and_scroll = function() {
pos = transform(target_page.ctm, pos); pos = transform(target_page.ctm, pos);
if(upside_down) { if(upside_down) {
pos[1] = target_page.height() - pos[1]; pos[1] = target_page.height() - pos[1];
} }
_.scroll_to(detail[0], pos); _.scroll_to(detail[0], pos);
}
if (target_page.loaded) {
transform_and_scroll();
} else { } else {
// Scroll to the exact position once loaded. // Scroll to the exact position once loaded.
_.load_page(target_page.n - 1, 1, function() { // load_page index starts from 0 _.load_page(target_page.n, 1, function() {
target_page = _.pages[target_page.n]; target_page = _.pages[target_page.n]; // Refresh reference
pos = transform(target_page.ctm, pos); transform_and_scroll();
if(upside_down) {
pos[1] = target_page.height() - pos[1];
}
_.scroll_to(detail[0], pos);
}); });
// In the meantime page gets loaded, scroll approximately position for maximum responsiveness. // In the meantime page gets loaded, scroll approximately position for maximum responsiveness.