Skip to content

Commit fe29404

Browse files
committed
events: plotly_click, plotly_hover, plotly_unhover
* Include the mouse event in the event data returned by `plotly_click`, `plotly_hover` and `plotly_unhover`. * Keep compatibility with IE9: - try `new MouseEvent(type, evt)`, - and revert to `initMouseEvent` in case of failure.
1 parent b106b9f commit fe29404

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

src/components/dragelement/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,22 @@ dragElement.init = function init(options) {
140140
if(options.doneFn) options.doneFn(gd._dragged, numClicks, e);
141141

142142
if(!gd._dragged) {
143-
var e2 = document.createEvent('MouseEvents');
144-
e2.initEvent('click', true, true);
143+
var e2;
144+
145+
try {
146+
e2 = new MouseEvent('click', e);
147+
}
148+
catch(err) {
149+
e2 = document.createEvent('MouseEvents');
150+
e2.initMouseEvent('click',
151+
e.bubbles, e.cancelable,
152+
e.view, e.detail,
153+
e.screenX, e.screenY,
154+
e.clientX, e.clientY,
155+
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
156+
e.button, e.relatedTarget);
157+
}
158+
145159
initialTarget.dispatchEvent(e2);
146160
}
147161

src/components/dragelement/unhover.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ unhover.raw = function unhoverRaw(gd, evt) {
4444
gd._hoverdata = undefined;
4545

4646
if(evt.target && oldhoverdata) {
47-
gd.emit('plotly_unhover', {points: oldhoverdata});
47+
gd.emit('plotly_unhover', {
48+
event: evt,
49+
points: oldhoverdata
50+
});
4851
}
4952
};

src/plots/cartesian/graph_interact.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ fx.hover = function(gd, evt, subplot) {
305305
function hover(gd, evt, subplot) {
306306
if(subplot === 'pie') {
307307
gd.emit('plotly_hover', {
308+
event: evt,
308309
points: [evt]
309310
});
310311
return;
@@ -624,10 +625,14 @@ function hover(gd, evt, subplot) {
624625
if(!hoverChanged(gd, evt, oldhoverdata)) return;
625626

626627
if(oldhoverdata) {
627-
gd.emit('plotly_unhover', { points: oldhoverdata });
628+
gd.emit('plotly_unhover', {
629+
event: evt,
630+
points: oldhoverdata
631+
});
628632
}
629633

630634
gd.emit('plotly_hover', {
635+
event: evt,
631636
points: gd._hoverdata,
632637
xaxes: xaArray,
633638
yaxes: yaArray,
@@ -1350,7 +1355,7 @@ function hoverChanged(gd, evt, oldhoverdata) {
13501355
fx.click = function(gd, evt) {
13511356
var annotationsDone = Registry.getComponentMethod('annotations', 'onClick')(gd, gd._hoverdata);
13521357

1353-
function emitClick() { gd.emit('plotly_click', {points: gd._hoverdata}); }
1358+
function emitClick() { gd.emit('plotly_click', {points: gd._hoverdata, event: evt}); }
13541359

13551360
if(gd._hoverdata && evt && evt.target) {
13561361
if(annotationsDone && annotationsDone.then) {

src/traces/pie/plot.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ module.exports = function plot(gd, cdpie) {
8585
slicePath = sliceTop.selectAll('path.surface').data([pt]),
8686
hasHoverData = false;
8787

88-
function handleMouseOver(evt) {
88+
function handleMouseOver() {
89+
var evt = d3.event;
90+
8991
// in case fullLayout or fullData has changed without a replot
9092
var fullLayout2 = gd._fullLayout,
9193
trace2 = gd._fullData[trace.index],
@@ -131,8 +133,11 @@ module.exports = function plot(gd, cdpie) {
131133
hasHoverData = true;
132134
}
133135

134-
function handleMouseOut(evt) {
136+
function handleMouseOut() {
137+
var evt = d3.event;
138+
135139
gd.emit('plotly_unhover', {
140+
event: evt,
136141
points: [evt]
137142
});
138143

@@ -145,7 +150,7 @@ module.exports = function plot(gd, cdpie) {
145150
function handleClick() {
146151
gd._hoverdata = [pt];
147152
gd._hoverdata.trace = cd.trace;
148-
Fx.click(gd, { target: true });
153+
Fx.click(gd, d3.event);
149154
}
150155

151156
slicePath.enter().append('path')

0 commit comments

Comments
 (0)