From a84232f97751db162c151772b62f037b2329a76d Mon Sep 17 00:00:00 2001
From: Max <m.giller.dev@gmail.com>
Date: Wed, 20 Oct 2021 15:46:27 +0200
Subject: [PATCH] Added simulation handling to Graph

---
 editor/js/editor.js |  8 +++-----
 editor/js/graph.js  | 26 ++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/editor/js/editor.js b/editor/js/editor.js
index cdd8d53..c9fd936 100644
--- a/editor/js/editor.js
+++ b/editor/js/editor.js
@@ -26,11 +26,7 @@ window.onload = function () {
             graph = new Graph.Graph(graphConfig);
             load();
 
-            // Deactivate physics after a short delay
-            setTimeout(() => {
-                graph.stopPhysics();
-                graph.storeCurrentData("Physics stopped");
-            }, graph.physicsDelay);
+            graph.restartSimulation();
         });
 };
 
@@ -71,4 +67,6 @@ function load() {
         graphObj.cooldownTicks(0);
         graphObj.graphData(data);
     });
+
+    graph.setRenderer(graphObj);
 }
diff --git a/editor/js/graph.js b/editor/js/graph.js
index d3a6c27..d1949d2 100644
--- a/editor/js/graph.js
+++ b/editor/js/graph.js
@@ -46,10 +46,36 @@ export class Graph extends ManagedData {
         this.calculateLinkTypes();
         this.onChangeCallbacks = [];
         this.physicsDelay = STOP_PHYSICS_DELAY;
+        this.physicsStopTimeoutId = undefined;
+        this.graphRenderer = undefined;
+    }
+
+    setRenderer(graphRenderer) {
+        this.graphRenderer = graphRenderer;
     }
 
     restartSimulation() {
+        if (this.physicsStopTimeoutId !== undefined) {
+            clearTimeout(this.physicsStopTimeoutId);
+        }
+
+        if (this.graphRenderer !== undefined) {
+            this.data[GRAPH_NODES].forEach((n) => {
+                n.x /= 10;
+                n.y /= 10;
+                n.fx = undefined;
+                n.fy = undefined;
+            });
+
+            this.graphRenderer.d3ReheatSimulation();
+        }
 
+        // Deactivate physics after a short delay
+        this.physicsStopTimeoutId = setTimeout(() => {
+            this.stopPhysics();
+            this.storeCurrentData("Physics stopped");
+            this.physicsStopTimeoutId = undefined;
+        }, this.physicsDelay);
     }
 
     triggerOnChange() {
-- 
GitLab