Skip to content

Commit

Permalink
Move hover fixes into new fx code
Browse files Browse the repository at this point in the history
  • Loading branch information
rreusser committed May 29, 2017
1 parent a0f8333 commit 0acdea4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
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

0 comments on commit 0acdea4

Please sign in to comment.