Example of Constraints in ASP.NET MVC Diagram Control
Constraints: each node allows less than the default, as its text says. The connector's ends cannot be dragged. Try to move, resize, select, delete (Delete key), edit (double-click or F2) or connect to each.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Constraints: each node allows less than the default, as its text says. The connector's ends cannot be dragged. Try to move,
resize, select, delete (Delete key), edit (double-click or F2) or connect to each.
</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; }
}
/** OnCollectionChange: what was added or removed. */
function collection(args) {
status(args.type + ': ' + args.element.id + '.');
}
return { collection: collection };
})();
</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/Constraints.cshtml; it draws what ViewData holds.
public ActionResult Constraints()
{
const UiDiagramNodeConstraints all = UiDiagramNodeConstraints.Default;
System.Func<string, double, double, string, UiDiagramNodeConstraints, UiDiagramNode> node = (id, x, y, text, constraints) =>
{
UiDiagramNode n = Node(id, x, y, text, UiDiagramShapeType.Basic, "Rectangle", 150, 60, "#e8f0fe");
n.Constraints = constraints;
return n;
};
var fixedEnds = Link("fixedEnds", "noDelete", "noIncoming", "Ends fixed", UiDiagramConnectorType.Straight);
fixedEnds.Constraints = UiDiagramConnectorConstraints.Default & ~UiDiagramConnectorConstraints.DragEnds;
ViewData["diagram"] = new UiDiagramOptions
{
Id = "constraintsDiagram",
Height = "460px",
AriaLabel = "Nodes with constraints",
Nodes = new List<UiDiagramNode>
{
node("noDrag", 120, 90, "Cannot move", all & ~UiDiagramNodeConstraints.Drag),
node("noResize", 340, 90, "Cannot resize or rotate", all & ~(UiDiagramNodeConstraints.Resize | UiDiagramNodeConstraints.Rotate)),
node("noSelect", 560, 90, "Cannot be selected", all & ~UiDiagramNodeConstraints.Select),
node("noDelete", 120, 260, "Cannot be deleted", all & ~UiDiagramNodeConstraints.Delete),
node("noEdit", 340, 260, "Text cannot be edited", all & ~UiDiagramNodeConstraints.Edit),
node("noIncoming", 560, 260, "No connector can end here", all & ~UiDiagramNodeConstraints.InConnect)
},
Connectors = new List<UiDiagramConnector> { fixedEnds },
OnCollectionChange = "DiagramSamples.collection"
};
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>