|
1 | 1 | import React from "react";
|
2 | 2 |
|
3 |
| -import { drag as d3Drag } from "d3-drag"; |
4 |
| -import { forceLink as d3ForceLink } from "d3-force"; |
5 |
| -import { select as d3Select, selectAll as d3SelectAll, event as d3Event } from "d3-selection"; |
6 |
| -import { zoom as d3Zoom } from "d3-zoom"; |
| 3 | +import { |
| 4 | + forceLink as d3ForceLink, |
| 5 | + drag as d3Drag, |
| 6 | + select as d3Select, |
| 7 | + selectAll as d3SelectAll, |
| 8 | + zoom as d3Zoom, |
| 9 | +} from "d3"; |
7 | 10 |
|
8 | 11 | import CONST from "./graph.const";
|
9 | 12 | import DEFAULT_CONFIG from "./graph.config";
|
@@ -182,7 +185,10 @@ export default class Graph extends React.Component {
|
182 | 185 | .on("end", this._onDragEnd);
|
183 | 186 |
|
184 | 187 | d3Select(`#${this.state.id}-${CONST.GRAPH_WRAPPER_ID}`)
|
185 |
| - .selectAll(".node") |
| 188 | + .selectAll("g.node[id]") |
| 189 | + .datum(function() { |
| 190 | + return this.id; // The variable `this` will be equal to each of the selected `HTMLElement`s! |
| 191 | + }) |
186 | 192 | .call(customNodeDrag);
|
187 | 193 | }
|
188 | 194 |
|
@@ -228,18 +234,16 @@ export default class Graph extends React.Component {
|
228 | 234 | /**
|
229 | 235 | * Handles d3 'drag' event.
|
230 | 236 | * {@link https://github.com/d3/d3-drag/blob/master/README.md#drag_subject|more about d3 drag}
|
231 |
| - * @param {Object} ev - if not undefined it will contain event data. |
232 |
| - * @param {number} index - index of the node that is being dragged. |
233 |
| - * @param {Array.<Object>} nodeList - array of d3 nodes. This list of nodes is provided by d3, each |
234 |
| - * node contains all information that was previously fed by rd3g. |
| 237 | + * @param {Object} d3Event - `DragEvent` contains d3 event data. |
| 238 | + * @param {string} datum - The datum we configured inside, our d3Select call, |
| 239 | + * currently happening inside _graphNodeDragConfig function. |
| 240 | + * in our case the datum, will be equal to the nodeId. |
235 | 241 | * @returns {undefined}
|
236 | 242 | */
|
237 |
| - _onDragMove = (ev, index, nodeList) => { |
238 |
| - const id = nodeList[index].id; |
239 |
| - |
| 243 | + _onDragMove = (d3Event, datum) => { |
240 | 244 | if (!this.state.config.staticGraph) {
|
241 | 245 | // this is where d3 and react bind
|
242 |
| - let draggedNode = this.state.nodes[id]; |
| 246 | + let draggedNode = this.state.nodes[datum]; |
243 | 247 |
|
244 | 248 | draggedNode.oldX = draggedNode.x;
|
245 | 249 | draggedNode.oldY = draggedNode.y;
|
@@ -320,9 +324,10 @@ export default class Graph extends React.Component {
|
320 | 324 |
|
321 | 325 | /**
|
322 | 326 | * Handler for 'zoom' event within zoom config.
|
| 327 | + * @param {Object} d3Event - `ZoomEvent` contains d3 event data |
323 | 328 | * @returns {Object} returns the transformed elements within the svg graph area.
|
324 | 329 | */
|
325 |
| - _zoomed = () => { |
| 330 | + _zoomed = d3Event => { |
326 | 331 | const transform = d3Event.transform;
|
327 | 332 |
|
328 | 333 | d3SelectAll(`#${this.state.id}-${CONST.GRAPH_CONTAINER_ID}`).attr("transform", transform);
|
|
0 commit comments