Example of Purchase Approval Workflow in ASP.NET MVC Diagram Control
Purchase Approval Workflow: a purchase request goes through its approvals. Approve or reject the step that is waiting (yellow); a request over $5,000 needs Finance too. Each step done turns green, and a dot runs along the path taken.
History
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Purchase Approval Workflow: a purchase request goes through its approvals. Approve or reject the step that is waiting (yellow); a
request over $5,000 needs Finance too. Each step done turns green, and a dot runs along the path taken.
</p>
<div class="d-flex flex-wrap gap-2 align-items-center mb-2" role="group" aria-label="Approval">
<label class="d-flex align-items-center gap-1">Amount ($)
<input class="form-control form-control-sm" type="number" id="amount" value="7500" min="0" step="100" style="width:8em">
</label>
<button type="button" class="btn btn-sm btn-outline-primary" id="approveStep">Approve</button>
<button type="button" class="btn btn-sm btn-outline-danger" id="rejectStep">Reject</button>
<button type="button" class="btn btn-sm btn-outline-secondary" id="resetFlow">Start again</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">History</h2>
<ol class="small" id="approvalHistory" aria-labelledby="historyTitle"></ol>
<script>
window.DiagramSamples = (function () {
'use strict';
function status(text) {
var el = document.getElementById('diagramStatus');
if (el) { el.textContent = text; }
}
/** Runs fn once the component has started. */
function whenReady(id, fn) {
var c = window.Lumen && Lumen.get(id);
if (c) { fn(c); } else { window.setTimeout(function () { whenReady(id, fn); }, 50); }
}
var ID = 'purchaseDiagram';
var STEPS = { request: { approve: 'manager' }, manager: { approve: 'amount', reject: 'rejected' }, finance: { approve: 'order', reject: 'rejected' },
order: { approve: 'received' }, received: { approve: 'closed' } };
var current = 'request';
var done = [];
var taken = {};
var busy = false;
function diagram() { return Lumen.get(ID); }
function nameOf(id) { var n = diagram().getNodeObject(id); return n.annotations[0].content; }
function link(from, to) { return diagram().connectors.filter(function (c) { return c.sourceID === from && c.targetID === to; })[0]; }
function finished() { return current === 'closed' || current === 'rejected'; }
function paint() {
var d = diagram();
d.nodes.forEach(function (n) {
n.style.fill = n.id === current ? (current === 'rejected' ? '#ffcdd2' : current === 'closed' ? '#c8e6c9' : '#fff59d') : done.indexOf(n.id) >= 0 ? '#c8e6c9' : null;
});
d.connectors.forEach(function (c) { c.style.strokeColor = taken[c.id] ? '#2e7d32' : null; c.style.strokeWidth = taken[c.id] ? 2 : 1; });
d.dataBind();
var step = STEPS[current];
document.getElementById('approveStep').disabled = busy || !step;
document.getElementById('rejectStep').disabled = busy || !step || !step.reject;
document.getElementById('approveStep').textContent = current === 'request' ? 'Submit' : 'Approve';
status(current === 'closed' ? 'Closed: the goods were received.' : current === 'rejected' ? 'Rejected.' : busy ? 'Moving on...' : 'Waiting for: ' + nameOf(current) + '.');
}
function log(text) {
var item = document.createElement('li');
item.textContent = text;
document.getElementById('approvalHistory').appendChild(item);
}
/** Goes to the next step, the dot running along the connector; the amount decides at the decision. */
function go(to, what) {
var c = link(current, to);
busy = true;
paint();
log(nameOf(current) + ': ' + what);
diagram().animateConnector(c.id, 700).then(function () {
taken[c.id] = true;
done.push(current);
current = to;
if (current === 'amount') {
var over = Number(document.getElementById('amount').value) > 5000;
go(over ? 'finance' : 'order', over ? 'over $5,000' : '$5,000 or less');
return;
}
busy = false;
paint();
});
}
document.addEventListener('click', function (e) {
var id = e.target && e.target.id;
if (id === 'approveStep' && STEPS[current] && !busy) { go(STEPS[current].approve, current === 'request' ? 'submitted' : 'approved'); }
else if (id === 'rejectStep' && STEPS[current] && STEPS[current].reject && !busy) { go(STEPS[current].reject, 'rejected'); }
else if (id === 'resetFlow' && !busy) {
current = 'request';
done = [];
taken = {};
document.getElementById('approvalHistory').innerHTML = '';
paint();
}
});
whenReady(ID, paint);
return { state: function () { return { current: current, done: done.slice(), busy: busy, finished: finished() }; } };
})();
</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/PurchaseApprovalWorkflow.cshtml; it draws what ViewData holds.
public ActionResult PurchaseApprovalWorkflow()
{
// Finance's approval leaves from its bottom and reaches the order from the right, clear of the decision's Yes.
UiDiagramConnector financeApproves = Link("p6", "finance", "order", "Approve");
financeApproves.SourcePortID = "bottom";
financeApproves.TargetPortID = "right";
var hidden = UiDiagramPortVisibility.Hidden;
UiDiagramNode finance = Node("finance", 340, 310, "Finance approval", UiDiagramShapeType.Flow, "Process", 150, 54);
finance.Ports.Add(new UiDiagramPort { Id = "bottom", Offset = new UiDiagramPoint { X = 0.5, Y = 1 }, Visibility = hidden });
UiDiagramNode order = Node("order", 110, 430, "Place the order", UiDiagramShapeType.Flow, "Process", 150, 54);
order.Ports.Add(new UiDiagramPort { Id = "right", Offset = new UiDiagramPoint { X = 1, Y = 0.5 }, Visibility = hidden });
ViewData["diagram"] = new UiDiagramOptions
{
Id = "purchaseDiagram",
Height = "640px",
AriaLabel = "Purchase approval workflow",
Nodes = new List<UiDiagramNode>
{
Node("request", 110, 80, "Purchase request", UiDiagramShapeType.Flow, "Terminator", 150, 50),
Node("manager", 110, 190, "Manager approval", UiDiagramShapeType.Flow, "Process", 150, 54),
Node("amount", 110, 310, "Over $5,000?", UiDiagramShapeType.Flow, "Decision", 150, 80),
finance,
order,
Node("received", 110, 550, "Goods received", UiDiagramShapeType.Flow, "Process", 150, 54),
Node("closed", 340, 550, "Closed", UiDiagramShapeType.Flow, "Terminator", 130, 50),
Node("rejected", 570, 190, "Rejected", UiDiagramShapeType.Flow, "Terminator", 130, 50)
},
Connectors = new List<UiDiagramConnector>
{
Link("p1", "request", "manager"), Link("p2", "manager", "amount", "Approve"), Link("p3", "manager", "rejected", "Reject"),
Link("p4", "amount", "finance", "Yes"), Link("p5", "amount", "order", "No"), financeApproves,
Link("p7", "finance", "rejected", "Reject"), Link("p8", "order", "received"), Link("p9", "received", "closed")
},
ReadOnly = true
};
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>