Skip to content

Commit

Permalink
feat: make centering nodes optional to better support elk (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveblue authored Nov 7, 2023
1 parent 24e0f6a commit f141e64
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions projects/swimlane/ngx-graph/src/lib/graph/graph.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ export class GraphComponent implements OnInit, OnChanges, OnDestroy, AfterViewIn
@Input() customColors: any;
@Input() animations: boolean = true;
@Input() deferDisplayUntilPosition: boolean = false;
@Output() select = new EventEmitter();
@Input() centerNodesOnPositionChange = true;

@Output() select = new EventEmitter();
@Output() activate: EventEmitter<any> = new EventEmitter();
@Output() deactivate: EventEmitter<any> = new EventEmitter();
@Output() zoomChange: EventEmitter<number> = new EventEmitter();
Expand Down Expand Up @@ -232,6 +233,7 @@ export class GraphComponent implements OnInit, OnChanges, OnDestroy, AfterViewIn
})
);
}

if (this.zoomToFit$) {
this.subscriptions.push(
this.zoomToFit$.subscribe(() => {
Expand Down Expand Up @@ -432,8 +434,8 @@ export class GraphComponent implements OnInit, OnChanges, OnDestroy, AfterViewIn
const oldNodes: Set<string> = new Set();

this.graph.nodes.map(n => {
n.transform = `translate(${n.position.x - n.dimension.width / 2 || 0}, ${
n.position.y - n.dimension.height / 2 || 0
n.transform = `translate(${n.position.x - (this.centerNodesOnPositionChange ? n.dimension.width / 2 : 0) || 0}, ${
n.position.y - (this.centerNodesOnPositionChange ? n.dimension.height / 2 : 0) || 0
})`;
if (!n.data) {
n.data = {};
Expand All @@ -449,9 +451,9 @@ export class GraphComponent implements OnInit, OnChanges, OnDestroy, AfterViewIn
const oldCompoundNodes: Set<string> = new Set();

(this.graph.clusters || []).map(n => {
n.transform = `translate(${n.position.x - n.dimension.width / 2 || 0}, ${
n.position.y - n.dimension.height / 2 || 0
})`;
n.transform = `translate(${
n.position.x - (this.centerNodesOnPositionChange ? n.dimension.width / 2 : 0) / 2 || 0
}, ${n.position.y - (this.centerNodesOnPositionChange ? n.dimension.height / 2 : 0) || 0})`;
if (!n.data) {
n.data = {};
}
Expand All @@ -463,9 +465,9 @@ export class GraphComponent implements OnInit, OnChanges, OnDestroy, AfterViewIn
});

(this.graph.compoundNodes || []).map(n => {
n.transform = `translate(${n.position.x - n.dimension.width / 2 || 0}, ${
n.position.y - n.dimension.height / 2 || 0
})`;
n.transform = `translate(${
n.position.x - (this.centerNodesOnPositionChange ? n.dimension.width / 2 : 0) / 2 || 0
}, ${n.position.y - (this.centerNodesOnPositionChange ? n.dimension.height / 2 : 0) || 0})`;
if (!n.data) {
n.data = {};
}
Expand Down Expand Up @@ -890,8 +892,8 @@ export class GraphComponent implements OnInit, OnChanges, OnDestroy, AfterViewIn
node.position.y += event.movementY / this.zoomLevel;

// move the node
const x = node.position.x - node.dimension.width / 2;
const y = node.position.y - node.dimension.height / 2;
const x = node.position.x - (this.centerNodesOnPositionChange ? node.dimension.width / 2 : 0);
const y = node.position.y - (this.centerNodesOnPositionChange ? node.dimension.height / 2 : 0);
node.transform = `translate(${x}, ${y})`;

for (const link of this.graph.edges) {
Expand Down

0 comments on commit f141e64

Please sign in to comment.