1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 04:50:09 +00:00

js: store pages in an array

This commit is contained in:
Lu Wang 2013-10-28 16:05:24 +08:00
parent aa30d82d7a
commit 7787b26e5b
2 changed files with 39 additions and 25 deletions

1
TODO
View File

@ -8,7 +8,6 @@ pdf:miui
tmp dir: use pid tmp dir: use pid
remove page from dom -- UI option remove page from dom -- UI option
view hash view hash
store $pages from 0,
- store a separate map/dict for 'original' page number - store a separate map/dict for 'original' page number
- OR adjust all destinations - OR adjust all destinations

View File

@ -190,14 +190,13 @@
/** /**
* export pdf2htmlEX.Viewer * export pdf2htmlEX.Viewer
* @constructor * @constructor
* @struct
*/ */
function Viewer(config) { function Viewer(config) {
/* do nothing if jQuery is not ready */ /* do nothing if jQuery is not ready */
if(!window.jQuery) return; if(!window.jQuery) return;
this.config = $.extend({}, DEFAULT_CONFIG, config); this.config = $.extend({}, DEFAULT_CONFIG, config);
this.pages_loading = {}; this.pages_loading = [];
this.init_before_loading_content(); this.init_before_loading_content();
var _ = this; var _ = this;
@ -237,15 +236,23 @@
this.render(); this.render();
}, },
/*
* set up this.pages and this.page_map
* pages is an array holding all the Page objects
* page-Map maps an original page number (in PDF) to the corresponding index in page
*/
find_pages : function() { find_pages : function() {
var new_pages = new Array(); var new_pages = [];
var new_page_map = {};
var $pl= $('.'+CSS_CLASS_NAMES.page_frame, this.$container); var $pl= $('.'+CSS_CLASS_NAMES.page_frame, this.$container);
/* don't use for(..in..) since $pl is more than an Array */ /* don't use for(..in..) since $pl is more than an Array */
for(var i = 0, l = $pl.length; i < l; ++i) { for(var i = 0, l = $pl.length; i < l; ++i) {
var p = new Page($pl[i], this.$container); var p = new Page($pl[i], this.$container);
new_pages[p.n] = p; new_pages.push(p);
new_page_map[p.n] = p;
} }
this.pages = new_pages; this.pages = new_pages;
this.page_map = new_page_map;
}, },
load_page : function(idx, pages_to_preload, successCallback, errorCallback) { load_page : function(idx, pages_to_preload, successCallback, errorCallback) {
@ -323,14 +330,14 @@
hide_pages : function() { hide_pages : function() {
var pl = this.pages; var pl = this.pages;
for(var i in pl) for(var i = 0, l = pl.length; i < l; ++i)
pl[i].hide(); pl[i].hide();
}, },
render : function () { render : function () {
/* hide (positional) invisible pages */ /* hide (positional) invisible pages */
var pl = this.pages; var pl = this.pages;
for(var i in pl) { for(var i = 0, l = pl.length; i < l; ++i) {
var p = pl[i]; var p = pl[i];
if(p.is_nearly_visible()){ if(p.is_nearly_visible()){
if (p.loaded) { if (p.loaded) {
@ -481,7 +488,7 @@
this.scale = ratio; this.scale = ratio;
// Rescale pages // Rescale pages
for(var i in pl) { for(var i = 0, l = pl.length; i < l; ++i) {
if (prerendering_enabled && i >= min_visible && i <= max_visible) if (prerendering_enabled && i >= min_visible && i <= max_visible)
pl[i].rescale(this.scale, true); // Force immediate refresh pl[i].rescale(this.scale, true); // Force immediate refresh
else else
@ -524,7 +531,7 @@
var y_center = this.$container.offset().top + this.$container.height() / 2; var y_center = this.$container.offset().top + this.$container.height() / 2;
var pl = this.pages; var pl = this.pages;
var last_page = -1; var last_page = -1;
for (var i in pl) { for(var i = 0, l = pl.length; i < l; ++i) {
if (pl[i].$p.offset().top > y_center) if (pl[i].$p.offset().top > y_center)
break; break;
last_page = i; last_page = i;
@ -534,8 +541,15 @@
get_containing_page : function(obj) { get_containing_page : function(obj) {
/* get the page obj containing obj */ /* get the page obj containing obj */
var p = obj.closest('.'+CSS_CLASS_NAMES.page_frame)[0]; var $p = obj.closest('.'+CSS_CLASS_NAMES.page_frame);
return p && this.pages[(new Page(p, null)).n]; if($p.length == 0) return;
/*
* Get original page number and map it to index of pages
* TODO: store the index on the dom element
*/
var pn = (new Page($p[0], null)).n;
var pm = this.page_map;
return (pn in pm) && this.pages[pm[pn]];
}, },
link_handler : function (e) { link_handler : function (e) {
@ -559,8 +573,11 @@
var ok = false; var ok = false;
var detail = JSON.parse(detail_str); var detail = JSON.parse(detail_str);
var target_page = _.pages[detail[0]]; var target_page_no = detail[0];
if(target_page === undefined) return; var page_map = this.page_map;
if(!(target_page_no in page_map)) return;
var target_page_idx = page_map[target_page_no];
var target_page = _.pages[target_page_idx];
var pos = [0,0]; var pos = [0,0];
var upside_down = true; var upside_down = true;
@ -599,33 +616,31 @@
} }
if(ok) { if(ok) {
var transform_and_scroll = function() { /* page should of type Page */
pos = transform(target_page.ctm, pos); var transform_and_scroll = function(page) {
pos = transform(page.ctm, pos);
if(upside_down) { if(upside_down) {
pos[1] = target_page.h - pos[1]; pos[1] = page.h - pos[1];
} }
_.scroll_to(detail[0], pos); _.scroll_to(target_page_idx, pos);
}; };
if (target_page.loaded) { if (target_page.loaded) {
transform_and_scroll(); transform_and_scroll(target_page);
} else { } else {
// Scroll to the exact position once loaded. // Scroll to the exact position once loaded.
_.load_page(target_page.n, 1, function() { _.load_page(target_page.n, undefined, transform_and_scroll);
target_page = _.pages[target_page.n]; // Refresh reference
transform_and_scroll();
});
// In the meantime page gets loaded, scroll approximately position for maximum responsiveness. // In the meantime page gets loaded, scroll approximately position for maximum responsiveness.
_.scroll_to(detail[0], [0,0]); _.scroll_to(target_page_idx, [0,0]);
} }
e.preventDefault(); e.preventDefault();
} }
}, },
/* pos=[x,y], where (0,0) is the top-left corner */ /* pos=[x,y], where (0,0) is the top-left corner */
scroll_to : function(pageno, pos) { scroll_to : function(page_idx, pos) {
var target_page = this.pages[pageno]; var target_page = this.pages[page_idx];
if(target_page === undefined) return; if(target_page === undefined) return;
if(pos === undefined) if(pos === undefined)