Skip to content

Commit

Permalink
If a new overlay is added with the same name overwrite is.
Browse files Browse the repository at this point in the history
Only throw an error is the existing layer is active and can therefor not be removed.
  • Loading branch information
jasny committed Nov 19, 2015
1 parent 83bb2d5 commit 16d7240
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 55 deletions.
6 changes: 5 additions & 1 deletion pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
164 changes: 110 additions & 54 deletions pdf.js.patch
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 16d7240

Please sign in to comment.