Skip to content

Commit 756c898

Browse files
authored
Fix calling of onZoomComplete from wheel event (#485)
* Fix calling of onZoomComplete from wheel event * CC * Add test * CC2 * Remove some unused code
1 parent 9a8426e commit 756c898

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

src/handlers.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ export function mouseUp(chart, event) {
9999
}
100100

101101
export function wheel(chart, event) {
102-
const {options: {zoom: zoomOptions}} = getState(chart);
103-
const {wheelModifierKey, onZoomRejected, onZoomComplete} = zoomOptions;
102+
const {handlers: {onZoomComplete}, options: {zoom: zoomOptions}} = getState(chart);
103+
const {wheelModifierKey, onZoomRejected} = zoomOptions;
104104

105105
// Before preventDefault, check if the modifier key required and pressed
106106
if (wheelModifierKey && !event[wheelModifierKey + 'Key']) {
@@ -114,7 +114,7 @@ export function wheel(chart, event) {
114114

115115
// Firefox always fires the wheel event twice:
116116
// First without the delta and right after that once with the delta properties.
117-
if (typeof event.deltaY === 'undefined') {
117+
if (event.deltaY === undefined) {
118118
return;
119119
}
120120

@@ -132,20 +132,26 @@ export function wheel(chart, event) {
132132
zoom(chart, amount);
133133

134134
if (onZoomComplete) {
135-
debounce(() => call(onZoomComplete, [{chart}]), 250);
135+
onZoomComplete();
136+
}
137+
}
138+
139+
function addDebouncedHandler(chart, name, handler, delay) {
140+
if (handler) {
141+
getState(chart).handlers[name] = debounce(() => call(handler, [{chart}]), delay);
136142
}
137143
}
138144

139145
export function addListeners(chart, options) {
140146
const canvas = chart.canvas;
147+
const {enabled: zoomEnabled, drag: dragEnabled, onZoomComplete} = options.zoom;
141148

142149
// Install listeners. Do this dynamically based on options so that we can turn zoom on and off
143150
// We also want to make sure listeners aren't always on. E.g. if you're scrolling down a page
144151
// and the mouse goes over a chart you don't want it intercepted unless the plugin is enabled
145-
const zoomEnabled = options.zoom && options.zoom.enabled;
146-
const dragEnabled = options.zoom.drag;
147152
if (zoomEnabled && !dragEnabled) {
148153
addHandler(chart, canvas, 'wheel', wheel);
154+
addDebouncedHandler(chart, 'onZoomComplete', onZoomComplete, 250);
149155
} else {
150156
removeHandler(chart, canvas, 'wheel');
151157
}

src/utils.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@ export function directionEnabled(mode, dir, chart) {
2727
export function debounce(fn, delay) {
2828
let timeout;
2929
return function() {
30-
if (delay) {
31-
clearTimeout(timeout);
32-
timeout = setTimeout(fn, delay);
33-
} else {
34-
fn();
35-
}
30+
clearTimeout(timeout);
31+
timeout = setTimeout(fn, delay);
3632
return delay;
3733
};
3834
}

test/specs/zoom.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,30 @@ describe('zoom', function() {
396396
});
397397
});
398398
});
399+
400+
describe('onZoomComplete', function() {
401+
it('should be called', function(done) {
402+
const chart = window.acquireChart({
403+
type: 'scatter',
404+
data,
405+
options: {
406+
plugins: {
407+
zoom: {
408+
zoom: {
409+
enabled: true,
410+
mode: 'xy',
411+
onZoomComplete: done
412+
}
413+
}
414+
}
415+
}
416+
});
417+
const wheelEv = {
418+
x: chart.scales.x.getPixelForValue(1.5),
419+
y: chart.scales.y.getPixelForValue(1.1),
420+
deltaY: 1
421+
};
422+
jasmine.triggerWheelEvent(chart, wheelEv);
423+
});
424+
});
399425
});

0 commit comments

Comments
 (0)