Change PDFSidebar.switchView
to act as a wrapper for a "private" PDFSidebar._switchView
method
The new "private" method will return a boolean, indicating if the `sidebarviewchanged` event was dispatched, thus allowing some simplification of the `PDFSidebar.setInitialView` method.
This commit is contained in:
parent
291e62b41e
commit
dd4620530d
@ -130,18 +130,15 @@ class PDFSidebar {
|
|||||||
}
|
}
|
||||||
this.isInitialViewSet = true;
|
this.isInitialViewSet = true;
|
||||||
|
|
||||||
if (this.isOpen && view === SidebarView.NONE) {
|
// If the user has already manually opened the sidebar, immediately closing
|
||||||
|
// it would be bad UX.
|
||||||
|
if (view === SidebarView.NONE) {
|
||||||
this._dispatchEvent();
|
this._dispatchEvent();
|
||||||
// If the user has already manually opened the sidebar,
|
|
||||||
// immediately closing it would be bad UX.
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let isViewPreserved = (view === this.visibleView);
|
// 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)) {
|
||||||
if (isViewPreserved) {
|
|
||||||
// Prevent dispatching two back-to-back `sidebarviewchanged` events,
|
|
||||||
// since `this.switchView` dispatched the event if the view changed.
|
|
||||||
this._dispatchEvent();
|
this._dispatchEvent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,14 +150,24 @@ class PDFSidebar {
|
|||||||
* The default value is `false`.
|
* The default value is `false`.
|
||||||
*/
|
*/
|
||||||
switchView(view, forceOpen = false) {
|
switchView(view, forceOpen = false) {
|
||||||
if (view === SidebarView.NONE) {
|
this._switchView(view, forceOpen);
|
||||||
this.close();
|
}
|
||||||
return;
|
|
||||||
}
|
/**
|
||||||
let isViewChanged = (view !== this.active);
|
* @returns {boolean} Indicating if `this._dispatchEvent` was called.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_switchView(view, forceOpen = false) {
|
||||||
|
const isViewChanged = (view !== this.active);
|
||||||
let shouldForceRendering = false;
|
let shouldForceRendering = false;
|
||||||
|
|
||||||
switch (view) {
|
switch (view) {
|
||||||
|
case SidebarView.NONE:
|
||||||
|
if (this.isOpen) {
|
||||||
|
this.close();
|
||||||
|
return true; // Closing will trigger rendering and dispatch the event.
|
||||||
|
}
|
||||||
|
return false;
|
||||||
case SidebarView.THUMBS:
|
case SidebarView.THUMBS:
|
||||||
this.thumbnailButton.classList.add('toggled');
|
this.thumbnailButton.classList.add('toggled');
|
||||||
this.outlineButton.classList.remove('toggled');
|
this.outlineButton.classList.remove('toggled');
|
||||||
@ -177,7 +184,7 @@ class PDFSidebar {
|
|||||||
break;
|
break;
|
||||||
case SidebarView.OUTLINE:
|
case SidebarView.OUTLINE:
|
||||||
if (this.outlineButton.disabled) {
|
if (this.outlineButton.disabled) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
this.thumbnailButton.classList.remove('toggled');
|
this.thumbnailButton.classList.remove('toggled');
|
||||||
this.outlineButton.classList.add('toggled');
|
this.outlineButton.classList.add('toggled');
|
||||||
@ -189,7 +196,7 @@ class PDFSidebar {
|
|||||||
break;
|
break;
|
||||||
case SidebarView.ATTACHMENTS:
|
case SidebarView.ATTACHMENTS:
|
||||||
if (this.attachmentsButton.disabled) {
|
if (this.attachmentsButton.disabled) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
this.thumbnailButton.classList.remove('toggled');
|
this.thumbnailButton.classList.remove('toggled');
|
||||||
this.outlineButton.classList.remove('toggled');
|
this.outlineButton.classList.remove('toggled');
|
||||||
@ -200,9 +207,8 @@ class PDFSidebar {
|
|||||||
this.attachmentsView.classList.remove('hidden');
|
this.attachmentsView.classList.remove('hidden');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error('PDFSidebar_switchView: "' + view +
|
console.error(`PDFSidebar._switchView: "${view}" is not a valid view.`);
|
||||||
'" is an unsupported value.');
|
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.
|
||||||
@ -210,7 +216,7 @@ class PDFSidebar {
|
|||||||
|
|
||||||
if (forceOpen && !this.isOpen) {
|
if (forceOpen && !this.isOpen) {
|
||||||
this.open();
|
this.open();
|
||||||
return; // NOTE: Opening will trigger rendering, and dispatch the event.
|
return true; // Opening will trigger rendering and dispatch the event.
|
||||||
}
|
}
|
||||||
if (shouldForceRendering) {
|
if (shouldForceRendering) {
|
||||||
this._forceRendering();
|
this._forceRendering();
|
||||||
@ -219,6 +225,7 @@ class PDFSidebar {
|
|||||||
this._dispatchEvent();
|
this._dispatchEvent();
|
||||||
}
|
}
|
||||||
this._hideUINotification(this.active);
|
this._hideUINotification(this.active);
|
||||||
|
return isViewChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
open() {
|
open() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user