Skip to content

Commit 359d50a

Browse files
committed
adds click emission; adds test cases; uses hasWebGLSupport
1 parent c4baeb2 commit 359d50a

File tree

3 files changed

+185
-2
lines changed

3 files changed

+185
-2
lines changed

src/plots/gl2d/scene2d.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ function Scene2D(options, fullLayout) {
5858
outerFill: true
5959
});
6060

61+
// last button state
62+
this.lastButtonState = 0;
63+
6164
// last pick result
6265
this.pickResult = null;
6366

@@ -463,8 +466,11 @@ proto.draw = function() {
463466
var glplot = this.glplot,
464467
camera = this.camera,
465468
mouseListener = camera.mouseListener,
469+
mouseUp = this.lastButtonState === 1 && mouseListener.buttons === 0,
466470
fullLayout = this.fullLayout;
467471

472+
this.lastButtonState = mouseListener.buttons;
473+
468474
this.cameraChanged();
469475

470476
var x = mouseListener.x * glplot.pixelRatio;
@@ -494,8 +500,23 @@ proto.draw = function() {
494500
(y / glplot.pixelRatio) - (size.t + (1 - domainY[1]) * size.h)
495501
);
496502

503+
var nextSelection = result && result.object._trace.handlePick(result);
504+
505+
if(nextSelection && mouseUp) {
506+
this.graphDiv.emit('plotly_click', {
507+
points: [{
508+
trace: nextSelection.trace,
509+
dataCoord: nextSelection.dataCoord.slice(),
510+
traceCoord: nextSelection.traceCoord.slice(),
511+
textLabel: nextSelection.textLabel,
512+
color: nextSelection.color,
513+
name: nextSelection.name,
514+
hoverinfo: nextSelection.hoverinfo
515+
}]
516+
});
517+
}
518+
497519
if(result && result.object._trace.hoverinfo !== 'skip' && fullLayout.hovermode) {
498-
var nextSelection = result.object._trace.handlePick(result);
499520

500521
if(nextSelection && (
501522
!this.lastPickResult ||

test/jasmine/assets/timed_click.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var mouseEvent = require('./mouse_event');
2+
3+
module.exports = function click(x, y) {
4+
mouseEvent('mousemove', x, y, {buttons: 0});
5+
6+
window.setTimeout(function() {
7+
8+
mouseEvent('mousedown', x, y, {buttons: 1});
9+
10+
window.setTimeout(function() {
11+
12+
mouseEvent('mouseup', x, y, {buttons: 0});
13+
14+
}, 50);
15+
16+
}, 150);
17+
};

test/jasmine/tests/gl2d_click_test.js

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ var Lib = require('@src/lib');
44
var createGraphDiv = require('../assets/create_graph_div');
55
var destroyGraphDiv = require('../assets/destroy_graph_div');
66
var customMatchers = require('../assets/custom_matchers');
7+
var hasWebGLSupport = require('../assets/has_webgl_support');
78

89
// cartesian click events events use the hover data
910
// from the mousemove events and then simulate
1011
// a click event on mouseup
11-
// var click = require('../assets/click');
12+
var click = require('../assets/timed_click');
1213
var hover = require('../assets/hover');
1314

1415
describe('Test click interactions:', function() {
16+
17+
if(!hasWebGLSupport('gl2d_click_test')) return;
18+
1519
var mock = require('@mocks/gl2d_14.json');
1620

1721
var mockCopy, gd;
@@ -97,6 +101,41 @@ describe('Test click interactions:', function() {
97101
}));
98102

99103

104+
});
105+
106+
it('event happens even on a click interaction', function(done) {
107+
108+
var modifiedMockCopy = Lib.extendDeep({}, mockCopy);
109+
modifiedMockCopy.data[0].hoverinfo = 'none';
110+
111+
Plotly.plot(gd, modifiedMockCopy.data, modifiedMockCopy.layout)
112+
113+
.then(new Promise(function() {
114+
115+
gd.on('plotly_hover', function(data) {
116+
futureData = data;
117+
});
118+
119+
click(654.7712871743302, 316.97670766680994);
120+
121+
window.setTimeout(function() {
122+
123+
expect(futureData.points.length).toEqual(1);
124+
125+
var pt = futureData.points[0];
126+
127+
expect(Object.keys(pt)).toEqual([
128+
'trace', 'dataCoord', 'traceCoord', 'textLabel', 'color',
129+
'name', 'hoverinfo', 'screenCoord'
130+
]);
131+
132+
expect(pt.traceCoord).toEqual([15.772, 0.387]);
133+
134+
done();
135+
}, 250);
136+
}));
137+
138+
100139
});
101140

102141
it('unhover happens', function(done) {
@@ -143,4 +182,110 @@ describe('Test click interactions:', function() {
143182

144183

145184
});
185+
186+
describe('click event is fired on click', function() {
187+
var futureData;
188+
189+
it('in general', function(done) {
190+
191+
var modifiedMockCopy = Lib.extendDeep({}, mockCopy);
192+
193+
Plotly.plot(gd, modifiedMockCopy.data, modifiedMockCopy.layout)
194+
195+
.then(new Promise(function() {
196+
197+
gd.on('plotly_click', function(data) {
198+
futureData = data;
199+
});
200+
201+
click(654.7712871743302, 316.97670766680994);
202+
203+
window.setTimeout(function() {
204+
expect(futureData.points[0].traceCoord).toEqual([15.772, 0.387]);
205+
206+
done();
207+
208+
}, 350);
209+
}));
210+
211+
});
212+
213+
it('even when hoverinfo (== plotly tooltip) is set to none', function(done) {
214+
215+
var modifiedMockCopy = Lib.extendDeep({}, mockCopy);
216+
modifiedMockCopy.data[0].hoverinfo = 'none';
217+
218+
Plotly.plot(gd, modifiedMockCopy.data, modifiedMockCopy.layout)
219+
220+
.then(new Promise(function() {
221+
222+
gd.on('plotly_hover', function(data) {
223+
futureData = data;
224+
});
225+
226+
hover(654.7712871743302, 316.97670766680994);
227+
228+
window.setTimeout(function() {
229+
230+
expect(futureData.points.length).toEqual(1);
231+
232+
var pt = futureData.points[0];
233+
234+
expect(Object.keys(pt)).toEqual([
235+
'trace', 'dataCoord', 'traceCoord', 'textLabel', 'color',
236+
'name', 'hoverinfo', 'screenCoord'
237+
]);
238+
239+
expect(pt.traceCoord).toEqual([15.772, 0.387]);
240+
241+
done();
242+
}, 250);
243+
}));
244+
245+
246+
});
247+
248+
it('unhover happens', function(done) {
249+
250+
var modifiedMockCopy = Lib.extendDeep({}, mockCopy);
251+
modifiedMockCopy.data[0].hoverinfo = 'none';
252+
253+
Plotly.plot(gd, modifiedMockCopy.data, modifiedMockCopy.layout)
254+
255+
.then(new Promise(function() {
256+
257+
futureData = undefined;
258+
259+
gd.on('plotly_unhover', function() {
260+
futureData = 'emitted plotly_unhover';
261+
});
262+
263+
hover(654.7712871743302, 316.97670766680994);
264+
265+
// fairly realistic simulation of moving with the cursor
266+
window.setTimeout(function() {
267+
268+
var x = 654, y = 316; // we start here
269+
var canceler = window.setInterval(function() {
270+
hover(x--, y++); // move the cursor
271+
}, 10);
272+
273+
window.setTimeout(function() {
274+
window.clearInterval(canceler); // stop the mouse at some point
275+
}, 250);
276+
277+
window.setTimeout(function() {
278+
279+
expect(futureData).toEqual('emitted plotly_unhover');
280+
281+
done();
282+
283+
}, 250);
284+
285+
}, 250);
286+
}));
287+
288+
});
289+
290+
});
146291
});

0 commit comments

Comments
 (0)