Remove the internal "secondarytoolbarreset" 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/secondary_toolbar.js` file.
This commit is contained in:
parent
234b57bb45
commit
b1ca270162
@ -52,14 +52,15 @@ import { PagesCountLimit } from "./pdf_viewer.js";
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class SecondaryToolbar {
|
class SecondaryToolbar {
|
||||||
|
#opts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {SecondaryToolbarOptions} options
|
* @param {SecondaryToolbarOptions} options
|
||||||
* @param {EventBus} eventBus
|
* @param {EventBus} eventBus
|
||||||
*/
|
*/
|
||||||
constructor(options, eventBus) {
|
constructor(options, eventBus) {
|
||||||
this.toolbar = options.toolbar;
|
this.#opts = options;
|
||||||
this.toggleButton = options.toggleButton;
|
const buttons = [
|
||||||
this.buttons = [
|
|
||||||
{
|
{
|
||||||
element: options.presentationModeButton,
|
element: options.presentationModeButton,
|
||||||
eventName: "presentationmode",
|
eventName: "presentationmode",
|
||||||
@ -141,28 +142,19 @@ class SecondaryToolbar {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
||||||
this.buttons.push({
|
buttons.push({
|
||||||
element: options.openFileButton,
|
element: options.openFileButton,
|
||||||
eventName: "openfile",
|
eventName: "openfile",
|
||||||
close: true,
|
close: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
this.items = {
|
|
||||||
firstPage: options.firstPageButton,
|
|
||||||
lastPage: options.lastPageButton,
|
|
||||||
pageRotateCw: options.pageRotateCwButton,
|
|
||||||
pageRotateCcw: options.pageRotateCcwButton,
|
|
||||||
};
|
|
||||||
|
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
this.opened = false;
|
this.opened = false;
|
||||||
|
|
||||||
// Bind the event listeners for click, cursor tool, and scroll/spread mode
|
// Bind the event listeners for click, cursor tool, and scroll/spread mode
|
||||||
// actions.
|
// actions.
|
||||||
this.#bindClickListeners();
|
this.#bindListeners(buttons);
|
||||||
this.#bindCursorToolsListener(options);
|
|
||||||
this.#bindScrollModeListener(options);
|
|
||||||
this.#bindSpreadModeListener(options);
|
|
||||||
|
|
||||||
this.reset();
|
this.reset();
|
||||||
}
|
}
|
||||||
@ -190,30 +182,40 @@ class SecondaryToolbar {
|
|||||||
this.#updateUIState();
|
this.#updateUIState();
|
||||||
|
|
||||||
// Reset the Scroll/Spread buttons too, since they're document specific.
|
// Reset the Scroll/Spread buttons too, since they're document specific.
|
||||||
this.eventBus.dispatch("secondarytoolbarreset", { source: this });
|
this.#scrollModeChanged({ mode: ScrollMode.VERTICAL });
|
||||||
|
this.#spreadModeChanged({ mode: SpreadMode.NONE });
|
||||||
}
|
}
|
||||||
|
|
||||||
#updateUIState() {
|
#updateUIState() {
|
||||||
this.items.firstPage.disabled = this.pageNumber <= 1;
|
const {
|
||||||
this.items.lastPage.disabled = this.pageNumber >= this.pagesCount;
|
firstPageButton,
|
||||||
this.items.pageRotateCw.disabled = this.pagesCount === 0;
|
lastPageButton,
|
||||||
this.items.pageRotateCcw.disabled = this.pagesCount === 0;
|
pageRotateCwButton,
|
||||||
|
pageRotateCcwButton,
|
||||||
|
} = this.#opts;
|
||||||
|
|
||||||
|
firstPageButton.disabled = this.pageNumber <= 1;
|
||||||
|
lastPageButton.disabled = this.pageNumber >= this.pagesCount;
|
||||||
|
pageRotateCwButton.disabled = this.pagesCount === 0;
|
||||||
|
pageRotateCcwButton.disabled = this.pagesCount === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#bindClickListeners() {
|
#bindListeners(buttons) {
|
||||||
|
const { eventBus } = this;
|
||||||
|
const { toggleButton } = this.#opts;
|
||||||
// Button to toggle the visibility of the secondary toolbar.
|
// Button to toggle the visibility of the secondary toolbar.
|
||||||
this.toggleButton.addEventListener("click", this.toggle.bind(this));
|
toggleButton.addEventListener("click", this.toggle.bind(this));
|
||||||
|
|
||||||
// All items within the secondary toolbar.
|
// All items within the secondary toolbar.
|
||||||
for (const { element, eventName, close, eventDetails } of this.buttons) {
|
for (const { element, eventName, close, eventDetails } of buttons) {
|
||||||
element.addEventListener("click", evt => {
|
element.addEventListener("click", evt => {
|
||||||
if (eventName !== null) {
|
if (eventName !== null) {
|
||||||
this.eventBus.dispatch(eventName, { source: this, ...eventDetails });
|
eventBus.dispatch(eventName, { source: this, ...eventDetails });
|
||||||
}
|
}
|
||||||
if (close) {
|
if (close) {
|
||||||
this.close();
|
this.close();
|
||||||
}
|
}
|
||||||
this.eventBus.dispatch("reporttelemetry", {
|
eventBus.dispatch("reporttelemetry", {
|
||||||
source: this,
|
source: this,
|
||||||
details: {
|
details: {
|
||||||
type: "buttons",
|
type: "buttons",
|
||||||
@ -222,72 +224,58 @@ class SecondaryToolbar {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eventBus._on("cursortoolchanged", this.#cursorToolChanged.bind(this));
|
||||||
|
eventBus._on("scrollmodechanged", this.#scrollModeChanged.bind(this));
|
||||||
|
eventBus._on("spreadmodechanged", this.#spreadModeChanged.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
#bindCursorToolsListener({ cursorSelectToolButton, cursorHandToolButton }) {
|
#cursorToolChanged({ tool }) {
|
||||||
this.eventBus._on("cursortoolchanged", ({ tool }) => {
|
const { cursorSelectToolButton, cursorHandToolButton } = this.#opts;
|
||||||
toggleCheckedBtn(cursorSelectToolButton, tool === CursorTool.SELECT);
|
|
||||||
toggleCheckedBtn(cursorHandToolButton, tool === CursorTool.HAND);
|
toggleCheckedBtn(cursorSelectToolButton, tool === CursorTool.SELECT);
|
||||||
});
|
toggleCheckedBtn(cursorHandToolButton, tool === CursorTool.HAND);
|
||||||
}
|
}
|
||||||
|
|
||||||
#bindScrollModeListener({
|
#scrollModeChanged({ mode }) {
|
||||||
scrollPageButton,
|
const {
|
||||||
scrollVerticalButton,
|
scrollPageButton,
|
||||||
scrollHorizontalButton,
|
scrollVerticalButton,
|
||||||
scrollWrappedButton,
|
scrollHorizontalButton,
|
||||||
spreadNoneButton,
|
scrollWrappedButton,
|
||||||
spreadOddButton,
|
spreadNoneButton,
|
||||||
spreadEvenButton,
|
spreadOddButton,
|
||||||
}) {
|
spreadEvenButton,
|
||||||
const scrollModeChanged = ({ mode }) => {
|
} = this.#opts;
|
||||||
toggleCheckedBtn(scrollPageButton, mode === ScrollMode.PAGE);
|
|
||||||
toggleCheckedBtn(scrollVerticalButton, mode === ScrollMode.VERTICAL);
|
|
||||||
toggleCheckedBtn(scrollHorizontalButton, mode === ScrollMode.HORIZONTAL);
|
|
||||||
toggleCheckedBtn(scrollWrappedButton, mode === ScrollMode.WRAPPED);
|
|
||||||
|
|
||||||
// Permanently *disable* the Scroll buttons when PAGE-scrolling is being
|
toggleCheckedBtn(scrollPageButton, mode === ScrollMode.PAGE);
|
||||||
// enforced for *very* long/large documents; please see the `BaseViewer`.
|
toggleCheckedBtn(scrollVerticalButton, mode === ScrollMode.VERTICAL);
|
||||||
const forceScrollModePage =
|
toggleCheckedBtn(scrollHorizontalButton, mode === ScrollMode.HORIZONTAL);
|
||||||
this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE;
|
toggleCheckedBtn(scrollWrappedButton, mode === ScrollMode.WRAPPED);
|
||||||
scrollPageButton.disabled = forceScrollModePage;
|
|
||||||
scrollVerticalButton.disabled = forceScrollModePage;
|
|
||||||
scrollHorizontalButton.disabled = forceScrollModePage;
|
|
||||||
scrollWrappedButton.disabled = forceScrollModePage;
|
|
||||||
|
|
||||||
// Temporarily *disable* the Spread buttons when horizontal scrolling is
|
// Permanently *disable* the Scroll buttons when PAGE-scrolling is being
|
||||||
// enabled, since the non-default Spread modes doesn't affect the layout.
|
// enforced for *very* long/large documents; please see the `BaseViewer`.
|
||||||
const isHorizontal = mode === ScrollMode.HORIZONTAL;
|
const forceScrollModePage =
|
||||||
spreadNoneButton.disabled = isHorizontal;
|
this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE;
|
||||||
spreadOddButton.disabled = isHorizontal;
|
scrollPageButton.disabled = forceScrollModePage;
|
||||||
spreadEvenButton.disabled = isHorizontal;
|
scrollVerticalButton.disabled = forceScrollModePage;
|
||||||
};
|
scrollHorizontalButton.disabled = forceScrollModePage;
|
||||||
this.eventBus._on("scrollmodechanged", scrollModeChanged);
|
scrollWrappedButton.disabled = forceScrollModePage;
|
||||||
|
|
||||||
this.eventBus._on("secondarytoolbarreset", evt => {
|
// Temporarily *disable* the Spread buttons when horizontal scrolling is
|
||||||
if (evt.source === this) {
|
// enabled, since the non-default Spread modes doesn't affect the layout.
|
||||||
scrollModeChanged({ mode: ScrollMode.VERTICAL });
|
const isHorizontal = mode === ScrollMode.HORIZONTAL;
|
||||||
}
|
spreadNoneButton.disabled = isHorizontal;
|
||||||
});
|
spreadOddButton.disabled = isHorizontal;
|
||||||
|
spreadEvenButton.disabled = isHorizontal;
|
||||||
}
|
}
|
||||||
|
|
||||||
#bindSpreadModeListener({
|
#spreadModeChanged({ mode }) {
|
||||||
spreadNoneButton,
|
const { spreadNoneButton, spreadOddButton, spreadEvenButton } = this.#opts;
|
||||||
spreadOddButton,
|
|
||||||
spreadEvenButton,
|
|
||||||
}) {
|
|
||||||
const spreadModeChanged = ({ mode }) => {
|
|
||||||
toggleCheckedBtn(spreadNoneButton, mode === SpreadMode.NONE);
|
|
||||||
toggleCheckedBtn(spreadOddButton, mode === SpreadMode.ODD);
|
|
||||||
toggleCheckedBtn(spreadEvenButton, mode === SpreadMode.EVEN);
|
|
||||||
};
|
|
||||||
this.eventBus._on("spreadmodechanged", spreadModeChanged);
|
|
||||||
|
|
||||||
this.eventBus._on("secondarytoolbarreset", evt => {
|
toggleCheckedBtn(spreadNoneButton, mode === SpreadMode.NONE);
|
||||||
if (evt.source === this) {
|
toggleCheckedBtn(spreadOddButton, mode === SpreadMode.ODD);
|
||||||
spreadModeChanged({ mode: SpreadMode.NONE });
|
toggleCheckedBtn(spreadEvenButton, mode === SpreadMode.EVEN);
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open() {
|
open() {
|
||||||
@ -295,7 +283,9 @@ class SecondaryToolbar {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.opened = true;
|
this.opened = true;
|
||||||
toggleExpandedBtn(this.toggleButton, true, this.toolbar);
|
|
||||||
|
const { toggleButton, toolbar } = this.#opts;
|
||||||
|
toggleExpandedBtn(toggleButton, true, toolbar);
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
@ -303,7 +293,9 @@ class SecondaryToolbar {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.opened = false;
|
this.opened = false;
|
||||||
toggleExpandedBtn(this.toggleButton, false, this.toolbar);
|
|
||||||
|
const { toggleButton, toolbar } = this.#opts;
|
||||||
|
toggleExpandedBtn(toggleButton, false, toolbar);
|
||||||
}
|
}
|
||||||
|
|
||||||
toggle() {
|
toggle() {
|
||||||
|
Loading…
Reference in New Issue
Block a user