-
Notifications
You must be signed in to change notification settings - Fork 0
Pass MouseEvent to the user #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
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`.
src/components/dragelement/index.js
Outdated
@@ -140,8 +140,7 @@ dragElement.init = function init(options) { | |||
if(options.doneFn) options.doneFn(gd._dragged, numClicks, e); | |||
|
|||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@n-riesco what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
var evt = futureData.event; | ||
expect(evt.clientX).toEqual(pointPos[0]); | ||
expect(evt.clientY).toEqual(pointPos[1]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could a test case that has a ctrlKey
pressed? ... to really 🔒 down the requirement,
* Keep compatibility with IE9.
src/components/dragelement/index.js
Outdated
@@ -141,7 +141,12 @@ dragElement.init = function init(options) { | |||
|
|||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@etpinard Shall I keep canBubble
and cancelable
set to true
? Or copy the the values from e
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -9,6 +9,18 @@ module.exports = function(type, x, y, opts) { | |||
if(opts && opts.buttons) { | |||
fullOpts.buttons = opts.buttons; | |||
} | |||
if(opts && opts.altKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Thanks!
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
} | ||
|
||
gd.emit('plotly_hover', { | ||
event: evt, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice here. Thanks!
test/jasmine/tests/click_test.js
Outdated
@@ -2,6 +2,7 @@ var Plotly = require('@lib/index'); | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's require this value instead of hard-coding it.
test/jasmine/tests/click_test.js
Outdated
@@ -817,6 +928,588 @@ describe('Test click interactions:', function() { | |||
}); | |||
}); | |||
|
|||
|
|||
describe('Test click interactions on a pie plot:', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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! ✅
test/jasmine/tests/click_test.js
Outdated
}); | ||
|
||
|
||
describe('Test click interactions on a geo plot:', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test/jasmine/tests/click_test.js
Outdated
@@ -817,6 +928,588 @@ describe('Test click interactions:', function() { | |||
}); | |||
}); | |||
|
|||
|
|||
describe('Test click interactions on a pie plot:', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
test/jasmine/tests/click_test.js
Outdated
}); | ||
|
||
|
||
describe('Test click interactions on a ternary plot:', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -9,6 +9,21 @@ var createGraphDiv = require('../assets/create_graph_div'); | |||
var destroyGraphDiv = require('../assets/destroy_graph_div'); | |||
var customMatchers = require('../assets/custom_matchers'); | |||
|
|||
var mouseEvent = require('../assets/mouse_event'); | |||
var click = require('../assets/click'); | |||
var HOVERMINTIME = 50; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(repeat) require from cartesian contants.js
@@ -600,3 +615,204 @@ describe('@noCI scattermapbox hover', function() { | |||
}); | |||
}); | |||
}); | |||
|
|||
|
|||
describe('@noCI Test plotly events on a scattermapbox plot:', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Thanks!
@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