Skip to content

Commit

Permalink
Merge pull request #2997 from jonfunkhouser/fxhover_touchevents
Browse files Browse the repository at this point in the history
DragElement: initialize clientX and clientY for touch events
  • Loading branch information
etpinard authored Sep 18, 2018
2 parents 96950d5 + fca6f9b commit fe76a86
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/dragelement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ dragElement.init = function init(options) {
initialEvent = e;
rightClick = e.buttons === 2 || e.ctrlKey;

// fix Fx.hover for touch events
if(typeof e.clientX === 'undefined' && typeof e.clientY === 'undefined') {
e.clientX = startX;
e.clientY = startY;
}

newMouseDownTime = (new Date()).getTime();
if(newMouseDownTime - gd._mouseDownTime < DBLCLICKDELAY) {
// in a click train
Expand Down
55 changes: 55 additions & 0 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var click = require('../assets/click');
var delay = require('../assets/delay');
var doubleClick = require('../assets/double_click');
var failTest = require('../assets/fail_test');
var touchEvent = require('../assets/touch_event');

var customAssertions = require('../assets/custom_assertions');
var assertHoverLabelStyle = customAssertions.assertHoverLabelStyle;
Expand All @@ -21,6 +22,20 @@ var assertElemRightTo = customAssertions.assertElemRightTo;
var assertElemTopsAligned = customAssertions.assertElemTopsAligned;
var assertElemInside = customAssertions.assertElemInside;

function touch(path, options) {
var len = path.length;
Lib.clearThrottle();
touchEvent('touchstart', path[0][0], path[0][1], options);

path.slice(1, len).forEach(function(pt) {
Lib.clearThrottle();
touchEvent('touchmove', pt[0], pt[1], options);
});

touchEvent('touchend', path[len - 1][0], path[len - 1][1], options);
return;
}

describe('hover info', function() {
'use strict';

Expand Down Expand Up @@ -2503,3 +2518,43 @@ describe('hovermode defaults to', function() {
.then(done);
});
});


describe('touch devices', function() {
afterEach(destroyGraphDiv);

['pan', 'zoom'].forEach(function(type) {
describe('dragmode:' + type, function() {
var data = [{x: [1, 2, 3], y: [1, 3, 2], type: 'bar'}];
var layout = {width: 600, height: 400, dragmode: type};
var gd;

beforeEach(function(done) {
gd = createGraphDiv();
Plotly.plot(gd, data, layout).then(done);
});

it('emits click events', function(done) {
var hoverHandler = jasmine.createSpy('hover');
var clickHandler = jasmine.createSpy('click');
gd.on('plotly_hover', hoverHandler);
gd.on('plotly_click', clickHandler);

var gdBB = gd.getBoundingClientRect();
var touchPoint = [[gdBB.left + 300, gdBB.top + 200]];

Promise.resolve()
.then(function() {
touch(touchPoint);
})
.then(delay(HOVERMINTIME * 1.1))
.then(function() {
expect(clickHandler).toHaveBeenCalled();
expect(hoverHandler).not.toHaveBeenCalled();
})
.catch(failTest)
.then(done);
});
});
});
});

0 comments on commit fe76a86

Please sign in to comment.