From 16d724094d2fc5f0e792a2cec03c8b99d2274e54 Mon Sep 17 00:00:00 2001 From: Arnold Daniels Date: Thu, 19 Nov 2015 00:29:20 -0400 Subject: [PATCH] If a new overlay is added with the same name overwrite is. Only throw an error is the existing layer is active and can therefor not be removed. --- pdf.js | 6 +- pdf.js.patch | 164 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 115 insertions(+), 55 deletions(-) diff --git a/pdf.js b/pdf.js index 9905d73..c05fe3d 100644 --- a/pdf.js +++ b/pdf.js @@ -13201,7 +13201,11 @@ var OverlayManager = { !(container = element.parentNode)) { throw new Error('Not enough parameters.'); } else if (this.overlays[name]) { - throw new Error('The overlay is already registered.'); + if (this.active !== name) { + this.unregister(name); + } else { + throw new Error('The overlay is already registered and active.'); + } } this.overlays[name] = { element: element, container: container, diff --git a/pdf.js.patch b/pdf.js.patch index 507c761..45ce1d9 100644 --- a/pdf.js.patch +++ b/pdf.js.patch @@ -1,54 +1,110 @@ -829c829,831 -< console.warn('#' + key + ' is undefined.'); ---- -> if (Object.keys(gL10nData).length > 0) { -> console.warn('#' + key + ' is undefined.'); -> } -10292,10295c10294,10304 -< PDFJS.imageResourcesPath = './images/'; -< PDFJS.workerSrc = '../build/pdf.worker.js'; -< PDFJS.cMapUrl = '../web/cmaps/'; -< PDFJS.cMapPacked = true; ---- -> var scriptTagContainer = document.body || -> document.getElementsByTagName('head')[0]; -> var pdfjsSrc = scriptTagContainer.lastChild.src; -> -> if (pdfjsSrc) { -> PDFJS.imageResourcesPath = pdfjsSrc.replace(/pdf\.js$/i, 'images/'); -> PDFJS.workerSrc = pdfjsSrc.replace(/pdf\.js$/i, 'pdf.worker.js'); -> PDFJS.cMapUrl = pdfjsSrc.replace(/pdf\.js$/i, 'cmaps/'); -> } -> -> PDFJS.cMapPacked = true; -11743c11752 -< return this.pdfDocument.numPages; ---- -> return this.pdfDocument ? this.pdfDocument.numPages : 0; -16488c16497 -< return this.pdfDocument.numPages; ---- -> return this.pdfDocument ? this.pdfDocument.numPages : 0; -17024c17033,17035 -< this.pdfDocument.cleanup(); ---- -> if (this.pdfDocument) { -> this.pdfDocument.cleanup(); -> } -17465c17476,17481 -< document.addEventListener('DOMContentLoaded', webViewerLoad, true); ---- -> // document.addEventListener('DOMContentLoaded', webViewerLoad, true); -> PDFJS.webViewerLoad = function (src) { -> if (src) DEFAULT_URL = src; -> -> webViewerLoad(); -> } -17588c17604 -< if (PDFViewerApplication.pdfHistory.isHashChangeUnlocked) { ---- -> if (PDFViewerApplication.pdfHistory && PDFViewerApplication.pdfHistory.isHashChangeUnlocked) { -17740a17757,17759 -> // Ignore mousewheel event if pdfViewer isn't loaded -> if (!PDFViewerApplication.pdfViewer) return; -> +--- pdf.orig.js 2015-11-19 00:10:27.980871274 -0400 ++++ pdf.js 2015-11-19 00:27:00.668845816 -0400 +@@ -826,7 +826,9 @@ + function getL10nData(key, args, fallback) { + var data = gL10nData[key]; + if (!data) { +- console.warn('#' + key + ' is undefined.'); ++ if (Object.keys(gL10nData).length > 0) { ++ console.warn('#' + key + ' is undefined.'); ++ } + if (!fallback) { + return null; + } +@@ -10289,10 +10291,17 @@ + var PAGE_NUMBER_LOADING_INDICATOR = 'visiblePageIsLoading'; + var DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000; + +-PDFJS.imageResourcesPath = './images/'; +- PDFJS.workerSrc = '../build/pdf.worker.js'; +- PDFJS.cMapUrl = '../web/cmaps/'; +- PDFJS.cMapPacked = true; ++var scriptTagContainer = document.body || ++ document.getElementsByTagName('head')[0]; ++var pdfjsSrc = scriptTagContainer.lastChild.src; ++ ++if (pdfjsSrc) { ++ PDFJS.imageResourcesPath = pdfjsSrc.replace(/pdf\.js$/i, 'images/'); ++ PDFJS.workerSrc = pdfjsSrc.replace(/pdf\.js$/i, 'pdf.worker.js'); ++ PDFJS.cMapUrl = pdfjsSrc.replace(/pdf\.js$/i, 'cmaps/'); ++} ++ ++PDFJS.cMapPacked = true; + + var mozL10n = document.mozL10n || document.webL10n; + +@@ -11740,7 +11749,7 @@ + * @returns {number} + */ + get pagesCount() { +- return this.pdfDocument.numPages; ++ return this.pdfDocument ? this.pdfDocument.numPages : 0; + }, + + /** +@@ -13192,7 +13201,11 @@ + !(container = element.parentNode)) { + throw new Error('Not enough parameters.'); + } else if (this.overlays[name]) { +- throw new Error('The overlay is already registered.'); ++ if (this.active !== name) { ++ this.unregister(name); ++ } else { ++ throw new Error('The overlay is already registered and active.'); ++ } + } + this.overlays[name] = { element: element, + container: container, +@@ -16485,7 +16498,7 @@ + }, + + get pagesCount() { +- return this.pdfDocument.numPages; ++ return this.pdfDocument ? this.pdfDocument.numPages : 0; + }, + + set page(val) { +@@ -17021,7 +17034,9 @@ + cleanup: function pdfViewCleanup() { + this.pdfViewer.cleanup(); + this.pdfThumbnailViewer.cleanup(); +- this.pdfDocument.cleanup(); ++ if (this.pdfDocument) { ++ this.pdfDocument.cleanup(); ++ } + }, + + forceRendering: function pdfViewForceRendering() { +@@ -17462,7 +17477,12 @@ + } + } + +-document.addEventListener('DOMContentLoaded', webViewerLoad, true); ++// document.addEventListener('DOMContentLoaded', webViewerLoad, true); ++PDFJS.webViewerLoad = function (src) { ++ if (src) DEFAULT_URL = src; ++ ++ webViewerLoad(); ++} + + document.addEventListener('pagerendered', function (e) { + var pageNumber = e.detail.pageNumber; +@@ -17585,7 +17605,7 @@ + }); + + window.addEventListener('hashchange', function webViewerHashchange(evt) { +- if (PDFViewerApplication.pdfHistory.isHashChangeUnlocked) { ++ if (PDFViewerApplication.pdfHistory && PDFViewerApplication.pdfHistory.isHashChangeUnlocked) { + var hash = document.location.hash.substring(1); + if (!hash) { + return; +@@ -17738,6 +17758,9 @@ + }, true); + + function handleMouseWheel(evt) { ++ // Ignore mousewheel event if pdfViewer isn't loaded ++ if (!PDFViewerApplication.pdfViewer) return; ++ + var MOUSE_WHEEL_DELTA_FACTOR = 40; + var ticks = (evt.type === 'DOMMouseScroll') ? -evt.detail : + evt.wheelDelta / MOUSE_WHEEL_DELTA_FACTOR;