-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdiagram.component.ts
More file actions
105 lines (94 loc) · 2.83 KB
/
diagram.component.ts
File metadata and controls
105 lines (94 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import type { NgDiagramConfig, Node } from 'ng-diagram';
import {
NgDiagramNodeTemplateMap,
NgDiagramBackgroundComponent,
NgDiagramComponent,
NgDiagramModelService,
} from 'ng-diagram';
import { LocalStorageModelAdapter } from './local-storage-model-adapter';
import { NodeComponent } from './node/node.component';
enum NodeTemplateType {
CustomNodeType = 'customNodeType',
}
@Component({
selector: 'diagram',
imports: [CommonModule, NgDiagramComponent, NgDiagramBackgroundComponent],
templateUrl: './diagram.component.html',
styleUrl: './diagram.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DiagramComponent {
nodeTemplateMap = new NgDiagramNodeTemplateMap([
[NodeTemplateType.CustomNodeType, NodeComponent],
]);
private modelService = inject(NgDiagramModelService);
config: NgDiagramConfig = {
zoom: {
zoomToFit: {
onInit: true,
padding: 180,
},
},
};
modelAdapter: LocalStorageModelAdapter = new LocalStorageModelAdapter(
'ng-diagram-custom-demo',
this.getDefaultDiagram()
);
addNode() {
const existingNodes = this.modelService.nodes();
const newId = `node-${crypto.randomUUID()}`;
const randomX = Math.floor(Math.random() * 400) + 50;
const randomY = Math.floor(Math.random() * 300) + 50;
const newNode: Node = {
id: newId,
position: { x: randomX, y: randomY },
data: { label: `Custom Node ${existingNodes.length + 1}` },
type: NodeTemplateType.CustomNodeType,
};
this.modelService.addNodes([newNode]);
}
reset() {
if (window.confirm('Are you sure you want to reset the diagram?')) {
this.resetDiagramToDefault();
}
}
private resetDiagramToDefault() {
const nodeIds = this.modelService.nodes().map((node) => node.id);
const edgeIds = this.modelService.edges().map((edge) => edge.id);
this.modelService.deleteNodes(nodeIds);
this.modelService.deleteEdges(edgeIds);
const defaultDiagram = this.getDefaultDiagram();
this.modelService.addNodes(defaultDiagram.nodes);
this.modelService.addEdges(defaultDiagram.edges);
}
private getDefaultDiagram() {
return {
nodes: [
{
id: '1',
position: { x: 0, y: 0 },
data: { label: 'Node 1' },
type: NodeTemplateType.CustomNodeType,
},
{
id: '2',
position: { x: 420, y: 0 },
data: { label: 'Node 2' },
type: NodeTemplateType.CustomNodeType,
},
],
edges: [
{
id: 'edge-1',
source: '1',
target: '2',
sourcePort: 'port-bottom',
targetPort: 'port-top',
data: {},
},
],
};
}
}