Skip to content

Commit

Permalink
Re-factor the "outlineloaded"/"attachmentsloaded" event handlers in `…
Browse files Browse the repository at this point in the history
…PDFSidebar`

With recent changes, these event handlers are now essentially identical. Hence a new helper function is added, to reduce unnecessary duplication (will also be helpful with upcoming changes).
  • Loading branch information
Snuffleupagus committed Aug 4, 2020
1 parent a91654b commit d089058
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions web/pdf_sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,32 +430,28 @@ class PDFSidebar {
});

// Disable/enable views.
this.eventBus._on("outlineloaded", evt => {
const outlineCount = evt.outlineCount;

this.outlineButton.disabled = !outlineCount;

if (outlineCount) {
this._showUINotification(SidebarView.OUTLINE);
} else if (this.active === SidebarView.OUTLINE) {
// If the outline view was opened during document load, switch away
// from it if it turns out that the document has no outline.
const onTreeLoaded = (count, button, view) => {
button.disabled = !count;

if (count) {
this._showUINotification(view);
} else if (this.active === view) {
// If the `view` was opened by the user during document load,
// switch away from it if it turns out to be empty.
this.switchView(SidebarView.THUMBS);
}
};

this.eventBus._on("outlineloaded", evt => {
onTreeLoaded(evt.outlineCount, this.outlineButton, SidebarView.OUTLINE);
});

this.eventBus._on("attachmentsloaded", evt => {
const attachmentsCount = evt.attachmentsCount;

this.attachmentsButton.disabled = !attachmentsCount;

if (attachmentsCount) {
this._showUINotification(SidebarView.ATTACHMENTS);
} else if (this.active === SidebarView.ATTACHMENTS) {
// If the attachments view was opened during document load, switch away
// from it if it turns out that the document has no attachments.
this.switchView(SidebarView.THUMBS);
}
onTreeLoaded(
evt.attachmentsCount,
this.attachmentsButton,
SidebarView.ATTACHMENTS
);
});

// Update the thumbnailViewer, if visible, when exiting presentation mode.
Expand Down

0 comments on commit d089058

Please sign in to comment.