Skip to content

Commit 6f41c7a

Browse files
chore(demo): Updates for dagre layout in Topology Package demo
1 parent d23e234 commit 6f41c7a

File tree

7 files changed

+227
-55
lines changed

7 files changed

+227
-55
lines changed

packages/demo-app-ts/src/demos/Layouts.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
useModel,
1111
ComponentFactory
1212
} from '@patternfly/react-topology';
13-
import defaultLayoutFactory from '../layouts/defaultLayoutFactory';
13+
import defaultLayoutFactory, { LayoutType } from '../layouts/defaultLayoutFactory';
1414
import defaultComponentFactory from '../components/defaultComponentFactory';
1515
import GroupHull from '../components/GroupHull';
1616
import Group from '../components/DemoDefaultGroup';
@@ -61,9 +61,9 @@ const layoutStory =
6161
return null;
6262
};
6363

64-
export const Force = withTopologySetup(layoutStory(getModel('Force')));
65-
export const Dagre = withTopologySetup(layoutStory(getModel('Dagre')));
66-
export const Cola = withTopologySetup(layoutStory(getModel('Cola')));
64+
export const Force = withTopologySetup(layoutStory(getModel(LayoutType.Force)));
65+
export const Dagre = withTopologySetup(layoutStory(getModel(LayoutType.Dagre)));
66+
export const Cola = withTopologySetup(layoutStory(getModel(LayoutType.Cola)));
6767

6868
export const Layouts: React.FunctionComponent = () => {
6969
const [activeKey, setActiveKey] = useState<string | number>(0);

packages/demo-app-ts/src/demos/statusConnectorsDemo/StatusConnectors.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import defaultComponentFactory from '../../components/defaultComponentFactory';
2020
import statusConnectorsComponentFactory from './statusConnectorsComponentFactory';
2121
import DemoControlBar from '../DemoControlBar';
22+
import { LayoutType } from '../../layouts/defaultLayoutFactory';
2223

2324
const DEFAULT_CHAR_WIDTH = 8;
2425
const DEFAULT_NODE_SIZE = 75;
@@ -184,7 +185,7 @@ export const StatusConnectorsDemo: React.FunctionComponent = () => {
184185
const graph = {
185186
id: 'g1',
186187
type: 'graph',
187-
layout: 'Dagre'
188+
layout: LayoutType.Dagre
188189
};
189190

190191
const model = { graph, nodes, edges };

packages/demo-app-ts/src/demos/topologyPackageDemo/DemoContext.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createContext } from 'react';
22
import { action, makeObservable, observable } from 'mobx';
33
import { LabelPosition } from '@patternfly/react-topology';
44
import { GeneratorEdgeOptions, GeneratorNodeOptions } from './generator';
5+
import { LayoutType } from '../../layouts/defaultLayoutFactory';
56

67
export class DemoModel {
78
protected nodeOptionsP: GeneratorNodeOptions = {
@@ -29,10 +30,12 @@ export class DemoModel {
2930
numGroups: 1,
3031
nestedLevel: 0
3132
};
32-
protected layoutP: string = 'ColaNoForce';
33+
protected layoutP: string = LayoutType.ColaNoForce;
3334
protected medScaleP: number = 0.5;
3435
protected lowScaleP: number = 0.3;
3536

37+
protected logEventsP: boolean = false;
38+
3639
constructor() {
3740
makeObservable<
3841
DemoModel,
@@ -42,25 +45,29 @@ export class DemoModel {
4245
| 'layoutP'
4346
| 'medScaleP'
4447
| 'lowScaleP'
48+
| 'logEventsP'
4549
| 'setNodeOptions'
4650
| 'setEdgeOptions'
4751
| 'setCreationCounts'
4852
| 'setLayout'
4953
| 'setMedScale'
5054
| 'setLowScale'
55+
| 'setLogEvents'
5156
>(this, {
5257
nodeOptionsP: observable.ref,
5358
edgeOptionsP: observable.shallow,
5459
creationCountsP: observable.shallow,
5560
layoutP: observable,
5661
medScaleP: observable,
5762
lowScaleP: observable,
63+
logEventsP: observable,
5864
setNodeOptions: action,
5965
setEdgeOptions: action,
6066
setCreationCounts: action,
6167
setLayout: action,
6268
setMedScale: action,
63-
setLowScale: action
69+
setLowScale: action,
70+
setLogEvents: action
6471
});
6572
}
6673

@@ -111,6 +118,12 @@ export class DemoModel {
111118
public setLowScale = (scale: number): void => {
112119
this.lowScaleP = scale;
113120
};
121+
public get logEvents(): boolean {
122+
return this.logEventsP;
123+
}
124+
public setLogEvents = (log: boolean): void => {
125+
this.logEventsP = log;
126+
};
114127
}
115128

116129
export const DemoContext = createContext<DemoModel>(new DemoModel());

packages/demo-app-ts/src/demos/topologyPackageDemo/OptionsViewBar.tsx

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useContext, useState, useRef } from 'react';
22
import {
33
Button,
4+
Checkbox,
45
Dropdown,
56
DropdownItem,
67
DropdownList,
@@ -14,6 +15,19 @@ import {
1415
} from '@patternfly/react-core';
1516
import { Controller, Model, observer } from '@patternfly/react-topology';
1617
import { DemoContext } from './DemoContext';
18+
import { LayoutType } from '../../layouts/defaultLayoutFactory';
19+
20+
const LayoutTitles: Record<string, string> = {
21+
[LayoutType.BreadthFirst]: 'Breadth First',
22+
[LayoutType.Cola]: 'Cola',
23+
[LayoutType.ColaGroups]: 'Cola Groups',
24+
[LayoutType.ColaNoForce]: 'Cola No Force',
25+
[LayoutType.Concentric]: 'Concentric',
26+
[LayoutType.Dagre]: 'Dagre',
27+
[LayoutType.DagreHorizontal]: 'Dagre Horizontal',
28+
[LayoutType.Force]: 'Force',
29+
[LayoutType.Grid]: 'Grid'
30+
};
1731

1832
const OptionsContextBar: React.FC<{ controller: Controller }> = observer(({ controller }) => {
1933
const options = useContext(DemoContext);
@@ -36,36 +50,39 @@ const OptionsContextBar: React.FC<{ controller: Controller }> = observer(({ cont
3650
<Dropdown
3751
toggle={(toggleRef: React.Ref<MenuToggleElement>) => (
3852
<MenuToggle ref={toggleRef} onClick={() => setLayoutDropdownOpen(!layoutDropdownOpen)}>
39-
{options.layout}
53+
{LayoutTitles[options.layout]}
4054
</MenuToggle>
4155
)}
4256
isOpen={layoutDropdownOpen}
4357
onOpenChange={(isOpen) => setLayoutDropdownOpen(isOpen)}
4458
>
4559
<DropdownList>
46-
<DropdownItem key={1} onClick={() => updateLayout('Force')}>
47-
Force
60+
<DropdownItem key={1} onClick={() => updateLayout(LayoutType.Force)}>
61+
{LayoutTitles[LayoutType.Force]}
62+
</DropdownItem>
63+
<DropdownItem key={2} onClick={() => updateLayout(LayoutType.Dagre)}>
64+
{LayoutTitles[LayoutType.Dagre]}
4865
</DropdownItem>
49-
<DropdownItem key={2} onClick={() => updateLayout('Dagre')}>
50-
Dagre
66+
<DropdownItem key={9} onClick={() => updateLayout(LayoutType.DagreHorizontal)}>
67+
{LayoutTitles[LayoutType.DagreHorizontal]}
5168
</DropdownItem>
52-
<DropdownItem key={3} onClick={() => updateLayout('Cola')}>
53-
Cola
69+
<DropdownItem key={3} onClick={() => updateLayout(LayoutType.Cola)}>
70+
{LayoutTitles[LayoutType.Cola]}
5471
</DropdownItem>
55-
<DropdownItem key={8} onClick={() => updateLayout('ColaGroups')}>
56-
ColaGroups
72+
<DropdownItem key={8} onClick={() => updateLayout(LayoutType.ColaGroups)}>
73+
{LayoutTitles[LayoutType.ColaGroups]}
5774
</DropdownItem>
58-
<DropdownItem key={4} onClick={() => updateLayout('ColaNoForce')}>
59-
ColaNoForce
75+
<DropdownItem key={4} onClick={() => updateLayout(LayoutType.ColaNoForce)}>
76+
{LayoutTitles[LayoutType.ColaNoForce]}
6077
</DropdownItem>
61-
<DropdownItem key={5} onClick={() => updateLayout('Grid')}>
62-
Grid
78+
<DropdownItem key={5} onClick={() => updateLayout(LayoutType.Grid)}>
79+
{LayoutTitles[LayoutType.Grid]}
6380
</DropdownItem>
64-
<DropdownItem key={6} onClick={() => updateLayout('Concentric')}>
65-
Concentric
81+
<DropdownItem key={6} onClick={() => updateLayout(LayoutType.Concentric)}>
82+
{LayoutTitles[LayoutType.Concentric]}
6683
</DropdownItem>
67-
<DropdownItem key={7} onClick={() => updateLayout('BreadthFirst')}>
68-
BreadthFirst
84+
<DropdownItem key={7} onClick={() => updateLayout(LayoutType.BreadthFirst)}>
85+
{LayoutTitles[LayoutType.BreadthFirst]}
6986
</DropdownItem>
7087
</DropdownList>
7188
</Dropdown>
@@ -193,6 +210,14 @@ const OptionsContextBar: React.FC<{ controller: Controller }> = observer(({ cont
193210
</Flex>
194211
</Flex>
195212
</ToolbarItem>
213+
<ToolbarItem>
214+
<Checkbox
215+
id="log-switch"
216+
isChecked={options.logEvents}
217+
onChange={(_event, checked) => options.setLogEvents(checked)}
218+
label="Log events"
219+
/>
220+
</ToolbarItem>
196221
</Flex>
197222
);
198223
});

packages/demo-app-ts/src/demos/topologyPackageDemo/TopologyPackage.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const TopologyViewComponent: FunctionComponent<TopologyViewComponentProps> = obs
4747
options.creationCounts.numNodes,
4848
options.creationCounts.numGroups,
4949
options.creationCounts.numEdges,
50-
options.creationCounts.nestedLevel
50+
options.creationCounts.nestedLevel,
51+
options.layout
5152
);
5253

5354
const model = {
@@ -60,6 +61,7 @@ const TopologyViewComponent: FunctionComponent<TopologyViewComponentProps> = obs
6061
};
6162

6263
controller.fromModel(model, true);
64+
controller.getGraph().layout();
6365
}, [controller, options.creationCounts, options.layout]);
6466

6567
// Once we have the graph, run the layout. This ensures the graph size is set (by the initial size observation in VisualizationSurface)
@@ -120,14 +122,16 @@ const TopologyViewComponent: FunctionComponent<TopologyViewComponentProps> = obs
120122
}, [selectedIds, controller]);
121123

122124
useEffect(() => {
123-
controller.addEventListener(GRAPH_POSITION_CHANGE_EVENT, graphPositionChangeListener);
124-
controller.addEventListener(GRAPH_LAYOUT_END_EVENT, layoutEndListener);
125+
if (options.logEvents) {
126+
controller.addEventListener(GRAPH_POSITION_CHANGE_EVENT, graphPositionChangeListener);
127+
controller.addEventListener(GRAPH_LAYOUT_END_EVENT, layoutEndListener);
128+
}
125129

126130
return () => {
127131
controller.removeEventListener(GRAPH_POSITION_CHANGE_EVENT, graphPositionChangeListener);
128132
controller.removeEventListener(GRAPH_LAYOUT_END_EVENT, layoutEndListener);
129133
};
130-
}, [controller]);
134+
}, [controller, options.logEvents]);
131135

132136
useEffect(() => {
133137
controller.getGraph().setDetailsLevelThresholds({

0 commit comments

Comments
 (0)