From 234b57bb45ac8be7d7b9866382db92781a0358eb Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 26 Dec 2023 10:56:38 +0100 Subject: [PATCH 1/2] Remove the internal "toolbarreset" event and slightly re-factor the code With modern JavaScript class features we can move the relevant event handling into private methods, and thus invoke it directly when resetting the toolbar UI-state. *Please note:* This patch slightly reduces the size of the `web/toolbar.js` file. --- web/toolbar.js | 160 +++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 86 deletions(-) diff --git a/web/toolbar.js b/web/toolbar.js index 3bf9e3fa9..45570687f 100644 --- a/web/toolbar.js +++ b/web/toolbar.js @@ -43,14 +43,16 @@ import { */ class Toolbar { + #opts; + /** * @param {ToolbarOptions} options * @param {EventBus} eventBus */ constructor(options, eventBus) { - this.toolbar = options.container; + this.#opts = options; this.eventBus = eventBus; - this.buttons = [ + const buttons = [ { element: options.previous, eventName: "previouspage" }, { element: options.next, eventName: "nextpage" }, { element: options.zoomIn, eventName: "zoomin" }, @@ -106,22 +108,12 @@ class Toolbar { }, }, ]; - this.items = { - numPages: options.numPages, - pageNumber: options.pageNumber, - scaleSelect: options.scaleSelect, - customScaleOption: options.customScaleOption, - previous: options.previous, - next: options.next, - zoomIn: options.zoomIn, - zoomOut: options.zoomOut, - }; // Bind the event listeners for click and various other actions. - this.#bindListeners(options); + this.#bindListeners(buttons); if (options.editorHighlightColorPicker) { - this.eventBus._on( + eventBus._on( "annotationeditoruimanager", ({ uiManager }) => { this.#setAnnotationEditorUIManager( @@ -172,18 +164,19 @@ class Toolbar { this.updateLoadingIndicatorState(); // Reset the Editor buttons too, since they're document specific. - this.eventBus.dispatch("toolbarreset", { source: this }); + this.#editorModeChanged({ mode: AnnotationEditorType.DISABLE }); } - #bindListeners(options) { - const { pageNumber, scaleSelect } = this.items; + #bindListeners(buttons) { + const { eventBus } = this; + const { pageNumber, scaleSelect } = this.#opts; const self = this; // The buttons within the toolbar. - for (const { element, eventName, eventDetails } of this.buttons) { + for (const { element, eventName, eventDetails } of buttons) { element.addEventListener("click", evt => { if (eventName !== null) { - this.eventBus.dispatch(eventName, { + eventBus.dispatch(eventName, { source: this, ...eventDetails, // evt.detail is the number of clicks. @@ -197,7 +190,7 @@ class Toolbar { this.select(); }); pageNumber.addEventListener("change", function () { - self.eventBus.dispatch("pagenumberchanged", { + eventBus.dispatch("pagenumberchanged", { source: self, value: this.value, }); @@ -207,15 +200,14 @@ class Toolbar { if (this.value === "custom") { return; } - self.eventBus.dispatch("scalechanged", { + eventBus.dispatch("scalechanged", { source: self, value: this.value, }); }); // Here we depend on browsers dispatching the "click" event *after* the // "change" event, when the