2012-09-22 14:47:44 +00:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
/* vim modeline copied from pdf.js */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pdf2htmlEX.js
|
|
|
|
*
|
|
|
|
* handles UI events/actions/effects
|
|
|
|
*
|
|
|
|
* Copyright 2012 Lu Wang <coolwanglu@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
var pdf2htmlEX = (function(){
|
|
|
|
var EPS = 1e-6;
|
|
|
|
var Page = function(page, box, visible) {
|
|
|
|
this.p = page;
|
|
|
|
this.b = box;
|
|
|
|
this.v = visible;
|
|
|
|
/*
|
|
|
|
* scale ratios
|
|
|
|
*
|
|
|
|
* default_r : the first one
|
|
|
|
* set_r : last set
|
|
|
|
* cur_r : currently using
|
|
|
|
*/
|
|
|
|
this.default_r = this.set_r = this.cur_r = page.height() / box.height();
|
|
|
|
};
|
|
|
|
Page.prototype.hide = function(){
|
|
|
|
if(this.v) {
|
|
|
|
this.v = false;
|
|
|
|
this.b.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Page.prototype.show = function(){
|
|
|
|
if(Math.abs(this.set_r - this.cur_r) > EPS) {
|
|
|
|
this.cur_r = this.set_r;
|
|
|
|
this.b.css('transform', 'scale('+this.cur_r.toFixed(3)+')');
|
|
|
|
}
|
|
|
|
if(!(this.v)) {
|
|
|
|
this.v = true;
|
|
|
|
this.b.show();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Page.prototype.rescale = function(ratio, is_relative) {
|
|
|
|
if(ratio == 0) {
|
|
|
|
this.set_r = this.default_r;
|
|
|
|
} else if (is_relative) {
|
|
|
|
this.set_r *= ratio;
|
|
|
|
} else {
|
|
|
|
this.set_r = ratio;
|
2012-09-23 05:04:29 +00:00
|
|
|
}
|
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
/* wait for redraw */
|
|
|
|
this.hide();
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
this.p.height(this.b.height() * this.set_r);
|
|
|
|
this.p.width(this.b.width() * this.set_r);
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
};
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
return {
|
|
|
|
pages : [],
|
|
|
|
container : null,
|
|
|
|
render_timer : null,
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
/* Constants */
|
|
|
|
render_timeout : 200,
|
|
|
|
scale_step : 0.9,
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
init_before_loading_content : function() {
|
|
|
|
/*hide all pages before loading, will reveal only visible ones later */
|
|
|
|
this.pre_hide_pages();
|
|
|
|
},
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
init_after_loading_content : function() {
|
|
|
|
this.container = $("#pdf-main");
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
var new_pages = new Array();
|
2012-09-24 08:43:24 +00:00
|
|
|
var pl= $(".p", this.container);
|
|
|
|
var pbl = $(".b", this.container);
|
2012-09-23 09:26:12 +00:00
|
|
|
for(var i = 0, l = pl.length; i < l; ++i) {
|
|
|
|
new_pages.push(new Page($(pl[i]), $(pbl[i]), false));
|
|
|
|
}
|
|
|
|
this.pages = new_pages;
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
var _ = this;
|
|
|
|
this.container.scroll(function(){ _.schedule_render(); });
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
//this.zoom_fixer();
|
2012-09-23 05:04:29 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
|
|
|
init : function() {
|
|
|
|
this.init_before_loading_content();
|
|
|
|
|
|
|
|
var _ = this;
|
|
|
|
$(document).ready(function(){_.init_after_loading_content();});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
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");
|
|
|
|
n.type = "text/css";
|
|
|
|
if (n.styleSheet) {
|
|
|
|
n.styleSheet.cssText = s;
|
|
|
|
} else {
|
|
|
|
n.appendChild(document.createTextNode(s));
|
|
|
|
}
|
|
|
|
document.getElementsByTagName("head")[0].appendChild(n);
|
|
|
|
},
|
|
|
|
|
|
|
|
hide_pages : function() {
|
|
|
|
var pl = this.pages;
|
|
|
|
for(var i = 0, l = pl.length; i < l; ++i)
|
|
|
|
pl[i].hide();
|
|
|
|
},
|
|
|
|
|
|
|
|
render : function () {
|
|
|
|
/* hide (positional) invisible pages */
|
|
|
|
var pl = this.pages;
|
|
|
|
var l = pl.length;
|
|
|
|
var ch = this.container.height();
|
|
|
|
|
|
|
|
var i;
|
|
|
|
for(i = 0; i < l; ++i) {
|
|
|
|
var p = pl[i];
|
|
|
|
|
2012-09-24 08:43:24 +00:00
|
|
|
if(p.p.position().top + p.p.height() >= 0) break;
|
2012-09-23 09:26:12 +00:00
|
|
|
if(i > 0) pl[i-1].hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
if((i > 0) && (i < l)) pl[i-1].show();
|
|
|
|
|
|
|
|
for(; i < l; ++i) {
|
|
|
|
var p = pl[i];
|
|
|
|
p.show();
|
|
|
|
|
2012-09-24 08:43:24 +00:00
|
|
|
if(p.p.position().top > ch) break;
|
2012-09-23 09:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(++i; i < l; ++i) {
|
|
|
|
pl[i].hide();
|
2012-09-22 14:47:44 +00:00
|
|
|
}
|
2012-09-23 09:26:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
schedule_render : function() {
|
|
|
|
if(this.render_timer)
|
|
|
|
clearTimeout(this.render_timer);
|
|
|
|
|
|
|
|
var _ = this;
|
|
|
|
this.render_timer = setTimeout(function () {
|
|
|
|
_.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(0, false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2012-09-24 12:20:34 +00:00
|
|
|
e.preventDefault();
|
2012-09-22 16:17:30 +00:00
|
|
|
}
|
2012-09-23 09:26:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
rescale : function (ratio, is_relative) {
|
|
|
|
var pl = this.pages;
|
|
|
|
for(var i = 0, l = pl.length; i < l; ++i) {
|
|
|
|
pl[i].rescale(ratio, is_relative);
|
2012-09-23 05:04:29 +00:00
|
|
|
}
|
2012-09-22 16:17:30 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
this.schedule_render();
|
|
|
|
},
|
2012-09-22 16:17:30 +00:00
|
|
|
|
2012-09-23 09:26:12 +00:00
|
|
|
__last_member__ : 'no comma' /*,*/
|
|
|
|
}.init();
|
|
|
|
})();
|