Merge pull request #11631 from Snuffleupagus/getGlobalEventBus-deprecate

[api-minor] Deprecate `getGlobalEventBus` and update the "viewer components" examples accordingly
This commit is contained in:
Tim van der Meij 2020-03-02 23:30:07 +01:00 committed by GitHub
commit 52749d1f0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 236 additions and 158 deletions

View File

@ -23,6 +23,8 @@ var DEFAULT_SCALE = 1.0;
var container = document.getElementById("pageContainer"); var container = document.getElementById("pageContainer");
var eventBus = new pdfjsViewer.EventBus();
// Fetch the PDF document from the URL using promises. // Fetch the PDF document from the URL using promises.
var loadingTask = pdfjsLib.getDocument(DEFAULT_URL); var loadingTask = pdfjsLib.getDocument(DEFAULT_URL);
loadingTask.promise.then(function(doc) { loadingTask.promise.then(function(doc) {
@ -39,6 +41,7 @@ loadingTask.promise.then(function(doc) {
id: pageNum, id: pageNum,
scale: DEFAULT_SCALE, scale: DEFAULT_SCALE,
defaultViewport: pdfPage.getViewport({ scale: DEFAULT_SCALE }), defaultViewport: pdfPage.getViewport({ scale: DEFAULT_SCALE }),
eventBus: eventBus,
annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(), annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(),
renderInteractiveForms: true, renderInteractiveForms: true,
}); });

View File

@ -35,6 +35,8 @@ var SCALE = 1.0;
var container = document.getElementById("pageContainer"); var container = document.getElementById("pageContainer");
var eventBus = new pdfjsViewer.EventBus();
// Loading document. // Loading document.
var loadingTask = pdfjsLib.getDocument({ var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL, url: DEFAULT_URL,
@ -50,6 +52,7 @@ loadingTask.promise.then(function(pdfDocument) {
id: PAGE_TO_VIEW, id: PAGE_TO_VIEW,
scale: SCALE, scale: SCALE,
defaultViewport: pdfPage.getViewport({ scale: SCALE }), defaultViewport: pdfPage.getViewport({ scale: SCALE }),
eventBus: eventBus,
// We can enable text/annotations layers, if needed // We can enable text/annotations layers, if needed
textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory(), textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory(),
annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(), annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(),

View File

@ -34,22 +34,28 @@ var SEARCH_FOR = ""; // try 'Mozilla';
var container = document.getElementById("viewerContainer"); var container = document.getElementById("viewerContainer");
var eventBus = new pdfjsViewer.EventBus();
// (Optionally) enable hyperlinks within PDF files. // (Optionally) enable hyperlinks within PDF files.
var pdfLinkService = new pdfjsViewer.PDFLinkService(); var pdfLinkService = new pdfjsViewer.PDFLinkService({
eventBus: eventBus,
});
// (Optionally) enable find controller. // (Optionally) enable find controller.
var pdfFindController = new pdfjsViewer.PDFFindController({ var pdfFindController = new pdfjsViewer.PDFFindController({
eventBus: eventBus,
linkService: pdfLinkService, linkService: pdfLinkService,
}); });
var pdfViewer = new pdfjsViewer.PDFViewer({ var pdfViewer = new pdfjsViewer.PDFViewer({
container: container, container: container,
eventBus: eventBus,
linkService: pdfLinkService, linkService: pdfLinkService,
findController: pdfFindController, findController: pdfFindController,
}); });
pdfLinkService.setViewer(pdfViewer); pdfLinkService.setViewer(pdfViewer);
document.addEventListener("pagesinit", function() { eventBus.on("pagesinit", function() {
// We can use pdfViewer now, e.g. let's change default scale. // We can use pdfViewer now, e.g. let's change default scale.
pdfViewer.currentScaleValue = "page-width"; pdfViewer.currentScaleValue = "page-width";

View File

@ -34,22 +34,28 @@ var SEARCH_FOR = ""; // try 'Mozilla';
var container = document.getElementById("viewerContainer"); var container = document.getElementById("viewerContainer");
var eventBus = new pdfjsViewer.EventBus();
// (Optionally) enable hyperlinks within PDF files. // (Optionally) enable hyperlinks within PDF files.
var pdfLinkService = new pdfjsViewer.PDFLinkService(); var pdfLinkService = new pdfjsViewer.PDFLinkService({
eventBus: eventBus,
});
// (Optionally) enable find controller. // (Optionally) enable find controller.
var pdfFindController = new pdfjsViewer.PDFFindController({ var pdfFindController = new pdfjsViewer.PDFFindController({
eventBus: eventBus,
linkService: pdfLinkService, linkService: pdfLinkService,
}); });
var pdfSinglePageViewer = new pdfjsViewer.PDFSinglePageViewer({ var pdfSinglePageViewer = new pdfjsViewer.PDFSinglePageViewer({
container: container, container: container,
eventBus: eventBus,
linkService: pdfLinkService, linkService: pdfLinkService,
findController: pdfFindController, findController: pdfFindController,
}); });
pdfLinkService.setViewer(pdfSinglePageViewer); pdfLinkService.setViewer(pdfSinglePageViewer);
document.addEventListener("pagesinit", function() { eventBus.on("pagesinit", function() {
// We can use pdfSinglePageViewer now, e.g. let's change default scale. // We can use pdfSinglePageViewer now, e.g. let's change default scale.
pdfSinglePageViewer.currentScaleValue = "page-width"; pdfSinglePageViewer.currentScaleValue = "page-width";

View File

@ -40,6 +40,7 @@ var PDFViewerApplication = {
pdfViewer: null, pdfViewer: null,
pdfHistory: null, pdfHistory: null,
pdfLinkService: null, pdfLinkService: null,
eventBus: null,
/** /**
* Opens PDF document specified by URL. * Opens PDF document specified by URL.
@ -340,7 +341,12 @@ var PDFViewerApplication = {
}, },
initUI: function pdfViewInitUI() { initUI: function pdfViewInitUI() {
var linkService = new pdfjsViewer.PDFLinkService(); var eventBus = new pdfjsViewer.EventBus();
this.eventBus = eventBus;
var linkService = new pdfjsViewer.PDFLinkService({
eventBus: eventBus,
});
this.pdfLinkService = linkService; this.pdfLinkService = linkService;
this.l10n = pdfjsViewer.NullL10n; this.l10n = pdfjsViewer.NullL10n;
@ -348,6 +354,7 @@ var PDFViewerApplication = {
var container = document.getElementById("viewerContainer"); var container = document.getElementById("viewerContainer");
var pdfViewer = new pdfjsViewer.PDFViewer({ var pdfViewer = new pdfjsViewer.PDFViewer({
container: container, container: container,
eventBus: eventBus,
linkService: linkService, linkService: linkService,
l10n: this.l10n, l10n: this.l10n,
useOnlyCssZoom: USE_ONLY_CSS_ZOOM, useOnlyCssZoom: USE_ONLY_CSS_ZOOM,
@ -357,6 +364,7 @@ var PDFViewerApplication = {
linkService.setViewer(pdfViewer); linkService.setViewer(pdfViewer);
this.pdfHistory = new pdfjsViewer.PDFHistory({ this.pdfHistory = new pdfjsViewer.PDFHistory({
eventBus: eventBus,
linkService: linkService, linkService: linkService,
}); });
linkService.setHistory(this.pdfHistory); linkService.setHistory(this.pdfHistory);
@ -394,15 +402,15 @@ var PDFViewerApplication = {
} }
}); });
document.addEventListener("pagesinit", function() { eventBus.on("pagesinit", function() {
// We can use pdfViewer now, e.g. let's change default scale. // We can use pdfViewer now, e.g. let's change default scale.
pdfViewer.currentScaleValue = DEFAULT_SCALE_VALUE; pdfViewer.currentScaleValue = DEFAULT_SCALE_VALUE;
}); });
document.addEventListener( eventBus.on(
"pagechanging", "pagechanging",
function(evt) { function(evt) {
var page = evt.detail.pageNumber; var page = evt.pageNumber;
var numPages = PDFViewerApplication.pagesCount; var numPages = PDFViewerApplication.pagesCount;
document.getElementById("pageNumber").value = page; document.getElementById("pageNumber").value = page;

View File

@ -33,18 +33,23 @@ var DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
var container = document.getElementById("viewerContainer"); var container = document.getElementById("viewerContainer");
var eventBus = new pdfjsViewer.EventBus();
// (Optionally) enable hyperlinks within PDF files. // (Optionally) enable hyperlinks within PDF files.
var pdfLinkService = new pdfjsViewer.PDFLinkService(); var pdfLinkService = new pdfjsViewer.PDFLinkService({
eventBus: eventBus,
});
var pdfViewer = new pdfjsViewer.PDFViewer({ var pdfViewer = new pdfjsViewer.PDFViewer({
container: container, container: container,
eventBus: eventBus,
linkService: pdfLinkService, linkService: pdfLinkService,
renderer: "svg", renderer: "svg",
textLayerMode: 0, textLayerMode: 0,
}); });
pdfLinkService.setViewer(pdfViewer); pdfLinkService.setViewer(pdfViewer);
document.addEventListener("pagesinit", function() { eventBus.on("pagesinit", function() {
// We can use pdfViewer now, e.g. let's change default scale. // We can use pdfViewer now, e.g. let's change default scale.
pdfViewer.currentScaleValue = "page-width"; pdfViewer.currentScaleValue = "page-width";
}); });

View File

@ -18,7 +18,7 @@ import {
animationStarted, animationStarted,
AutoPrintRegExp, AutoPrintRegExp,
DEFAULT_SCALE_VALUE, DEFAULT_SCALE_VALUE,
getGlobalEventBus, EventBus,
getPDFFileNameFromURL, getPDFFileNameFromURL,
isValidRotation, isValidRotation,
isValidScrollMode, isValidScrollMode,
@ -343,7 +343,7 @@ const PDFViewerApplication = {
const eventBus = const eventBus =
appConfig.eventBus || appConfig.eventBus ||
getGlobalEventBus(AppOptions.get("eventBusDispatchToDOM")); new EventBus({ dispatchToDOM: AppOptions.get("eventBusDispatchToDOM") });
this.eventBus = eventBus; this.eventBus = eventBus;
const pdfRenderingQueue = new PDFRenderingQueue(); const pdfRenderingQueue = new PDFRenderingQueue();
@ -1598,45 +1598,45 @@ const PDFViewerApplication = {
_boundEvents.beforePrint = this.beforePrint.bind(this); _boundEvents.beforePrint = this.beforePrint.bind(this);
_boundEvents.afterPrint = this.afterPrint.bind(this); _boundEvents.afterPrint = this.afterPrint.bind(this);
eventBus.on("resize", webViewerResize); eventBus._on("resize", webViewerResize);
eventBus.on("hashchange", webViewerHashchange); eventBus._on("hashchange", webViewerHashchange);
eventBus.on("beforeprint", _boundEvents.beforePrint); eventBus._on("beforeprint", _boundEvents.beforePrint);
eventBus.on("afterprint", _boundEvents.afterPrint); eventBus._on("afterprint", _boundEvents.afterPrint);
eventBus.on("pagerendered", webViewerPageRendered); eventBus._on("pagerendered", webViewerPageRendered);
eventBus.on("updateviewarea", webViewerUpdateViewarea); eventBus._on("updateviewarea", webViewerUpdateViewarea);
eventBus.on("pagechanging", webViewerPageChanging); eventBus._on("pagechanging", webViewerPageChanging);
eventBus.on("scalechanging", webViewerScaleChanging); eventBus._on("scalechanging", webViewerScaleChanging);
eventBus.on("rotationchanging", webViewerRotationChanging); eventBus._on("rotationchanging", webViewerRotationChanging);
eventBus.on("sidebarviewchanged", webViewerSidebarViewChanged); eventBus._on("sidebarviewchanged", webViewerSidebarViewChanged);
eventBus.on("pagemode", webViewerPageMode); eventBus._on("pagemode", webViewerPageMode);
eventBus.on("namedaction", webViewerNamedAction); eventBus._on("namedaction", webViewerNamedAction);
eventBus.on("presentationmodechanged", webViewerPresentationModeChanged); eventBus._on("presentationmodechanged", webViewerPresentationModeChanged);
eventBus.on("presentationmode", webViewerPresentationMode); eventBus._on("presentationmode", webViewerPresentationMode);
eventBus.on("openfile", webViewerOpenFile); eventBus._on("openfile", webViewerOpenFile);
eventBus.on("print", webViewerPrint); eventBus._on("print", webViewerPrint);
eventBus.on("download", webViewerDownload); eventBus._on("download", webViewerDownload);
eventBus.on("firstpage", webViewerFirstPage); eventBus._on("firstpage", webViewerFirstPage);
eventBus.on("lastpage", webViewerLastPage); eventBus._on("lastpage", webViewerLastPage);
eventBus.on("nextpage", webViewerNextPage); eventBus._on("nextpage", webViewerNextPage);
eventBus.on("previouspage", webViewerPreviousPage); eventBus._on("previouspage", webViewerPreviousPage);
eventBus.on("zoomin", webViewerZoomIn); eventBus._on("zoomin", webViewerZoomIn);
eventBus.on("zoomout", webViewerZoomOut); eventBus._on("zoomout", webViewerZoomOut);
eventBus.on("zoomreset", webViewerZoomReset); eventBus._on("zoomreset", webViewerZoomReset);
eventBus.on("pagenumberchanged", webViewerPageNumberChanged); eventBus._on("pagenumberchanged", webViewerPageNumberChanged);
eventBus.on("scalechanged", webViewerScaleChanged); eventBus._on("scalechanged", webViewerScaleChanged);
eventBus.on("rotatecw", webViewerRotateCw); eventBus._on("rotatecw", webViewerRotateCw);
eventBus.on("rotateccw", webViewerRotateCcw); eventBus._on("rotateccw", webViewerRotateCcw);
eventBus.on("switchscrollmode", webViewerSwitchScrollMode); eventBus._on("switchscrollmode", webViewerSwitchScrollMode);
eventBus.on("scrollmodechanged", webViewerScrollModeChanged); eventBus._on("scrollmodechanged", webViewerScrollModeChanged);
eventBus.on("switchspreadmode", webViewerSwitchSpreadMode); eventBus._on("switchspreadmode", webViewerSwitchSpreadMode);
eventBus.on("spreadmodechanged", webViewerSpreadModeChanged); eventBus._on("spreadmodechanged", webViewerSpreadModeChanged);
eventBus.on("documentproperties", webViewerDocumentProperties); eventBus._on("documentproperties", webViewerDocumentProperties);
eventBus.on("find", webViewerFind); eventBus._on("find", webViewerFind);
eventBus.on("findfromurlhash", webViewerFindFromUrlHash); eventBus._on("findfromurlhash", webViewerFindFromUrlHash);
eventBus.on("updatefindmatchescount", webViewerUpdateFindMatchesCount); eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount);
eventBus.on("updatefindcontrolstate", webViewerUpdateFindControlState); eventBus._on("updatefindcontrolstate", webViewerUpdateFindControlState);
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
eventBus.on("fileinputchange", webViewerFileInputChange); eventBus._on("fileinputchange", webViewerFileInputChange);
} }
}, },
@ -1672,45 +1672,45 @@ const PDFViewerApplication = {
unbindEvents() { unbindEvents() {
const { eventBus, _boundEvents } = this; const { eventBus, _boundEvents } = this;
eventBus.off("resize", webViewerResize); eventBus._off("resize", webViewerResize);
eventBus.off("hashchange", webViewerHashchange); eventBus._off("hashchange", webViewerHashchange);
eventBus.off("beforeprint", _boundEvents.beforePrint); eventBus._off("beforeprint", _boundEvents.beforePrint);
eventBus.off("afterprint", _boundEvents.afterPrint); eventBus._off("afterprint", _boundEvents.afterPrint);
eventBus.off("pagerendered", webViewerPageRendered); eventBus._off("pagerendered", webViewerPageRendered);
eventBus.off("updateviewarea", webViewerUpdateViewarea); eventBus._off("updateviewarea", webViewerUpdateViewarea);
eventBus.off("pagechanging", webViewerPageChanging); eventBus._off("pagechanging", webViewerPageChanging);
eventBus.off("scalechanging", webViewerScaleChanging); eventBus._off("scalechanging", webViewerScaleChanging);
eventBus.off("rotationchanging", webViewerRotationChanging); eventBus._off("rotationchanging", webViewerRotationChanging);
eventBus.off("sidebarviewchanged", webViewerSidebarViewChanged); eventBus._off("sidebarviewchanged", webViewerSidebarViewChanged);
eventBus.off("pagemode", webViewerPageMode); eventBus._off("pagemode", webViewerPageMode);
eventBus.off("namedaction", webViewerNamedAction); eventBus._off("namedaction", webViewerNamedAction);
eventBus.off("presentationmodechanged", webViewerPresentationModeChanged); eventBus._off("presentationmodechanged", webViewerPresentationModeChanged);
eventBus.off("presentationmode", webViewerPresentationMode); eventBus._off("presentationmode", webViewerPresentationMode);
eventBus.off("openfile", webViewerOpenFile); eventBus._off("openfile", webViewerOpenFile);
eventBus.off("print", webViewerPrint); eventBus._off("print", webViewerPrint);
eventBus.off("download", webViewerDownload); eventBus._off("download", webViewerDownload);
eventBus.off("firstpage", webViewerFirstPage); eventBus._off("firstpage", webViewerFirstPage);
eventBus.off("lastpage", webViewerLastPage); eventBus._off("lastpage", webViewerLastPage);
eventBus.off("nextpage", webViewerNextPage); eventBus._off("nextpage", webViewerNextPage);
eventBus.off("previouspage", webViewerPreviousPage); eventBus._off("previouspage", webViewerPreviousPage);
eventBus.off("zoomin", webViewerZoomIn); eventBus._off("zoomin", webViewerZoomIn);
eventBus.off("zoomout", webViewerZoomOut); eventBus._off("zoomout", webViewerZoomOut);
eventBus.off("zoomreset", webViewerZoomReset); eventBus._off("zoomreset", webViewerZoomReset);
eventBus.off("pagenumberchanged", webViewerPageNumberChanged); eventBus._off("pagenumberchanged", webViewerPageNumberChanged);
eventBus.off("scalechanged", webViewerScaleChanged); eventBus._off("scalechanged", webViewerScaleChanged);
eventBus.off("rotatecw", webViewerRotateCw); eventBus._off("rotatecw", webViewerRotateCw);
eventBus.off("rotateccw", webViewerRotateCcw); eventBus._off("rotateccw", webViewerRotateCcw);
eventBus.off("switchscrollmode", webViewerSwitchScrollMode); eventBus._off("switchscrollmode", webViewerSwitchScrollMode);
eventBus.off("scrollmodechanged", webViewerScrollModeChanged); eventBus._off("scrollmodechanged", webViewerScrollModeChanged);
eventBus.off("switchspreadmode", webViewerSwitchSpreadMode); eventBus._off("switchspreadmode", webViewerSwitchSpreadMode);
eventBus.off("spreadmodechanged", webViewerSpreadModeChanged); eventBus._off("spreadmodechanged", webViewerSpreadModeChanged);
eventBus.off("documentproperties", webViewerDocumentProperties); eventBus._off("documentproperties", webViewerDocumentProperties);
eventBus.off("find", webViewerFind); eventBus._off("find", webViewerFind);
eventBus.off("findfromurlhash", webViewerFindFromUrlHash); eventBus._off("findfromurlhash", webViewerFindFromUrlHash);
eventBus.off("updatefindmatchescount", webViewerUpdateFindMatchesCount); eventBus._off("updatefindmatchescount", webViewerUpdateFindMatchesCount);
eventBus.off("updatefindcontrolstate", webViewerUpdateFindControlState); eventBus._off("updatefindcontrolstate", webViewerUpdateFindControlState);
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
eventBus.off("fileinputchange", webViewerFileInputChange); eventBus._off("fileinputchange", webViewerFileInputChange);
} }
_boundEvents.beforePrint = null; _boundEvents.beforePrint = null;

View File

@ -426,7 +426,7 @@ class BaseViewer {
// evicted from the buffer and destroyed even if we pause its rendering. // evicted from the buffer and destroyed even if we pause its rendering.
this._buffer.push(pageView); this._buffer.push(pageView);
}; };
this.eventBus.on("pagerender", this._onBeforeDraw); this.eventBus._on("pagerender", this._onBeforeDraw);
this._onAfterDraw = evt => { this._onAfterDraw = evt => {
if (evt.cssTransform || onePageRenderedCapability.settled) { if (evt.cssTransform || onePageRenderedCapability.settled) {
@ -434,10 +434,10 @@ class BaseViewer {
} }
onePageRenderedCapability.resolve(); onePageRenderedCapability.resolve();
this.eventBus.off("pagerendered", this._onAfterDraw); this.eventBus._off("pagerendered", this._onAfterDraw);
this._onAfterDraw = null; this._onAfterDraw = null;
}; };
this.eventBus.on("pagerendered", this._onAfterDraw); this.eventBus._on("pagerendered", this._onAfterDraw);
// Fetch a single page so we can get a viewport that will be the default // Fetch a single page so we can get a viewport that will be the default
// viewport for all pages // viewport for all pages
@ -582,11 +582,11 @@ class BaseViewer {
this._spreadMode = SpreadMode.NONE; this._spreadMode = SpreadMode.NONE;
if (this._onBeforeDraw) { if (this._onBeforeDraw) {
this.eventBus.off("pagerender", this._onBeforeDraw); this.eventBus._off("pagerender", this._onBeforeDraw);
this._onBeforeDraw = null; this._onBeforeDraw = null;
} }
if (this._onAfterDraw) { if (this._onAfterDraw) {
this.eventBus.off("pagerendered", this._onAfterDraw); this.eventBus._off("pagerendered", this._onAfterDraw);
this._onAfterDraw = null; this._onAfterDraw = null;
} }
// Remove the pages from the DOM... // Remove the pages from the DOM...
@ -1093,17 +1093,20 @@ class BaseViewer {
* @param {HTMLDivElement} textLayerDiv * @param {HTMLDivElement} textLayerDiv
* @param {number} pageIndex * @param {number} pageIndex
* @param {PageViewport} viewport * @param {PageViewport} viewport
* @param {boolean} enhanceTextSelection
* @param {EventBus} eventBus
* @returns {TextLayerBuilder} * @returns {TextLayerBuilder}
*/ */
createTextLayerBuilder( createTextLayerBuilder(
textLayerDiv, textLayerDiv,
pageIndex, pageIndex,
viewport, viewport,
enhanceTextSelection = false enhanceTextSelection = false,
eventBus
) { ) {
return new TextLayerBuilder({ return new TextLayerBuilder({
textLayerDiv, textLayerDiv,
eventBus: this.eventBus, eventBus,
pageIndex, pageIndex,
viewport, viewport,
findController: this.isInPresentationMode ? null : this.findController, findController: this.isInPresentationMode ? null : this.findController,

View File

@ -146,13 +146,15 @@ class IPDFTextLayerFactory {
* @param {number} pageIndex * @param {number} pageIndex
* @param {PageViewport} viewport * @param {PageViewport} viewport
* @param {boolean} enhanceTextSelection * @param {boolean} enhanceTextSelection
* @param {EventBus} eventBus
* @returns {TextLayerBuilder} * @returns {TextLayerBuilder}
*/ */
createTextLayerBuilder( createTextLayerBuilder(
textLayerDiv, textLayerDiv,
pageIndex, pageIndex,
viewport, viewport,
enhanceTextSelection = false enhanceTextSelection = false,
eventBus
) {} ) {}
} }

View File

@ -43,7 +43,7 @@ class PDFAttachmentViewer {
this.reset(); this.reset();
this.eventBus.on( this.eventBus._on(
"fileattachmentannotation", "fileattachmentannotation",
this._appendAttachment.bind(this) this._appendAttachment.bind(this)
); );

View File

@ -122,11 +122,11 @@ class PDFCursorTools {
* @private * @private
*/ */
_addEventListeners() { _addEventListeners() {
this.eventBus.on("switchcursortool", evt => { this.eventBus._on("switchcursortool", evt => {
this.switchTool(evt.tool); this.switchTool(evt.tool);
}); });
this.eventBus.on("presentationmodechanged", evt => { this.eventBus._on("presentationmodechanged", evt => {
if (evt.switchInProgress) { if (evt.switchInProgress) {
return; return;
} }

View File

@ -84,10 +84,10 @@ class PDFDocumentProperties {
); );
if (eventBus) { if (eventBus) {
eventBus.on("pagechanging", evt => { eventBus._on("pagechanging", evt => {
this._currentPageNumber = evt.pageNumber; this._currentPageNumber = evt.pageNumber;
}); });
eventBus.on("rotationchanging", evt => { eventBus._on("rotationchanging", evt => {
this._pagesRotation = evt.pagesRotation; this._pagesRotation = evt.pagesRotation;
}); });
} }

View File

@ -25,7 +25,7 @@ const MATCHES_COUNT_LIMIT = 1000;
* is done by PDFFindController. * is done by PDFFindController.
*/ */
class PDFFindBar { class PDFFindBar {
constructor(options, eventBus = getGlobalEventBus(), l10n = NullL10n) { constructor(options, eventBus, l10n = NullL10n) {
this.opened = false; this.opened = false;
this.bar = options.bar || null; this.bar = options.bar || null;
@ -38,7 +38,7 @@ class PDFFindBar {
this.findResultsCount = options.findResultsCount || null; this.findResultsCount = options.findResultsCount || null;
this.findPreviousButton = options.findPreviousButton || null; this.findPreviousButton = options.findPreviousButton || null;
this.findNextButton = options.findNextButton || null; this.findNextButton = options.findNextButton || null;
this.eventBus = eventBus; this.eventBus = eventBus || getGlobalEventBus();
this.l10n = l10n; this.l10n = l10n;
// Add event listeners to the DOM elements. // Add event listeners to the DOM elements.
@ -83,7 +83,7 @@ class PDFFindBar {
this.dispatchEvent("entirewordchange"); this.dispatchEvent("entirewordchange");
}); });
this.eventBus.on("resize", this._adjustWidth.bind(this)); this.eventBus._on("resize", this._adjustWidth.bind(this));
} }
reset() { reset() {

View File

@ -67,12 +67,12 @@ class PDFFindController {
/** /**
* @param {PDFFindControllerOptions} options * @param {PDFFindControllerOptions} options
*/ */
constructor({ linkService, eventBus = getGlobalEventBus() }) { constructor({ linkService, eventBus }) {
this._linkService = linkService; this._linkService = linkService;
this._eventBus = eventBus; this._eventBus = eventBus || getGlobalEventBus();
this._reset(); this._reset();
eventBus.on("findbarclose", this._onFindBarClose.bind(this)); eventBus._on("findbarclose", this._onFindBarClose.bind(this));
} }
get highlightMatches() { get highlightMatches() {

View File

@ -69,17 +69,17 @@ class PDFHistory {
this._isViewerInPresentationMode = false; this._isViewerInPresentationMode = false;
// Ensure that we don't miss either a 'presentationmodechanged' or a // Ensure that we don't miss either a 'presentationmodechanged' or a
// 'pagesinit' event, by registering the listeners immediately. // 'pagesinit' event, by registering the listeners immediately.
this.eventBus.on("presentationmodechanged", evt => { this.eventBus._on("presentationmodechanged", evt => {
this._isViewerInPresentationMode = evt.active || evt.switchInProgress; this._isViewerInPresentationMode = evt.active || evt.switchInProgress;
}); });
this.eventBus.on("pagesinit", () => { this.eventBus._on("pagesinit", () => {
this._isPagesLoaded = false; this._isPagesLoaded = false;
const onPagesLoaded = evt => { const onPagesLoaded = evt => {
this.eventBus.off("pagesloaded", onPagesLoaded); this.eventBus._off("pagesloaded", onPagesLoaded);
this._isPagesLoaded = !!evt.pagesCount; this._isPagesLoaded = !!evt.pagesCount;
}; };
this.eventBus.on("pagesloaded", onPagesLoaded); this.eventBus._on("pagesloaded", onPagesLoaded);
}); });
} }
@ -684,7 +684,7 @@ class PDFHistory {
pageHide: this._pageHide.bind(this), pageHide: this._pageHide.bind(this),
}; };
this.eventBus.on("updateviewarea", this._boundEvents.updateViewarea); this.eventBus._on("updateviewarea", this._boundEvents.updateViewarea);
window.addEventListener("popstate", this._boundEvents.popState); window.addEventListener("popstate", this._boundEvents.popState);
window.addEventListener("pagehide", this._boundEvents.pageHide); window.addEventListener("pagehide", this._boundEvents.pageHide);
} }
@ -696,7 +696,7 @@ class PDFHistory {
if (!this._boundEvents) { if (!this._boundEvents) {
return; // The event listeners were already removed. return; // The event listeners were already removed.
} }
this.eventBus.off("updateviewarea", this._boundEvents.updateViewarea); this.eventBus._off("updateviewarea", this._boundEvents.updateViewarea);
window.removeEventListener("popstate", this._boundEvents.popState); window.removeEventListener("popstate", this._boundEvents.popState);
window.removeEventListener("pagehide", this._boundEvents.pageHide); window.removeEventListener("pagehide", this._boundEvents.pageHide);

View File

@ -40,7 +40,7 @@ class PDFOutlineViewer {
this.reset(); this.reset();
eventBus.on("toggleoutlinetree", this.toggleOutlineTree.bind(this)); eventBus._on("toggleoutlinetree", this.toggleOutlineTree.bind(this));
} }
reset() { reset() {

View File

@ -448,7 +448,8 @@ class PDFPageView {
textLayerDiv, textLayerDiv,
this.id - 1, this.id - 1,
this.viewport, this.viewport,
this.textLayerMode === TextLayerMode.ENABLE_ENHANCE this.textLayerMode === TextLayerMode.ENABLE_ENHANCE,
this.eventBus
); );
} }
this.textLayer = textLayer; this.textLayer = textLayer;

View File

@ -430,7 +430,7 @@ class PDFSidebar {
}); });
// Disable/enable views. // Disable/enable views.
this.eventBus.on("outlineloaded", evt => { this.eventBus._on("outlineloaded", evt => {
const outlineCount = evt.outlineCount; const outlineCount = evt.outlineCount;
this.outlineButton.disabled = !outlineCount; this.outlineButton.disabled = !outlineCount;
@ -444,7 +444,7 @@ class PDFSidebar {
} }
}); });
this.eventBus.on("attachmentsloaded", evt => { this.eventBus._on("attachmentsloaded", evt => {
if (evt.attachmentsCount) { if (evt.attachmentsCount) {
this.attachmentsButton.disabled = false; this.attachmentsButton.disabled = false;
@ -473,7 +473,7 @@ class PDFSidebar {
}); });
// Update the thumbnailViewer, if visible, when exiting presentation mode. // Update the thumbnailViewer, if visible, when exiting presentation mode.
this.eventBus.on("presentationmodechanged", evt => { this.eventBus._on("presentationmodechanged", evt => {
if (!evt.active && !evt.switchInProgress && this.isThumbnailViewVisible) { if (!evt.active && !evt.switchInProgress && this.isThumbnailViewVisible) {
this._updateThumbnailViewer(); this._updateThumbnailViewer();
} }

View File

@ -151,11 +151,11 @@ class PDFSidebarResizer {
window.addEventListener("mouseup", _boundEvents.mouseUp); window.addEventListener("mouseup", _boundEvents.mouseUp);
}); });
this.eventBus.on("sidebarviewchanged", evt => { this.eventBus._on("sidebarviewchanged", evt => {
this.sidebarOpen = !!(evt && evt.view); this.sidebarOpen = !!(evt && evt.view);
}); });
this.eventBus.on("resize", evt => { this.eventBus._on("resize", evt => {
// When the *entire* viewer is resized, such that it becomes narrower, // When the *entire* viewer is resized, such that it becomes narrower,
// ensure that the sidebar doesn't end up being too wide. // ensure that the sidebar doesn't end up being too wide.
if (!evt || evt.source !== window) { if (!evt || evt.source !== window) {

View File

@ -20,7 +20,7 @@ class PDFSinglePageViewer extends BaseViewer {
constructor(options) { constructor(options) {
super(options); super(options);
this.eventBus.on("pagesinit", evt => { this.eventBus._on("pagesinit", evt => {
// Since the pages are placed in a `DocumentFragment`, make sure that // Since the pages are placed in a `DocumentFragment`, make sure that
// the current page becomes visible upon loading of the document. // the current page becomes visible upon loading of the document.
this._ensurePageViewVisible(); this._ensurePageViewVisible();

View File

@ -21,12 +21,7 @@ import {
DefaultTextLayerFactory, DefaultTextLayerFactory,
TextLayerBuilder, TextLayerBuilder,
} from "./text_layer_builder.js"; } from "./text_layer_builder.js";
import { import { EventBus, NullL10n, ProgressBar } from "./ui_utils.js";
EventBus,
getGlobalEventBus,
NullL10n,
ProgressBar,
} from "./ui_utils.js";
import { PDFLinkService, SimpleLinkService } from "./pdf_link_service.js"; import { PDFLinkService, SimpleLinkService } from "./pdf_link_service.js";
import { DownloadManager } from "./download_manager.js"; import { DownloadManager } from "./download_manager.js";
import { GenericL10n } from "./genericl10n.js"; import { GenericL10n } from "./genericl10n.js";
@ -41,9 +36,6 @@ const pdfjsVersion = PDFJSDev.eval("BUNDLE_VERSION");
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD"); const pdfjsBuild = PDFJSDev.eval("BUNDLE_BUILD");
// For backwards compatibility, ensure that events are re-dispatched to the DOM.
getGlobalEventBus(/* dispatchToDOM = */ true);
export { export {
PDFViewer, PDFViewer,
PDFSinglePageViewer, PDFSinglePageViewer,

View File

@ -159,11 +159,11 @@ class SecondaryToolbar {
this._bindSpreadModeListener(options); this._bindSpreadModeListener(options);
// Bind the event listener for adjusting the 'max-height' of the toolbar. // Bind the event listener for adjusting the 'max-height' of the toolbar.
this.eventBus.on("resize", this._setMaxHeight.bind(this)); this.eventBus._on("resize", this._setMaxHeight.bind(this));
// Hide the Scroll/Spread mode buttons, when they're not applicable to the // Hide the Scroll/Spread mode buttons, when they're not applicable to the
// current `BaseViewer` instance (in particular `PDFSinglePageViewer`). // current `BaseViewer` instance (in particular `PDFSinglePageViewer`).
this.eventBus.on("baseviewerinit", evt => { this.eventBus._on("baseviewerinit", evt => {
if (evt.source instanceof PDFSinglePageViewer) { if (evt.source instanceof PDFSinglePageViewer) {
this.toolbarButtonContainer.classList.add( this.toolbarButtonContainer.classList.add(
"hiddenScrollModeButtons", "hiddenScrollModeButtons",
@ -233,7 +233,7 @@ class SecondaryToolbar {
} }
_bindCursorToolsListener(buttons) { _bindCursorToolsListener(buttons) {
this.eventBus.on("cursortoolchanged", function({ tool }) { this.eventBus._on("cursortoolchanged", function({ tool }) {
buttons.cursorSelectToolButton.classList.toggle( buttons.cursorSelectToolButton.classList.toggle(
"toggled", "toggled",
tool === CursorTool.SELECT tool === CursorTool.SELECT
@ -267,9 +267,9 @@ class SecondaryToolbar {
buttons.spreadOddButton.disabled = isScrollModeHorizontal; buttons.spreadOddButton.disabled = isScrollModeHorizontal;
buttons.spreadEvenButton.disabled = isScrollModeHorizontal; buttons.spreadEvenButton.disabled = isScrollModeHorizontal;
} }
this.eventBus.on("scrollmodechanged", scrollModeChanged); this.eventBus._on("scrollmodechanged", scrollModeChanged);
this.eventBus.on("secondarytoolbarreset", evt => { this.eventBus._on("secondarytoolbarreset", evt => {
if (evt.source === this) { if (evt.source === this) {
scrollModeChanged({ mode: ScrollMode.VERTICAL }); scrollModeChanged({ mode: ScrollMode.VERTICAL });
} }
@ -291,9 +291,9 @@ class SecondaryToolbar {
mode === SpreadMode.EVEN mode === SpreadMode.EVEN
); );
} }
this.eventBus.on("spreadmodechanged", spreadModeChanged); this.eventBus._on("spreadmodechanged", spreadModeChanged);
this.eventBus.on("secondarytoolbarreset", evt => { this.eventBus._on("secondarytoolbarreset", evt => {
if (evt.source === this) { if (evt.source === this) {
spreadModeChanged({ mode: SpreadMode.NONE }); spreadModeChanged({ mode: SpreadMode.NONE });
} }

View File

@ -123,7 +123,7 @@ class TextLayerBuilder {
this._updateMatches(); this._updateMatches();
} }
}; };
this.eventBus.on( this.eventBus._on(
"updatetextlayermatches", "updatetextlayermatches",
this._onUpdateTextLayerMatches this._onUpdateTextLayerMatches
); );
@ -139,7 +139,7 @@ class TextLayerBuilder {
this.textLayerRenderTask = null; this.textLayerRenderTask = null;
} }
if (this._onUpdateTextLayerMatches) { if (this._onUpdateTextLayerMatches) {
this.eventBus.off( this.eventBus._off(
"updatetextlayermatches", "updatetextlayermatches",
this._onUpdateTextLayerMatches this._onUpdateTextLayerMatches
); );
@ -444,19 +444,22 @@ class DefaultTextLayerFactory {
* @param {number} pageIndex * @param {number} pageIndex
* @param {PageViewport} viewport * @param {PageViewport} viewport
* @param {boolean} enhanceTextSelection * @param {boolean} enhanceTextSelection
* @param {EventBus} eventBus
* @returns {TextLayerBuilder} * @returns {TextLayerBuilder}
*/ */
createTextLayerBuilder( createTextLayerBuilder(
textLayerDiv, textLayerDiv,
pageIndex, pageIndex,
viewport, viewport,
enhanceTextSelection = false enhanceTextSelection = false,
eventBus
) { ) {
return new TextLayerBuilder({ return new TextLayerBuilder({
textLayerDiv, textLayerDiv,
pageIndex, pageIndex,
viewport, viewport,
enhanceTextSelection, enhanceTextSelection,
eventBus,
}); });
} }
} }

View File

@ -159,7 +159,7 @@ class Toolbar {
// Suppress context menus for some controls. // Suppress context menus for some controls.
scaleSelect.oncontextmenu = noContextMenuHandler; scaleSelect.oncontextmenu = noContextMenuHandler;
this.eventBus.on("localized", () => { this.eventBus._on("localized", () => {
this._wasLocalized = true; this._wasLocalized = true;
this._adjustScaleWidth(); this._adjustScaleWidth();
this._updateUIState(true); this._updateUIState(true);

View File

@ -757,9 +757,8 @@ const animationStarted = new Promise(function(resolve) {
}); });
/** /**
* Simple event bus for an application. Listeners are attached using the * Simple event bus for an application. Listeners are attached using the `on`
* `on` and `off` methods. To raise an event, the `dispatch` method shall be * and `off` methods. To raise an event, the `dispatch` method shall be used.
* used.
*/ */
class EventBus { class EventBus {
constructor({ dispatchToDOM = false } = {}) { constructor({ dispatchToDOM = false } = {}) {
@ -767,22 +766,20 @@ class EventBus {
this._dispatchToDOM = dispatchToDOM === true; this._dispatchToDOM = dispatchToDOM === true;
} }
/**
* @param {string} eventName
* @param {function} listener
*/
on(eventName, listener) { on(eventName, listener) {
let eventListeners = this._listeners[eventName]; this._on(eventName, listener, { external: true });
if (!eventListeners) {
eventListeners = [];
this._listeners[eventName] = eventListeners;
}
eventListeners.push(listener);
} }
/**
* @param {string} eventName
* @param {function} listener
*/
off(eventName, listener) { off(eventName, listener) {
const eventListeners = this._listeners[eventName]; this._off(eventName, listener, { external: true });
let i;
if (!eventListeners || (i = eventListeners.indexOf(listener)) < 0) {
return;
}
eventListeners.splice(i, 1);
} }
dispatch(eventName) { dispatch(eventName) {
@ -796,16 +793,62 @@ class EventBus {
} }
// Passing all arguments after the eventName to the listeners. // Passing all arguments after the eventName to the listeners.
const args = Array.prototype.slice.call(arguments, 1); const args = Array.prototype.slice.call(arguments, 1);
let externalListeners;
// Making copy of the listeners array in case if it will be modified // Making copy of the listeners array in case if it will be modified
// during dispatch. // during dispatch.
eventListeners.slice(0).forEach(function(listener) { eventListeners.slice(0).forEach(function({ listener, external }) {
if (external) {
if (!externalListeners) {
externalListeners = [];
}
externalListeners.push(listener);
return;
}
listener.apply(null, args); listener.apply(null, args);
}); });
// Dispatch any "external" listeners *after* the internal ones, to give the
// viewer components time to handle events and update their state first.
if (externalListeners) {
externalListeners.forEach(function(listener) {
listener.apply(null, args);
});
externalListeners = null;
}
if (this._dispatchToDOM) { if (this._dispatchToDOM) {
this._dispatchDOMEvent(eventName, args); this._dispatchDOMEvent(eventName, args);
} }
} }
/**
* @ignore
*/
_on(eventName, listener, options = null) {
let eventListeners = this._listeners[eventName];
if (!eventListeners) {
this._listeners[eventName] = eventListeners = [];
}
eventListeners.push({
listener,
external: options ? options.external : false,
});
}
/**
* @ignore
*/
_off(eventName, listener, options = null) {
const eventListeners = this._listeners[eventName];
if (!eventListeners) {
return;
}
for (let i = 0, ii = eventListeners.length; i < ii; i++) {
if (eventListeners[i].listener === listener) {
eventListeners.splice(i, 1);
return;
}
}
}
/** /**
* @private * @private
*/ */
@ -832,6 +875,9 @@ class EventBus {
let globalEventBus = null; let globalEventBus = null;
function getGlobalEventBus(dispatchToDOM = false) { function getGlobalEventBus(dispatchToDOM = false) {
console.error(
"getGlobalEventBus is deprecated, use a manually created EventBus instance instead."
);
if (!globalEventBus) { if (!globalEventBus) {
globalEventBus = new EventBus({ dispatchToDOM }); globalEventBus = new EventBus({ dispatchToDOM });
} }

View File

@ -60,7 +60,7 @@ function getViewerConfiguration() {
appContainer: document.body, appContainer: document.body,
mainContainer: document.getElementById("viewerContainer"), mainContainer: document.getElementById("viewerContainer"),
viewerContainer: document.getElementById("viewer"), viewerContainer: document.getElementById("viewer"),
eventBus: null, // Using global event bus with (optional) DOM events. eventBus: null,
toolbar: { toolbar: {
container: document.getElementById("toolbarViewer"), container: document.getElementById("toolbarViewer"),
numPages: document.getElementById("numPages"), numPages: document.getElementById("numPages"),