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

Adds the missing node type interface into the graph

parent d488c0bc
No related branches found
No related tags found
1 merge request!2Implemented editor in the react framework
Pipeline #56800 passed
...@@ -157,6 +157,36 @@ export class Graph extends ManagedData { ...@@ -157,6 +157,36 @@ export class Graph extends ManagedData {
}; };
} }
/**
* Adds a pre-created node type to the graph.
* @param nodeType New node type object.
* @returns True, if successful.
*/
public addNodeType(nodeType: NodeType) {
if (this.data.types.includes(nodeType)) {
return true; // Already exists in graph.
}
// Update id
nodeType.id = this.nextTypeId;
this.nextTypeId += 1;
// Is valid node?
if (nodeType.name == undefined) {
nodeType.name = "Unnamed";
}
if (nodeType.color == undefined) {
nodeType.color = "#000000";
}
this.data.types.push(node);
this.triggerOnChange();
this.storeCurrentData("Added node [" + nodeType + "]");
return true;
}
/** /**
* Adds a pre-created node to the graph. * Adds a pre-created node to the graph.
* @param node New node object. * @param node New node object.
...@@ -180,21 +210,59 @@ export class Graph extends ManagedData { ...@@ -180,21 +210,59 @@ export class Graph extends ManagedData {
// Just give first type in list // Just give first type in list
node.type = this.types[0]; node.type = this.types[0];
} else { } else {
// Give empty type const newType = new NodeType(this);
// TODO: Properly add new type, with proper ID. Implemented this.addType(..); newType.add();
node.type = new NodeType(this); node.type = newType;
} }
} }
this.data.nodes.push(node); this.data.nodes.push(node);
this.triggerOnChange(); this.triggerOnChange();
// TODO: Use toString implementation of node
this.storeCurrentData("Added node [" + node + "]"); this.storeCurrentData("Added node [" + node + "]");
return true; return true;
} }
/**
* Deletes a node type from the graph. Only works if at least one type remains after deletion.
* @param nodeType Node type object to remove.
* @returns True, if successful.
*/
public deleteNodeType(nodeType: NodeType): boolean {
// Only allow deletion if at least one other type remains
if (this.types.length <= 1) {
return false;
}
if (!this.data.types.includes(nodeType)) {
return true; // Doesn't even exist in graph to begin with.
}
this.data.types = this.data.types.filter((n: NodeType) => !n.equals(nodeType));
try {
// No save points should be created when replacing usages
this.disableStoring();
// Replace all usages of this type with the first one in the list
this.nodes.forEach((n: Node) => {
if (n.type.equals(nodeType)) {
n.type = this.types[0];
}
});
} finally {
this.enableStoring();
}
this.triggerOnChange();
this.storeCurrentData(
"Deleted type [" + nodeType + "] and replaced usages"
);
return true;
}
/** /**
* Deletes a node from the graph. * Deletes a node from the graph.
* @param node Node object to remove. * @param node Node object to remove.
...@@ -220,7 +288,6 @@ export class Graph extends ManagedData { ...@@ -220,7 +288,6 @@ export class Graph extends ManagedData {
} }
this.triggerOnChange(); this.triggerOnChange();
// TODO: Use toString implementation of node
this.storeCurrentData( this.storeCurrentData(
"Deleted node [" + node + "] and all connected links" "Deleted node [" + node + "] and all connected links"
); );
...@@ -262,7 +329,6 @@ export class Graph extends ManagedData { ...@@ -262,7 +329,6 @@ export class Graph extends ManagedData {
this.data.links.push(link); this.data.links.push(link);
this.triggerOnChange(); this.triggerOnChange();
// TODO: Use toString implementation of link
this.storeCurrentData("Added link [" + link + "]"); this.storeCurrentData("Added link [" + link + "]");
return true; return true;
...@@ -281,7 +347,6 @@ export class Graph extends ManagedData { ...@@ -281,7 +347,6 @@ export class Graph extends ManagedData {
this.data.links = this.data.links.filter((l: Link) => !l.equals(link)); this.data.links = this.data.links.filter((l: Link) => !l.equals(link));
this.triggerOnChange(); this.triggerOnChange();
// TODO: Use toString implementation of link
this.storeCurrentData("Deleted link [" + link + "]"); this.storeCurrentData("Deleted link [" + link + "]");
return true; return true;
......
...@@ -13,13 +13,16 @@ export class NodeType extends GraphElement { ...@@ -13,13 +13,16 @@ export class NodeType extends GraphElement {
} }
public delete(): boolean { public delete(): boolean {
// TODO: Implement return this.graph.deleteNodeType(this);
throw new Error('Function "delete()" has not been implemented.');
} }
public add(graph: Graph = this.graph): boolean { public add(graph: Graph = this.graph): boolean {
// TODO: Implement this.graph = graph;
throw new Error('Function "add(graph)" has not been implemented.'); if (this.graph == undefined) {
return false;
}
return this.graph.addNodeType(this);
} }
public getCleanInstance(): any { public getCleanInstance(): any {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment