Skip to content

Commit

Permalink
feat(Topology): Add option to fit layout to screen upon layout comple…
Browse files Browse the repository at this point in the history
…tion (#8210)
  • Loading branch information
jeff-phillips-18 committed Oct 21, 2022
1 parent ef07e5b commit 01da036
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
PipelineDagreLayout,
PipelineNodeModel,
getSpacerNodes,
getEdgesFromNodes
getEdgesFromNodes,
useVisualizationController
} from '@patternfly/react-topology';
import '@patternfly/react-styles/css/components/Topology/topology-components.css';
import withTopologySetup from './utils/withTopologySetup';
Expand Down Expand Up @@ -77,6 +78,9 @@ const getModel = (layout: string): Model => {
export const PipelineLayout = withTopologySetup(() => {
useLayoutFactory((type: string, graph: Graph): Layout | undefined => new PipelineDagreLayout(graph, { nodesep: 95 }));
useComponentFactory(pipelineComponentFactory);
const controller = useVisualizationController();
controller.setFitToScreenOnLayout(true);

// support pan zoom and drag
useComponentFactory(
React.useCallback<ComponentFactory>(kind => {
Expand Down
20 changes: 19 additions & 1 deletion packages/react-topology/src/Visualization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
ModelKind,
LayoutFactory,
Layout,
ViewPaddingSettings
ViewPaddingSettings,
GRAPH_LAYOUT_END_EVENT,
GraphLayoutEndEventListener
} from './types';
import defaultElementFactory from './elements/defaultElementFactory';
import Stateful from './utils/Stateful';
Expand Down Expand Up @@ -59,6 +61,8 @@ export class Visualization extends Stateful implements Controller {

private eventListeners: { [type: string]: EventListener[] } = {};

private fitToScreenListener: GraphLayoutEndEventListener;

@observable.shallow
private readonly store = {};

Expand Down Expand Up @@ -254,6 +258,20 @@ export class Visualization extends Stateful implements Controller {
}
}

setFitToScreenOnLayout(fitToScreen: boolean, padding: number = 80): void {
if (this.fitToScreenListener) {
this.removeEventListener(GRAPH_LAYOUT_END_EVENT, this.fitToScreenListener);
}

if (fitToScreen) {
this.fitToScreenListener = ({ graph }): void => {
graph.fit(padding);
};
this.addEventListener(GRAPH_LAYOUT_END_EVENT, this.fitToScreenListener);
return;
}
}

shouldRenderNode(node: Node): boolean {
if (!this.viewConstraintsEnabled) {
return true;
Expand Down
1 change: 1 addition & 0 deletions packages/react-topology/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export interface Controller extends WithState {
getGraph(): Graph;
setGraph(graph: Graph): void;
getLayout(type: string | undefined): Layout | undefined;
setFitToScreenOnLayout(fitToScreen: boolean, padding?: number): void;
getElementById(id: string): GraphElement | undefined;
getNodeById(id: string): Node | undefined;
getEdgeById(id: string): Edge | undefined;
Expand Down

0 comments on commit 01da036

Please sign in to comment.