diff --git a/forum-network/src/classes/supporting/vertex.js b/forum-network/src/classes/supporting/vertex.js index d02d4b8..4e2e79a 100644 --- a/forum-network/src/classes/supporting/vertex.js +++ b/forum-network/src/classes/supporting/vertex.js @@ -142,6 +142,18 @@ export class Vertex { cb: () => graph.resetEditorDocument(), }); + if (vertex) { + form.button({ + id: 'delete', + name: 'Delete Vertex', + cb: () => { + graph.deleteVertex(vertex.id); + graph.redraw(); + graph.resetEditorDocument(); + }, + }); + } + return doc; } } diff --git a/forum-network/src/classes/supporting/wdg.js b/forum-network/src/classes/supporting/wdg.js index 4f72777..c0e71cb 100644 --- a/forum-network/src/classes/supporting/wdg.js +++ b/forum-network/src/classes/supporting/wdg.js @@ -146,22 +146,6 @@ export class WeightedDirectedGraph { return edges?.get(edgeKey); } - deleteEdge(type, from, to) { - from = from instanceof Vertex ? from : this.getVertex(from); - to = to instanceof Vertex ? to : this.getVertex(to); - const edges = this.edgeTypes.get(type); - const edgeKey = Edge.getKey({ type, from, to }); - if (!edges) return; - const edge = edges.get(edgeKey); - if (!edge) return; - to.edges.from.forEach((x, i) => (x === edge) && to.edges.from.splice(i, 1)); - from.edges.to.forEach((x, i) => (x === edge) && from.edges.to.splice(i, 1)); - edges.delete(edgeKey); - if (edges.size === 0) { - this.edgeTypes.delete(type); - } - } - getEdgeWeight(type, from, to) { return this.getEdge(type, from, to)?.weight; } @@ -219,4 +203,28 @@ export class WeightedDirectedGraph { } return Array.from(this.vertices.values()).filter((vertex) => vertex.type === type).length; } + + deleteEdge(type, from, to) { + from = from instanceof Vertex ? from : this.getVertex(from); + to = to instanceof Vertex ? to : this.getVertex(to); + const edges = this.edgeTypes.get(type); + const edgeKey = Edge.getKey({ type, from, to }); + if (!edges) return; + const edge = edges.get(edgeKey); + if (!edge) return; + to.edges.from.forEach((x, i) => (x === edge) && to.edges.from.splice(i, 1)); + from.edges.to.forEach((x, i) => (x === edge) && from.edges.to.splice(i, 1)); + edges.delete(edgeKey); + if (edges.size === 0) { + this.edgeTypes.delete(type); + } + } + + deleteVertex(id) { + const vertex = this.getVertex(id); + for (const { type, from, to } of [...vertex.edges.to, ...vertex.edges.from]) { + this.deleteEdge(type, from, to); + } + this.vertices.delete(id); + } }