Skip to content

Commit

Permalink
Re-attach event handlers only when needed (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Dec 8, 2021
1 parent b1d2245 commit bb6dee3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ function removeHandler(chart, type) {

function addHandler(chart, target, type, handler) {
const {handlers, options} = getState(chart);
const oldHandler = handlers[type];
if (oldHandler && oldHandler.target === target) {
// already attached
return;
}
removeHandler(chart, type);
handlers[type] = (event) => handler(chart, event, options);
handlers[type].target = target;
Expand Down
36 changes: 36 additions & 0 deletions test/specs/zoom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,42 @@ describe('zoom', function() {
expect(chart.scales.x.min).not.toBe(1);
});

it('should detect configuration change', function() {
const startSpy = jasmine.createSpy('started');
const chart = window.acquireChart({
type: 'scatter',
data,
options: {
plugins: {
zoom: {
zoom: {
wheel: {
enabled: false,
},
mode: 'xy',
onZoomStart: startSpy
}
}
}
}
});
const wheelEv = {
x: chart.scales.x.getPixelForValue(1.5),
y: chart.scales.y.getPixelForValue(1.1),
deltaY: 1
};
jasmine.triggerWheelEvent(chart, wheelEv);
expect(startSpy).not.toHaveBeenCalled();
expect(chart.scales.x.min).toBe(1);

chart.options.plugins.zoom.zoom.wheel.enabled = true;
chart.update();

jasmine.triggerWheelEvent(chart, wheelEv);
expect(startSpy).toHaveBeenCalled();
expect(chart.scales.x.min).not.toBe(1);
});

it('should call onZoomRejected when onZoomStart returns false', function() {
const rejectSpy = jasmine.createSpy('rejected');
const chart = window.acquireChart({
Expand Down

0 comments on commit bb6dee3

Please sign in to comment.