Example of Events in ASP.NET MVC Diagram Control
Events: click, double-click, select, move, resize, rotate, reconnect, add, delete or edit the text, and each event the diagram raises is written below, newest first.
Events
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Events: click, double-click, select, move, resize, rotate, reconnect, add, delete or edit the text, and each event the diagram
raises is written below, newest first.
</p>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<div class="d-flex align-items-center mt-2">
<h2 class="h6 mb-0 me-2" id="eventLogTitle">Events</h2>
<button type="button" class="btn btn-sm btn-outline-secondary" id="clearEvents">Clear</button>
</div>
<ol class="small mt-1" id="eventLog" aria-labelledby="eventLogTitle"></ol>
<script>
window.DiagramSamples = (function () {
'use strict';
function details(args) {
var target = args.element || args.source || args.connector;
var parts = [];
if (target && target.id) { parts.push(target.id); }
if (args.type) { parts.push(args.type); }
if (args.state) { parts.push(args.state); }
if (args.change) { parts.push(args.change); }
if (args.name === 'selectionChange') { parts.push(args.newValue.length + ' selected'); }
if (args.name === 'textEdit') { parts.push('"' + args.newValue + '"'); }
return parts.length ? ': ' + parts.join(', ') : '';
}
/** Every event: its name and what it is about, newest first. */
function log(args) {
var list = document.getElementById('eventLog');
var item = document.createElement('li');
item.textContent = args.name + details(args);
list.insertBefore(item, list.firstChild);
while (list.children.length > 40) { list.removeChild(list.lastChild); }
}
document.addEventListener('click', function (e) {
if (e.target && e.target.id === 'clearEvents') { document.getElementById('eventLog').innerHTML = ''; }
});
return { log: log };
})();
</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/Events.cshtml; it draws what ViewData holds.
public ActionResult Events()
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "eventsDiagram",
Height = "420px",
AriaLabel = "Diagram whose events are logged",
Nodes = new List<UiDiagramNode>
{
Node("start", 120, 100, "Start", UiDiagramShapeType.Flow, "Terminator"),
Node("work", 120, 230, "Do the work"),
Node("check", 360, 230, "Done?", UiDiagramShapeType.Flow, "Decision", 120, 80)
},
Connectors = new List<UiDiagramConnector> { Link("e1", "start", "work"), Link("e2", "work", "check") },
OnClick = "DiagramSamples.log",
OnDoubleClick = "DiagramSamples.log",
OnSelectionChange = "DiagramSamples.log",
OnPositionChange = "DiagramSamples.log",
OnSizeChange = "DiagramSamples.log",
OnRotateChange = "DiagramSamples.log",
OnConnectionChange = "DiagramSamples.log",
OnCollectionChange = "DiagramSamples.log",
OnTextEdit = "DiagramSamples.log",
OnHistoryChange = "DiagramSamples.log"
};
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>