Re-factor the "outlineloaded"/"attachmentsloaded" event handlers in 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).
This commit is contained in:
Jonas Jenwald 2020-08-04 18:29:14 +02:00
parent 6e9da55a39
commit 54fc7058e4

View File

@ -430,32 +430,28 @@ class PDFSidebar {
}); });
// Disable/enable views. // Disable/enable views.
this.eventBus._on("outlineloaded", evt => { const onTreeLoaded = (count, button, view) => {
const outlineCount = evt.outlineCount; button.disabled = !count;
this.outlineButton.disabled = !outlineCount; if (count) {
this._showUINotification(view);
if (outlineCount) { } else if (this.active === view) {
this._showUINotification(SidebarView.OUTLINE); // If the `view` was opened by the user during document load,
} else if (this.active === SidebarView.OUTLINE) { // switch away from it if it turns out to be empty.
// If the outline view was opened during document load, switch away
// from it if it turns out that the document has no outline.
this.switchView(SidebarView.THUMBS); this.switchView(SidebarView.THUMBS);
} }
};
this.eventBus._on("outlineloaded", evt => {
onTreeLoaded(evt.outlineCount, this.outlineButton, SidebarView.OUTLINE);
}); });
this.eventBus._on("attachmentsloaded", evt => { this.eventBus._on("attachmentsloaded", evt => {
const attachmentsCount = evt.attachmentsCount; onTreeLoaded(
evt.attachmentsCount,
this.attachmentsButton.disabled = !attachmentsCount; this.attachmentsButton,
SidebarView.ATTACHMENTS
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);
}
}); });
// Update the thumbnailViewer, if visible, when exiting presentation mode. // Update the thumbnailViewer, if visible, when exiting presentation mode.