1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 30x 26x 26x 26x 26x 26x 4x 4x 4x 4x 1x 3x 3x 1x 1x 1x 2x 1x 2x 3x 2x 2x 1x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 4x 4x 3x 3x 4x 2x 2x 50x 48x 2x 1x 1x 50x 3x 47x 1x 46x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 2x 45x 46x 46x 2x 44x 44x 44x 44x 44x | // @flow /* Copyright(c) 2018 Uber Technologies, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import * as d3 from 'd3'; import * as React from 'react'; // This works in Typescript but causes an import loop for Flowtype. We'll just use `any` below. // import { type LayoutEngine } from '../utilities/layout-engine/layout-engine-config'; import Edge from './edge'; import GraphUtils from './graph-util'; import NodeText from './node-text'; export type INode = { title: string; x?: number | null; y?: number | null; type?: string; subtype?: string | null; [key: string]: any; }; type INodeProps = { data: INode; index: number; id: string; nodeTypes: any; // TODO: make a nodeTypes interface nodeSubtypes: any; // TODO: make a nodeSubtypes interface opacity?: number; nodeKey: string; nodeSize?: number; onNodeMouseEnter: (event: any, data: any, hovered: boolean) => void; onNodeMouseLeave: (event: any, data: any) => void; onNodeMove: (point: IPoint, index: number, shiftKey: boolean) => void; onNodeSelected: (data: any, index: number, shiftKey: boolean) => void; onNodeUpdate: (point: IPoint, index: number, shiftKey: boolean) => void; renderNode?: ( nodeRef: any, data: any, index: number, selected: boolean, hovered: boolean ) => any; renderNodeText?: (data: any, index: number, id: string | number, isSelected: boolean) => any; isSelected: boolean; layoutEngine?: any; }; type INodeState = { hovered: boolean; x: number; y: number; selected: boolean; mouseDown: boolean; }; export type IPoint = { x: number; y: number; }; class Node extends React.Component<INodeProps, INodeState> { static defaultProps = { isSelected: false, nodeSize: 154, onNodeMouseEnter: () => { return; }, onNodeMouseLeave: () => { return; }, onNodeMove: () => { return; }, onNodeSelected: () => { return; }, onNodeUpdate: () => { return; } }; static getDerivedStateFromProps(nextProps: INodeProps, prevState: INodeState) { return { selected: nextProps.isSelected, x: nextProps.data.x || 0, y: nextProps.data.y || 0 }; } nodeRef: any; oldSibling: any; constructor(props: INodeProps) { super(props); this.state = { hovered: false, mouseDown: false, selected: false, x: props.data.x || 0, y: props.data.y || 0 }; this.nodeRef = React.createRef(); } componentDidMount() { const dragFunction = d3 .drag() .on('drag', this.handleMouseMove) .on('start', this.handleDragStart) .on('end', this.handleDragEnd); d3 .select(this.nodeRef.current) .on('mouseout', this.handleMouseOut) .call(dragFunction); } handleMouseMove = () => { const mouseButtonDown = d3.event.sourceEvent.buttons === 1; const shiftKey = d3.event.sourceEvent.shiftKey; const { nodeSize, layoutEngine, nodeKey } = this.props; if (!mouseButtonDown) { return; } // While the mouse is down, this function handles all mouse movement const newState = { x: d3.event.x, y: d3.event.y }; if (shiftKey) { // draw edge // undo the target offset subtraction done by Edge const off = Edge.calculateOffset(nodeSize, this.props.data, newState, nodeKey); newState.x += off.xOff; newState.y += off.yOff; // now tell the graph that we're actually drawing an edge } else { // move node if (layoutEngine) { Object.assign(newState, layoutEngine.getPositionForNode(newState)); } this.setState(newState); } this.props.onNodeMove(newState, this.props.index, shiftKey); } handleDragStart = () => { Iif (!this.nodeRef.current) { return; } if (!this.oldSibling) { this.oldSibling = this.nodeRef.current.parentElement.nextSibling; } // Moves child to the end of the element stack to re-arrange the z-index this.nodeRef.current.parentElement.parentElement.appendChild(this.nodeRef.current.parentElement); } handleDragEnd = () => { Iif (!this.nodeRef.current) { return; } if (this.oldSibling && this.oldSibling.parentElement) { this.oldSibling.parentElement.insertBefore(this.nodeRef.current.parentElement, this.oldSibling); } this.setState({ mouseDown: false }); const { x, y } = this.state; const { data, index } = this.props; const shiftKey = d3.event.sourceEvent.shiftKey; this.props.onNodeUpdate( { x, y }, index, shiftKey ); this.props.onNodeSelected(data, index, shiftKey); } handleMouseOver = (event: any) => { // Detect if mouse is already down and do nothing. let hovered = false; if ((d3.event && d3.event.buttons !== 1) || (event && event.buttons !== 1)) { hovered = true; this.setState({ hovered }); } this.props.onNodeMouseEnter(event, this.props.data, hovered); } handleMouseOut = (event: any) => { // Detect if mouse is already down and do nothing. Sometimes the system lags on // drag and we don't want the mouseOut to fire while the user is moving the // node around this.setState({ hovered: false }); this.props.onNodeMouseLeave(event, this.props.data); } static getNodeTypeXlinkHref(data: INode, nodeTypes: any) { if (data.type && nodeTypes[data.type]) { return nodeTypes[data.type].shapeId; } else if (nodeTypes.emptyNode) { return nodeTypes.emptyNode.shapeId; } return null; } static getNodeSubtypeXlinkHref(data: INode, nodeSubtypes?: any) { if (data.subtype && nodeSubtypes && nodeSubtypes[data.subtype]) { return nodeSubtypes[data.subtype].shapeId; } else if (nodeSubtypes && nodeSubtypes.emptyNode) { return nodeSubtypes.emptyNode.shapeId; } return null; } renderShape() { const { renderNode, data, index, nodeTypes, nodeSubtypes } = this.props; const { hovered, selected } = this.state; const props = { height: this.props.nodeSize || 0, width: this.props.nodeSize || 0 }; const nodeShapeContainerClassName = GraphUtils.classNames('shape'); const nodeClassName = GraphUtils.classNames('node', { selected, hovered }); const nodeSubtypeClassName = GraphUtils.classNames('subtype-shape', { selected: this.state.selected }); const nodeTypeXlinkHref = Node.getNodeTypeXlinkHref(data, nodeTypes) || ''; const nodeSubtypeXlinkHref = Node.getNodeSubtypeXlinkHref(data, nodeSubtypes) || ''; // get width and height defined on def element const defSvgNodeElement: any = nodeTypeXlinkHref ? document.querySelector(`defs>${nodeTypeXlinkHref}`) : null; const nodeWidthAttr = defSvgNodeElement ? defSvgNodeElement.getAttribute('width') : 0; const nodeHeightAttr = defSvgNodeElement ? defSvgNodeElement.getAttribute('height') : 0; props.width = nodeWidthAttr ? parseInt(nodeWidthAttr, 10) : props.width; props.height = nodeHeightAttr ? parseInt(nodeHeightAttr, 10) : props.height; if (renderNode) { // Originally: graphView, domNode, datum, index, elements. return renderNode(this.nodeRef, data, index, selected, hovered); } else { return ( <g className={nodeShapeContainerClassName} {...props}> {!!data.subtype && ( <use data-index={index} className={nodeSubtypeClassName} x={-props.width / 2} y={-props.height / 2} width={props.width} height={props.height} xlinkHref={nodeSubtypeXlinkHref} /> )} <use data-index={index} className={nodeClassName} x={-props.width / 2} y={-props.height / 2} width={props.width} height={props.height} xlinkHref={nodeTypeXlinkHref} /> </g> ); } } renderText() { const { data, index, id, nodeTypes, renderNodeText, isSelected } = this.props; if (renderNodeText) { return renderNodeText(data, index, id, isSelected); } return (<NodeText data={data} nodeTypes={nodeTypes} isSelected={this.state.selected} />); } render() { const { x, y } = this.state; const { opacity, id, data } = this.props; const className = GraphUtils.classNames('node', data.type, { hovered: this.state.hovered, selected: this.state.selected }); return ( <g className={className} onMouseOver={this.handleMouseOver} onMouseOut={this.handleMouseOut} id={id} ref={this.nodeRef} opacity={opacity} transform={`translate(${x}, ${y})`} > {this.renderShape()} {this.renderText()} </g> ); } } export default Node; |