Example of User Handles in ASP.NET MVC Diagram Control
User Handles: select a node and use the buttons by it: clone it, delete it, or draw a connector from it (drag from the node to another after pressing the pencil). The handles can be reached with Tab and pressed with Enter.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
User Handles: select a node and use the buttons by it: clone it, delete it, or draw a connector from it (drag from the node to another
after pressing the pencil). The handles can be reached with Tab and pressed with Enter.
</p>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p class="small text-muted mt-2" id="diagramStatus" role="status" aria-live="polite"></p>
<script>
window.DiagramSamples = (function () {
'use strict';
function status(text) {
var el = document.getElementById('diagramStatus');
if (el) { el.textContent = text; }
}
/** OnUserHandleClick: clone, delete, or draw a connector from the node. */
function handle(args) {
var d = Lumen.get('handlesDiagram');
var o = args.element;
if (!o) { return; }
if (args.handle === 'clone') {
d.copy();
var made = d.paste();
status('Cloned ' + o.id + (made.length ? ' as ' + made[0].id : '') + '.');
} else if (args.handle === 'delete') {
d.remove(o);
status('Deleted ' + o.id + '.');
} else if (args.handle === 'draw') {
d.setTool('DrawOnce', { id: 'link', type: 'Orthogonal', sourcePoint: { x: 0, y: 0 }, targetPoint: { x: 0, y: 0 } });
status('Drag from ' + o.id + ' to another node to draw a connector.');
}
}
return { handle: handle };
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DiagramController : Controller
{
// The page is Views/Diagram/UserHandles.cshtml; it draws what ViewData holds.
public ActionResult UserHandles()
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "handlesDiagram",
Height = "440px",
AriaLabel = "Diagram with user handles",
Nodes = new List<UiDiagramNode>
{
Node("idea", 160, 140, "Idea", UiDiagramShapeType.Basic, "Ellipse", 130, 70, "#e8f0fe"),
Node("plan", 420, 140, "Plan", UiDiagramShapeType.Basic, "Rectangle", 130, 60, "#e6f4ea"),
Node("build", 420, 320, "Build", UiDiagramShapeType.Basic, "Rectangle", 130, 60, "#fff4e5")
},
Connectors = new List<UiDiagramConnector> { Link("u1", "idea", "plan") },
UserHandles = new List<UiDiagramUserHandle>
{
new UiDiagramUserHandle { Name = "clone", Tooltip = "Clone", Side = UiDiagramSide.Right, Offset = 0, Margin = 20,
PathData = "M16 1H4a2 2 0 0 0-2 2v14h2V3h12V1zm3 4H8a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h11a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2zm0 16H8V7h11v14z" },
new UiDiagramUserHandle { Name = "delete", Tooltip = "Delete", Side = UiDiagramSide.Right, Offset = 1, Margin = 20, BackgroundColor = "#c62828",
PathData = "M6 19a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z" },
new UiDiagramUserHandle { Name = "draw", Tooltip = "Draw a connector from here", Side = UiDiagramSide.Bottom, Offset = 0.5, Margin = 20,
PathData = "M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04a1 1 0 0 0 0-1.41l-2.34-2.34a1 1 0 0 0-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z" }
},
OnUserHandleClick = "DiagramSamples.handle"
};
return View();
}
private static UiDiagramNode Node(string id, double x, double y, string text, UiDiagramShapeType type = UiDiagramShapeType.Flow, string shape = "Process",
double width = 120, double height = 54, string fill = null)
{
var node = new UiDiagramNode
{
Id = id, OffsetX = x, OffsetY = y, Width = width, Height = height,
Shape = new UiDiagramShape { Type = type, Shape = shape },
Style = new UiDiagramStyle { Fill = fill }
};
if (text != null) { node.Annotations.Add(new UiDiagramAnnotation { Content = text }); }
return node;
}
private static UiDiagramConnector Link(string id, string from, string to, string label = null,
UiDiagramConnectorType type = UiDiagramConnectorType.Orthogonal)
{
var c = new UiDiagramConnector { Id = id, SourceID = from, TargetID = to, Type = type };
if (label != null) { c.Annotations.Add(new UiDiagramAnnotation { Content = label, Offset = new UiDiagramPoint { X = 0.5, Y = 0 } }); }
return c;
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>