Skip to content
Snippets Groups Projects
Commit b167d87f authored by Maximilian Giller's avatar Maximilian Giller :squid:
Browse files

Added basic getters

parent 35b7df82
No related branches found
No related tags found
1 merge request!2Implemented editor in the react framework
Pipeline #56379 failed
import ManagedData from "../manageddata";
import {Link} from "./link";
import {NodeType} from "./nodetype";
import {Node} from "./node";
import {GLOBAL_PARAMS} from "../helper/serializableitem";
import { Link } from "./link";
import { NodeType } from "./nodetype";
import { Node } from "./node";
import { GLOBAL_PARAMS } from "../helper/serializableitem";
import { GraphElement } from "./graphelement";
const GRAPH_PARAMS = [...GLOBAL_PARAMS];
const GRAPH_DATA_PARAMS = ["nodes", "links", "types"];
type GraphData = { nodes: Node[], links: Link[], types: NodeType[] };
type GraphData = { nodes: Node[]; links: Link[]; types: NodeType[] };
export class Graph extends ManagedData {
public data: GraphData;
private nextNodeId: number = 0;
private nextLinkId: number = 0;
private nextTypeId: number = 0;
private nextNodeId = 0;
private nextLinkId = 0;
private nextTypeId = 0;
// Callbacks
public onChangeCallbacks: { (data: any): void; } [];
public onChangeCallbacks: { (data: any): void }[];
constructor(data: GraphData) {
super(data);
......@@ -27,6 +27,30 @@ export class Graph extends ManagedData {
this.prepareIds(data);
}
/**
* Intuitive getter for links.
* @returns All links associated with the graph.
*/
public get links(): Link[] {
return this.data.links;
}
/**
* Intuitive getter for nodes.
* @returns All nodes associated with the graph.number
*/
public get nodes(): Node[] {
return this.data.nodes;
}
/**
* Intuitive getter for node types.
* @returns All node types associated with the graph.
*/
public get types(): NodeType[] {
return this.data.types;
}
/**
* Determines the highest, used ids for GraphElements in data for later use.
* @param data Data to analyse.
......@@ -49,7 +73,7 @@ export class Graph extends ManagedData {
* @returns Highest id in list.
*/
private getHighestId(elements: GraphElement[]): number {
let highest: number = 0;
let highest = 0;
elements.forEach((element) => {
if (highest < element.id) {
highest = element.id;
......@@ -82,7 +106,7 @@ export class Graph extends ManagedData {
protected storableData(data: GraphData): any {
let clean: GraphData;
clean.links = data.links.map((link) => link.getCleanInstance());
clean.nodes = data.nodes.map((node) => node.getCleanInstance());
clean.types = data.types.map((type) => type.getCleanInstance());
......@@ -99,11 +123,11 @@ export class Graph extends ManagedData {
* @param data GraphData object to serialize.
* @returns Serialized data.
*/
private serializeData(data: GraphData) : any {
private serializeData(data: GraphData): any {
return {
...this.serializeProperties(GRAPH_PARAMS),
...this.serializeProperties(GRAPH_DATA_PARAMS, data),
}
};
}
/**
......@@ -113,7 +137,7 @@ export class Graph extends ManagedData {
*/
public addNode(node: Node) {
if (this.data.nodes.includes(node)) {
return true; // Already exists in graph.
return true; // Already exists in graph.
}
// Update id
......@@ -136,7 +160,7 @@ export class Graph extends ManagedData {
*/
public deleteNode(node: Node): boolean {
if (!this.data.nodes.includes(node)) {
return true; // Doesn't even exist in graph to begin with.
return true; // Doesn't even exist in graph to begin with.
}
this.data.nodes.filter((n: Node) => n !== node);
......@@ -155,7 +179,9 @@ export class Graph extends ManagedData {
this.triggerOnChange();
// TODO: Use toString implementation of node
this.storeCurrentData("Deleted node [" + node + "] and all connected links");
this.storeCurrentData(
"Deleted node [" + node + "] and all connected links"
);
return true;
}
......@@ -167,7 +193,7 @@ export class Graph extends ManagedData {
*/
public addLink(link: Link): boolean {
if (this.data.links.includes(link)) {
return true; // Already exists in graph.
return true; // Already exists in graph.
}
// Updateid
......@@ -190,7 +216,7 @@ export class Graph extends ManagedData {
*/
public deleteLink(link: Link): boolean {
if (!this.data.links.includes(link)) {
return true; // Doesn't even exist in graph to begin with.
return true; // Doesn't even exist in graph to begin with.
}
this.data.links.filter((l: Link) => l !== link);
......@@ -201,4 +227,4 @@ export class Graph extends ManagedData {
return true;
}
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment