Skip to content

Commit fe76a86

Browse files
authored
Merge pull request #2997 from jonfunkhouser/fxhover_touchevents
DragElement: initialize clientX and clientY for touch events
2 parents 96950d5 + fca6f9b commit fe76a86

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/components/dragelement/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ dragElement.init = function init(options) {
137137
initialEvent = e;
138138
rightClick = e.buttons === 2 || e.ctrlKey;
139139

140+
// fix Fx.hover for touch events
141+
if(typeof e.clientX === 'undefined' && typeof e.clientY === 'undefined') {
142+
e.clientX = startX;
143+
e.clientY = startY;
144+
}
145+
140146
newMouseDownTime = (new Date()).getTime();
141147
if(newMouseDownTime - gd._mouseDownTime < DBLCLICKDELAY) {
142148
// in a click train

test/jasmine/tests/hover_label_test.js

+55
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var click = require('../assets/click');
1313
var delay = require('../assets/delay');
1414
var doubleClick = require('../assets/double_click');
1515
var failTest = require('../assets/fail_test');
16+
var touchEvent = require('../assets/touch_event');
1617

1718
var customAssertions = require('../assets/custom_assertions');
1819
var assertHoverLabelStyle = customAssertions.assertHoverLabelStyle;
@@ -21,6 +22,20 @@ var assertElemRightTo = customAssertions.assertElemRightTo;
2122
var assertElemTopsAligned = customAssertions.assertElemTopsAligned;
2223
var assertElemInside = customAssertions.assertElemInside;
2324

25+
function touch(path, options) {
26+
var len = path.length;
27+
Lib.clearThrottle();
28+
touchEvent('touchstart', path[0][0], path[0][1], options);
29+
30+
path.slice(1, len).forEach(function(pt) {
31+
Lib.clearThrottle();
32+
touchEvent('touchmove', pt[0], pt[1], options);
33+
});
34+
35+
touchEvent('touchend', path[len - 1][0], path[len - 1][1], options);
36+
return;
37+
}
38+
2439
describe('hover info', function() {
2540
'use strict';
2641

@@ -2503,3 +2518,43 @@ describe('hovermode defaults to', function() {
25032518
.then(done);
25042519
});
25052520
});
2521+
2522+
2523+
describe('touch devices', function() {
2524+
afterEach(destroyGraphDiv);
2525+
2526+
['pan', 'zoom'].forEach(function(type) {
2527+
describe('dragmode:' + type, function() {
2528+
var data = [{x: [1, 2, 3], y: [1, 3, 2], type: 'bar'}];
2529+
var layout = {width: 600, height: 400, dragmode: type};
2530+
var gd;
2531+
2532+
beforeEach(function(done) {
2533+
gd = createGraphDiv();
2534+
Plotly.plot(gd, data, layout).then(done);
2535+
});
2536+
2537+
it('emits click events', function(done) {
2538+
var hoverHandler = jasmine.createSpy('hover');
2539+
var clickHandler = jasmine.createSpy('click');
2540+
gd.on('plotly_hover', hoverHandler);
2541+
gd.on('plotly_click', clickHandler);
2542+
2543+
var gdBB = gd.getBoundingClientRect();
2544+
var touchPoint = [[gdBB.left + 300, gdBB.top + 200]];
2545+
2546+
Promise.resolve()
2547+
.then(function() {
2548+
touch(touchPoint);
2549+
})
2550+
.then(delay(HOVERMINTIME * 1.1))
2551+
.then(function() {
2552+
expect(clickHandler).toHaveBeenCalled();
2553+
expect(hoverHandler).not.toHaveBeenCalled();
2554+
})
2555+
.catch(failTest)
2556+
.then(done);
2557+
});
2558+
});
2559+
});
2560+
});

0 commit comments

Comments
 (0)