Pass MouseEvent to the user#9
Conversation
* Include the mouse event in the event data returned by `plotly_click`, `plotly_hover` and `plotly_unhover`.
* Test event property in `plotly_click`, `plotly_hover` and `plotly_unhover`.
| if(!gd._dragged) { | ||
| var e2 = document.createEvent('MouseEvents'); | ||
| e2.initEvent('click', true, true); | ||
| var e2 = new MouseEvent('click', e); |
There was a problem hiding this comment.
Oh. I think that will break click in IE9 and even up to IE11.
I'd be ok with something like:
var e2
try {
e2 = new MouseEvent('click', e);
} catch(e) {
e2 = document.createEvent('MouseEvents');
e2.initEvent('click', true, true);
}and hence only passing MouseEvent in browsers that supports it.
|
|
||
| var evt = futureData.event; | ||
| expect(evt.clientX).toEqual(pointPos[0]); | ||
| expect(evt.clientY).toEqual(pointPos[1]); |
There was a problem hiding this comment.
Maybe we could a test case that has a ctrlKey pressed? ... to really 🔒 down the requirement,
* Keep compatibility with IE9.
| if(!gd._dragged) { | ||
| var e2 = document.createEvent('MouseEvents'); | ||
| e2.initEvent('click', true, true); | ||
| e2.initMouseEvent('click', true, true, |
There was a problem hiding this comment.
@etpinard Shall I keep canBubble and cancelable set to true? Or copy the the values from e?
There was a problem hiding this comment.
Oh, looks like initMouseEvent is deprecated. We should try using new MouseEvent first and then fallback to initMouseEvent in browsers that don't support it.
| if(opts && opts.buttons) { | ||
| fullOpts.buttons = opts.buttons; | ||
| } | ||
| if(opts && opts.altKey) { |
| expect(evt.clientX).toEqual(pointPos[0]); | ||
| expect(evt.clientY).toEqual(pointPos[1]); | ||
| Object.getOwnPropertyNames(clickOpts).forEach(function(opt) { | ||
| expect(evt[opt]).toEqual(clickOpts[opt], opt); |
| } | ||
|
|
||
| gd.emit('plotly_hover', { | ||
| event: evt, |
There was a problem hiding this comment.
Great. This will work for all subplots that call Fx.hover if I remember correctly that means: cartesian, ternary, geo and mapbox subplots. @n-riesco would you mind double checking?
Now, at the very least, we should make sure event is passed in pie chart hover data. Pie hover is a little different but shouldn't make it too hard to implement.
This leaves our gl2d, gl3d and parcoords (and polar but who cares about polar?). I'll be ok leaving them out of the first iteration. We should instead spent some time down the road merging their hover logic with Fx.hover.
There was a problem hiding this comment.
@etpinard It doesn't work for pie, ternary, geo or mapbox, because when they call to Fx.click(gd, evt) they make up an evt.
I reckon that to make it work for all these types of subplots, first I need to locate where they call Fx.click, Fx.hover and Fx.unhover and then see if it's possible to send the original event.
For example, here: why do we need to set the event to {target: true}? Is is OK if we just do Fx.click(gd, d3.event);
There was a problem hiding this comment.
Is is OK if we just do Fx.click(gd, d3.event);
That sounds ok to me, yes.
* Since `initMouseEvent` is deprecated, we try `new MouseEvent()` first.
* Added click event after mouseup, otherwise click events in pie plots don't get triggered.
|
@etpinard I still have to add tests for the geo and mapbox cases. I'll do it later in the evening. |
|
@etpinard This PR is ready for review again. |
| map.on('click', function() { | ||
| Fx.click(gd, { target: true }); | ||
| map.on('click', function(evt) { | ||
| Fx.click(gd, evt.originalEvent); |
| var Lib = require('@src/lib'); | ||
| var Drawing = require('@src/components/drawing'); | ||
| var DBLCLICKDELAY = require('@src/constants/interactions').DBLCLICKDELAY; | ||
| var HOVERMINTIME = 50; |
There was a problem hiding this comment.
Let's require this value instead of hard-coding it.
| }); | ||
|
|
||
|
|
||
| describe('Test click interactions on a pie plot:', function() { |
There was a problem hiding this comment.
While at it, @n-riesco would be mind resolving:
- onClick for pies slices doesn't return any trace info plotly/plotly.js#1456 and
- Custom hover event for pie chart fail when hoverinfo set to 'none' plotly/plotly.js#902
This should be pretty easy. Thanks in advance! ✅
| }); | ||
|
|
||
|
|
||
| describe('Test click interactions on a geo plot:', function() { |
There was a problem hiding this comment.
| }); | ||
|
|
||
|
|
||
| describe('Test click interactions on a pie plot:', function() { |
There was a problem hiding this comment.
Also, can you move these cases to https://github.com/plotly/plotly.js/blob/master/test/jasmine/tests/hover_pie_test.js
I like separating our test cases per subplot types.
| }); | ||
|
|
||
|
|
||
| describe('Test click interactions on a ternary plot:', function() { |
There was a problem hiding this comment.
|
|
||
| var mouseEvent = require('../assets/mouse_event'); | ||
| var click = require('../assets/click'); | ||
| var HOVERMINTIME = 50; |
There was a problem hiding this comment.
(repeat) require from cartesian contants.js
| }); | ||
|
|
||
|
|
||
| describe('@noCI Test plotly events on a scattermapbox plot:', function() { |
|
@n-riesco This PR looks great! PR away in the main repo whenever you're ready 🚀 |
* To document the need for an if-block.
|
@etpinard I'll address plotly#1456 and plotly#902 tomorrow. I'll also squash some of the commits before opening a PR in the main repo. |
* Test that 'plotly_hover' is triggered when `hoverinfo` is set to `"none"`.
* Test that 'plotly_click' doesn't emit an undefined trace.

This PR addresses plotly#988 for scatter plots.
cc/ @etpinard