Skip to content

Commit 4e6d84a

Browse files
authored
fix: pinch events (#905)
1 parent c53861b commit 4e6d84a

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

src/hammer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ function handlePinch(chart, state, e) {
6969
function startPinch(chart, state, event) {
7070
if (state.options.zoom.pinch.enabled) {
7171
const point = getRelativePosition(event, chart);
72-
call(state.options.zoom.onZoomStart, [{chart, event, point}]);
73-
state.scale = 1;
72+
if (call(state.options.zoom.onZoomStart, [{chart, event, point}]) === false) {
73+
state.scale = null;
74+
call(state.options.zoom.onZoomRejected, [{chart, event}]);
75+
} else {
76+
state.scale = 1;
77+
}
7478
}
7579
}
7680

test/specs/zoom.pinch.spec.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
describe('pinch', () => {
2+
const data = {
3+
datasets: [{
4+
data: [{
5+
x: 1,
6+
y: 3
7+
}, {
8+
x: 2,
9+
y: 2
10+
}, {
11+
x: 3,
12+
y: 1
13+
}]
14+
}]
15+
};
16+
17+
describe('events', () => {
18+
it('should call onZoomStart', function(done) {
19+
const startSpy = jasmine.createSpy('started');
20+
const chart = window.acquireChart({
21+
type: 'scatter',
22+
data,
23+
options: {
24+
plugins: {
25+
zoom: {
26+
zoom: {
27+
mode: 'xy',
28+
onZoomStart: startSpy,
29+
pinch: {
30+
enabled: true,
31+
},
32+
},
33+
}
34+
}
35+
}
36+
});
37+
38+
Simulator.gestures.pinch(chart.canvas, {pos: [chart.width / 2, chart.height / 2]}, function() {
39+
expect(startSpy).toHaveBeenCalled();
40+
expect(chart.scales.x.min).not.toBe(1);
41+
done();
42+
});
43+
});
44+
45+
it('should call onZoomRejected when onStartZoom returns false', function(done) {
46+
const rejectSpy = jasmine.createSpy('rejected');
47+
const chart = window.acquireChart({
48+
type: 'scatter',
49+
data,
50+
options: {
51+
plugins: {
52+
zoom: {
53+
zoom: {
54+
mode: 'xy',
55+
onZoomStart: () => false,
56+
onZoomRejected: rejectSpy,
57+
pinch: {
58+
enabled: true,
59+
}
60+
}
61+
}
62+
}
63+
}
64+
});
65+
66+
Simulator.gestures.pinch(chart.canvas, {}, function() {
67+
expect(rejectSpy).toHaveBeenCalled();
68+
expect(chart.scales.x.min).toBe(1);
69+
done();
70+
});
71+
});
72+
73+
it('should call onZoomComplete', function(done) {
74+
const chart = window.acquireChart({
75+
type: 'scatter',
76+
data,
77+
options: {
78+
plugins: {
79+
zoom: {
80+
zoom: {
81+
mode: 'xy',
82+
onZoomComplete(ctx) {
83+
expect(ctx.chart.scales.x.min).not.toBe(1);
84+
done();
85+
},
86+
pinch: {
87+
enabled: true,
88+
},
89+
},
90+
}
91+
}
92+
}
93+
});
94+
Simulator.gestures.pinch(chart.canvas, {});
95+
});
96+
97+
});
98+
});

0 commit comments

Comments
 (0)