1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 13:00:08 +00:00

relaxing dependency on jQuery

This commit is contained in:
Lu Wang 2013-11-06 16:33:15 +08:00
parent 4fec92efba
commit 515f496cce
7 changed files with 367 additions and 54 deletions

178
3rdparty/PDF.js/LICENSE vendored Normal file
View File

@ -0,0 +1,178 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

23
3rdparty/PDF.js/build.sh vendored Executable file
View File

@ -0,0 +1,23 @@
#!/bin/sh
# Compile and optimize JS code
# Copyright 2013 Lu Wang <coolwanglu@gmail.com>
BASEDIR=$(dirname $0)
CLOSURE_COMPILER_DIR="$BASEDIR/../closure-compiler"
CLOSURE_COMPILER_JAR="$CLOSURE_COMPILER_DIR/compiler.jar"
INPUT="$BASEDIR/compatibility.js"
OUTPUT_FN="compatibility.min.js"
OUTPUT="$BASEDIR/$OUTPUT_FN"
(echo "Building $OUTPUT_FN with closure-compiler..." && \
java -jar "$CLOSURE_COMPILER_JAR" \
--compilation_level ADVANCED_OPTIMIZATIONS \
--warning_level VERBOSE \
--js "$INPUT" \
--js_output_file "$OUTPUT" && \
echo 'Done.') || \
(echo 'Failed. Read `3rdparty/closure-compiler/README` for more detail.' && \
echo 'Using the uncompressed version.' && \
cat "$INPUT" > "$OUTPUT")

77
3rdparty/PDF.js/compatibility.js vendored Normal file
View File

@ -0,0 +1,77 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @license Copyright 2012 Mozilla Foundation * Apachine License Version 2.0 */
'use strict';
// HTMLElement classList property
(function checkClassListProperty() {
var div = document.createElement('div');
if ('classList' in div)
return; // classList property exists
function changeList(element, itemName, add, remove) {
var s = element.className || '';
var list = s.split(/\s+/g);
if (list[0] === '') list.shift();
var index = list.indexOf(itemName);
if (index < 0 && add)
list.push(itemName);
if (index >= 0 && remove)
list.splice(index, 1);
element.className = list.join(' ');
return (index >= 0);
}
var classListPrototype = {
add: function(name) {
changeList(this.element, name, true, false);
},
contains: function(name) {
return changeList(this.element, name, false, false);
},
remove: function(name) {
changeList(this.element, name, false, true);
},
toggle: function(name) {
changeList(this.element, name, true, true);
}
};
Object.defineProperty(HTMLElement.prototype, 'classList', {
get: function() {
if (this._classList)
return this._classList;
var classList = Object.create(classListPrototype, {
element: {
value: this,
writable: false,
enumerable: true
}
});
Object.defineProperty(this, '_classList', {
value: classList,
writable: false,
enumerable: false
});
return classList;
},
enumerable: true
});
})();

4
3rdparty/PDF.js/compatibility.min.js vendored Normal file
View File

@ -0,0 +1,4 @@
/*
Copyright 2012 Mozilla Foundation * Apachine License Version 2.0 */
(function(){function b(a,b,e,f){var c=(a.className||"").split(/\s+/g);""===c[0]&&c.shift();var d=c.indexOf(b);0>d&&e&&c.push(b);0<=d&&f&&c.splice(d,1);a.className=c.join(" ");return 0<=d}if(!("classList"in document.createElement("div"))){var e={add:function(a){b(this.element,a,!0,!1)},contains:function(a){return b(this.element,a,!1,!1)},remove:function(a){b(this.element,a,!1,!0)},toggle:function(a){b(this.element,a,!0,!0)}};Object.defineProperty(HTMLElement.prototype,"classList",{get:function(){if(this.a)return this.a;
var a=Object.create(e,{element:{value:this,writable:!1,enumerable:!0}});Object.defineProperty(this,"_classList",{value:a,writable:!1,enumerable:!1});return a},enumerable:!0})}})();

View File

@ -254,6 +254,7 @@ install (TARGETS pdf2htmlEX DESTINATION bin)
set(PDF2HTMLEX_RESOURCE
${CMAKE_SOURCE_DIR}/3rdparty/jQuery/jquery-1.9.1.min.js
${CMAKE_SOURCE_DIR}/3rdparty/PDF.js/compatibility.min.js
${CMAKE_SOURCE_DIR}/share/base.css
${CMAKE_SOURCE_DIR}/share/fancy.css
${CMAKE_SOURCE_DIR}/share/LICENSE

View File

@ -47,6 +47,10 @@ $css
# <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
@jquery-1.9.1.min.js
# compatibility.min.js, extracted from PDF.js
# To support old browsers like IE9
@compatibility.min.js
# entry point of pdf2htmlEX.Viewer
# You can override default configuration by passing an object to the constructor of Viewer
# Refer to DEFAULT_CONFIG in viewer.js for possible keys

View File

@ -32,8 +32,7 @@
'use strict';
/* The namespace */
(function(pdf2htmlEX){
(function(pdf2htmlEX, document){
/**
* @const
* @struct
@ -75,6 +74,8 @@
/** @const */
var EPS = 1e-6;
/************************************/
/* utility function */
function invert(ctm) {
var det = ctm[0] * ctm[3] - ctm[1] * ctm[2];
return [ ctm[3] / det
@ -89,8 +90,11 @@
return [ctm[0] * pos[0] + ctm[2] * pos[1] + ctm[4]
,ctm[1] * pos[0] + ctm[3] * pos[1] + ctm[5]];
};
/**
* @constructor
* @param{Element} page The element for the page
* @struct
*/
function Page(page) {
@ -100,18 +104,18 @@
this.shown = false;
this.p = page; // page frame element
var $p = $(page);
this.n = parseInt($p.data('page-no'), 16);
// content box
this.$b = $p.children('.'+CSS_CLASS_NAMES.page_content_box);
this.n = parseInt(page.getAttribute('data-page-no'), 16);
// page size
// Need to make rescale work when page_content_box is not loaded, yet
this.original_height = this.p.clientHeight;
this.original_width = this.p.clientWidth;
// content box
var content_box = page.querySelector('.'+CSS_CLASS_NAMES.page_content_box);
// if page is loaded
if (this.$b.length > 0) {
if (content_box) {
this.content_box = content_box;
/*
* scale ratios
*
@ -119,8 +123,8 @@
* set_r : last set
* cur_r : currently using
*/
this.default_r = this.set_r = this.cur_r = this.original_height / this.$b.height();
this.page_data = $($p.children('.'+CSS_CLASS_NAMES.page_data)[0]).data('data');
this.default_r = this.set_r = this.cur_r = this.original_height / content_box.clientHeight;
this.page_data = JSON.parse(page.querySelector('.'+CSS_CLASS_NAMES.page_data).getAttribute('data-data'));
this.ctm = this.page_data['ctm'];
this.ictm = invert(this.ctm);
@ -131,13 +135,15 @@
$.extend(Page.prototype, /** @lends {Page.prototype} */ {
/* hide & show are for contents, the page frame is still there */
hide : function(){
this.$b.removeClass('opened');
this.shown = false;
if(this.loaded) {
this.content_box.classList.remove('opened');
this.shown = false;
}
},
show : function(){
if (this.loaded) {
if (! this.shown) {
this.$b.addClass('opened');
this.content_box.classList.add('opened');
this.shown = true;
}
}
@ -151,7 +157,9 @@
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)+')');
var s = this.content_box.style;
s.msTransform = s.webkitTransform = s.transform = 'scale('+this.cur_r.toFixed(3)+')';
}
var s = this.p.style;
@ -218,14 +226,14 @@
},
init_after_loading_content : function() {
this.$sidebar = $('#'+this.config['sidebar_id']);
this.$outline = $('#'+this.config['outline_id']);
this.$container = $('#'+this.config['container_id']);
this.$sidebar = $(document.getElementById(this.config['sidebar_id']));
this.$outline = $(document.getElementById(this.config['outline_id']));
this.container = document.getElementById(this.config['container_id']);
this.$loading_indicator = $('.'+this.config['loading_indicator_cls']);
// Open the outline if nonempty
if(this.$outline.children().length > 0) {
this.$sidebar.addClass('opened');
this.$sidebar[0].classList.add('opened');
}
this.find_pages();
@ -233,13 +241,13 @@
// register schedule rendering
// renew old schedules since scroll() may be called frequently
var _ = this;
this.$container.scroll(function(){ _.schedule_render(true); });
$(this.container).scroll(function(){ _.schedule_render(true); });
if(this.config['register_key_handler'])
this.register_key_handler();
// handle links
this.$container.add(this.$outline)
$(this.container).add(this.$outline)
.on('click', '.'+CSS_CLASS_NAMES.link, this, this.link_handler);
this.render();
@ -253,12 +261,15 @@
find_pages : function() {
var new_pages = [];
var new_page_map = {};
var $pl= this.$container.children('.'+CSS_CLASS_NAMES.page_frame);
/* 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]);
new_page_map[p.n] = i;
new_pages.push(p);
var nodes = this.container.childNodes;
for(var i = 0, l = nodes.length; i < l; ++i) {
var cur_node = nodes[i];
if((cur_node.nodeType == Node.ELEMENT_NODE)
&& cur_node.classList.contains(CSS_CLASS_NAMES.page_frame)) {
var p = new Page(cur_node);
new_pages.push(p);
new_page_map[p.n] = new_pages.length - 1;
}
}
this.pages = new_pages;
this.page_map = new_page_map;
@ -290,31 +301,43 @@
url: url,
dataType: 'text'
}).done(function(data){
// find the page element in the data
var div = document.createElement('div');
div.innerHTML = data;
var new_page = null;
var nodes = div.childNodes;
for(var i = 0, l = nodes.length; i < l; ++i) {
var cur_node = nodes[i];
if(cur_node.classList.contains(CSS_CLASS_NAMES.page_frame)) {
new_page = cur_node;
break;
}
}
// replace the old page with loaded data
// the loading indicator on this page should also be destroyed
var p = _.pages[idx];
$(p.p).replaceWith(data);
var p = _.pages[_idx];
_.container.replaceChild(new_page, p.p);
p = new Page(new_page);
_.pages[_idx] = p;
var $new_pf = _.$container.children('#' + CSS_CLASS_NAMES.page_frame + p.n.toString(16));
//TODO: check $new_pf is not empty
p = new Page($new_pf[0]);
p.hide();
p.rescale(_.scale);
// disable background image dragging
/* TODO
$new_pf.find('.'+CSS_CLASS_NAMES.background_image).on('dragstart', function(e){return false;});
*/
_.pages[idx] = p;
_.schedule_render(false);
// Reset loading token
delete _.pages_loading[idx];
if (successCallback) successCallback(_.pages[idx]);
if (successCallback) successCallback(_.pages[_idx]);
}).fail(function(jqXHR, textStatus, errorThrown){
// Reset loading token
delete _.pages_loading[idx];
delete _.pages_loading[_idx];
if (errorCallback) errorCallback();
});
}
@ -348,8 +371,9 @@
if(p.is_nearly_visible()){
if (p.loaded) {
p.show();
} else
this.load_page(pm[p.n]);
} else {
this.load_page(i);
}
} else {
p.hide();
}
@ -360,7 +384,7 @@
* renew: renew the existing schedule instead of using the old one
*/
schedule_render : function(renew) {
if(this.render_timer) {
if(this.render_timer !== undefined) {
if(!renew) return;
clearTimeout(this.render_timer);
}
@ -429,7 +453,7 @@
if (with_alt) { // alt-pageup -> scroll one page up
_.scroll_to(_.get_prev_page());
} else { // pageup -> scroll one screen up
_.$container.scrollTop(_.$container.scrollTop()-_.$container.height());
_.container.scrollTop -= _.container.clientHeight;
}
handled = true;
break;
@ -437,19 +461,19 @@
if (with_alt) { // alt-pagedown -> scroll one page down
_.scroll_to(_.get_next_page());
} else { // pagedown -> scroll one screen down
_.$container.scrollTop(_.$container.scrollTop()+_.$container.height());
_.container.scrollTop += _.container.clientHeight;
}
handled = true;
break;
case 35: // End
if (with_ctrl) {
_.$container.scrollTop(_.$container[0].scrollHeight);
_.container.scrollTop = _.container.scrollHeight;
handled = true;
}
break;
case 36: // Home
if (e.with_ctrl) {
_.$container.scrollTop(0);
_.container.scrollTop = 0;
handled = true;
}
break;
@ -496,16 +520,17 @@
for(var i = 0, l = pl.length; i < l; ++i)
pl[i].rescale(this.scale);
var container = this.container;
// Correct container scroll to keep view aligned while zooming
var correction_top = active_page_ele.offsetTop - prev_offset[1];
this.$container.scrollTop( this.$container.scrollTop() + correction_top + offsetY );
container.scrollTop += correction_top + offsetY;
// Take the center of the view as a reference
var prev_center_x = this.$container.width() / 2 - prev_offset[0];
var prev_center_x = container.clientWidth / 2 - prev_offset[0];
// Calculate the difference respect the center of the view after the zooming
var correction_left = prev_center_x * (this.scale/old_scale - 1) + active_page_ele.offsetLeft - prev_offset[0];
// Scroll the container accordingly to keep alignment to the initial reference
this.$container.scrollLeft( this.$container.scrollLeft() + correction_left + offsetX );
container.scrollLeft += correction_left + offsetX;
// some pages' visibility may be toggled, wait for next render()
// renew old schedules since rescale() may be called frequently
@ -516,7 +541,7 @@
var active_page = this.get_active_page();
if(!active_page) return;
this.rescale(this.$container.width() / active_page.width(), false);
this.rescale(this.container.clientWidth / active_page.width(), false);
this.scroll_to(this.page_map[active_page.n], [0,0]);
},
@ -524,7 +549,7 @@
var active_page = this.get_active_page();
if(!active_page) return;
this.rescale(this.$container.height() / active_page.height(), false);
this.rescale(this.container.clientHeight / active_page.height(), false);
this.scroll_to(this.page_map[active_page.n], [0,0]);
},
@ -553,12 +578,12 @@
link_handler : function (e) {
var _ = e.data;
var t = $(e.currentTarget);
var target = e.currentTarget;
var cur_pos = [0,0];
// cur_page might be undefined, e.g. from Outline
var cur_page = _.get_containing_page(t);
var cur_page = _.get_containing_page(target);
if(cur_page)
{
cur_pos = cur_page.position();
@ -566,7 +591,7 @@
cur_pos = transform(cur_page.ictm, [cur_pos[0], cur_page.height()-cur_pos[1]]);
}
var detail_str = /** @type{string} */ (t.attr('data-dest-detail'));
var detail_str = /** @type{string} */ (e.target.getAttribute('data-dest-detail'));
if(detail_str === undefined) return;
var ok = false;
@ -649,12 +674,13 @@
var cur_target_pos = target_page.position();
this.$container.scrollLeft(this.$container.scrollLeft()-cur_target_pos[0]+pos[0]);
this.$container.scrollTop(this.$container.scrollTop()-cur_target_pos[1]+pos[1]);
var container = this.container;
container.scrollLeft += pos[0] - cur_target_pos[0];
container.scrollTop += pos[1] - cur_target_pos[1];
},
__last_member__ : 'no comma' /*,*/
});
pdf2htmlEX['Viewer'] = Viewer;
})(window['pdf2htmlEX'] = window['pdf2htmlEX'] || {});
})((window['pdf2htmlEX'] = window['pdf2htmlEX'] || {}), document);