Reduce duplication when registering event listeners for the Toolbar buttons

This uses the same kind of format as is being used in the `SecondaryToolbar` class.
This commit is contained in:
Jonas Jenwald 2019-11-15 23:19:35 +01:00
parent be02e67972
commit 3edaebbded

View File

@ -56,12 +56,34 @@ class Toolbar {
this.toolbar = options.container; this.toolbar = options.container;
this.eventBus = eventBus; this.eventBus = eventBus;
this.l10n = l10n; this.l10n = l10n;
this.items = options; this.buttons = [
{ element: options.previous, eventName: 'previouspage', },
{ element: options.next, eventName: 'nextpage', },
{ element: options.zoomIn, eventName: 'zoomin', },
{ element: options.zoomOut, eventName: 'zoomout', },
{ element: options.openFile, eventName: 'openfile', },
{ element: options.print, eventName: 'print', },
{ element: options.presentationModeButton,
eventName: 'presentationmode', },
{ element: options.download, eventName: 'download', },
{ element: options.viewBookmark, eventName: null, },
];
this.items = {
numPages: options.numPages,
pageNumber: options.pageNumber,
scaleSelectContainer: options.scaleSelectContainer,
scaleSelect: options.scaleSelect,
customScaleOption: options.customScaleOption,
previous: options.previous,
next: options.next,
zoomIn: options.zoomIn,
zoomOut: options.zoomOut,
};
this._wasLocalized = false; this._wasLocalized = false;
this.reset(); this.reset();
// Bind the event listeners for click and hand tool actions. // Bind the event listeners for click and various other actions.
this._bindListeners(); this._bindListeners();
} }
@ -95,66 +117,41 @@ class Toolbar {
} }
_bindListeners() { _bindListeners() {
let { eventBus, items, } = this; const { pageNumber, scaleSelect, } = this.items;
let self = this; const self = this;
items.previous.addEventListener('click', function() { // The buttons within the toolbar.
eventBus.dispatch('previouspage', { source: self, }); for (const { element, eventName, } of this.buttons) {
}); element.addEventListener('click', (evt) => {
if (eventName !== null) {
items.next.addEventListener('click', function() { this.eventBus.dispatch(eventName, { source: this, });
eventBus.dispatch('nextpage', { source: self, }); }
}); });
}
items.zoomIn.addEventListener('click', function() { // The non-button elements within the toolbar.
eventBus.dispatch('zoomin', { source: self, }); pageNumber.addEventListener('click', function() {
});
items.zoomOut.addEventListener('click', function() {
eventBus.dispatch('zoomout', { source: self, });
});
items.pageNumber.addEventListener('click', function() {
this.select(); this.select();
}); });
pageNumber.addEventListener('change', function() {
items.pageNumber.addEventListener('change', function() { self.eventBus.dispatch('pagenumberchanged', {
eventBus.dispatch('pagenumberchanged', {
source: self, source: self,
value: this.value, value: this.value,
}); });
}); });
items.scaleSelect.addEventListener('change', function() { scaleSelect.addEventListener('change', function() {
if (this.value === 'custom') { if (this.value === 'custom') {
return; return;
} }
eventBus.dispatch('scalechanged', { self.eventBus.dispatch('scalechanged', {
source: self, source: self,
value: this.value, value: this.value,
}); });
}); });
items.presentationModeButton.addEventListener('click', function() {
eventBus.dispatch('presentationmode', { source: self, });
});
items.openFile.addEventListener('click', function() {
eventBus.dispatch('openfile', { source: self, });
});
items.print.addEventListener('click', function() {
eventBus.dispatch('print', { source: self, });
});
items.download.addEventListener('click', function() {
eventBus.dispatch('download', { source: self, });
});
// Suppress context menus for some controls. // Suppress context menus for some controls.
items.scaleSelect.oncontextmenu = noContextMenuHandler; scaleSelect.oncontextmenu = noContextMenuHandler;
eventBus.on('localized', () => { this.eventBus.on('localized', () => {
this._localized(); this._localized();
}); });
} }