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

Bugs with focusedNodeId #374

Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/components/graph/Graph.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,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 @@ -151,7 +149,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 @@ -538,7 +536,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 @@ -326,21 +328,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