Skip to content

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

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open

Pass MouseEvent to the user #9

wants to merge 27 commits into from

Conversation

n-riesco
Copy link
Owner

This PR addresses plotly#988 for scatter plots.

cc/ @etpinard

* 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`.
@@ -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);

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.

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?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etpinard How about initMouseEvent?

MDN
MSDN


var evt = futureData.event;
expect(evt.clientX).toEqual(pointPos[0]);
expect(evt.clientY).toEqual(pointPos[1]);
Copy link

@etpinard etpinard Mar 16, 2017

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,

@@ -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,
Copy link
Owner Author

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?

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.

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@@ -9,6 +9,18 @@ module.exports = function(type, x, y, opts) {
if(opts && opts.buttons) {
fullOpts.buttons = opts.buttons;
}
if(opts && opts.altKey) {

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);

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,

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.

Copy link
Owner Author

@n-riesco n-riesco Mar 16, 2017

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);

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.

@n-riesco
Copy link
Owner Author

@etpinard I still have to add tests for the geo and mapbox cases. I'll do it later in the evening.

@n-riesco
Copy link
Owner Author

@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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice here. Thanks!

@@ -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;

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.

@@ -817,6 +928,588 @@ describe('Test click interactions:', function() {
});
});


describe('Test click interactions on a pie plot:', function() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});


describe('Test click interactions on a geo plot:', function() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -817,6 +928,588 @@ describe('Test click interactions:', function() {
});
});


describe('Test click interactions on a pie plot:', function() {

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.

});


describe('Test click interactions on a ternary plot:', function() {

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;

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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks!

@etpinard
Copy link

@n-riesco This PR looks great! PR away in the main repo whenever you're ready 🚀

@n-riesco
Copy link
Owner Author

@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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants