Re-factor the PDFSidebar.{setInitialView, switchView} methods (PR 10502 follow-up)

This removes the internal `_switchView`-method, since looking at all of this again it feels simpler to instead track the initial event dispatching.
This commit is contained in:
Jonas Jenwald 2022-05-26 12:16:56 +02:00
parent 13fda7caeb
commit d289da76a7

View File

@ -68,6 +68,7 @@ class PDFSidebar {
this.isOpen = false; this.isOpen = false;
this.active = SidebarView.THUMBS; this.active = SidebarView.THUMBS;
this.isInitialViewSet = false; this.isInitialViewSet = false;
this.isInitialEventDispatched = false;
/** /**
* Callback used when the sidebar has been opened/closed, to ensure that * Callback used when the sidebar has been opened/closed, to ensure that
@ -103,6 +104,7 @@ class PDFSidebar {
reset() { reset() {
this.isInitialViewSet = false; this.isInitialViewSet = false;
this.isInitialEventDispatched = false;
this._hideUINotification(/* reset = */ true); this._hideUINotification(/* reset = */ true);
this.switchView(SidebarView.THUMBS); this.switchView(SidebarView.THUMBS);
@ -136,9 +138,11 @@ class PDFSidebar {
this._dispatchEvent(); this._dispatchEvent();
return; return;
} }
// Prevent dispatching two back-to-back `sidebarviewchanged` events, this.switchView(view, /* forceOpen = */ true);
// since `this._switchView` dispatched the event if the view changed.
if (!this._switchView(view, /* forceOpen */ true)) { // Prevent dispatching two back-to-back "sidebarviewchanged" events,
// since `this.switchView` dispatched the event if the view changed.
if (!this.isInitialEventDispatched) {
this._dispatchEvent(); this._dispatchEvent();
} }
} }
@ -150,14 +154,6 @@ class PDFSidebar {
* The default value is `false`. * The default value is `false`.
*/ */
switchView(view, forceOpen = false) { switchView(view, forceOpen = false) {
this._switchView(view, forceOpen);
}
/**
* @returns {boolean} Indicating if `this._dispatchEvent` was called.
* @private
*/
_switchView(view, forceOpen = false) {
const isViewChanged = view !== this.active; const isViewChanged = view !== this.active;
let shouldForceRendering = false; let shouldForceRendering = false;
@ -165,9 +161,8 @@ class PDFSidebar {
case SidebarView.NONE: case SidebarView.NONE:
if (this.isOpen) { if (this.isOpen) {
this.close(); this.close();
return true; // Closing will trigger rendering and dispatch the event.
} }
return false; return; // Closing will trigger rendering and dispatch the event.
case SidebarView.THUMBS: case SidebarView.THUMBS:
if (this.isOpen && isViewChanged) { if (this.isOpen && isViewChanged) {
shouldForceRendering = true; shouldForceRendering = true;
@ -175,22 +170,22 @@ class PDFSidebar {
break; break;
case SidebarView.OUTLINE: case SidebarView.OUTLINE:
if (this.outlineButton.disabled) { if (this.outlineButton.disabled) {
return false; return;
} }
break; break;
case SidebarView.ATTACHMENTS: case SidebarView.ATTACHMENTS:
if (this.attachmentsButton.disabled) { if (this.attachmentsButton.disabled) {
return false; return;
} }
break; break;
case SidebarView.LAYERS: case SidebarView.LAYERS:
if (this.layersButton.disabled) { if (this.layersButton.disabled) {
return false; return;
} }
break; break;
default: default:
console.error(`PDFSidebar._switchView: "${view}" is not a valid view.`); console.error(`PDFSidebar.switchView: "${view}" is not a valid view.`);
return false; return;
} }
// Update the active view *after* it has been validated above, // Update the active view *after* it has been validated above,
// in order to prevent setting it to an invalid state. // in order to prevent setting it to an invalid state.
@ -222,7 +217,7 @@ class PDFSidebar {
if (forceOpen && !this.isOpen) { if (forceOpen && !this.isOpen) {
this.open(); this.open();
return true; // Opening will trigger rendering and dispatch the event. return; // Opening will trigger rendering and dispatch the event.
} }
if (shouldForceRendering) { if (shouldForceRendering) {
this._updateThumbnailViewer(); this._updateThumbnailViewer();
@ -231,7 +226,6 @@ class PDFSidebar {
if (isViewChanged) { if (isViewChanged) {
this._dispatchEvent(); this._dispatchEvent();
} }
return isViewChanged;
} }
open() { open() {
@ -280,6 +274,10 @@ class PDFSidebar {
* @private * @private
*/ */
_dispatchEvent() { _dispatchEvent() {
if (this.isInitialViewSet && !this.isInitialEventDispatched) {
this.isInitialEventDispatched = true;
}
this.eventBus.dispatch("sidebarviewchanged", { this.eventBus.dispatch("sidebarviewchanged", {
source: this, source: this,
view: this.visibleView, view: this.visibleView,