From 38ff79186a3330666ff712f10d25e8f45cb876a0 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Fri, 21 Sep 2018 20:00:58 +0200 Subject: [PATCH] Replace callbacks for updating the UI with dispatching events on the event bus This makes it more similar to how other components update the viewer UI and avoids the need to have extra member variables and checks. --- web/app.js | 43 +++++++++++++++++++++----------------- web/pdf_find_controller.js | 23 +++++++++----------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/web/app.js b/web/app.js index 63e7142a2..62b65b157 100644 --- a/web/app.js +++ b/web/app.js @@ -346,25 +346,6 @@ let PDFViewerApplication = { linkService: pdfLinkService, eventBus, }); - this.findController.onUpdateResultsCount = (matchesCount) => { - if (this.supportsIntegratedFind) { - this.externalServices.updateFindMatchesCount(matchesCount); - } else { - this.findBar.updateResultsCount(matchesCount); - } - }; - this.findController.onUpdateState = (state, previous, matchesCount) => { - if (this.supportsIntegratedFind) { - this.externalServices.updateFindControlState({ - result: state, - findPrevious: previous, - matchesCount, - }); - } else { - this.findBar.updateUIState(state, previous, matchesCount); - } - }; - this.pdfViewer.setFindController(this.findController); // TODO: improve `PDFFindBar` constructor parameter passing @@ -1343,6 +1324,8 @@ let PDFViewerApplication = { eventBus.on('documentproperties', webViewerDocumentProperties); eventBus.on('find', webViewerFind); eventBus.on('findfromurlhash', webViewerFindFromUrlHash); + eventBus.on('updatefindmatchescount', webViewerUpdateFindMatchesCount); + eventBus.on('updatefindcontrolstate', webViewerUpdateFindControlState); if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) { eventBus.on('fileinputchange', webViewerFileInputChange); } @@ -1414,6 +1397,8 @@ let PDFViewerApplication = { eventBus.off('documentproperties', webViewerDocumentProperties); eventBus.off('find', webViewerFind); eventBus.off('findfromurlhash', webViewerFindFromUrlHash); + eventBus.off('updatefindmatchescount', webViewerUpdateFindMatchesCount); + eventBus.off('updatefindcontrolstate', webViewerUpdateFindControlState); if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) { eventBus.off('fileinputchange', webViewerFileInputChange); } @@ -1976,6 +1961,26 @@ function webViewerFindFromUrlHash(evt) { }); } +function webViewerUpdateFindMatchesCount({ matchesCount, }) { + if (PDFViewerApplication.supportsIntegratedFind) { + PDFViewerApplication.externalServices.updateFindMatchesCount(matchesCount); + } else { + PDFViewerApplication.findBar.updateResultsCount(matchesCount); + } +} + +function webViewerUpdateFindControlState({ state, previous, matchesCount, }) { + if (PDFViewerApplication.supportsIntegratedFind) { + PDFViewerApplication.externalServices.updateFindControlState({ + result: state, + findPrevious: previous, + matchesCount, + }); + } else { + PDFViewerApplication.findBar.updateUIState(state, previous, matchesCount); + } +} + function webViewerScaleChanging(evt) { PDFViewerApplication.toolbar.setPageScale(evt.presetValue, evt.scale); diff --git a/web/pdf_find_controller.js b/web/pdf_find_controller.js index 082eb6a63..a74df0893 100644 --- a/web/pdf_find_controller.js +++ b/web/pdf_find_controller.js @@ -57,9 +57,6 @@ class PDFFindController { this._linkService = linkService; this._eventBus = eventBus; - this.onUpdateResultsCount = null; - this.onUpdateState = null; - this._reset(); eventBus.on('findbarclose', () => { @@ -564,19 +561,19 @@ class PDFFindController { } _updateUIResultsCount() { - if (!this.onUpdateResultsCount) { - return; - } - const matchesCount = this._requestMatchesCount(); - this.onUpdateResultsCount(matchesCount); + this._eventBus.dispatch('updatefindmatchescount', { + source: this, + matchesCount: this._requestMatchesCount(), + }); } _updateUIState(state, previous) { - if (!this.onUpdateState) { - return; - } - const matchesCount = this._requestMatchesCount(); - this.onUpdateState(state, previous, matchesCount); + this._eventBus.dispatch('updatefindcontrolstate', { + source: this, + state, + previous, + matchesCount: this._requestMatchesCount(), + }); } }