-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Correctly assign click event target in shadow dom #7357
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e435fdf
Correctly assign click event target in shadow dom
alexcjohnson f7c3279
draftlog for 7357
alexcjohnson fe62eb8
loosen click_test slightly so it passes locally
alexcjohnson e48bc1f
test click events in shadow dom
alexcjohnson 89d6404
Merge branch 'master' into alex/shadow-click
alexcjohnson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Fix click event handling for plots in shadow DOM elements [[#7357](https://github.com/plotly/plotly.js/pull/7357)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
module.exports = function createShadowGraphDiv() { | ||
var container = document.createElement('div'); | ||
container.id = 'shadowcontainer'; | ||
document.body.appendChild(container); | ||
var root = container.attachShadow({mode: 'open'}); | ||
var gd = document.createElement('div'); | ||
gd.id = 'graph2'; | ||
root.appendChild(gd); | ||
|
||
// force the shadow container to be at position 0,0 no matter what | ||
container.style.position = 'fixed'; | ||
container.style.left = 0; | ||
container.style.top = 0; | ||
|
||
var style = document.createElement('style'); | ||
root.appendChild(style); | ||
|
||
for (var plotlyStyle of document.querySelectorAll('[id^="plotly.js-"]')) { | ||
for (var rule of plotlyStyle.sheet.rules) { | ||
style.sheet.insertRule(rule.cssText); | ||
} | ||
} | ||
return gd; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
'use strict'; | ||
|
||
module.exports = function destroyGraphDiv() { | ||
var gd = document.getElementById('graph'); | ||
|
||
if(gd) document.body.removeChild(gd); | ||
// remove both plain graphs and shadow DOM graph containers | ||
['graph', 'shadowcontainer'].forEach(function(id) { | ||
var el = document.getElementById(id); | ||
if(el) document.body.removeChild(el); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ var DBLCLICKDELAY = require('../../../src/plot_api/plot_config').dfltConfig.doub | |
var d3Select = require('../../strict-d3').select; | ||
var d3SelectAll = require('../../strict-d3').selectAll; | ||
var createGraphDiv = require('../assets/create_graph_div'); | ||
var createShadowGraphDiv = require('../assets/create_shadow_graph_div'); | ||
var destroyGraphDiv = require('../assets/destroy_graph_div'); | ||
|
||
var mouseEvent = require('../assets/mouse_event'); | ||
|
@@ -1059,17 +1060,17 @@ describe('Test click interactions:', function() { | |
width: 600, | ||
height: 600 | ||
}).then(function() { | ||
expect(gd.layout.xaxis.range).toBeCloseToArray([1, 2.068]); | ||
expect(gd.layout.xaxis.range).toBeCloseToArray([1, 2.068], 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I slightly loosened this test, then this whole file passed locally. I had to look it up, apparently "1" as the second arg means a range of 10^-1, ie a span of 0.1, ie +/- 0.05, whereas the default is +/- 0.005 and I was getting errors of ~0.007. |
||
|
||
return doubleClick(300, 300); | ||
}) | ||
.then(function() { | ||
expect(gd.layout.xaxis.range).toBeCloseToArray([-0.2019, 3.249]); | ||
expect(gd.layout.xaxis.range).toBeCloseToArray([-0.2019, 3.249], 1); | ||
|
||
return doubleClick(300, 300); | ||
}) | ||
.then(function() { | ||
expect(gd.layout.xaxis.range).toBeCloseToArray([1, 2.068]); | ||
expect(gd.layout.xaxis.range).toBeCloseToArray([1, 2.068], 1); | ||
}) | ||
.then(done, done.fail); | ||
}); | ||
|
@@ -1164,6 +1165,56 @@ describe('Test click interactions:', function() { | |
}); | ||
}); | ||
|
||
describe('Click events in Shadow DOM', function() { | ||
afterEach(destroyGraphDiv); | ||
|
||
function fig() { | ||
var x = []; | ||
var y = []; | ||
for (var i = 0; i <= 20; i++) { | ||
for (var j = 0; j <= 20; j++) { | ||
x.push(i); | ||
y.push(j); | ||
} | ||
} | ||
return { | ||
data: [{x: x, y: y, mode: 'markers'}], | ||
layout: { | ||
width: 400, | ||
height: 400, | ||
margin: {l: 100, r: 100, t: 100, b: 100}, | ||
xaxis: {range: [0, 20]}, | ||
yaxis: {range: [0, 20]}, | ||
} | ||
}; | ||
} | ||
|
||
it('should select the same point in regular and shadow DOM', function(done) { | ||
var clickData; | ||
var clickX = 120; | ||
var clickY = 150; | ||
var expectedX = 2; // counts up 1 every 10px from 0 at 100px | ||
var expectedY = 15; // counts down 1 every 10px from 20 at 100px | ||
|
||
function check(gd) { | ||
gd.on('plotly_click', function(event) { clickData = event; }); | ||
click(clickX, clickY); | ||
expect(clickData.points.length).toBe(1); | ||
var pt = clickData.points[0]; | ||
expect(pt.x).toBe(expectedX); | ||
expect(pt.y).toBe(expectedY); | ||
clickData = null; | ||
} | ||
|
||
Plotly.newPlot(createGraphDiv(), fig()) | ||
.then(check) | ||
.then(destroyGraphDiv) | ||
.then(function() { return Plotly.newPlot(createShadowGraphDiv(), fig()) }) | ||
.then(check) | ||
.then(done, done.fail); | ||
}); | ||
}); | ||
|
||
|
||
describe('dragbox', function() { | ||
afterEach(destroyGraphDiv); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice cleanup. 🏆