This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aviv Turgeman <[email protected]>
- Loading branch information
Showing
15 changed files
with
1,028 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React, { FC, useEffect, useState } from 'react'; | ||
|
||
import { NodeNetworkStateModelGroupVersionKind } from '@models'; | ||
import { useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk'; | ||
import { | ||
action, | ||
createTopologyControlButtons, | ||
defaultControlButtonsOptions, | ||
SELECTION_EVENT, | ||
TopologyControlBar, | ||
TopologySideBar, | ||
TopologyView, | ||
Visualization, | ||
VisualizationProvider, | ||
VisualizationSurface, | ||
} from '@patternfly/react-topology'; | ||
import { V1beta1NodeNetworkState } from '@types'; | ||
|
||
import { componentFactory, layoutFactory } from './utils/factory'; | ||
import { transformDataToTopologyModel } from './utils/utils'; | ||
|
||
const Topology: FC = () => { | ||
const [selectedIds, setSelectedIds] = useState<string[]>([]); | ||
const [visualization, setVisualization] = useState<Visualization>(null); | ||
|
||
const [data, loaded, error] = useK8sWatchResource<V1beta1NodeNetworkState[]>({ | ||
groupVersionKind: NodeNetworkStateModelGroupVersionKind, | ||
isList: true, | ||
namespaced: false, | ||
}); | ||
|
||
useEffect(() => { | ||
if (loaded && !error) { | ||
const topologyModel = transformDataToTopologyModel(data); | ||
|
||
if (!visualization) { | ||
const newVisualization = new Visualization(); | ||
newVisualization.registerLayoutFactory(layoutFactory); | ||
newVisualization.registerComponentFactory(componentFactory); | ||
newVisualization.addEventListener(SELECTION_EVENT, setSelectedIds); | ||
newVisualization.fromModel(topologyModel); | ||
setVisualization(newVisualization); | ||
} else { | ||
visualization.fromModel(topologyModel); | ||
} | ||
} | ||
}, [data, loaded, error]); | ||
|
||
const topologySideBar = ( | ||
<TopologySideBar | ||
className="topology-example-sidebar" | ||
show={selectedIds.length > 0} | ||
onClose={() => setSelectedIds([])} | ||
> | ||
<div style={{ marginTop: 27, marginLeft: 20, height: '800px' }}>{selectedIds[0]}</div> | ||
</TopologySideBar> | ||
); | ||
|
||
return ( | ||
<TopologyView | ||
sideBar={topologySideBar} | ||
controlBar={ | ||
<TopologyControlBar | ||
controlButtons={createTopologyControlButtons({ | ||
...defaultControlButtonsOptions, | ||
zoomInCallback: action(() => { | ||
visualization.getGraph().scaleBy(4 / 3); | ||
}), | ||
zoomOutCallback: action(() => { | ||
visualization.getGraph().scaleBy(0.75); | ||
}), | ||
fitToScreenCallback: action(() => { | ||
visualization.getGraph().fit(80); | ||
}), | ||
resetViewCallback: action(() => { | ||
visualization.getGraph().reset(); | ||
visualization.getGraph().layout(); | ||
}), | ||
legend: false, | ||
})} | ||
/> | ||
} | ||
> | ||
<VisualizationProvider controller={visualization}> | ||
<VisualizationSurface state={{ selectedIds }} /> | ||
</VisualizationProvider> | ||
</TopologyView> | ||
); | ||
}; | ||
|
||
export default Topology; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React, { FC } from 'react'; | ||
|
||
import { DefaultGroup, Node } from '@patternfly/react-topology'; | ||
|
||
type CustomGroupProps = { | ||
element: Node; | ||
} & any; | ||
|
||
const CustomGroup: FC<CustomGroupProps> = ({ element, ...rest }) => { | ||
const data = element.getData(); | ||
|
||
return ( | ||
<DefaultGroup className="custom-group-node" badge={data?.badge} element={element} {...rest} /> | ||
); | ||
}; | ||
|
||
export default CustomGroup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React, { FC } from 'react'; | ||
|
||
import { | ||
DefaultNode, | ||
Node, | ||
WithDndDropProps, | ||
WithDragNodeProps, | ||
WithSelectionProps, | ||
} from '@patternfly/react-topology'; | ||
|
||
import { ICON_SIZE } from '../utils/constants'; | ||
|
||
type CustomNodeProps = { | ||
element: Node; | ||
} & WithSelectionProps & | ||
WithDragNodeProps & | ||
WithDndDropProps; | ||
|
||
const CustomNode: FC<CustomNodeProps> = ({ element, onSelect, selected }) => { | ||
const data = element.getData(); | ||
const Icon = data.icon; | ||
const { width, height } = element.getBounds(); | ||
|
||
const xCenter = (width - ICON_SIZE) / 2; | ||
const yCenter = (height - ICON_SIZE) / 2; | ||
|
||
return ( | ||
<DefaultNode | ||
badge={data.badge} | ||
element={element} | ||
onSelect={onSelect} | ||
selected={selected} | ||
truncateLength={8} | ||
> | ||
<g transform={`translate(${xCenter}, ${yCenter})`}> | ||
<Icon style={{ color: '#393F44' }} width={ICON_SIZE} height={ICON_SIZE} /> | ||
</g> | ||
</DefaultNode> | ||
); | ||
}; | ||
|
||
export default CustomNode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { EncodedExtension } from '@openshift/dynamic-plugin-sdk'; | ||
import type { HrefNavItem, RoutePage } from '@openshift-console/dynamic-plugin-sdk'; | ||
|
||
export const TopologyExposedModules = { | ||
Topology: './views/topology/Topology', | ||
}; | ||
|
||
export const TopologyExtensions: EncodedExtension[] = [ | ||
{ | ||
type: 'console.navigation/href', | ||
properties: { | ||
id: 'topology', | ||
perspective: 'admin', | ||
href: 'nmstate-topology', | ||
section: 'networking', | ||
name: '%plugin__nmstate-console-plugin~Topology%', | ||
dataAttributes: { | ||
'data-quickstart-id': 'qs-nav-topology', | ||
'data-test-id': 'state-nav-topology', | ||
}, | ||
insertAfter: 'state', | ||
}, | ||
} as EncodedExtension<HrefNavItem>, | ||
{ | ||
type: 'console.page/route', | ||
properties: { | ||
path: ['nmstate-topology'], | ||
component: { | ||
$codeRef: 'Topology', | ||
}, | ||
}, | ||
} as EncodedExtension<RoutePage>, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const NODE_DIAMETER = 35; | ||
export const CONNECTOR_TARGET_DROP = 'connector-target-drop'; | ||
export const GROUP = 'group'; | ||
export const ICON_SIZE = 15; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
ColaLayout, | ||
ComponentFactory, | ||
DefaultEdge, | ||
DragObjectWithType, | ||
Edge, | ||
Graph, | ||
GraphComponent, | ||
graphDropTargetSpec, | ||
GraphElement, | ||
groupDropTargetSpec, | ||
Layout, | ||
LayoutFactory, | ||
ModelKind, | ||
Node, | ||
nodeDragSourceSpec, | ||
nodeDropTargetSpec, | ||
withDndDrop, | ||
withDragNode, | ||
withPanZoom, | ||
withSelection, | ||
withTargetDrag, | ||
} from '@patternfly/react-topology'; | ||
|
||
import CustomGroup from '../components/CustomGroup'; | ||
import CustomNode from '../components/CustomNode'; | ||
|
||
import { CONNECTOR_TARGET_DROP, GROUP } from './constants'; | ||
|
||
export const layoutFactory: LayoutFactory = (type: string, graph: Graph): Layout | undefined => | ||
new ColaLayout(graph); | ||
|
||
export const componentFactory: ComponentFactory = (kind: ModelKind, type: string): any => { | ||
switch (type) { | ||
case GROUP: | ||
return withDndDrop(groupDropTargetSpec)( | ||
withDragNode(nodeDragSourceSpec(GROUP))(withSelection()(CustomGroup)), | ||
); | ||
default: | ||
switch (kind) { | ||
case ModelKind.graph: | ||
return withDndDrop(graphDropTargetSpec())(withPanZoom()(GraphComponent)); | ||
case ModelKind.node: | ||
return withDndDrop(nodeDropTargetSpec([CONNECTOR_TARGET_DROP]))( | ||
withDragNode(nodeDragSourceSpec(ModelKind.node, true, true))( | ||
withSelection()(CustomNode), | ||
), | ||
); | ||
case ModelKind.edge: | ||
return withTargetDrag< | ||
DragObjectWithType, | ||
Node, | ||
{ dragging?: boolean }, | ||
{ | ||
element: GraphElement; | ||
} | ||
>({ | ||
item: { type: CONNECTOR_TARGET_DROP }, | ||
begin: (monitor, props) => { | ||
props.element.raise(); | ||
return props.element; | ||
}, | ||
drag: (event, monitor, props) => { | ||
(props.element as Edge).setEndPoint(event.x, event.y); | ||
}, | ||
end: (dropResult, monitor, props) => { | ||
if (monitor.didDrop() && dropResult && props) { | ||
(props.element as Edge).setTarget(dropResult); | ||
} | ||
(props.element as Edge).setEndPoint(); | ||
}, | ||
collect: (monitor) => ({ | ||
dragging: monitor.isDragging(), | ||
}), | ||
})(DefaultEdge); | ||
default: | ||
return undefined; | ||
} | ||
} | ||
}; |
Oops, something went wrong.