Skip to content

Recompute hover on click to increase click robustness #1646

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

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/components/fx/click.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@
'use strict';

var Registry = require('../../registry');
var hover = require('./hover').hover;

module.exports = function click(gd, evt) {
module.exports = function click(gd, evt, subplot) {
var annotationsDone = Registry.getComponentMethod('annotations', 'onClick')(gd, gd._hoverdata);

// fallback to fail-safe in case the plot type's hover method doesn't pass the subplot.
// Ternary, for example, didn't, but it was caught because tested.
if(subplot !== undefined) {
// The true flag at the end causes it to re-run the hover computation to figure out *which*
// point is being clicked. Without this, clicking is somewhat unreliable.
hover(gd, evt, subplot, true);
}

function emitClick() { gd.emit('plotly_click', {points: gd._hoverdata, event: evt}); }

if(gd._hoverdata && evt && evt.target) {
Expand Down
12 changes: 6 additions & 6 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var HOVERTEXTPAD = constants.HOVERTEXTPAD;
//
// We wrap the hovers in a timer, to limit their frequency.
// The actual rendering is done by private function _hover.
exports.hover = function hover(gd, evt, subplot) {
exports.hover = function hover(gd, evt, subplot, noHoverEvent) {
if(typeof gd === 'string') gd = document.getElementById(gd);
if(gd._lastHoverTime === undefined) gd._lastHoverTime = 0;

Expand All @@ -78,13 +78,13 @@ exports.hover = function hover(gd, evt, subplot) {
// Is it more than 100ms since the last update? If so, force
// an update now (synchronously) and exit
if(Date.now() > gd._lastHoverTime + constants.HOVERMINTIME) {
_hover(gd, evt, subplot);
_hover(gd, evt, subplot, noHoverEvent);
gd._lastHoverTime = Date.now();
return;
}
// Queue up the next hover for 100ms from now (if no further events)
gd._hoverTimer = setTimeout(function() {
_hover(gd, evt, subplot);
_hover(gd, evt, subplot, noHoverEvent);
gd._lastHoverTime = Date.now();
gd._hoverTimer = undefined;
}, constants.HOVERMINTIME);
Expand Down Expand Up @@ -168,8 +168,8 @@ exports.loneHover = function loneHover(hoverItem, opts) {
};

// The actual implementation is here:
function _hover(gd, evt, subplot) {
if(subplot === 'pie' || subplot === 'sankey') {
function _hover(gd, evt, subplot, noHoverEvent) {
if((subplot === 'pie' || subplot === 'sankey') && !noHoverEvent) {
gd.emit('plotly_hover', {
event: evt.originalEvent,
points: [evt]
Expand Down Expand Up @@ -504,7 +504,7 @@ function _hover(gd, evt, subplot) {
}

// don't emit events if called manually
if(!evt.target || !hoverChanged(gd, evt, oldhoverdata)) return;
if(!evt.target || noHoverEvent || !hoverChanged(gd, evt, oldhoverdata)) return;

if(oldhoverdata) {
gd.emit('plotly_unhover', {
Expand Down
2 changes: 1 addition & 1 deletion src/plots/cartesian/graph_interact.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module.exports = function initInteractions(gd) {
};

maindrag.onclick = function(evt) {
Fx.click(gd, evt);
Fx.click(gd, evt, subplot);
};

// corner draggers
Expand Down
2 changes: 1 addition & 1 deletion src/plots/ternary/ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ proto.initInteractions = function() {
};

dragger.onclick = function(evt) {
Fx.click(gd, evt);
Fx.click(gd, evt, _this.id);
};

dragElement.init(dragOptions);
Expand Down
17 changes: 17 additions & 0 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,23 @@ describe('hover info', function() {
Plotly.plot(gd, data, layout).then(done);
});

it('should skip the hover event if explicitly instructed', function(done) {
var hoverHandler = jasmine.createSpy();
gd.on('plotly_hover', hoverHandler);

var gdBB = gd.getBoundingClientRect();
var event = {clientX: gdBB.left + 300, clientY: gdBB.top + 200};

Promise.resolve().then(function() {
Fx.hover(gd, event, 'xy', true);
})
.then(function() {
expect(hoverHandler).not.toHaveBeenCalled();
})
.catch(fail)
.then(done);
});

it('should emit events only if the event looks user-driven', function(done) {
var hoverHandler = jasmine.createSpy();
gd.on('plotly_hover', hoverHandler);
Expand Down