Skip to content

Commit

Permalink
Bugs with focusedNodeId (#374)
Browse files Browse the repository at this point in the history
* Bugs with focusedNodeId

* Update snapshot
  • Loading branch information
TranquilMarmot authored Dec 18, 2020
1 parent 04a4095 commit b005d2d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/components/graph/Graph.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ export default class Graph extends React.Component {
* @returns {Object} - Focus and zoom animation properties.
*/
_generateFocusAnimationProps = () => {
const { focusedNodeId } = this.state;

// In case an older animation was still not complete, clear previous timeout to ensure the new one is not cancelled
if (this.state.enableFocusAnimation) {
if (this.focusAnimationTimeout) {
Expand All @@ -153,7 +151,7 @@ export default class Graph extends React.Component {

return {
style: { transitionDuration: `${transitionDuration}s` },
transform: focusedNodeId ? this.state.focusTransformation : null,
transform: this.state.focusTransformation,
};
};

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

// if we're given a function to call when the zoom changes, we create a debounced version of it
Expand Down
24 changes: 19 additions & 5 deletions src/components/graph/graph.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
forceSimulation as d3ForceSimulation,
forceManyBody as d3ForceManyBody,
} from "d3-force";
import { select as d3Select } from "d3-selection";
import { zoom as d3Zoom, zoomIdentity as d3ZoomIdentity } from "d3-zoom";

import CONST from "./graph.const";
import DEFAULT_CONFIG from "./graph.config";
Expand Down Expand Up @@ -327,21 +329,33 @@ function checkForGraphConfigChanges(nextProps, currentState) {
* selected node.
* @param {Object} d3Node - node to focus the graph view on.
* @param {Object} config - same as {@link #graphrenderer|config in renderGraph}.
* @param {string} containerElId - ID of container element
* @returns {string|undefined} transform rule to apply.
* @memberof Graph/helper
*/
function getCenterAndZoomTransformation(d3Node, config) {
function getCenterAndZoomTransformation(d3Node, config, containerElId) {
if (!d3Node) {
return;
}

const { width, height, focusZoom } = config;

const selector = d3Select(`#${containerElId}`);

// in order to initialize the new position
selector.call(
d3Zoom().transform,
d3ZoomIdentity
.translate(width / 2, height / 2)
.scale(focusZoom)
.translate(-d3Node.x, -d3Node.y)
);

return `
translate(${width / 2}, ${height / 2})
scale(${focusZoom})
translate(${-d3Node.x}, ${-d3Node.y})
`;
translate(${width / 2}, ${height / 2})
scale(${focusZoom})
translate(${-d3Node.x}, ${-d3Node.y})
`;
}

/**
Expand Down
1 change: 0 additions & 1 deletion test/graph/__snapshots__/graph.snapshot.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ exports[`Snapshot - Graph Component should match snapshot 1`] = `
"transitionDuration": "0s",
}
}
transform={null}
>
<g>
<path
Expand Down

0 comments on commit b005d2d

Please sign in to comment.