From 1f62b4fde9b436c0d23bade4e24ea357216eee82 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bianchi Date: Tue, 13 Aug 2024 18:11:55 +0200 Subject: [PATCH] fix(Blazor.Dagre): bubbling "child added" event so parents can update their own node/cluster list. Signed-off-by: Jean-Baptiste Bianchi --- .../Models/ClusterViewModel.cs | 18 ++++++++++++++++++ .../Models/GraphViewModel.cs | 1 - 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Neuroglia.Blazor.Dagre/Models/ClusterViewModel.cs b/src/Neuroglia.Blazor.Dagre/Models/ClusterViewModel.cs index 1c02f385..d41247dd 100644 --- a/src/Neuroglia.Blazor.Dagre/Models/ClusterViewModel.cs +++ b/src/Neuroglia.Blazor.Dagre/Models/ClusterViewModel.cs @@ -66,6 +66,7 @@ public ClusterViewModel( child.Changed += OnChildChanged; if (child is IClusterViewModel cluster) { + cluster.ChildAdded += this.OnChildAdded; this._allClusters.Add(cluster.Id, cluster); this.Flatten(cluster); } @@ -106,6 +107,7 @@ public virtual void AddChild(INodeViewModel node) this.ChildAdded?.Invoke(this, node); if (node is IClusterViewModel cluster) { + cluster.ChildAdded += this.OnChildAdded; this._allClusters.Add(cluster.Id, cluster); this.Flatten(cluster); return; @@ -114,6 +116,22 @@ public virtual void AddChild(INodeViewModel node) this.OnChange(); } + /// + public virtual void OnChildAdded(object? sender, INodeViewModel child) + { + if (child is IClusterViewModel cluster) + { + this._allClusters.Add(cluster.Id, cluster); + this.Flatten(cluster); + } + else if (child is INodeViewModel node) + { + this._allNodes.Add(node.Id, node); + } + this.OnChange(); + this.ChildAdded?.Invoke(this, child); + } + /// /// Adds nested nodes/clusters to allNodes/Clusters /// diff --git a/src/Neuroglia.Blazor.Dagre/Models/GraphViewModel.cs b/src/Neuroglia.Blazor.Dagre/Models/GraphViewModel.cs index 1cf1d98e..7d819302 100644 --- a/src/Neuroglia.Blazor.Dagre/Models/GraphViewModel.cs +++ b/src/Neuroglia.Blazor.Dagre/Models/GraphViewModel.cs @@ -429,7 +429,6 @@ public virtual async Task OnWheelAsync(ElementReference sender, WheelEventArgs e if (this.Wheel != null) await this.Wheel.Invoke(new(e, sender, element)); } - /// public virtual void OnChildAdded(object? sender, INodeViewModel child) { if (child is IClusterViewModel cluster)