Example of History Manager in ASP.NET MVC Diagram Control
History Manager: move, resize, connect, add or delete, then undo and redo (Ctrl+Z and Ctrl+Y, or the buttons). The list shows what was done; the diagram keeps the last 50 changes.
Changes
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
History Manager: move, resize, connect, add or delete, then undo and redo (Ctrl+Z and Ctrl+Y, or the buttons). The list shows
what was done; the diagram keeps the last 50 changes.
</p>
<div class="mb-2" role="toolbar" aria-label="History" data-dg-target="historyDiagram">
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="undo" disabled>Undo</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="redo" disabled>Redo</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-dg-command="clearHistory">Clear history</button>
</div>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p class="small text-muted mt-2" id="diagramStatus" role="status" aria-live="polite"></p>
<h2 class="h6 mt-2" id="historyTitle">Changes</h2>
<ol class="small" id="historyList" aria-labelledby="historyTitle"></ol>
<script>
window.DiagramSamples = (function () {
'use strict';
function status(text) {
var el = document.getElementById('diagramStatus');
if (el) { el.textContent = text; }
}
/** The toolbar's buttons: data-dg-command is the diagram's method, data-dg-arg its argument. */
document.addEventListener('click', function (e) {
var button = e.target.closest ? e.target.closest('[data-dg-command]') : null;
if (!button) { return; }
var bar = button.closest('[data-dg-target]');
var diagram = bar ? Lumen.get(bar.getAttribute('data-dg-target')) : null;
if (!diagram) { return; }
var arg = button.getAttribute('data-dg-arg');
var done = diagram[button.getAttribute('data-dg-command')](arg === null ? undefined : arg);
var nothing = done === false || done === null || done === 0 || (Array.isArray(done) && !done.length);
status(button.textContent + (nothing ? ': nothing to do (select more, or something else).' : ': done.'));
});
/** OnHistoryChange: the buttons follow what can be undone and redone; the change goes in the list. */
function history(args) {
var bar = document.querySelector('[data-dg-target="historyDiagram"]');
bar.querySelector('[data-dg-command="undo"]').disabled = !args.canUndo;
bar.querySelector('[data-dg-command="redo"]').disabled = !args.canRedo;
var list = document.getElementById('historyList');
if (args.change === 'Cleared') { list.innerHTML = ''; return; }
var item = document.createElement('li');
item.textContent = args.change;
list.insertBefore(item, list.firstChild);
while (list.children.length > 50) { list.removeChild(list.lastChild); }
}
return { history: history };
})();
</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/HistoryManager.cshtml; it draws what ViewData holds.
public ActionResult HistoryManager()
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "historyDiagram",
Height = "420px",
AriaLabel = "Diagram with undo and redo",
Nodes = new List<UiDiagramNode>
{
Node("draft", 120, 120, "Draft", UiDiagramShapeType.Flow, "Document"),
Node("review", 340, 120, "Review"),
Node("publish", 560, 120, "Publish", UiDiagramShapeType.Flow, "Terminator")
},
Connectors = new List<UiDiagramConnector> { Link("h1", "draft", "review"), Link("h2", "review", "publish") },
HistoryLimit = 50,
OnHistoryChange = "DiagramSamples.history"
};
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>