|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import React from "react"; |
| 17 | +import React, { useEffect } from "react"; |
18 | 18 | import PropTypes from "prop-types"; |
19 | 19 | import SVG from "react-inlinesvg"; |
20 | | -import { get } from "lodash"; |
21 | 20 |
|
22 | | -import { CardNode, CardNodeColumn, |
23 | | - CardNodeSubtitle, |
24 | | - CardNodeTitle |
25 | | -} from "@carbon/charts-react"; |
| 21 | +import { CardNode, CardNodeColumn, CardNodeTitle, CardNodeSubtitle } from "@carbon/charts-react"; |
| 22 | +import { IMAGE_STYLES } from "./react-nodes-carbon-constants"; |
26 | 23 |
|
27 | | -class CardNodeWrapper extends React.Component { |
| 24 | +const CardNodeWrapper = ({ nodeData, pipelineData, canvasController }) => { |
28 | 25 |
|
29 | | - componentDidMount() { |
30 | | - this.setNodeSize(); |
31 | | - } |
| 26 | + const divRef = React.createRef(); |
32 | 27 |
|
33 | | - componentDidUpdate() { |
34 | | - this.setNodeSize(); |
35 | | - } |
| 28 | + const { label, description, op, type, image } = nodeData; |
| 29 | + const { color, shape } = nodeData?.app_data?.react_nodes_data || {}; |
36 | 30 |
|
37 | | - // Sets the node size (which will set the containing foreignObject size) based on display size |
38 | | - // of the Card Wrapper. A slight pause is required to allow the Card Wrapper contents to resolve |
39 | | - // to their ultimate display size. |
40 | | - setNodeSize() { |
41 | | - setTimeout(() => { |
42 | | - const node = this.props.canvasController.getNode(this.props.nodeData.id, this.props.pipelineData.id); |
43 | | - |
44 | | - if (!node.isResized && this.divRef && |
45 | | - (this.divRef.current.scrollHeight > this.divRef.current.clientHeight || |
46 | | - this.divRef.current.scrollWidth > this.divRef.current.clientWidth)) { |
47 | | - node.isResized = true; |
48 | | - node.resizeHeight = this.divRef.current.scrollHeight + 10; |
49 | | - node.resizeWidth = this.divRef.current.scrollWidth; |
50 | | - this.props.canvasController.setNodeProperties(this.props.nodeData.id, node, this.props.pipelineData.id); |
51 | | - } |
52 | | - }, 100); |
53 | | - } |
| 31 | + const imageOverride = image === "useDefaultIcon" ? "images/custom-canvases/flows/palette/icons/supernode.svg" : image; |
54 | 32 |
|
| 33 | + const isSuperNodeAndExpanded = type === "super_node" && nodeData.is_expanded; |
55 | 34 |
|
56 | | - render() { |
57 | | - const styleImage = { height: "24px", width: "24px", y: 0 }; |
58 | | - |
59 | | - const image = this.props.nodeData.image === "useDefaultIcon" |
60 | | - ? "images/custom-canvases/flows/palette/icons/supernode.svg" |
61 | | - : this.props.nodeData.image; |
62 | | - |
63 | | - if (this.props.nodeData.type === "super_node" && |
64 | | - this.props.nodeData.is_expanded) { |
65 | | - // The SVG area that shows the sub-flow will overlay this Card Node that |
66 | | - // forms the background for the supernode. |
67 | | - return ( |
68 | | - <div className={"card-node"}> |
69 | | - <CardNode> |
70 | | - <CardNodeColumn> |
71 | | - <SVG src={image} style={styleImage} /> |
72 | | - </CardNodeColumn> |
73 | | - <CardNodeColumn> |
74 | | - <CardNodeTitle>{this.props.nodeData.label}</CardNodeTitle> |
75 | | - </CardNodeColumn> |
76 | | - </CardNode> |
77 | | - </div> |
78 | | - ); |
79 | | - } |
80 | | - |
81 | | - // Read attributes from the nodeData, in the pipeline flow JSON file, |
82 | | - // for the node we are displaying. |
83 | | - const type = get(this, "props.nodeData.op"); // Derive the type from the operator |
84 | | - const color = get(this, "props.nodeData.app_data.react_nodes_data.color"); |
85 | | - const shape = get(this, "props.nodeData.app_data.react_nodes_data.shape"); |
86 | | - |
87 | | - // Set the classes based on the node attributes. |
88 | | - let className = "card-node"; |
89 | | - className += type === "card-node-with-outline" ? " card-node-outline-div" : ""; |
| 35 | + let className = "card-node"; |
| 36 | + if (!isSuperNodeAndExpanded) { |
| 37 | + className += op === "card-node-with-outline" ? " card-node-outline-div" : ""; |
90 | 38 | className += shape === "curved-corners" ? " card-node-curved-corners" : ""; |
91 | | - |
92 | | - this.divRef = React.createRef(); |
93 | | - |
94 | | - return ( |
95 | | - <div className={className} ref={this.divRef}> |
96 | | - <CardNode color={color}> |
97 | | - <CardNodeColumn> |
98 | | - <SVG src={image} style={styleImage} /> |
99 | | - </CardNodeColumn> |
100 | | - <CardNodeColumn> |
101 | | - <CardNodeTitle>{this.props.nodeData.label}</CardNodeTitle> |
102 | | - <CardNodeSubtitle>{this.props.nodeData.description}</CardNodeSubtitle> |
103 | | - </CardNodeColumn> |
104 | | - </CardNode> |
105 | | - </div> |
106 | | - ); |
107 | 39 | } |
108 | | -} |
| 40 | + |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + const setNodeSize = () => { |
| 44 | + setTimeout(() => { |
| 45 | + const node = canvasController.getNode(nodeData.id, pipelineData.id); |
| 46 | + |
| 47 | + if (!node.isResized && divRef.current && |
| 48 | + (divRef.current.scrollHeight > divRef.current.clientHeight || |
| 49 | + divRef.current.scrollWidth > divRef.current.clientWidth)) { |
| 50 | + node.isResized = true; |
| 51 | + node.resizeHeight = divRef.current.scrollHeight + 10; |
| 52 | + node.resizeWidth = divRef.current.scrollWidth; |
| 53 | + canvasController.setNodeProperties(nodeData.id, node, pipelineData.id); |
| 54 | + } |
| 55 | + }, 100); |
| 56 | + }; |
| 57 | + |
| 58 | + setNodeSize(); |
| 59 | + |
| 60 | + }, [nodeData, pipelineData, canvasController]); |
| 61 | + |
| 62 | + return ( |
| 63 | + <div className={className} ref={divRef}> |
| 64 | + <CardNode color={color}> |
| 65 | + <CardNodeColumn> |
| 66 | + <SVG src={imageOverride} style={IMAGE_STYLES} /> |
| 67 | + </CardNodeColumn> |
| 68 | + <CardNodeColumn> |
| 69 | + <CardNodeTitle>{label}</CardNodeTitle> |
| 70 | + {!isSuperNodeAndExpanded && <CardNodeSubtitle>{description}</CardNodeSubtitle>} |
| 71 | + </CardNodeColumn> |
| 72 | + </CardNode> |
| 73 | + </div> |
| 74 | + ); |
| 75 | +}; |
109 | 76 |
|
110 | 77 | CardNodeWrapper.propTypes = { |
111 | 78 | nodeData: PropTypes.object, |
|
0 commit comments