Skip to content

Commit 5a8b747

Browse files
committed
[api-minor] Deprecate getGlobalEventBus and update the "viewer components" examples accordingly
To avoid outright breaking third-party usages of the "viewer components" the `getGlobalEventBus` functionality is left intact, but a deprecation message is printed if the function is invoked. The various examples are updated to *explicitly* initialize an `EventBus` instance, and provide that when initializing the relevant viewer components.
1 parent 30e0f02 commit 5a8b747

File tree

16 files changed

+67
-34
lines changed

16 files changed

+67
-34
lines changed

examples/acroforms/acroforms.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var DEFAULT_SCALE = 1.0;
2323

2424
var container = document.getElementById("pageContainer");
2525

26+
var eventBus = new pdfjsViewer.EventBus();
27+
2628
// Fetch the PDF document from the URL using promises.
2729
var loadingTask = pdfjsLib.getDocument(DEFAULT_URL);
2830
loadingTask.promise.then(function(doc) {
@@ -39,6 +41,7 @@ loadingTask.promise.then(function(doc) {
3941
id: pageNum,
4042
scale: DEFAULT_SCALE,
4143
defaultViewport: pdfPage.getViewport({ scale: DEFAULT_SCALE }),
44+
eventBus: eventBus,
4245
annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(),
4346
renderInteractiveForms: true,
4447
});

examples/components/pageviewer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ var SCALE = 1.0;
3535

3636
var container = document.getElementById("pageContainer");
3737

38+
var eventBus = new pdfjsViewer.EventBus();
39+
3840
// Loading document.
3941
var loadingTask = pdfjsLib.getDocument({
4042
url: DEFAULT_URL,
@@ -50,6 +52,7 @@ loadingTask.promise.then(function(pdfDocument) {
5052
id: PAGE_TO_VIEW,
5153
scale: SCALE,
5254
defaultViewport: pdfPage.getViewport({ scale: SCALE }),
55+
eventBus: eventBus,
5356
// We can enable text/annotations layers, if needed
5457
textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory(),
5558
annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(),

examples/components/simpleviewer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,28 @@ var SEARCH_FOR = ""; // try 'Mozilla';
3434

3535
var container = document.getElementById("viewerContainer");
3636

37+
var eventBus = new pdfjsViewer.EventBus();
38+
3739
// (Optionally) enable hyperlinks within PDF files.
38-
var pdfLinkService = new pdfjsViewer.PDFLinkService();
40+
var pdfLinkService = new pdfjsViewer.PDFLinkService({
41+
eventBus: eventBus,
42+
});
3943

4044
// (Optionally) enable find controller.
4145
var pdfFindController = new pdfjsViewer.PDFFindController({
46+
eventBus: eventBus,
4247
linkService: pdfLinkService,
4348
});
4449

4550
var pdfViewer = new pdfjsViewer.PDFViewer({
4651
container: container,
52+
eventBus: eventBus,
4753
linkService: pdfLinkService,
4854
findController: pdfFindController,
4955
});
5056
pdfLinkService.setViewer(pdfViewer);
5157

52-
document.addEventListener("pagesinit", function() {
58+
eventBus.on("pagesinit", function() {
5359
// We can use pdfViewer now, e.g. let's change default scale.
5460
pdfViewer.currentScaleValue = "page-width";
5561

examples/components/singlepageviewer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,28 @@ var SEARCH_FOR = ""; // try 'Mozilla';
3434

3535
var container = document.getElementById("viewerContainer");
3636

37+
var eventBus = new pdfjsViewer.EventBus();
38+
3739
// (Optionally) enable hyperlinks within PDF files.
38-
var pdfLinkService = new pdfjsViewer.PDFLinkService();
40+
var pdfLinkService = new pdfjsViewer.PDFLinkService({
41+
eventBus: eventBus,
42+
});
3943

4044
// (Optionally) enable find controller.
4145
var pdfFindController = new pdfjsViewer.PDFFindController({
46+
eventBus: eventBus,
4247
linkService: pdfLinkService,
4348
});
4449

4550
var pdfSinglePageViewer = new pdfjsViewer.PDFSinglePageViewer({
4651
container: container,
52+
eventBus: eventBus,
4753
linkService: pdfLinkService,
4854
findController: pdfFindController,
4955
});
5056
pdfLinkService.setViewer(pdfSinglePageViewer);
5157

52-
document.addEventListener("pagesinit", function() {
58+
eventBus.on("pagesinit", function() {
5359
// We can use pdfSinglePageViewer now, e.g. let's change default scale.
5460
pdfSinglePageViewer.currentScaleValue = "page-width";
5561

examples/mobile-viewer/viewer.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var PDFViewerApplication = {
4040
pdfViewer: null,
4141
pdfHistory: null,
4242
pdfLinkService: null,
43+
eventBus: null,
4344

4445
/**
4546
* Opens PDF document specified by URL.
@@ -340,14 +341,19 @@ var PDFViewerApplication = {
340341
},
341342

342343
initUI: function pdfViewInitUI() {
343-
var linkService = new pdfjsViewer.PDFLinkService();
344+
var eventBus = new pdfjsViewer.EventBus();
345+
346+
var linkService = new pdfjsViewer.PDFLinkService({
347+
eventBus: eventBus,
348+
});
344349
this.pdfLinkService = linkService;
345350

346351
this.l10n = pdfjsViewer.NullL10n;
347352

348353
var container = document.getElementById("viewerContainer");
349354
var pdfViewer = new pdfjsViewer.PDFViewer({
350355
container: container,
356+
eventBus: eventBus,
351357
linkService: linkService,
352358
l10n: this.l10n,
353359
useOnlyCssZoom: USE_ONLY_CSS_ZOOM,
@@ -357,6 +363,7 @@ var PDFViewerApplication = {
357363
linkService.setViewer(pdfViewer);
358364

359365
this.pdfHistory = new pdfjsViewer.PDFHistory({
366+
eventBus: eventBus,
360367
linkService: linkService,
361368
});
362369
linkService.setHistory(this.pdfHistory);
@@ -394,15 +401,15 @@ var PDFViewerApplication = {
394401
}
395402
});
396403

397-
document.addEventListener("pagesinit", function() {
404+
eventBus.on("pagesinit", function() {
398405
// We can use pdfViewer now, e.g. let's change default scale.
399406
pdfViewer.currentScaleValue = DEFAULT_SCALE_VALUE;
400407
});
401408

402-
document.addEventListener(
409+
eventBus.on(
403410
"pagechanging",
404411
function(evt) {
405-
var page = evt.detail.pageNumber;
412+
var page = evt.pageNumber;
406413
var numPages = PDFViewerApplication.pagesCount;
407414

408415
document.getElementById("pageNumber").value = page;

examples/svgviewer/viewer.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,23 @@ var DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
3333

3434
var container = document.getElementById("viewerContainer");
3535

36+
var eventBus = new pdfjsViewer.EventBus();
37+
3638
// (Optionally) enable hyperlinks within PDF files.
37-
var pdfLinkService = new pdfjsViewer.PDFLinkService();
39+
var pdfLinkService = new pdfjsViewer.PDFLinkService({
40+
eventBus: eventBus,
41+
});
3842

3943
var pdfViewer = new pdfjsViewer.PDFViewer({
4044
container: container,
45+
eventBus: eventBus,
4146
linkService: pdfLinkService,
4247
renderer: "svg",
4348
textLayerMode: 0,
4449
});
4550
pdfLinkService.setViewer(pdfViewer);
4651

47-
document.addEventListener("pagesinit", function() {
52+
eventBus.on("pagesinit", function() {
4853
// We can use pdfViewer now, e.g. let's change default scale.
4954
pdfViewer.currentScaleValue = "page-width";
5055
});

web/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
animationStarted,
1919
AutoPrintRegExp,
2020
DEFAULT_SCALE_VALUE,
21-
getGlobalEventBus,
21+
EventBus,
2222
getPDFFileNameFromURL,
2323
isValidRotation,
2424
isValidScrollMode,
@@ -343,7 +343,7 @@ const PDFViewerApplication = {
343343

344344
const eventBus =
345345
appConfig.eventBus ||
346-
getGlobalEventBus(AppOptions.get("eventBusDispatchToDOM"));
346+
new EventBus({ dispatchToDOM: AppOptions.get("eventBusDispatchToDOM") });
347347
this.eventBus = eventBus;
348348

349349
const pdfRenderingQueue = new PDFRenderingQueue();

web/base_viewer.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,17 +1088,20 @@ class BaseViewer {
10881088
* @param {HTMLDivElement} textLayerDiv
10891089
* @param {number} pageIndex
10901090
* @param {PageViewport} viewport
1091+
* @param {boolean} enhanceTextSelection
1092+
* @param {EventBus} eventBus
10911093
* @returns {TextLayerBuilder}
10921094
*/
10931095
createTextLayerBuilder(
10941096
textLayerDiv,
10951097
pageIndex,
10961098
viewport,
1097-
enhanceTextSelection = false
1099+
enhanceTextSelection = false,
1100+
eventBus
10981101
) {
10991102
return new TextLayerBuilder({
11001103
textLayerDiv,
1101-
eventBus: this.eventBus,
1104+
eventBus,
11021105
pageIndex,
11031106
viewport,
11041107
findController: this.isInPresentationMode ? null : this.findController,

web/interfaces.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@ class IPDFTextLayerFactory {
146146
* @param {number} pageIndex
147147
* @param {PageViewport} viewport
148148
* @param {boolean} enhanceTextSelection
149+
* @param {EventBus} eventBus
149150
* @returns {TextLayerBuilder}
150151
*/
151152
createTextLayerBuilder(
152153
textLayerDiv,
153154
pageIndex,
154155
viewport,
155-
enhanceTextSelection = false
156+
enhanceTextSelection = false,
157+
eventBus
156158
) {}
157159
}
158160

web/pdf_find_bar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const MATCHES_COUNT_LIMIT = 1000;
2525
* is done by PDFFindController.
2626
*/
2727
class PDFFindBar {
28-
constructor(options, eventBus = getGlobalEventBus(), l10n = NullL10n) {
28+
constructor(options, eventBus, l10n = NullL10n) {
2929
this.opened = false;
3030

3131
this.bar = options.bar || null;
@@ -38,7 +38,7 @@ class PDFFindBar {
3838
this.findResultsCount = options.findResultsCount || null;
3939
this.findPreviousButton = options.findPreviousButton || null;
4040
this.findNextButton = options.findNextButton || null;
41-
this.eventBus = eventBus;
41+
this.eventBus = eventBus || getGlobalEventBus();
4242
this.l10n = l10n;
4343

4444
// Add event listeners to the DOM elements.

0 commit comments

Comments
 (0)