forked from patternfly/react-topology
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultGroupExpanded.tsx
More file actions
231 lines (217 loc) · 7.59 KB
/
Copy pathDefaultGroupExpanded.tsx
File metadata and controls
231 lines (217 loc) · 7.59 KB
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
import * as React from 'react';
import { observer } from 'mobx-react';
import { polygonHull } from 'd3-polygon';
import * as _ from 'lodash';
import { css } from '@patternfly/react-styles';
import styles from '../../css/topology-components';
import CollapseIcon from '@patternfly/react-icons/dist/esm/icons/compress-alt-icon';
import NodeLabel from '../nodes/labels/NodeLabel';
import { Layer } from '../layers';
import { GROUPS_LAYER } from '../../const';
import { hullPath, maxPadding, useCombineRefs, useHover } from '../../utils';
import { BadgeLocation, isGraph, Node, NodeShape, NodeStyle, PointTuple } from '../../types';
import {
useDragNode,
useSvgAnchor,
WithContextMenuProps,
WithDndDropProps,
WithDragNodeProps,
WithSelectionProps
} from '../../behavior';
import { CollapsibleGroupProps } from './types';
import Rect from '../../geom/Rect';
type DefaultGroupExpandedProps = {
className?: string;
element: Node;
droppable?: boolean;
canDrop?: boolean;
dropTarget?: boolean;
dragging?: boolean;
hover?: boolean;
label?: string; // Defaults to element.getLabel()
secondaryLabel?: string;
showLabel?: boolean; // Defaults to true
truncateLength?: number; // Defaults to 13
badge?: string;
badgeColor?: string;
badgeTextColor?: string;
badgeBorderColor?: string;
badgeClassName?: string;
badgeLocation?: BadgeLocation;
labelIconClass?: string; // Icon to show in label
labelIcon?: string;
labelIconPadding?: number;
hulledOutline?: boolean;
} & CollapsibleGroupProps & WithDragNodeProps & WithSelectionProps & WithDndDropProps & WithContextMenuProps;
type PointWithSize = [number, number, number];
// Return the point whose Y is the largest value.
// If multiple points are found, compute the center X between them
// export for testing only
export function computeLabelLocation(points: PointWithSize[]): PointWithSize {
let lowPoints: PointWithSize[];
const threshold = 5;
_.forEach(points, p => {
const delta = !lowPoints ? Infinity : Math.round(p[1]) - Math.round(lowPoints[0][1]);
if (delta > threshold) {
lowPoints = [p];
} else if (Math.abs(delta) <= threshold) {
lowPoints.push(p);
}
});
return [
(_.minBy(lowPoints, p => p[0])[0] + _.maxBy(lowPoints, p => p[0])[0]) / 2,
lowPoints[0][1],
// use the max size value
_.maxBy(lowPoints, p => p[2])[2]
];
}
const DefaultGroupExpanded: React.FunctionComponent<DefaultGroupExpandedProps> = ({
className,
element,
collapsible,
selected,
onSelect,
hover,
label,
secondaryLabel,
showLabel = true,
truncateLength,
dndDropRef,
droppable,
canDrop,
dropTarget,
onContextMenu,
contextMenuOpen,
dragging,
dragNodeRef,
badge,
badgeColor,
badgeTextColor,
badgeBorderColor,
badgeClassName,
badgeLocation,
labelIconClass,
labelIcon,
labelIconPadding,
onCollapseChange,
hulledOutline,
}) => {
const [hovered, hoverRef] = useHover();
const [labelHover, labelHoverRef] = useHover();
const dragLabelRef = useDragNode()[1];
const refs = useCombineRefs<SVGPathElement>(hoverRef, dragNodeRef);
const isHover = hover !== undefined ? hover : hovered;
const anchorRef = useSvgAnchor();
const outlineRef = useCombineRefs(dndDropRef, anchorRef);
const labelLocation = React.useRef<PointWithSize>();
const pathRef = React.useRef<string>();
const boxRef = React.useRef<Rect | null>(null);
const nodeElement = element as Node;
let parent = element.getParent();
let altGroup = false;
while (!isGraph(parent)) {
altGroup = !altGroup;
parent = parent.getParent();
}
// cast to number and coerce
const padding = maxPadding(element.getStyle<NodeStyle>().padding ?? 17);
const hullPadding = (point: PointWithSize | PointTuple) => (point[2] || 0) + padding;
if (!droppable || !pathRef.current || !labelLocation.current) {
const children = element.getNodes().filter(c => c.isVisible());
if (children.length === 0) {
return null;
}
const points: (PointWithSize | PointTuple)[] = [];
_.forEach(children, c => {
if (c.getNodeShape() === NodeShape.circle) {
const bounds = c.getBounds();
const { width, height } = bounds;
const { x, y } = bounds.getCenter();
const radius = Math.max(width, height) / 2;
points.push([x, y, radius] as PointWithSize);
} else {
// add all 4 corners
const { width, height, x, y } = c.getBounds();
points.push([x, y, 0] as PointWithSize);
points.push([x + width, y, 0] as PointWithSize);
points.push([x, y + height, 0] as PointWithSize);
points.push([x + width, y + height, 0] as PointWithSize);
}
});
if (hulledOutline) {
const hullPoints: (PointWithSize | PointTuple)[] =
points.length > 2 ? polygonHull(points as PointTuple[]) : (points as PointTuple[]);
if (!hullPoints) {
return null;
}
// change the box only when not dragging
pathRef.current = hullPath(hullPoints as PointTuple[], hullPadding);
// Compute the location of the group label.
labelLocation.current = computeLabelLocation(hullPoints as PointWithSize[]);
} else {
boxRef.current = nodeElement.getBounds();
labelLocation.current = [boxRef.current.x + boxRef.current.width / 2, boxRef.current.y + boxRef.current.height, 0];
}
}
const groupClassName = css(
styles.topologyGroup,
className,
altGroup && 'pf-m-alt-group',
canDrop && 'pf-m-highlight',
dragging && 'pf-m-dragging',
selected && 'pf-m-selected'
);
const innerGroupClassName = css(
styles.topologyGroup,
className,
altGroup && 'pf-m-alt-group',
canDrop && 'pf-m-highlight',
dragging && 'pf-m-dragging',
selected && 'pf-m-selected',
(isHover || labelHover) && 'pf-m-hover',
canDrop && dropTarget && 'pf-m-drop-target'
);
return (
<g ref={labelHoverRef} onContextMenu={onContextMenu} onClick={onSelect} className={groupClassName}>
<Layer id={GROUPS_LAYER}>
<g ref={refs} onContextMenu={onContextMenu} onClick={onSelect} className={innerGroupClassName}>
{hulledOutline ? (
<path ref={outlineRef} className={styles.topologyGroupBackground} d={pathRef.current} />
) : (
<rect ref={outlineRef} className={styles.topologyGroupBackground} x={boxRef.current.x} y={boxRef.current.y} width={boxRef.current.width} height={boxRef.current.height}/>
)}
</g>
</Layer>
{showLabel && (label || element.getLabel()) && (
<NodeLabel
className={styles.topologyGroupLabel}
x={labelLocation.current[0]}
y={labelLocation.current[1] + (hulledOutline ? hullPadding(labelLocation.current) : 0) + 24}
paddingX={8}
paddingY={5}
dragRef={dragNodeRef ? dragLabelRef : undefined}
status={element.getNodeStatus()}
secondaryLabel={secondaryLabel}
truncateLength={truncateLength}
badge={badge}
badgeColor={badgeColor}
badgeTextColor={badgeTextColor}
badgeBorderColor={badgeBorderColor}
badgeClassName={badgeClassName}
badgeLocation={badgeLocation}
labelIconClass={labelIconClass}
labelIcon={labelIcon}
labelIconPadding={labelIconPadding}
onContextMenu={onContextMenu}
contextMenuOpen={contextMenuOpen}
hover={isHover || labelHover}
actionIcon={collapsible ? <CollapseIcon /> : undefined}
onActionIconClick={() => onCollapseChange(element, true)}
>
{label || element.getLabel()}
</NodeLabel>
)}
</g>
);
};
export default observer(DefaultGroupExpanded);