Newer
Older
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
import { GraphElement } from "./graphelement";
import { Node } from "./node";
import { NodeType } from "./nodetype";
import { Graph } from "./graph";
export interface LinkData {
source: number;
target: number;
type?: string;
}
export interface SimLinkData extends LinkData {
index: number;
}
export interface GraphLink {
id: number;
source: Node;
target: Node;
type?: NodeType;
// Properties used by the force graph simulation
index?: number;
}
export class Link
extends GraphElement<LinkData, SimLinkData>
implements GraphLink
{
public source: Node;
public target: Node;
type?: NodeType;
// These parameters will be added by the force graph implementation
public index?: number;
constructor(source?: Node, target?: Node, graph?: Graph) {
super(0, graph);
this.source = source;
this.target = target;
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
106
107
}
/**
* Id of the source node.
* @returns Source id.
*/
public get sourceId(): number {
return this.source.id;
}
/**
* Id of the target node.
* @returns Target id.
*/
public get targetId(): number {
return this.target.id;
}
public delete() {
return this.graph.deleteLink(this.id);
}
/**
* Determines if the given node is part of the link structure.
* @param node Node to check for.
* @returns True, if node is either source or target node of link.
*/
public contains(node: Node): boolean {
return this.source === node || this.target === node;
}
public toJSONSerializableObject(): LinkData {
return {
source: this.source.id,
target: this.target.id,
};
}
public toHistorySerializableObject(): SimLinkData {
return { ...this.toJSONSerializableObject(), index: this.index };
}
public toString(): string {
return this.source.toString() + " -> " + this.target.toString();
}
public isInitialized(): boolean {
return (
super.isInitialized() &&
this.source != undefined &&
this.target != undefined
);
}
public equals(other: GraphElement<LinkData, SimLinkData>): boolean {
if (other.constructor != this.constructor) {
return false;
}
const link = other as Link;
return (
link.sourceId === this.sourceId && link.targetId === this.targetId
);
}
}