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

Proper and custom equals method for nodes, links and nodetypes

parent 4cb45c60
No related branches found
No related tags found
1 merge request!2Implemented editor in the react framework
Pipeline #56691 failed
...@@ -190,7 +190,7 @@ export class Graph extends ManagedData { ...@@ -190,7 +190,7 @@ export class Graph extends ManagedData {
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 = this.data.nodes.filter((n: Node) => n.id !== node.id); this.data.nodes = this.data.nodes.filter((n: Node) => !n.equals(node));
try { try {
// No save points should be created when deleting the links // No save points should be created when deleting the links
...@@ -263,11 +263,7 @@ export class Graph extends ManagedData { ...@@ -263,11 +263,7 @@ export class Graph extends ManagedData {
return true; // Doesn't even exist in graph to begin with. return true; // Doesn't even exist in graph to begin with.
} }
// TODO: Filter links by link id this.data.links = this.data.links.filter((l: Link) => !l.equals(link));
this.data.links = this.data.links.filter(
(l: Link) =>
l.sourceId !== link.sourceId || l.targetId !== link.targetId
);
this.triggerOnChange(); this.triggerOnChange();
// TODO: Use toString implementation of link // TODO: Use toString implementation of link
...@@ -316,9 +312,8 @@ export class Graph extends ManagedData { ...@@ -316,9 +312,8 @@ export class Graph extends ManagedData {
// TODO: Remove, when types are directly parsed and not just implicit // TODO: Remove, when types are directly parsed and not just implicit
data.nodes.forEach((node) => { data.nodes.forEach((node) => {
const sharedType: NodeType = data.types.find( const sharedType: NodeType = data.types.find((type) =>
// TODO: Use id instead, but not defined at the moment type.equals(node.type)
(type) => type.name === node.type.name
); );
if (sharedType !== undefined) { if (sharedType !== undefined) {
......
...@@ -48,4 +48,16 @@ export class GraphElement extends SerializableItem { ...@@ -48,4 +48,16 @@ export class GraphElement extends SerializableItem {
'Function "getCleanInstance()" has not been implemented.' 'Function "getCleanInstance()" has not been implemented.'
); );
} }
/**
* Compares to objects. Can be a custom implementation.
* @returns True, if given object is identical.
*/
public equals(other: GraphElement): boolean {
return (
other.node == this.node &&
other.link == this.link &&
other.id == this.id
);
}
} }
...@@ -128,4 +128,16 @@ export class Link extends GraphElement { ...@@ -128,4 +128,16 @@ export class Link extends GraphElement {
return source.toString() + " -> " + target.toString(); return source.toString() + " -> " + target.toString();
} }
public equals(other: GraphElement): boolean {
if (!other.link || other.node) {
return false;
}
const link = other as Link;
return (
link.sourceId === this.sourceId && link.targetId === this.targetId
);
}
} }
...@@ -43,4 +43,14 @@ export class NodeType extends GraphElement { ...@@ -43,4 +43,14 @@ export class NodeType extends GraphElement {
public toString(): string { public toString(): string {
return this.name; return this.name;
} }
public equals(other: GraphElement): boolean {
if (other.link || other.node) {
return false;
}
const type = other as NodeType;
return type.name === this.name;
}
} }
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