Skip to content

Commit 9e14d04

Browse files
Merge pull request #17464 from Snuffleupagus/rm-toolbar-reset-events
Remove the internal "toolbarreset"/"secondarytoolbarreset" events and slightly re-factor the code
2 parents afa65dc + b1ca270 commit 9e14d04

File tree

2 files changed

+148
-168
lines changed

2 files changed

+148
-168
lines changed

web/secondary_toolbar.js

+74-82
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ import { PagesCountLimit } from "./pdf_viewer.js";
5252
*/
5353

5454
class SecondaryToolbar {
55+
#opts;
56+
5557
/**
5658
* @param {SecondaryToolbarOptions} options
5759
* @param {EventBus} eventBus
5860
*/
5961
constructor(options, eventBus) {
60-
this.toolbar = options.toolbar;
61-
this.toggleButton = options.toggleButton;
62-
this.buttons = [
62+
this.#opts = options;
63+
const buttons = [
6364
{
6465
element: options.presentationModeButton,
6566
eventName: "presentationmode",
@@ -141,28 +142,19 @@ class SecondaryToolbar {
141142
},
142143
];
143144
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
144-
this.buttons.push({
145+
buttons.push({
145146
element: options.openFileButton,
146147
eventName: "openfile",
147148
close: true,
148149
});
149150
}
150-
this.items = {
151-
firstPage: options.firstPageButton,
152-
lastPage: options.lastPageButton,
153-
pageRotateCw: options.pageRotateCwButton,
154-
pageRotateCcw: options.pageRotateCcwButton,
155-
};
156151

157152
this.eventBus = eventBus;
158153
this.opened = false;
159154

160155
// Bind the event listeners for click, cursor tool, and scroll/spread mode
161156
// actions.
162-
this.#bindClickListeners();
163-
this.#bindCursorToolsListener(options);
164-
this.#bindScrollModeListener(options);
165-
this.#bindSpreadModeListener(options);
157+
this.#bindListeners(buttons);
166158

167159
this.reset();
168160
}
@@ -190,30 +182,40 @@ class SecondaryToolbar {
190182
this.#updateUIState();
191183

192184
// Reset the Scroll/Spread buttons too, since they're document specific.
193-
this.eventBus.dispatch("secondarytoolbarreset", { source: this });
185+
this.#scrollModeChanged({ mode: ScrollMode.VERTICAL });
186+
this.#spreadModeChanged({ mode: SpreadMode.NONE });
194187
}
195188

196189
#updateUIState() {
197-
this.items.firstPage.disabled = this.pageNumber <= 1;
198-
this.items.lastPage.disabled = this.pageNumber >= this.pagesCount;
199-
this.items.pageRotateCw.disabled = this.pagesCount === 0;
200-
this.items.pageRotateCcw.disabled = this.pagesCount === 0;
190+
const {
191+
firstPageButton,
192+
lastPageButton,
193+
pageRotateCwButton,
194+
pageRotateCcwButton,
195+
} = this.#opts;
196+
197+
firstPageButton.disabled = this.pageNumber <= 1;
198+
lastPageButton.disabled = this.pageNumber >= this.pagesCount;
199+
pageRotateCwButton.disabled = this.pagesCount === 0;
200+
pageRotateCcwButton.disabled = this.pagesCount === 0;
201201
}
202202

203-
#bindClickListeners() {
203+
#bindListeners(buttons) {
204+
const { eventBus } = this;
205+
const { toggleButton } = this.#opts;
204206
// Button to toggle the visibility of the secondary toolbar.
205-
this.toggleButton.addEventListener("click", this.toggle.bind(this));
207+
toggleButton.addEventListener("click", this.toggle.bind(this));
206208

207209
// All items within the secondary toolbar.
208-
for (const { element, eventName, close, eventDetails } of this.buttons) {
210+
for (const { element, eventName, close, eventDetails } of buttons) {
209211
element.addEventListener("click", evt => {
210212
if (eventName !== null) {
211-
this.eventBus.dispatch(eventName, { source: this, ...eventDetails });
213+
eventBus.dispatch(eventName, { source: this, ...eventDetails });
212214
}
213215
if (close) {
214216
this.close();
215217
}
216-
this.eventBus.dispatch("reporttelemetry", {
218+
eventBus.dispatch("reporttelemetry", {
217219
source: this,
218220
details: {
219221
type: "buttons",
@@ -222,88 +224,78 @@ class SecondaryToolbar {
222224
});
223225
});
224226
}
227+
228+
eventBus._on("cursortoolchanged", this.#cursorToolChanged.bind(this));
229+
eventBus._on("scrollmodechanged", this.#scrollModeChanged.bind(this));
230+
eventBus._on("spreadmodechanged", this.#spreadModeChanged.bind(this));
225231
}
226232

227-
#bindCursorToolsListener({ cursorSelectToolButton, cursorHandToolButton }) {
228-
this.eventBus._on("cursortoolchanged", ({ tool }) => {
229-
toggleCheckedBtn(cursorSelectToolButton, tool === CursorTool.SELECT);
230-
toggleCheckedBtn(cursorHandToolButton, tool === CursorTool.HAND);
231-
});
233+
#cursorToolChanged({ tool }) {
234+
const { cursorSelectToolButton, cursorHandToolButton } = this.#opts;
235+
236+
toggleCheckedBtn(cursorSelectToolButton, tool === CursorTool.SELECT);
237+
toggleCheckedBtn(cursorHandToolButton, tool === CursorTool.HAND);
232238
}
233239

234-
#bindScrollModeListener({
235-
scrollPageButton,
236-
scrollVerticalButton,
237-
scrollHorizontalButton,
238-
scrollWrappedButton,
239-
spreadNoneButton,
240-
spreadOddButton,
241-
spreadEvenButton,
242-
}) {
243-
const scrollModeChanged = ({ mode }) => {
244-
toggleCheckedBtn(scrollPageButton, mode === ScrollMode.PAGE);
245-
toggleCheckedBtn(scrollVerticalButton, mode === ScrollMode.VERTICAL);
246-
toggleCheckedBtn(scrollHorizontalButton, mode === ScrollMode.HORIZONTAL);
247-
toggleCheckedBtn(scrollWrappedButton, mode === ScrollMode.WRAPPED);
240+
#scrollModeChanged({ mode }) {
241+
const {
242+
scrollPageButton,
243+
scrollVerticalButton,
244+
scrollHorizontalButton,
245+
scrollWrappedButton,
246+
spreadNoneButton,
247+
spreadOddButton,
248+
spreadEvenButton,
249+
} = this.#opts;
248250

249-
// Permanently *disable* the Scroll buttons when PAGE-scrolling is being
250-
// enforced for *very* long/large documents; please see the `BaseViewer`.
251-
const forceScrollModePage =
252-
this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE;
253-
scrollPageButton.disabled = forceScrollModePage;
254-
scrollVerticalButton.disabled = forceScrollModePage;
255-
scrollHorizontalButton.disabled = forceScrollModePage;
256-
scrollWrappedButton.disabled = forceScrollModePage;
251+
toggleCheckedBtn(scrollPageButton, mode === ScrollMode.PAGE);
252+
toggleCheckedBtn(scrollVerticalButton, mode === ScrollMode.VERTICAL);
253+
toggleCheckedBtn(scrollHorizontalButton, mode === ScrollMode.HORIZONTAL);
254+
toggleCheckedBtn(scrollWrappedButton, mode === ScrollMode.WRAPPED);
257255

258-
// Temporarily *disable* the Spread buttons when horizontal scrolling is
259-
// enabled, since the non-default Spread modes doesn't affect the layout.
260-
const isHorizontal = mode === ScrollMode.HORIZONTAL;
261-
spreadNoneButton.disabled = isHorizontal;
262-
spreadOddButton.disabled = isHorizontal;
263-
spreadEvenButton.disabled = isHorizontal;
264-
};
265-
this.eventBus._on("scrollmodechanged", scrollModeChanged);
256+
// Permanently *disable* the Scroll buttons when PAGE-scrolling is being
257+
// enforced for *very* long/large documents; please see the `BaseViewer`.
258+
const forceScrollModePage =
259+
this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE;
260+
scrollPageButton.disabled = forceScrollModePage;
261+
scrollVerticalButton.disabled = forceScrollModePage;
262+
scrollHorizontalButton.disabled = forceScrollModePage;
263+
scrollWrappedButton.disabled = forceScrollModePage;
266264

267-
this.eventBus._on("secondarytoolbarreset", evt => {
268-
if (evt.source === this) {
269-
scrollModeChanged({ mode: ScrollMode.VERTICAL });
270-
}
271-
});
265+
// Temporarily *disable* the Spread buttons when horizontal scrolling is
266+
// enabled, since the non-default Spread modes doesn't affect the layout.
267+
const isHorizontal = mode === ScrollMode.HORIZONTAL;
268+
spreadNoneButton.disabled = isHorizontal;
269+
spreadOddButton.disabled = isHorizontal;
270+
spreadEvenButton.disabled = isHorizontal;
272271
}
273272

274-
#bindSpreadModeListener({
275-
spreadNoneButton,
276-
spreadOddButton,
277-
spreadEvenButton,
278-
}) {
279-
const spreadModeChanged = ({ mode }) => {
280-
toggleCheckedBtn(spreadNoneButton, mode === SpreadMode.NONE);
281-
toggleCheckedBtn(spreadOddButton, mode === SpreadMode.ODD);
282-
toggleCheckedBtn(spreadEvenButton, mode === SpreadMode.EVEN);
283-
};
284-
this.eventBus._on("spreadmodechanged", spreadModeChanged);
273+
#spreadModeChanged({ mode }) {
274+
const { spreadNoneButton, spreadOddButton, spreadEvenButton } = this.#opts;
285275

286-
this.eventBus._on("secondarytoolbarreset", evt => {
287-
if (evt.source === this) {
288-
spreadModeChanged({ mode: SpreadMode.NONE });
289-
}
290-
});
276+
toggleCheckedBtn(spreadNoneButton, mode === SpreadMode.NONE);
277+
toggleCheckedBtn(spreadOddButton, mode === SpreadMode.ODD);
278+
toggleCheckedBtn(spreadEvenButton, mode === SpreadMode.EVEN);
291279
}
292280

293281
open() {
294282
if (this.opened) {
295283
return;
296284
}
297285
this.opened = true;
298-
toggleExpandedBtn(this.toggleButton, true, this.toolbar);
286+
287+
const { toggleButton, toolbar } = this.#opts;
288+
toggleExpandedBtn(toggleButton, true, toolbar);
299289
}
300290

301291
close() {
302292
if (!this.opened) {
303293
return;
304294
}
305295
this.opened = false;
306-
toggleExpandedBtn(this.toggleButton, false, this.toolbar);
296+
297+
const { toggleButton, toolbar } = this.#opts;
298+
toggleExpandedBtn(toggleButton, false, toolbar);
307299
}
308300

309301
toggle() {

0 commit comments

Comments
 (0)