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

support hashchange

This commit is contained in:
Lu Wang 2013-11-16 16:50:18 +08:00
parent 32366bf580
commit a60570f81c
3 changed files with 70 additions and 47 deletions

4
TODO
View File

@ -1,3 +1,6 @@
position history stack (popstate)
scale limits
onresize
built in support for ttfautohint built in support for ttfautohint
beforeprint/afterprint beforeprint/afterprint
@ -10,7 +13,6 @@ more information on demo page:
pdf:miui pdf:miui
tmp dir: use pid tmp dir: use pid
view hash view hash
position history stack (popstate)
- dots issue - dots issue
- AdobeXML*.pdf - AdobeXML*.pdf

View File

@ -46,7 +46,7 @@ $css
# Refer to DEFAULT_CONFIG in viewer.js for possible keys # Refer to DEFAULT_CONFIG in viewer.js for possible keys
# E.g. # E.g.
# pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({ # pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({
# 'register_key_handler' : false # 'key_handler' : false
# }); # });
@pdf2htmlEX.min.js @pdf2htmlEX.min.js
""" """

View File

@ -63,7 +63,9 @@ var DEFAULT_CONFIG = {
// zoom ratio step for each zoom in/out event // zoom ratio step for each zoom in/out event
'scale_step' : 0.9, 'scale_step' : 0.9,
// register global key handler // register global key handler
'register_key_handler' : true, 'key_handler' : true,
// register hashchange handler
'hashchange_handler' : true,
'__dummy__' : 'no comma' '__dummy__' : 'no comma'
}; };
@ -277,12 +279,19 @@ Viewer.prototype = {
// disable dragging of background images // disable dragging of background images
disable_dragstart(document.getElementsByClassName(CSS_CLASS_NAMES.background_image)); disable_dragstart(document.getElementsByClassName(CSS_CLASS_NAMES.background_image));
if (this.config['register_key_handler']) if (this.config['key_handler'])
this.register_key_handler(); this.register_key_handler();
var self = this;
if (this.config['hashchange_handler']) {
window.addEventListener('hashchange', function(e) {
self.navigate_to_dest(document.location.hash.substring(1));
}, false);
}
// register schedule rendering // register schedule rendering
// renew old schedules since scroll() may be called frequently // renew old schedules since scroll() may be called frequently
var self = this;
this.container.addEventListener('scroll', function() { this.container.addEventListener('scroll', function() {
self.schedule_render(true); self.schedule_render(true);
}, false); }, false);
@ -398,8 +407,12 @@ Viewer.prototype = {
if (pages_to_preload === undefined) if (pages_to_preload === undefined)
pages_to_preload = this.config['preload_pages']; pages_to_preload = this.config['preload_pages'];
if (--pages_to_preload > 0) if (--pages_to_preload > 0) {
this.load_page(idx+1, pages_to_preload); var self = this;
setTimeout(function() {
self.load_page(idx+1, pages_to_preload);
},0);
}
}, },
/* /*
@ -678,23 +691,25 @@ Viewer.prototype = {
*/ */
link_handler : function (e) { link_handler : function (e) {
var target = /** @type{Node} */(e.target); var target = /** @type{Node} */(e.target);
var cur_pos = [0,0];
// cur_page might be undefined, e.g. from Outline
var cur_page = this.get_containing_page(target);
if (cur_page)
{
cur_pos = cur_page.view_position();
//get the coordinates in default user system
cur_pos = transform(cur_page.ictm, [cur_pos[0], cur_page.height()-cur_pos[1]]);
}
var detail_str = /** @type{string} */ (target.getAttribute('data-dest-detail')); var detail_str = /** @type{string} */ (target.getAttribute('data-dest-detail'));
if (!detail_str) return; if (!detail_str) return;
var ok = false; this.navigate_to_dest(detail_str, this.get_containing_page(target));
e.preventDefault();
},
/**
* @param{string} detail_str may come from user provided hashtag, need sanitzing
* @param{Page=} src_page page containing the source event (e.g. link)
*/
navigate_to_dest : function(detail_str, src_page) {
try {
var detail = JSON.parse(detail_str); var detail = JSON.parse(detail_str);
} catch(e) {
return;
}
if(!(detail instanceof Array)) return;
var target_page_no = detail[0]; var target_page_no = detail[0];
var page_map = this.page_map; var page_map = this.page_map;
@ -702,9 +717,17 @@ Viewer.prototype = {
var target_page_idx = page_map[target_page_no]; var target_page_idx = page_map[target_page_no];
var target_page = this.pages[target_page_idx]; var target_page = this.pages[target_page_idx];
// cur_page might be undefined, e.g. from Outline
var cur_page = src_page || this.pages[this.cur_page_idx];
var cur_pos = cur_page.view_position();
//get the coordinates in default user system
cur_pos = transform(cur_page.ictm, [cur_pos[0], cur_page.height()-cur_pos[1]]);
var pos = [0,0]; var pos = [0,0];
var zoom = this.scale; var zoom = this.scale;
var upside_down = true; var upside_down = true;
var ok = false;
// TODO: fitb* // TODO: fitb*
// TODO: BBox // TODO: BBox
switch(detail[1]) { switch(detail[1]) {
@ -738,11 +761,11 @@ Viewer.prototype = {
ok = true; ok = true;
break; break;
default: default:
ok = false;
break; break;
} }
if (ok) { if (!ok) return;
this.rescale(zoom, false); this.rescale(zoom, false);
var self = this; var self = this;
@ -769,8 +792,6 @@ Viewer.prototype = {
// In the meantime page gets loaded, scroll approximately position for maximum responsiveness. // In the meantime page gets loaded, scroll approximately position for maximum responsiveness.
this.scroll_to(target_page_idx, [0,0]); this.scroll_to(target_page_idx, [0,0]);
} }
e.preventDefault();
}
}, },
/** /**