Skip to content

Commit

Permalink
Move AddLink to DiagramManager & Add LinkAdded event
Browse files Browse the repository at this point in the history
  • Loading branch information
zHaytam committed Jun 2, 2020
1 parent f0be780 commit b8f9426
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion samples/TestComponents/Simple.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected override void OnInitialized()

var node1 = NewNode(0, 0);
var node2 = NewNode(300, 300);
node1.GetPort(PortAlignment.RIGHT).AddLink(node2.GetPort(PortAlignment.LEFT));
diagramManager.AddLink(node1.GetPort(PortAlignment.RIGHT), node2.GetPort(PortAlignment.LEFT));
diagramManager.AddNode(node1);
diagramManager.AddNode(node2);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Blazor.Diagrams.Core/DiagramManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class DiagramManager
public event Action<Node> NodeAdded;
public event Action<Node> NodeRemoved;
public event Action<Node, bool> NodeSelectionChanged;
public event Action<Link> LinkAdded;

public DiagramManager()
{
Expand Down Expand Up @@ -50,6 +51,15 @@ public void RemoveNode(Node node)
}
}

public Link AddLink(Port source, Port target)
{
var link = new Link(source, target);
source.AddLink(link);
target.AddLink(link);
LinkAdded?.Invoke(link);
return link;
}

public void SelectNode(Node node)
{
if (SelectedNode == node)
Expand Down
19 changes: 7 additions & 12 deletions src/Blazor.Diagrams.Core/Models/Port.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Blazor.Diagrams.Core.Models
{
public class Port : Model
{
private List<Link> _links = new List<Link>(4);

public Port(PortAlignment alignment = PortAlignment.BOTTOM, Point position = null, Size size = null)
{
Alignment = alignment;
Expand All @@ -23,22 +26,14 @@ public Port(string id, PortAlignment alignment = PortAlignment.BOTTOM, Point pos
public Point Offset { get; set; }
public Point Position { get; set; }
public Size Size { get; set; }
public List<Link> Links { get; } = new List<Link>();

public Link AddLink(Port targetPort)
{
var link = new Link(this, targetPort);
AddLink(link);
targetPort.AddLink(link);
return link;
}

internal void AddLink(Link link) => Links.Add(link);
public ReadOnlyCollection<Link> Links => _links.AsReadOnly();

public void RefreshAll()
{
Refresh();
Links.ForEach(l => l.Refresh());
_links.ForEach(l => l.Refresh());
}

internal void AddLink(Link link) => _links.Add(link);
}
}

0 comments on commit b8f9426

Please sign in to comment.