| 533 | // Functions |
| 534 | |
| 535 | function runLayout(graph) { |
| 536 | const autoLayoutElements = []; |
| 537 | const manualLayoutElements = []; |
| 538 | graph.getElements().forEach((el) => { |
| 539 | if (el.get('hidden')) return; |
| 540 | if (el.get('type') === 'fta.ConditioningEvent') { |
| 541 | manualLayoutElements.push(el); |
| 542 | } else { |
| 543 | autoLayoutElements.push(el); |
| 544 | } |
| 545 | }); |
| 546 | // Automatic Layout |
| 547 | DirectedGraph.layout(graph.getSubgraph(autoLayoutElements), { |
| 548 | rankDir: 'TB', |
| 549 | setVertices: true, |
| 550 | }); |
| 551 | // Manual Layout |
| 552 | manualLayoutElements.forEach((el) => { |
| 553 | const [neighbor] = graph.getNeighbors(el, { inbound: true }); |
| 554 | if (!neighbor) return; |
| 555 | const neighborPosition = neighbor.getBBox().bottomRight(); |
| 556 | el.position( |
| 557 | neighborPosition.x + 20, |
| 558 | neighborPosition.y - el.size().height / 2 - 15 |
| 559 | ); |
| 560 | }); |
| 561 | // Make sure the root element of the graph is always at the same position after the layout. |
| 562 | const rootCenter = { x: 500, y: 100 }; |
| 563 | const [source] = graph.getSources(); |
| 564 | const { width, height } = source.size(); |
| 565 | const diff = source |
| 566 | .position() |
| 567 | .difference({ |
| 568 | x: rootCenter.x - width / 2, |
| 569 | y: rootCenter.y - height / 2, |
| 570 | }); |
| 571 | graph.translate(-diff.x, -diff.y); |
| 572 | } |
| 573 | |
| 574 | function addTools(paper, elements) { |
| 575 | const toolName = 'expand-tools'; |