Skip to content
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

Fix geo plot hover interaction for Firefox #5607

Merged
Merged
Show file tree
Hide file tree
Changes from 10 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
37 changes: 37 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,18 @@ lib.isIOS = function() {
return IS_IOS_REGEX.test(window.navigator.userAgent);
};

var FIREFOX_VERSION_REGEX = /Firefox\/(\d+)\.\d+/;
lib.getFirefoxVersion = function() {
var match = FIREFOX_VERSION_REGEX.exec(window.navigator.userAgent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears regex.exec was not used in any other places of our code.
Wondering is it is possible to convert this regex.exec to a regex.test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not, as regex.test does only return true or false, but we need to evaluate the exact version of Firefox in the user agent string.
However, exec has the same compatibility as test: see mdn

if(match && match.length === 2) {
var versionInt = parseInt(match[1]);
if(!isNaN(versionInt)) {
return versionInt;
}
}
return null;
};

lib.isD3Selection = function(obj) {
return obj instanceof d3.selection;
};
Expand Down Expand Up @@ -1267,3 +1279,28 @@ lib.join2 = function(arr, mainSeparator, lastSeparator) {
lib.bigFont = function(size) {
return Math.round(1.2 * size);
};

/**
* Return the mouse position from the last event registered by D3.
* @returns An array with two numbers, representing the x and y coordinates of the mouse pointer
* at the event relative to the targeted node.
*/
lib.getPositionFromD3Event = function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking suggestion to avoid if statement inside the function:

lib.getPositionFromD3Event = isProblematicFirefox ? 
    function() {
        // layerX and layerY are non-standard, so we only fallback to them when we have to:
        return [
            d3.event.layerX,
            d3.event.layerY
        ];
    } : function() {
        return [
            d3.event.offsetX,
            d3.event.offsetY
        ];
    };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I find ternary operators way harder to read - but I'd say that's just personal preference in the end :D

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the point of @archmoj's comment was not so much about whether to use if or a ternary operator, but pulling the conditional out of the function and into the initial script evaluation, for better performance. I agree that ternaries can be confusing when used on such large expressions, but we can do something like:

if (isProblematicFirefox) {
    lib.getPositionFromD3Event = function() {...}
} else {
    lib.getPositionFromD3Event = function() {...}
}

var firefoxVersion = lib.getFirefoxVersion();

// see https://bugzilla.mozilla.org/show_bug.cgi?id=1684973
var isProblematicFirefox = firefoxVersion && firefoxVersion < 86;

if(isProblematicFirefox) {
// layerX and layerY are non-standard, so we only fallback to them when we have to:
return [
d3.event.layerX,
d3.event.layerY
];
} else {
return [
d3.event.offsetX,
d3.event.offsetY
];
}
};
2 changes: 1 addition & 1 deletion src/plots/geo/geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ proto.updateFx = function(fullLayout, geoLayout) {
}

bgRect.on('mousemove', function() {
var lonlat = _this.projection.invert(d3.mouse(this));
var lonlat = _this.projection.invert(Lib.getPositionFromD3Event());

if(!lonlat || isNaN(lonlat[0]) || isNaN(lonlat[1])) {
return dragElement.unhover(gd, d3.event);
Expand Down
23 changes: 14 additions & 9 deletions test/jasmine/tests/choropleth_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ describe('Test choropleth hover:', function() {

function run(hasCssTransform, pos, fig, content, style) {
gd = createGraphDiv();
var scale = 1;

style = style || {
bgcolor: 'rgb(68, 68, 68)',
Expand All @@ -177,14 +176,8 @@ describe('Test choropleth hover:', function() {
fontFamily: 'Arial'
};

return Plotly.newPlot(gd, fig)
.then(function() {
if(hasCssTransform) {
scale = 0.5;
transformPlot(gd, 'translate(-25%, -25%) scale(0.5)');
}

mouseEvent('mousemove', scale * pos[0], scale * pos[1]);
function assertHoverLabel(posX, posY) {
mouseEvent('mousemove', posX, posY);
assertHoverLabelContent({
nums: content[0],
name: content[1]
Expand All @@ -193,6 +186,18 @@ describe('Test choropleth hover:', function() {
d3Select('g.hovertext'),
style
);
}

return Plotly.newPlot(gd, fig)
.then(function() {
if(hasCssTransform) {
transformPlot(gd, 'translate3d(10px, 10px, 0) scale(1)');
assertHoverLabel(pos[0] + 10, pos[1] + 10);
transformPlot(gd, 'translate(-25%, -25%) scale(0.5)');
assertHoverLabel(0.5 * pos[0], 0.5 * pos[1]);
} else {
assertHoverLabel(pos[0], pos[1]);
}
});
}

Expand Down