Skip to content

Commit b005d2d

Browse files
Bugs with focusedNodeId (#374)
* Bugs with focusedNodeId * Update snapshot
1 parent 04a4095 commit b005d2d

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/components/graph/Graph.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ export default class Graph extends React.Component {
135135
* @returns {Object} - Focus and zoom animation properties.
136136
*/
137137
_generateFocusAnimationProps = () => {
138-
const { focusedNodeId } = this.state;
139-
140138
// In case an older animation was still not complete, clear previous timeout to ensure the new one is not cancelled
141139
if (this.state.enableFocusAnimation) {
142140
if (this.focusAnimationTimeout) {
@@ -153,7 +151,7 @@ export default class Graph extends React.Component {
153151

154152
return {
155153
style: { transitionDuration: `${transitionDuration}s` },
156-
transform: focusedNodeId ? this.state.focusTransformation : null,
154+
transform: this.state.focusTransformation,
157155
};
158156
};
159157

@@ -566,7 +564,9 @@ export default class Graph extends React.Component {
566564
const transform = newConfig.panAndZoom !== this.state.config.panAndZoom ? 1 : this.state.transform;
567565
const focusedNodeId = nextProps.data.focusedNodeId;
568566
const d3FocusedNode = this.state.d3Nodes.find(node => `${node.id}` === `${focusedNodeId}`);
569-
const focusTransformation = getCenterAndZoomTransformation(d3FocusedNode, this.state.config);
567+
const containerElId = `${this.state.id}-${CONST.GRAPH_WRAPPER_ID}`;
568+
const focusTransformation =
569+
getCenterAndZoomTransformation(d3FocusedNode, this.state.config, containerElId) || this.state.focusTransformation;
570570
const enableFocusAnimation = this.props.data.focusedNodeId !== nextProps.data.focusedNodeId;
571571

572572
// if we're given a function to call when the zoom changes, we create a debounced version of it

src/components/graph/graph.helper.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {
2525
forceSimulation as d3ForceSimulation,
2626
forceManyBody as d3ForceManyBody,
2727
} from "d3-force";
28+
import { select as d3Select } from "d3-selection";
29+
import { zoom as d3Zoom, zoomIdentity as d3ZoomIdentity } from "d3-zoom";
2830

2931
import CONST from "./graph.const";
3032
import DEFAULT_CONFIG from "./graph.config";
@@ -327,21 +329,33 @@ function checkForGraphConfigChanges(nextProps, currentState) {
327329
* selected node.
328330
* @param {Object} d3Node - node to focus the graph view on.
329331
* @param {Object} config - same as {@link #graphrenderer|config in renderGraph}.
332+
* @param {string} containerElId - ID of container element
330333
* @returns {string|undefined} transform rule to apply.
331334
* @memberof Graph/helper
332335
*/
333-
function getCenterAndZoomTransformation(d3Node, config) {
336+
function getCenterAndZoomTransformation(d3Node, config, containerElId) {
334337
if (!d3Node) {
335338
return;
336339
}
337340

338341
const { width, height, focusZoom } = config;
339342

343+
const selector = d3Select(`#${containerElId}`);
344+
345+
// in order to initialize the new position
346+
selector.call(
347+
d3Zoom().transform,
348+
d3ZoomIdentity
349+
.translate(width / 2, height / 2)
350+
.scale(focusZoom)
351+
.translate(-d3Node.x, -d3Node.y)
352+
);
353+
340354
return `
341-
translate(${width / 2}, ${height / 2})
342-
scale(${focusZoom})
343-
translate(${-d3Node.x}, ${-d3Node.y})
344-
`;
355+
translate(${width / 2}, ${height / 2})
356+
scale(${focusZoom})
357+
translate(${-d3Node.x}, ${-d3Node.y})
358+
`;
345359
}
346360

347361
/**

test/graph/__snapshots__/graph.snapshot.spec.js.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ exports[`Snapshot - Graph Component should match snapshot 1`] = `
113113
"transitionDuration": "0s",
114114
}
115115
}
116-
transform={null}
117116
>
118117
<g>
119118
<path

0 commit comments

Comments
 (0)