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 ManagedData from "../manageddata";
import {Link} from "./link"; import { Link } from "./link";
import {NodeType} from "./nodetype"; import { NodeType } from "./nodetype";
import {Node} from "./node"; import { Node } from "./node";
import {GLOBAL_PARAMS} from "../helper/serializableitem"; import { GLOBAL_PARAMS } from "../helper/serializableitem";
import { GraphElement } from "./graphelement"; import { GraphElement } from "./graphelement";
const GRAPH_PARAMS = [...GLOBAL_PARAMS]; const GRAPH_PARAMS = [...GLOBAL_PARAMS];
const GRAPH_DATA_PARAMS = ["nodes", "links", "types"]; 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 { export class Graph extends ManagedData {
public data: GraphData; public data: GraphData;
private nextNodeId: number = 0; private nextNodeId = 0;
private nextLinkId: number = 0; private nextLinkId = 0;
private nextTypeId: number = 0; private nextTypeId = 0;
// Callbacks // Callbacks
public onChangeCallbacks: { (data: any): void; } []; public onChangeCallbacks: { (data: any): void }[];
constructor(data: GraphData) { constructor(data: GraphData) {
super(data); super(data);
...@@ -27,6 +27,30 @@ export class Graph extends ManagedData { ...@@ -27,6 +27,30 @@ export class Graph extends ManagedData {
this.prepareIds(data); 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. * Determines the highest, used ids for GraphElements in data for later use.
* @param data Data to analyse. * @param data Data to analyse.
...@@ -49,7 +73,7 @@ export class Graph extends ManagedData { ...@@ -49,7 +73,7 @@ export class Graph extends ManagedData {
* @returns Highest id in list. * @returns Highest id in list.
*/ */
private getHighestId(elements: GraphElement[]): number { private getHighestId(elements: GraphElement[]): number {
let highest: number = 0; let highest = 0;
elements.forEach((element) => { elements.forEach((element) => {
if (highest < element.id) { if (highest < element.id) {
highest = element.id; highest = element.id;
...@@ -82,7 +106,7 @@ export class Graph extends ManagedData { ...@@ -82,7 +106,7 @@ export class Graph extends ManagedData {
protected storableData(data: GraphData): any { protected storableData(data: GraphData): any {
let clean: GraphData; let clean: GraphData;
clean.links = data.links.map((link) => link.getCleanInstance()); clean.links = data.links.map((link) => link.getCleanInstance());
clean.nodes = data.nodes.map((node) => node.getCleanInstance()); clean.nodes = data.nodes.map((node) => node.getCleanInstance());
clean.types = data.types.map((type) => type.getCleanInstance()); clean.types = data.types.map((type) => type.getCleanInstance());
...@@ -99,11 +123,11 @@ export class Graph extends ManagedData { ...@@ -99,11 +123,11 @@ export class Graph extends ManagedData {
* @param data GraphData object to serialize. * @param data GraphData object to serialize.
* @returns Serialized data. * @returns Serialized data.
*/ */
private serializeData(data: GraphData) : any { private serializeData(data: GraphData): any {
return { return {
...this.serializeProperties(GRAPH_PARAMS), ...this.serializeProperties(GRAPH_PARAMS),
...this.serializeProperties(GRAPH_DATA_PARAMS, data), ...this.serializeProperties(GRAPH_DATA_PARAMS, data),
} };
} }
/** /**
...@@ -113,7 +137,7 @@ export class Graph extends ManagedData { ...@@ -113,7 +137,7 @@ export class Graph extends ManagedData {
*/ */
public addNode(node: Node) { public addNode(node: Node) {
if (this.data.nodes.includes(node)) { if (this.data.nodes.includes(node)) {
return true; // Already exists in graph. return true; // Already exists in graph.
} }
// Update id // Update id
...@@ -136,7 +160,7 @@ export class Graph extends ManagedData { ...@@ -136,7 +160,7 @@ export class Graph extends ManagedData {
*/ */
public deleteNode(node: Node): boolean { public deleteNode(node: Node): boolean {
if (!this.data.nodes.includes(node)) { 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); this.data.nodes.filter((n: Node) => n !== node);
...@@ -155,7 +179,9 @@ export class Graph extends ManagedData { ...@@ -155,7 +179,9 @@ export class Graph extends ManagedData {
this.triggerOnChange(); this.triggerOnChange();
// TODO: Use toString implementation of node // 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; return true;
} }
...@@ -167,7 +193,7 @@ export class Graph extends ManagedData { ...@@ -167,7 +193,7 @@ export class Graph extends ManagedData {
*/ */
public addLink(link: Link): boolean { public addLink(link: Link): boolean {
if (this.data.links.includes(link)) { if (this.data.links.includes(link)) {
return true; // Already exists in graph. return true; // Already exists in graph.
} }
// Updateid // Updateid
...@@ -190,7 +216,7 @@ export class Graph extends ManagedData { ...@@ -190,7 +216,7 @@ export class Graph extends ManagedData {
*/ */
public deleteLink(link: Link): boolean { public deleteLink(link: Link): boolean {
if (!this.data.links.includes(link)) { 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); this.data.links.filter((l: Link) => l !== link);
...@@ -201,4 +227,4 @@ export class Graph extends ManagedData { ...@@ -201,4 +227,4 @@ export class Graph extends ManagedData {
return true; return true;
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment