Skip to content

Commit 5907af0

Browse files
click to reset to chromosome only works upon click on text
1 parent 5158014 commit 5907af0

1 file changed

Lines changed: 52 additions & 30 deletions

File tree

src/components/tracksLegendPanel/legend-multi-brush.js

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class LegendMultiBrush extends Component {
6262
this.state = {
6363
hoveredChromo: null,
6464
};
65+
this.prevOverlayCursor = null;
6566
}
6667

6768
createDefaults(domain) {
@@ -286,46 +287,66 @@ class LegendMultiBrush extends Component {
286287
// Add global click handler on SVG to detect chromosome clicks based on X position (overlay-friendly)
287288
d3.select(this.container).on("click", (event) => {
288289
const { chromoBins } = this.props;
289-
const [mouseX] = d3.pointer(event, this.container);
290-
291-
const stageX = mouseX - margins.legend.padding;
292-
if (stageX < 0 || stageX > this.stageWidth) return;
293-
294-
// Find the chromosome under this X
295-
const clickedChromo = Object.values(chromoBins).find((d) => {
296-
const start = this.genomeScale(d.startPlace);
297-
const width =
298-
this.genomeScale(d.endPlace) - this.genomeScale(d.startPlace);
299-
return stageX >= start && stageX <= start + width;
300-
});
301-
290+
// Temporarily disable brush overlays/selections to see what is under the cursor
291+
const overlays = Array.from(
292+
this.container.querySelectorAll(".overlay, .selection")
293+
);
294+
const prevPointers = overlays.map((n) => n.style.pointerEvents);
295+
overlays.forEach((n) => (n.style.pointerEvents = "none"));
296+
const hitElement = document.elementFromPoint(
297+
event.clientX,
298+
event.clientY
299+
);
300+
overlays.forEach((n, i) => (n.style.pointerEvents = prevPointers[i]));
301+
302+
const clickedChromo =
303+
hitElement && chromoBins[d3.select(hitElement).attr("id")];
302304
if (clickedChromo) {
303305
this.handleChromosomeClick(clickedChromo);
306+
return;
304307
}
305308
});
306309

307310
d3.select(this.container).on("mousemove", (event) => {
308311
const { chromoBins } = this.props;
309-
const [mouseX] = d3.pointer(event, this.container);
310312

311-
const stageX = mouseX - margins.legend.padding;
312-
if (stageX < 0 || stageX > this.stageWidth) {
313-
if (this.state.hoveredChromo !== null) {
314-
this.setState({ hoveredChromo: null });
315-
}
316-
return;
313+
// Temporarily disable brush overlays/selections to see what is under the cursor
314+
const overlays = Array.from(
315+
this.container.querySelectorAll(".overlay, .selection")
316+
);
317+
const prevPointers = overlays.map((n) => n.style.pointerEvents);
318+
overlays.forEach((n) => (n.style.pointerEvents = "none"));
319+
const hitElement = document.elementFromPoint(
320+
event.clientX,
321+
event.clientY
322+
);
323+
overlays.forEach((n, i) => (n.style.pointerEvents = prevPointers[i]));
324+
325+
const hitSel = d3.select(hitElement);
326+
const chromoId = hitSel.attr("id");
327+
const newHover =
328+
chromoId && hitSel?.classed("chromo-text")
329+
? chromoBins[chromoId]?.chromosome || null
330+
: null;
331+
332+
if (this.state.hoveredChromo !== newHover) {
333+
this.setState({ hoveredChromo: newHover });
317334
}
318335

319-
const hovered = Object.values(chromoBins).find((d) => {
320-
const start = this.genomeScale(d.startPlace);
321-
const width =
322-
this.genomeScale(d.endPlace) - this.genomeScale(d.startPlace);
323-
return stageX >= start && stageX <= start + width;
324-
});
325-
326-
const current = hovered ? hovered.chromosome : null;
327-
if (this.state.hoveredChromo !== current) {
328-
this.setState({ hoveredChromo: current });
336+
if (overlays.length) {
337+
if (newHover) {
338+
overlays.forEach((overlayNode) => {
339+
if (this.prevOverlayCursor === null) {
340+
this.prevOverlayCursor = overlayNode.style.cursor || "";
341+
}
342+
overlayNode.style.cursor = "pointer";
343+
});
344+
} else {
345+
overlays.forEach((overlayNode) => {
346+
overlayNode.style.cursor = this.prevOverlayCursor || "";
347+
});
348+
this.prevOverlayCursor = null;
349+
}
329350
}
330351
});
331352
}
@@ -422,6 +443,7 @@ class LegendMultiBrush extends Component {
422443
{...margins.chromoBox}
423444
/>
424445
<text
446+
id={d.chromosome}
425447
className={`chromo-text${
426448
hoveredChromo === d.chromosome
427449
? " chromosome-highlighted"

0 commit comments

Comments
 (0)