From b167d87f6534cfeb55765882e21ef943948e74b0 Mon Sep 17 00:00:00 2001 From: Max <m.giller@tu-bs.de> Date: Thu, 28 Apr 2022 11:36:37 +0200 Subject: [PATCH] Added basic getters --- src/editor/js/structures/graph/graph.ts | 64 +++++++++++++++++-------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/src/editor/js/structures/graph/graph.ts b/src/editor/js/structures/graph/graph.ts index ccadd99..7a68061 100644 --- a/src/editor/js/structures/graph/graph.ts +++ b/src/editor/js/structures/graph/graph.ts @@ -1,24 +1,24 @@ 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 +} -- GitLab