You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
438 lines
14 KiB
438 lines
14 KiB
/* Copyright 2017 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. |
|
*/ |
|
'use strict'; |
|
|
|
Object.defineProperty(exports, "__esModule", { |
|
value: true |
|
}); |
|
exports.localized = exports.animationStarted = exports.normalizeWheelEventDelta = exports.binarySearchFirstItem = exports.watchScroll = exports.scrollIntoView = exports.getOutputScale = exports.approximateFraction = exports.roundToDivide = exports.getVisibleElements = exports.parseQueryString = exports.noContextMenuHandler = exports.getPDFFileNameFromURL = exports.ProgressBar = exports.EventBus = exports.mozL10n = exports.RendererType = exports.cloneObj = exports.VERTICAL_PADDING = exports.SCROLLBAR_PADDING = exports.MAX_AUTO_SCALE = exports.UNKNOWN_SCALE = exports.MAX_SCALE = exports.MIN_SCALE = exports.DEFAULT_SCALE = exports.DEFAULT_SCALE_VALUE = exports.CSS_UNITS = undefined; |
|
|
|
var _pdfjs = require('./pdfjs'); |
|
|
|
var CSS_UNITS = 96.0 / 72.0; |
|
var DEFAULT_SCALE_VALUE = 'auto'; |
|
var DEFAULT_SCALE = 1.0; |
|
var MIN_SCALE = 0.25; |
|
var MAX_SCALE = 10.0; |
|
var UNKNOWN_SCALE = 0; |
|
var MAX_AUTO_SCALE = 1.25; |
|
var SCROLLBAR_PADDING = 40; |
|
var VERTICAL_PADDING = 5; |
|
var RendererType = { |
|
CANVAS: 'canvas', |
|
SVG: 'svg' |
|
}; |
|
var mozL10n = typeof document !== 'undefined' ? document.mozL10n || document.webL10n : undefined; |
|
_pdfjs.PDFJS.disableFullscreen = _pdfjs.PDFJS.disableFullscreen === undefined ? false : _pdfjs.PDFJS.disableFullscreen; |
|
_pdfjs.PDFJS.useOnlyCssZoom = _pdfjs.PDFJS.useOnlyCssZoom === undefined ? false : _pdfjs.PDFJS.useOnlyCssZoom; |
|
_pdfjs.PDFJS.maxCanvasPixels = _pdfjs.PDFJS.maxCanvasPixels === undefined ? 16777216 : _pdfjs.PDFJS.maxCanvasPixels; |
|
_pdfjs.PDFJS.disableHistory = _pdfjs.PDFJS.disableHistory === undefined ? false : _pdfjs.PDFJS.disableHistory; |
|
_pdfjs.PDFJS.disableTextLayer = _pdfjs.PDFJS.disableTextLayer === undefined ? false : _pdfjs.PDFJS.disableTextLayer; |
|
_pdfjs.PDFJS.ignoreCurrentPositionOnZoom = _pdfjs.PDFJS.ignoreCurrentPositionOnZoom === undefined ? false : _pdfjs.PDFJS.ignoreCurrentPositionOnZoom; |
|
{ |
|
_pdfjs.PDFJS.locale = _pdfjs.PDFJS.locale === undefined && typeof navigator !== 'undefined' ? navigator.language : _pdfjs.PDFJS.locale; |
|
} |
|
function getOutputScale(ctx) { |
|
var devicePixelRatio = window.devicePixelRatio || 1; |
|
var backingStoreRatio = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1; |
|
var pixelRatio = devicePixelRatio / backingStoreRatio; |
|
return { |
|
sx: pixelRatio, |
|
sy: pixelRatio, |
|
scaled: pixelRatio !== 1 |
|
}; |
|
} |
|
function scrollIntoView(element, spot, skipOverflowHiddenElements) { |
|
var parent = element.offsetParent; |
|
if (!parent) { |
|
console.error('offsetParent is not set -- cannot scroll'); |
|
return; |
|
} |
|
var checkOverflow = skipOverflowHiddenElements || false; |
|
var offsetY = element.offsetTop + element.clientTop; |
|
var offsetX = element.offsetLeft + element.clientLeft; |
|
while (parent.clientHeight === parent.scrollHeight || checkOverflow && getComputedStyle(parent).overflow === 'hidden') { |
|
if (parent.dataset._scaleY) { |
|
offsetY /= parent.dataset._scaleY; |
|
offsetX /= parent.dataset._scaleX; |
|
} |
|
offsetY += parent.offsetTop; |
|
offsetX += parent.offsetLeft; |
|
parent = parent.offsetParent; |
|
if (!parent) { |
|
return; |
|
} |
|
} |
|
if (spot) { |
|
if (spot.top !== undefined) { |
|
offsetY += spot.top; |
|
} |
|
if (spot.left !== undefined) { |
|
offsetX += spot.left; |
|
parent.scrollLeft = offsetX; |
|
} |
|
} |
|
parent.scrollTop = offsetY; |
|
} |
|
function watchScroll(viewAreaElement, callback) { |
|
var debounceScroll = function debounceScroll(evt) { |
|
if (rAF) { |
|
return; |
|
} |
|
rAF = window.requestAnimationFrame(function viewAreaElementScrolled() { |
|
rAF = null; |
|
var currentY = viewAreaElement.scrollTop; |
|
var lastY = state.lastY; |
|
if (currentY !== lastY) { |
|
state.down = currentY > lastY; |
|
} |
|
state.lastY = currentY; |
|
callback(state); |
|
}); |
|
}; |
|
var state = { |
|
down: true, |
|
lastY: viewAreaElement.scrollTop, |
|
_eventHandler: debounceScroll |
|
}; |
|
var rAF = null; |
|
viewAreaElement.addEventListener('scroll', debounceScroll, true); |
|
return state; |
|
} |
|
function parseQueryString(query) { |
|
var parts = query.split('&'); |
|
var params = {}; |
|
for (var i = 0, ii = parts.length; i < ii; ++i) { |
|
var param = parts[i].split('='); |
|
var key = param[0].toLowerCase(); |
|
var value = param.length > 1 ? param[1] : null; |
|
params[decodeURIComponent(key)] = decodeURIComponent(value); |
|
} |
|
return params; |
|
} |
|
function binarySearchFirstItem(items, condition) { |
|
var minIndex = 0; |
|
var maxIndex = items.length - 1; |
|
if (items.length === 0 || !condition(items[maxIndex])) { |
|
return items.length; |
|
} |
|
if (condition(items[minIndex])) { |
|
return minIndex; |
|
} |
|
while (minIndex < maxIndex) { |
|
var currentIndex = minIndex + maxIndex >> 1; |
|
var currentItem = items[currentIndex]; |
|
if (condition(currentItem)) { |
|
maxIndex = currentIndex; |
|
} else { |
|
minIndex = currentIndex + 1; |
|
} |
|
} |
|
return minIndex; |
|
} |
|
function approximateFraction(x) { |
|
if (Math.floor(x) === x) { |
|
return [x, 1]; |
|
} |
|
var xinv = 1 / x; |
|
var limit = 8; |
|
if (xinv > limit) { |
|
return [1, limit]; |
|
} else if (Math.floor(xinv) === xinv) { |
|
return [1, xinv]; |
|
} |
|
var x_ = x > 1 ? xinv : x; |
|
var a = 0, |
|
b = 1, |
|
c = 1, |
|
d = 1; |
|
while (true) { |
|
var p = a + c, |
|
q = b + d; |
|
if (q > limit) { |
|
break; |
|
} |
|
if (x_ <= p / q) { |
|
c = p; |
|
d = q; |
|
} else { |
|
a = p; |
|
b = q; |
|
} |
|
} |
|
var result; |
|
if (x_ - a / b < c / d - x_) { |
|
result = x_ === x ? [a, b] : [b, a]; |
|
} else { |
|
result = x_ === x ? [c, d] : [d, c]; |
|
} |
|
return result; |
|
} |
|
function roundToDivide(x, div) { |
|
var r = x % div; |
|
return r === 0 ? x : Math.round(x - r + div); |
|
} |
|
function getVisibleElements(scrollEl, views, sortByVisibility) { |
|
var top = scrollEl.scrollTop, |
|
bottom = top + scrollEl.clientHeight; |
|
var left = scrollEl.scrollLeft, |
|
right = left + scrollEl.clientWidth; |
|
function isElementBottomBelowViewTop(view) { |
|
var element = view.div; |
|
var elementBottom = element.offsetTop + element.clientTop + element.clientHeight; |
|
return elementBottom > top; |
|
} |
|
var visible = [], |
|
view, |
|
element; |
|
var currentHeight, viewHeight, hiddenHeight, percentHeight; |
|
var currentWidth, viewWidth; |
|
var firstVisibleElementInd = views.length === 0 ? 0 : binarySearchFirstItem(views, isElementBottomBelowViewTop); |
|
for (var i = firstVisibleElementInd, ii = views.length; i < ii; i++) { |
|
view = views[i]; |
|
element = view.div; |
|
currentHeight = element.offsetTop + element.clientTop; |
|
viewHeight = element.clientHeight; |
|
if (currentHeight > bottom) { |
|
break; |
|
} |
|
currentWidth = element.offsetLeft + element.clientLeft; |
|
viewWidth = element.clientWidth; |
|
if (currentWidth + viewWidth < left || currentWidth > right) { |
|
continue; |
|
} |
|
hiddenHeight = Math.max(0, top - currentHeight) + Math.max(0, currentHeight + viewHeight - bottom); |
|
percentHeight = (viewHeight - hiddenHeight) * 100 / viewHeight | 0; |
|
visible.push({ |
|
id: view.id, |
|
x: currentWidth, |
|
y: currentHeight, |
|
view: view, |
|
percent: percentHeight |
|
}); |
|
} |
|
var first = visible[0]; |
|
var last = visible[visible.length - 1]; |
|
if (sortByVisibility) { |
|
visible.sort(function (a, b) { |
|
var pc = a.percent - b.percent; |
|
if (Math.abs(pc) > 0.001) { |
|
return -pc; |
|
} |
|
return a.id - b.id; |
|
}); |
|
} |
|
return { |
|
first: first, |
|
last: last, |
|
views: visible |
|
}; |
|
} |
|
function noContextMenuHandler(e) { |
|
e.preventDefault(); |
|
} |
|
function isDataSchema(url) { |
|
var i = 0, |
|
ii = url.length; |
|
while (i < ii && url[i].trim() === '') { |
|
i++; |
|
} |
|
return url.substr(i, 5).toLowerCase() === 'data:'; |
|
} |
|
function getPDFFileNameFromURL(url) { |
|
var defaultFilename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'document.pdf'; |
|
|
|
if (isDataSchema(url)) { |
|
console.warn('getPDFFileNameFromURL: ' + 'ignoring "data:" URL for performance reasons.'); |
|
return defaultFilename; |
|
} |
|
var reURI = /^(?:(?:[^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/; |
|
var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i; |
|
var splitURI = reURI.exec(url); |
|
var suggestedFilename = reFilename.exec(splitURI[1]) || reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[3]); |
|
if (suggestedFilename) { |
|
suggestedFilename = suggestedFilename[0]; |
|
if (suggestedFilename.indexOf('%') !== -1) { |
|
try { |
|
suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0]; |
|
} catch (e) {} |
|
} |
|
} |
|
return suggestedFilename || defaultFilename; |
|
} |
|
function normalizeWheelEventDelta(evt) { |
|
var delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY); |
|
var angle = Math.atan2(evt.deltaY, evt.deltaX); |
|
if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) { |
|
delta = -delta; |
|
} |
|
var MOUSE_DOM_DELTA_PIXEL_MODE = 0; |
|
var MOUSE_DOM_DELTA_LINE_MODE = 1; |
|
var MOUSE_PIXELS_PER_LINE = 30; |
|
var MOUSE_LINES_PER_PAGE = 30; |
|
if (evt.deltaMode === MOUSE_DOM_DELTA_PIXEL_MODE) { |
|
delta /= MOUSE_PIXELS_PER_LINE * MOUSE_LINES_PER_PAGE; |
|
} else if (evt.deltaMode === MOUSE_DOM_DELTA_LINE_MODE) { |
|
delta /= MOUSE_LINES_PER_PAGE; |
|
} |
|
return delta; |
|
} |
|
function cloneObj(obj) { |
|
var result = {}; |
|
for (var i in obj) { |
|
if (Object.prototype.hasOwnProperty.call(obj, i)) { |
|
result[i] = obj[i]; |
|
} |
|
} |
|
return result; |
|
} |
|
var animationStarted = new Promise(function (resolve) { |
|
window.requestAnimationFrame(resolve); |
|
}); |
|
var localized = new Promise(function (resolve, reject) { |
|
if (!mozL10n) { |
|
resolve(); |
|
return; |
|
} |
|
if (mozL10n.getReadyState() !== 'loading') { |
|
resolve(); |
|
return; |
|
} |
|
window.addEventListener('localized', function localized(evt) { |
|
resolve(); |
|
}); |
|
}); |
|
var EventBus = function EventBusClosure() { |
|
function EventBus() { |
|
this._listeners = Object.create(null); |
|
} |
|
EventBus.prototype = { |
|
on: function EventBus_on(eventName, listener) { |
|
var eventListeners = this._listeners[eventName]; |
|
if (!eventListeners) { |
|
eventListeners = []; |
|
this._listeners[eventName] = eventListeners; |
|
} |
|
eventListeners.push(listener); |
|
}, |
|
off: function EventBus_on(eventName, listener) { |
|
var eventListeners = this._listeners[eventName]; |
|
var i; |
|
if (!eventListeners || (i = eventListeners.indexOf(listener)) < 0) { |
|
return; |
|
} |
|
eventListeners.splice(i, 1); |
|
}, |
|
dispatch: function EventBus_dispath(eventName) { |
|
var eventListeners = this._listeners[eventName]; |
|
if (!eventListeners || eventListeners.length === 0) { |
|
return; |
|
} |
|
var args = Array.prototype.slice.call(arguments, 1); |
|
eventListeners.slice(0).forEach(function (listener) { |
|
listener.apply(null, args); |
|
}); |
|
} |
|
}; |
|
return EventBus; |
|
}(); |
|
var ProgressBar = function ProgressBarClosure() { |
|
function clamp(v, min, max) { |
|
return Math.min(Math.max(v, min), max); |
|
} |
|
function ProgressBar(id, opts) { |
|
this.visible = true; |
|
this.div = document.querySelector(id + ' .progress'); |
|
this.bar = this.div.parentNode; |
|
this.height = opts.height || 100; |
|
this.width = opts.width || 100; |
|
this.units = opts.units || '%'; |
|
this.div.style.height = this.height + this.units; |
|
this.percent = 0; |
|
} |
|
ProgressBar.prototype = { |
|
updateBar: function ProgressBar_updateBar() { |
|
if (this._indeterminate) { |
|
this.div.classList.add('indeterminate'); |
|
this.div.style.width = this.width + this.units; |
|
return; |
|
} |
|
this.div.classList.remove('indeterminate'); |
|
var progressSize = this.width * this._percent / 100; |
|
this.div.style.width = progressSize + this.units; |
|
}, |
|
get percent() { |
|
return this._percent; |
|
}, |
|
set percent(val) { |
|
this._indeterminate = isNaN(val); |
|
this._percent = clamp(val, 0, 100); |
|
this.updateBar(); |
|
}, |
|
setWidth: function ProgressBar_setWidth(viewer) { |
|
if (viewer) { |
|
var container = viewer.parentNode; |
|
var scrollbarWidth = container.offsetWidth - viewer.offsetWidth; |
|
if (scrollbarWidth > 0) { |
|
this.bar.setAttribute('style', 'width: calc(100% - ' + scrollbarWidth + 'px);'); |
|
} |
|
} |
|
}, |
|
hide: function ProgressBar_hide() { |
|
if (!this.visible) { |
|
return; |
|
} |
|
this.visible = false; |
|
this.bar.classList.add('hidden'); |
|
document.body.classList.remove('loadingInProgress'); |
|
}, |
|
show: function ProgressBar_show() { |
|
if (this.visible) { |
|
return; |
|
} |
|
this.visible = true; |
|
document.body.classList.add('loadingInProgress'); |
|
this.bar.classList.remove('hidden'); |
|
} |
|
}; |
|
return ProgressBar; |
|
}(); |
|
exports.CSS_UNITS = CSS_UNITS; |
|
exports.DEFAULT_SCALE_VALUE = DEFAULT_SCALE_VALUE; |
|
exports.DEFAULT_SCALE = DEFAULT_SCALE; |
|
exports.MIN_SCALE = MIN_SCALE; |
|
exports.MAX_SCALE = MAX_SCALE; |
|
exports.UNKNOWN_SCALE = UNKNOWN_SCALE; |
|
exports.MAX_AUTO_SCALE = MAX_AUTO_SCALE; |
|
exports.SCROLLBAR_PADDING = SCROLLBAR_PADDING; |
|
exports.VERTICAL_PADDING = VERTICAL_PADDING; |
|
exports.cloneObj = cloneObj; |
|
exports.RendererType = RendererType; |
|
exports.mozL10n = mozL10n; |
|
exports.EventBus = EventBus; |
|
exports.ProgressBar = ProgressBar; |
|
exports.getPDFFileNameFromURL = getPDFFileNameFromURL; |
|
exports.noContextMenuHandler = noContextMenuHandler; |
|
exports.parseQueryString = parseQueryString; |
|
exports.getVisibleElements = getVisibleElements; |
|
exports.roundToDivide = roundToDivide; |
|
exports.approximateFraction = approximateFraction; |
|
exports.getOutputScale = getOutputScale; |
|
exports.scrollIntoView = scrollIntoView; |
|
exports.watchScroll = watchScroll; |
|
exports.binarySearchFirstItem = binarySearchFirstItem; |
|
exports.normalizeWheelEventDelta = normalizeWheelEventDelta; |
|
exports.animationStarted = animationStarted; |
|
exports.localized = localized; |