Use const in ui_utils.js

This commit is contained in:
Wojciech Maj 2019-12-22 18:16:32 +01:00
parent 6316b2a195
commit cd03fe014d
No known key found for this signature in database
GPG Key ID: 24A586806A31F908

View File

@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* eslint no-var: error, prefer-const: error */
const CSS_UNITS = 96.0 / 72.0; const CSS_UNITS = 96.0 / 72.0;
const DEFAULT_SCALE_VALUE = 'auto'; const DEFAULT_SCALE_VALUE = 'auto';
@ -69,7 +70,7 @@ function formatL10nValue(text, args) {
* No-op implementation of the localization service. * No-op implementation of the localization service.
* @implements {IL10n} * @implements {IL10n}
*/ */
let NullL10n = { const NullL10n = {
async getLanguage() { async getLanguage() {
return 'en-us'; return 'en-us';
}, },
@ -92,13 +93,13 @@ let NullL10n = {
* not required, true otherwise. * not required, true otherwise.
*/ */
function getOutputScale(ctx) { function getOutputScale(ctx) {
let devicePixelRatio = window.devicePixelRatio || 1; const devicePixelRatio = window.devicePixelRatio || 1;
let backingStoreRatio = ctx.webkitBackingStorePixelRatio || const backingStoreRatio = ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio || ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio || ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio || ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1; ctx.backingStorePixelRatio || 1;
let pixelRatio = devicePixelRatio / backingStoreRatio; const pixelRatio = devicePixelRatio / backingStoreRatio;
return { return {
sx: pixelRatio, sx: pixelRatio,
sy: pixelRatio, sy: pixelRatio,
@ -157,7 +158,7 @@ function scrollIntoView(element, spot, skipOverflowHiddenElements = false) {
* PDF.js friendly one: with scroll debounce and scroll direction. * PDF.js friendly one: with scroll debounce and scroll direction.
*/ */
function watchScroll(viewAreaElement, callback) { function watchScroll(viewAreaElement, callback) {
let debounceScroll = function(evt) { const debounceScroll = function(evt) {
if (rAF) { if (rAF) {
return; return;
} }
@ -165,14 +166,14 @@ function watchScroll(viewAreaElement, callback) {
rAF = window.requestAnimationFrame(function viewAreaElementScrolled() { rAF = window.requestAnimationFrame(function viewAreaElementScrolled() {
rAF = null; rAF = null;
let currentX = viewAreaElement.scrollLeft; const currentX = viewAreaElement.scrollLeft;
let lastX = state.lastX; const lastX = state.lastX;
if (currentX !== lastX) { if (currentX !== lastX) {
state.right = currentX > lastX; state.right = currentX > lastX;
} }
state.lastX = currentX; state.lastX = currentX;
let currentY = viewAreaElement.scrollTop; const currentY = viewAreaElement.scrollTop;
let lastY = state.lastY; const lastY = state.lastY;
if (currentY !== lastY) { if (currentY !== lastY) {
state.down = currentY > lastY; state.down = currentY > lastY;
} }
@ -181,7 +182,7 @@ function watchScroll(viewAreaElement, callback) {
}); });
}; };
let state = { const state = {
right: true, right: true,
down: true, down: true,
lastX: viewAreaElement.scrollLeft, lastX: viewAreaElement.scrollLeft,
@ -198,12 +199,12 @@ function watchScroll(viewAreaElement, callback) {
* Helper function to parse query string (e.g. ?param1=value&parm2=...). * Helper function to parse query string (e.g. ?param1=value&parm2=...).
*/ */
function parseQueryString(query) { function parseQueryString(query) {
let parts = query.split('&'); const parts = query.split('&');
let params = Object.create(null); const params = Object.create(null);
for (let i = 0, ii = parts.length; i < ii; ++i) { for (let i = 0, ii = parts.length; i < ii; ++i) {
let param = parts[i].split('='); const param = parts[i].split('=');
let key = param[0].toLowerCase(); const key = param[0].toLowerCase();
let value = param.length > 1 ? param[1] : null; const value = param.length > 1 ? param[1] : null;
params[decodeURIComponent(key)] = decodeURIComponent(value); params[decodeURIComponent(key)] = decodeURIComponent(value);
} }
return params; return params;
@ -230,8 +231,8 @@ function binarySearchFirstItem(items, condition) {
} }
while (minIndex < maxIndex) { while (minIndex < maxIndex) {
let currentIndex = (minIndex + maxIndex) >> 1; const currentIndex = (minIndex + maxIndex) >> 1;
let currentItem = items[currentIndex]; const currentItem = items[currentIndex];
if (condition(currentItem)) { if (condition(currentItem)) {
maxIndex = currentIndex; maxIndex = currentIndex;
} else { } else {
@ -253,21 +254,21 @@ function approximateFraction(x) {
if (Math.floor(x) === x) { if (Math.floor(x) === x) {
return [x, 1]; return [x, 1];
} }
let xinv = 1 / x; const xinv = 1 / x;
let limit = 8; const limit = 8;
if (xinv > limit) { if (xinv > limit) {
return [1, limit]; return [1, limit];
} else if (Math.floor(xinv) === xinv) { } else if (Math.floor(xinv) === xinv) {
return [1, xinv]; return [1, xinv];
} }
let x_ = x > 1 ? xinv : x; const x_ = x > 1 ? xinv : x;
// a/b and c/d are neighbours in Farey sequence. // a/b and c/d are neighbours in Farey sequence.
let a = 0, b = 1, c = 1, d = 1; let a = 0, b = 1, c = 1, d = 1;
// Limiting search to order 8. // Limiting search to order 8.
while (true) { while (true) {
// Generating next term in sequence (order of q). // Generating next term in sequence (order of q).
let p = a + c, q = b + d; const p = a + c, q = b + d;
if (q > limit) { if (q > limit) {
break; break;
} }
@ -288,7 +289,7 @@ function approximateFraction(x) {
} }
function roundToDivide(x, div) { function roundToDivide(x, div) {
let r = x % div; const r = x % div;
return r === 0 ? x : Math.round(x - r + div); return r === 0 ? x : Math.round(x - r + div);
} }
@ -525,7 +526,7 @@ function getVisibleElements(scrollEl, views, sortByVisibility = false,
if (sortByVisibility) { if (sortByVisibility) {
visible.sort(function(a, b) { visible.sort(function(a, b) {
let pc = a.percent - b.percent; const pc = a.percent - b.percent;
if (Math.abs(pc) > 0.001) { if (Math.abs(pc) > 0.001) {
return -pc; return -pc;
} }
@ -543,7 +544,8 @@ function noContextMenuHandler(evt) {
} }
function isDataSchema(url) { function isDataSchema(url) {
let i = 0, ii = url.length; let i = 0;
const ii = url.length;
while (i < ii && url[i].trim() === '') { while (i < ii && url[i].trim() === '') {
i++; i++;
} }
@ -570,7 +572,7 @@ function getPDFFileNameFromURL(url, defaultFilename = 'document.pdf') {
// SCHEME HOST 1.PATH 2.QUERY 3.REF // SCHEME HOST 1.PATH 2.QUERY 3.REF
// Pattern to get last matching NAME.pdf // Pattern to get last matching NAME.pdf
const reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i; const reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
let splitURI = reURI.exec(url); const splitURI = reURI.exec(url);
let suggestedFilename = reFilename.exec(splitURI[1]) || let suggestedFilename = reFilename.exec(splitURI[1]) ||
reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[2]) ||
reFilename.exec(splitURI[3]); reFilename.exec(splitURI[3]);
@ -592,7 +594,7 @@ function getPDFFileNameFromURL(url, defaultFilename = 'document.pdf') {
function normalizeWheelEventDelta(evt) { function normalizeWheelEventDelta(evt) {
let delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY); let delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY);
let angle = Math.atan2(evt.deltaY, evt.deltaX); const angle = Math.atan2(evt.deltaY, evt.deltaX);
if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) { if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) {
// All that is left-up oriented has to change the sign. // All that is left-up oriented has to change the sign.
delta = -delta; delta = -delta;
@ -680,14 +682,14 @@ function waitOnEventOrTimeout({ target, name, delay = 0, }) {
} }
const timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT); const timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT);
let timeout = setTimeout(timeoutHandler, delay); const timeout = setTimeout(timeoutHandler, delay);
}); });
} }
/** /**
* Promise that is resolved when DOM window becomes visible. * Promise that is resolved when DOM window becomes visible.
*/ */
let animationStarted = new Promise(function (resolve) { const animationStarted = new Promise(function (resolve) {
if ((typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) && if ((typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) &&
typeof window === 'undefined') { typeof window === 'undefined') {
// Prevent "ReferenceError: window is not defined" errors when running the // Prevent "ReferenceError: window is not defined" errors when running the
@ -719,7 +721,7 @@ class EventBus {
} }
off(eventName, listener) { off(eventName, listener) {
let eventListeners = this._listeners[eventName]; const eventListeners = this._listeners[eventName];
let i; let i;
if (!eventListeners || ((i = eventListeners.indexOf(listener)) < 0)) { if (!eventListeners || ((i = eventListeners.indexOf(listener)) < 0)) {
return; return;
@ -728,7 +730,7 @@ class EventBus {
} }
dispatch(eventName) { dispatch(eventName) {
let eventListeners = this._listeners[eventName]; const eventListeners = this._listeners[eventName];
if (!eventListeners || eventListeners.length === 0) { if (!eventListeners || eventListeners.length === 0) {
if (this._dispatchToDOM) { if (this._dispatchToDOM) {
const args = Array.prototype.slice.call(arguments, 1); const args = Array.prototype.slice.call(arguments, 1);
@ -755,7 +757,7 @@ class EventBus {
const details = Object.create(null); const details = Object.create(null);
if (args && args.length > 0) { if (args && args.length > 0) {
const obj = args[0]; const obj = args[0];
for (let key in obj) { for (const key in obj) {
const value = obj[key]; const value = obj[key];
if (key === 'source') { if (key === 'source') {
if (value === window || value === document) { if (value === window || value === document) {
@ -811,7 +813,7 @@ class ProgressBar {
} }
this.div.classList.remove('indeterminate'); this.div.classList.remove('indeterminate');
let progressSize = this.width * this._percent / 100; const progressSize = this.width * this._percent / 100;
this.div.style.width = progressSize + this.units; this.div.style.width = progressSize + this.units;
} }
@ -829,8 +831,8 @@ class ProgressBar {
if (!viewer) { if (!viewer) {
return; return;
} }
let container = viewer.parentNode; const container = viewer.parentNode;
let scrollbarWidth = container.offsetWidth - viewer.offsetWidth; const scrollbarWidth = container.offsetWidth - viewer.offsetWidth;
if (scrollbarWidth > 0) { if (scrollbarWidth > 0) {
this.bar.setAttribute('style', 'width: calc(100% - ' + this.bar.setAttribute('style', 'width: calc(100% - ' +
scrollbarWidth + 'px);'); scrollbarWidth + 'px);');