diff --git a/share/pdf2htmlEX.js b/share/pdf2htmlEX.js index 4376732..007529a 100644 --- a/share/pdf2htmlEX.js +++ b/share/pdf2htmlEX.js @@ -16,10 +16,20 @@ var pdf2htmlEX = (function(){ page_boxes : [], container : null, selective_render_timer : null, + default_scale_ratio : 1.0, + cur_scale_ratio : 1.0, + + + + /* Constants */ + render_timeout : 200, + scale_step : 0.9, + + init_before_loading_content : function() { /*hide all pages before loading, will reveal only visible ones later */ - this.hide_pages(); + this.pre_hide_pages(); }, init_after_loading_content : function() { @@ -27,16 +37,17 @@ var pdf2htmlEX = (function(){ this.page_boxes = $(".b"); this.container = $("#pdf-main"); + if(this.pages.length > 0) + { + this.default_scale_ratio = this.cur_scale_ratio + = $(this.pages[0]).height() / $(this.page_boxes[0]).height(); + } + this.selective_render(); var _ = this; - this.container.scroll(function(){ - if(_.selective_render_timer) - clearTimeout(_.selective_render_timer); - _.selective_render_timer = setTimeout(function () { - _.selective_render(); - }, 200); - }); + this.container.scroll(function(){ _.schedule_render(); }); + this.zoom_fixer(); }, init : function() { @@ -48,7 +59,7 @@ var pdf2htmlEX = (function(){ return this; }, - hide_pages : function() { + pre_hide_pages : function() { /* pages might have not been loaded yet, so add a CSS rule */ var s = '.b{display:none;}'; var n = document.createElement("style"); @@ -61,6 +72,13 @@ var pdf2htmlEX = (function(){ document.getElementsByTagName("head")[0].appendChild(n); }, + hide_pages : function() { + for(var i = 0, l = this.page_boxes.length; i < l; ++i) + { + $(this.page_boxes[i]).hide(); + } + }, + selective_render : function () { /* hide (positional) invisible pages */ var l = this.pages.length; @@ -87,6 +105,77 @@ var pdf2htmlEX = (function(){ } }, + schedule_render : function() { + if(this.selective_render_timer) + clearTimeout(this.selective_render_timer); + + var _ = this; + this.selective_render_timer = setTimeout(function () { + _.selective_render(); + }, this.render_timeout); + }, + + zoom_fixer : function () { + /* + * When user try to zoom in/out using ctrl + +/- or mouse wheel + * handle this and prevent the default behaviours + * + * Code credit to PDF.js + */ + var _ = this; + // Firefox specific event, so that we can prevent browser from zooming + $(window).on('DOMMouseScroll', function(e) { + if (e.ctrlKey) { + e.preventDefault(); + _.rescale(Math.pow(_.scale_step, e.detail), true); + } + }); + + $(window).on('keydown', function keydown(e) { + if (e.ctrlKey || e.metaKey) { + switch (e.keyCode) { + case 61: // FF/Mac '=' + case 107: // FF '+' and '=' + case 187: // Chrome '+' + _.rescale(1.0 / _.scale_step, true); + break; + case 173: // FF/Mac '-' + case 109: // FF '-' + case 189: // Chrome '-' + _.rescale(_.scale_step, true); + break; + case 48: // '0' + _.rescale(_.default_scale_ratio, false); + break; + default: + return; + } + } + e.preventDefault(); + }); + }, + + rescale : function (ratio, is_relative) { + console.log('RESCALE'); + + if(is_relative) + ratio *= this.cur_scale_ratio; + + this.cur_scale_ratio = ratio; + this.hide_pages(); + + for(var i = 0, l = this.page_boxes.length; i < l; ++i) + { + var p = $(this.pages[i]); + var pb = $(this.page_boxes[i]); + p.height(pb.height() * ratio); + p.width(pb.width() * ratio); + pb.css('transform', 'scale('+ratio+')'); + } + + this.schedule_render(); + }, + __last_member__ : 'no comma' /*,*/ }.init(); })();