From cc79880d57350a6a435b34f06d3d86b06883df5a Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Mon, 28 Oct 2013 16:29:58 +0800 Subject: [PATCH] fix js --- TODO | 1 + share/js_src/pdf2htmlEX.js.in | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index 2163841..86372ef 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,4 @@ +page idx vs no move $container outside Page tracking current page more js annoatation diff --git a/share/js_src/pdf2htmlEX.js.in b/share/js_src/pdf2htmlEX.js.in index 7c53520..089efbe 100644 --- a/share/js_src/pdf2htmlEX.js.in +++ b/share/js_src/pdf2htmlEX.js.in @@ -193,7 +193,7 @@ */ function Viewer(config) { /* do nothing if jQuery is not ready */ - if(!window.jQuery) return; + if(!jQuery) return; this.config = $.extend({}, DEFAULT_CONFIG, config); this.pages_loading = []; @@ -248,24 +248,25 @@ /* don't use for(..in..) since $pl is more than an Array */ for(var i = 0, l = $pl.length; i < l; ++i) { var p = new Page($pl[i], this.$container); + new_page_map[p.n] = i; new_pages.push(p); - new_page_map[p.n] = p; } this.pages = new_pages; this.page_map = new_page_map; }, load_page : function(idx, pages_to_preload, successCallback, errorCallback) { - if (idx >= this.pages.length) + var pages = this.pages; + if (idx >= pages.length) return; // Page does not exist - if (this.pages[idx].loaded) + if (pages[idx].loaded) return; // Page is loaded if (this.pages_loading[idx]) return; // Page is already loading - var page_no_hex = idx.toString(16); + var page_no_hex = pages[idx].n.toString(16); var $pf = this.$container.find('#' + CSS_CLASS_NAMES.page_frame + page_no_hex); if($pf.length == 0) return; // Page does not exist @@ -298,7 +299,7 @@ // Reset loading token delete _.pages_loading[idx]; - if (successCallback) successCallback(); + if (successCallback) successCallback(_.pages[idx]); } ).fail(function(jqXHR, textStatus, errorThrown){ // Reset loading token @@ -337,13 +338,14 @@ render : function () { /* hide (positional) invisible pages */ var pl = this.pages; + var pm = this.page_map; for(var i = 0, l = pl.length; i < l; ++i) { var p = pl[i]; if(p.is_nearly_visible()){ if (p.loaded) { p.show(); } else - this.load_page(p.n); + this.load_page(pm[p.n]); } else { p.hide(); } @@ -470,7 +472,8 @@ // Immediate rendering optimizations enabled to improve reactiveness while zooming // Find out which pages are visible var min_visible, max_visible; - min_visible = max_visible = active_page.n; + // TODO: page index + min_visible = max_visible = this.page_map[active_page.n]; while (pl[min_visible] && pl[min_visible].is_visible()) { --min_visible; } ++ min_visible; while (pl[max_visible] && pl[max_visible].is_visible()) { ++max_visible; } @@ -515,7 +518,7 @@ if(!active_page) return; this.rescale(this.$container.width() / active_page.w, false); - this.scroll_to(active_page.n, [0,0]); + this.scroll_to(this.page_map[active_page.n], [0,0]); }, fit_height : function () { @@ -523,7 +526,7 @@ if(!active_page) return; this.rescale(this.$container.height() / active_page.h, false); - this.scroll_to(active_page.n, [0,0]); + this.scroll_to(this.page_map[active_page.n], [0,0]); }, get_active_page : function () { @@ -629,7 +632,7 @@ transform_and_scroll(target_page); } else { // Scroll to the exact position once loaded. - _.load_page(target_page.n, undefined, transform_and_scroll); + _.load_page(target_page_idx, undefined, transform_and_scroll); // In the meantime page gets loaded, scroll approximately position for maximum responsiveness. _.scroll_to(target_page_idx, [0,0]);