Newer
Older
import { Graph } from "./graph";
import { GraphElement } from "./graphelement";
import { NodeType } from "./nodetype";
import { SerializedURL } from "../helper/serializedurl";
import { Link } from "./link";
import { GLOBAL_PARAMS } from "../helper/serializableitem";
const NODE_PARAMS = [
"label",
"icon",
"description",
"references",
"video",
"type",
"banner",
];
const NODE_SIM_PARAMS = ["index", "x", "y", "vx", "vy", "fx", "fy"]; // Based on https://github.com/d3/d3-force#simulation_nodes
export class Node extends GraphElement {
public label: string;
public description: string;
public type: NodeType;
public icon: SerializedURL;
public banner: SerializedURL;
public video: SerializedURL;
public references: SerializedURL[];
constructor(graph: Graph = undefined) {
super(graph);
this.isNode = true;
}
public delete() {
return this.graph.deleteNode(this);
}
public add(graph: Graph = this.graph) {
this.graph = graph;
if (this.graph == undefined) {
return false;
}
return this.graph.addNode(this);
}
/**
* Calculates a list of all connected links to the current node.
* @returns Array containing all connected links.
*/
public get links(): Link[] {
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
const links: Link[] = [];
this.graph.links.forEach((link) => {
if (link.contains(this)) {
links.push(link);
}
});
return links;
}
/**
* Connects a given node to itself. Only works if they are in the same graph.
* @param node Other node to connect.
* @returns The created link, if successful, otherwise undefined.
*/
public connect(node: Node): Link {
if (this.graph !== node.graph) {
return undefined;
}
const link = new Link(this.graph);
link.source = this;
link.target = node;
if (link.add()) {
return link;
}
}
public serialize(): any {
return this.serializeProperties(NODE_PARAMS);
}
public getCleanInstance(): any {
return {
...this.serialize(),
...this.serializeProperties(NODE_SIM_PARAMS),
public static parse(raw: any): Node {
const node: Node = new Node();
node.id = Number(raw.id);
node.label = raw.name;
node.description = raw.description;
node.type = NodeType.parse(raw.type);
return node;
}
public toString(): string {
return this.label;
}