Spike, not the final UI: it answers whether flare-ui can drive Alkane OS's on-box pages, and what it costs. What works: - schema/alkane-bb.json authors the sidebar as data (3 sections, 14 tabs); - src/bin/ui-spike.rs renders it through flare_ui::nav + flare_ui::shell, so the unified sidebar, ordering and surface filtering come from the schema rather than from hand-written HTML; - the render uses the shared bf- design tokens, the same ones the rest of the suite uses. Size, measured rather than assumed (armv7, release, stripped): ui-baseline 323 KiB (maud + serde_json, no flare-ui) ui-spike 771 KiB (flare-ui + wschema + sha2) marginal 448 KiB Vendoring: third-party/flare-ui is the full v0.2.9 tree pinned in flare-ui/flare-ui.lock (a partial archive does not compile, since the crate include_str!s assets from dash-editor/). vendor/ is re-generated as a cargo directory source with the new registry dependencies (maud, sha2, typenum, digest, generic-array, block-buffer, crypto-common, cfg-if, etc.) so the build still works offline. third-party/README.md records how to re-pin.
365 lines
15 KiB
JavaScript
365 lines
15 KiB
JavaScript
// The automation flowchart on screen: nodes, edges and the hands that edit
|
|
// them. It draws the graph flow-doc.js derives and reports what the operator
|
|
// did through hooks; the page that owns the document applies each edit and
|
|
// draws again. Nothing here changes a document.
|
|
//
|
|
// Pointer: drag a step to reorder it; drag from a node's port (the dot under
|
|
// it) to the step that should run next, or to End. Keyboard: arrows move
|
|
// between nodes, Alt+arrows move a step, C then a node and Enter connects,
|
|
// Delete removes, Escape cancels. Both browsers the portal supports deliver
|
|
// these as pointer and key events; nothing needs a secure context.
|
|
(function () {
|
|
"use strict";
|
|
|
|
var SVG = "http://www.w3.org/2000/svg";
|
|
var DRAG_THRESHOLD = 5;
|
|
var counter = 0;
|
|
|
|
function element(tag, className, text) {
|
|
var node = document.createElement(tag);
|
|
if (className) node.className = className;
|
|
if (text != null) node.textContent = text;
|
|
return node;
|
|
}
|
|
|
|
function svg(tag, attributes) {
|
|
var node = document.createElementNS(SVG, tag);
|
|
Object.keys(attributes).forEach(function (name) { node.setAttribute(name, attributes[name]); });
|
|
return node;
|
|
}
|
|
|
|
// hooks: select(id), connect(from, to), move(id, index), remove(id).
|
|
// connect, move and remove return the id to focus afterwards, or a refusal
|
|
// as {error}. The page calls render(graph, state) after each one.
|
|
function mount(host, hooks) {
|
|
var model = window.FlareFlowDoc;
|
|
var uid = "bf-flowchart-" + (counter += 1);
|
|
var root = element("div", "bf-flowchart");
|
|
root.setAttribute("role", "group");
|
|
root.setAttribute("aria-label", "Automation flowchart");
|
|
var edges = svg("svg", { "class": "bf-flowchart__edges", "aria-hidden": "true", focusable: "false" });
|
|
var chain = element("ol", "bf-flowchart__chain");
|
|
var side = element("div", "bf-flowchart__side");
|
|
var live = element("p", "bf-visually-hidden");
|
|
live.setAttribute("role", "status");
|
|
live.setAttribute("aria-live", "polite");
|
|
root.append(edges, chain, side, live);
|
|
host.appendChild(root);
|
|
|
|
var graph = null;
|
|
var state = {};
|
|
var focusId = null;
|
|
var connecting = null;
|
|
var drag = null;
|
|
|
|
function say(message) { live.textContent = message; }
|
|
function nodes() { return Array.from(root.querySelectorAll("button[data-flow-node]")); }
|
|
function byId(id) { return nodes().find(function (node) { return node.dataset.flowNode === id; }); }
|
|
function name(id) {
|
|
if (id === "start") return "Start";
|
|
if (id === "end") return graph.kind === "loop" ? "Next run" : "End";
|
|
var index = model.indexOf(graph, id);
|
|
return "step " + (index + 1);
|
|
}
|
|
|
|
function focus(id) {
|
|
var target = byId(id) || byId("start");
|
|
if (!target) return;
|
|
nodes().forEach(function (node) { node.tabIndex = node === target ? 0 : -1; });
|
|
focusId = target.dataset.flowNode;
|
|
target.focus({ preventScroll: false });
|
|
}
|
|
|
|
function terminal(id, title, detail, extra) {
|
|
var item = element("li", "bf-flowchart__item");
|
|
var button = element("button", "bf-flowchart__node bf-flowchart__node--" + extra);
|
|
button.type = "button";
|
|
button.dataset.flowNode = id;
|
|
button.append(element("strong", null, title), element("small", null, detail));
|
|
item.appendChild(button);
|
|
return item;
|
|
}
|
|
|
|
function port(item, id) {
|
|
var dot = element("span", "bf-flowchart__port");
|
|
dot.dataset.flowPort = id;
|
|
dot.title = "Drag to the step that runs next";
|
|
item.appendChild(dot);
|
|
}
|
|
|
|
function stepItem(node, index) {
|
|
var errors = (state.errors && state.errors[node.id]) || [];
|
|
var item = element("li", "bf-flowchart__item");
|
|
var button = element("button", "bf-flowchart__node");
|
|
button.type = "button";
|
|
button.dataset.flowNode = node.id;
|
|
button.setAttribute("aria-pressed", String(node.id === state.selected));
|
|
var body = element("span");
|
|
body.append(
|
|
element("strong", null, model.label(node.step)),
|
|
element("small", null, model.describe(node.step))
|
|
);
|
|
button.append(element("span", "bf-flowchart__number", String(index + 1)), body);
|
|
if (errors.length) {
|
|
var error = element("span", "bf-flowchart__error", errors.join(" "));
|
|
error.id = uid + "-error-" + node.id;
|
|
button.classList.add("bf-flowchart__node--invalid");
|
|
button.setAttribute("aria-describedby", error.id);
|
|
button.appendChild(error);
|
|
}
|
|
item.appendChild(button);
|
|
port(item, node.id);
|
|
return item;
|
|
}
|
|
|
|
function render(nextGraph, nextState) {
|
|
var hadFocus = root.contains(document.activeElement);
|
|
graph = nextGraph;
|
|
state = nextState || {};
|
|
if (state.focus) focusId = state.focus;
|
|
chain.textContent = "";
|
|
side.textContent = "";
|
|
var loop = graph.kind === "loop";
|
|
var start = terminal("start", "Start", loop ? "Every " + (state.interval || "?") + " s" : "When run", "start");
|
|
port(start, "start");
|
|
chain.appendChild(start);
|
|
model.order(graph).forEach(function (node, index) { chain.appendChild(stepItem(node, index)); });
|
|
chain.appendChild(terminal("end", loop ? "Next run" : "End", loop ? "Waits for the interval" : "Run complete", "end"));
|
|
if (graph.nodes.some(function (node) { return node.id === "stop"; })) {
|
|
var stop = element("div", "bf-flowchart__node bf-flowchart__node--stop");
|
|
stop.dataset.flowStop = "";
|
|
stop.append(element("strong", null, "Stopped"), element("small", null, "A guard was false or a wait timed out"));
|
|
side.appendChild(stop);
|
|
}
|
|
root.classList.toggle("bf-flowchart--connecting", Boolean(connecting));
|
|
var keep = byId(focusId) ? focusId : (byId(state.selected) ? state.selected : "start");
|
|
nodes().forEach(function (node) { node.tabIndex = node.dataset.flowNode === keep ? 0 : -1; });
|
|
focusId = keep;
|
|
if (hadFocus || state.focus) focus(keep);
|
|
draw();
|
|
}
|
|
|
|
// Edges are drawn from the nodes' measured boxes, so the chart follows
|
|
// whatever width the page gives it.
|
|
function draw() {
|
|
edges.textContent = "";
|
|
if (!graph || root.hidden || !root.offsetParent) return;
|
|
var frame = root.getBoundingClientRect();
|
|
edges.setAttribute("width", String(frame.width));
|
|
edges.setAttribute("height", String(frame.height));
|
|
var defs = svg("defs", {});
|
|
var marker = svg("marker", { id: uid + "-arrow", viewBox: "0 0 8 8", refX: "7", refY: "4", markerWidth: "8", markerHeight: "8", orient: "auto-start-reverse" });
|
|
marker.appendChild(svg("path", { d: "M0,0 L8,4 L0,8 z", "class": "bf-flowchart__arrow" }));
|
|
defs.appendChild(marker);
|
|
edges.appendChild(defs);
|
|
function box(id) {
|
|
var node = id === "stop" ? side.querySelector("[data-flow-stop]") : byId(id);
|
|
if (!node) return null;
|
|
var r = node.getBoundingClientRect();
|
|
return { left: r.left - frame.left, right: r.right - frame.left, top: r.top - frame.top, bottom: r.bottom - frame.top, x: r.left - frame.left + r.width / 2, y: r.top - frame.top + r.height / 2 };
|
|
}
|
|
var lane = Math.max.apply(null, nodes().map(function (node) { return box(node.dataset.flowNode).right; })) + 12;
|
|
graph.edges.forEach(function (edge) {
|
|
var a = box(edge.from);
|
|
var b = box(edge.to);
|
|
if (!a || !b) return;
|
|
var d;
|
|
var text = null;
|
|
if (edge.kind === "next") {
|
|
d = "M" + a.x + "," + a.bottom + " V" + b.top;
|
|
} else if (edge.kind === "repeat") {
|
|
var left = Math.min(a.left, b.left) - 12;
|
|
d = "M" + a.left + "," + a.y + " H" + left + " V" + b.y + " H" + b.left;
|
|
text = { x: left + 4, y: b.y - 6, value: "repeat" };
|
|
} else if (edge.to === "stop") {
|
|
d = "M" + a.right + "," + a.y + " H" + b.x + " V" + (b.top > a.y ? b.top : b.bottom);
|
|
text = { x: a.right + 4, y: a.y - 4, value: edge.kind === "false" ? "false" : "timed out" };
|
|
} else {
|
|
d = "M" + a.right + "," + a.y + " H" + lane + " V" + b.y + " H" + b.right;
|
|
text = { x: a.right + 4, y: a.y - 4, value: edge.kind === "false" ? "false" : "timed out" };
|
|
}
|
|
edges.appendChild(svg("path", { d: d, "class": "bf-flowchart__edge bf-flowchart__edge--" + edge.kind, "marker-end": "url(#" + uid + "-arrow)" }));
|
|
if (text) {
|
|
var label = svg("text", { x: String(text.x), y: String(text.y), "class": "bf-flowchart__label" });
|
|
label.textContent = text.value;
|
|
edges.appendChild(label);
|
|
}
|
|
});
|
|
}
|
|
|
|
function result(outcome, fallback) {
|
|
if (outcome && outcome.error) {
|
|
say(outcome.error);
|
|
return null;
|
|
}
|
|
return outcome || fallback;
|
|
}
|
|
|
|
function finishConnect(to) {
|
|
var from = connecting;
|
|
connecting = null;
|
|
root.classList.remove("bf-flowchart--connecting");
|
|
var id = result(hooks.connect(from, to), to);
|
|
if (id) {
|
|
say("Connected: " + name(typeof id === "string" ? id : to) + " runs next.");
|
|
if (typeof id === "string") focus(id);
|
|
}
|
|
}
|
|
|
|
root.addEventListener("click", function (event) {
|
|
var button = event.target.closest("button[data-flow-node]");
|
|
if (!button || drag) return;
|
|
var id = button.dataset.flowNode;
|
|
focusId = id;
|
|
if (connecting) {
|
|
finishConnect(id);
|
|
return;
|
|
}
|
|
hooks.select(id === "start" || id === "end" ? null : id);
|
|
});
|
|
|
|
root.addEventListener("focusin", function (event) {
|
|
var button = event.target.closest("button[data-flow-node]");
|
|
if (button) {
|
|
focusId = button.dataset.flowNode;
|
|
nodes().forEach(function (node) { node.tabIndex = node === button ? 0 : -1; });
|
|
}
|
|
});
|
|
|
|
root.addEventListener("keydown", function (event) {
|
|
var button = event.target.closest("button[data-flow-node]");
|
|
if (!button) return;
|
|
var id = button.dataset.flowNode;
|
|
var list = nodes();
|
|
var at = list.indexOf(button);
|
|
var step = id !== "start" && id !== "end";
|
|
var key = event.key;
|
|
if (key === "Escape") {
|
|
if (!connecting) return;
|
|
connecting = null;
|
|
root.classList.remove("bf-flowchart--connecting");
|
|
say("Connecting cancelled.");
|
|
} else if (event.altKey && (key === "ArrowUp" || key === "ArrowDown")) {
|
|
if (!step) return;
|
|
var moved = result(hooks.move(id, model.indexOf(graph, id) + (key === "ArrowUp" ? -1 : 1)));
|
|
if (moved) focus(moved);
|
|
} else if (key === "ArrowUp" || key === "ArrowDown" || key === "Home" || key === "End") {
|
|
var next = key === "Home" ? 0 : key === "End" ? list.length - 1 : at + (key === "ArrowUp" ? -1 : 1);
|
|
if (next < 0 || next >= list.length) return;
|
|
focus(list[next].dataset.flowNode);
|
|
} else if ((key === "Delete" || key === "Backspace") && step && !connecting) {
|
|
var after = result(hooks.remove(id));
|
|
if (after) {
|
|
say("Removed " + name(id) + ".");
|
|
focus(after);
|
|
}
|
|
} else if ((key === "c" || key === "C") && !event.ctrlKey && !event.metaKey && !event.altKey && id !== "end") {
|
|
connecting = id;
|
|
root.classList.add("bf-flowchart--connecting");
|
|
say("Connecting from " + name(id) + ". Choose the step that runs next and press Enter, or Escape to cancel.");
|
|
} else {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
});
|
|
|
|
// Pointer: a press on a step is a click until it travels; then the step
|
|
// moves among the others and letting go asks the page to move it there.
|
|
// A press on a port draws a line to the pointer; letting go over a node
|
|
// connects to it.
|
|
root.addEventListener("pointerdown", function (event) {
|
|
if (event.button !== 0) return;
|
|
var dot = event.target.closest("[data-flow-port]");
|
|
if (dot) {
|
|
event.preventDefault();
|
|
var from = dot.getBoundingClientRect();
|
|
drag = { port: dot.dataset.flowPort, x: from.left + from.width / 2, y: from.top + from.height / 2, active: true };
|
|
drag.line = svg("path", { "class": "bf-flowchart__edge bf-flowchart__edge--pending" });
|
|
edges.appendChild(drag.line);
|
|
document.body.classList.add("bf-dragging");
|
|
return;
|
|
}
|
|
var button = event.target.closest("button[data-flow-node]");
|
|
if (!button || button.dataset.flowNode === "start" || button.dataset.flowNode === "end") return;
|
|
drag = { item: button.parentNode, id: button.dataset.flowNode, y: event.clientY, active: false };
|
|
});
|
|
|
|
function stepItems() {
|
|
return Array.from(chain.children).filter(function (item) {
|
|
var node = item.querySelector("[data-flow-node]");
|
|
return node && node.dataset.flowNode !== "start" && node.dataset.flowNode !== "end";
|
|
});
|
|
}
|
|
|
|
document.addEventListener("pointermove", function (event) {
|
|
if (!drag) return;
|
|
if (drag.port) {
|
|
var frame = root.getBoundingClientRect();
|
|
drag.line.setAttribute("d", "M" + (drag.x - frame.left) + "," + (drag.y - frame.top) +
|
|
" L" + (event.clientX - frame.left) + "," + (event.clientY - frame.top));
|
|
return;
|
|
}
|
|
if (!drag.active) {
|
|
if (Math.abs(event.clientY - drag.y) < DRAG_THRESHOLD) return;
|
|
drag.active = true;
|
|
drag.item.classList.add("bf-flowchart__item--dragging");
|
|
document.body.classList.add("bf-dragging");
|
|
edges.textContent = "";
|
|
}
|
|
event.preventDefault();
|
|
var others = stepItems().filter(function (item) { return item !== drag.item; });
|
|
var before = others.find(function (item) {
|
|
var r = item.getBoundingClientRect();
|
|
return event.clientY < r.top + r.height / 2;
|
|
});
|
|
if (before) chain.insertBefore(drag.item, before);
|
|
else if (others.length) others[others.length - 1].after(drag.item);
|
|
});
|
|
|
|
function endDrag(commit, event) {
|
|
var current = drag;
|
|
if (!current) return;
|
|
document.body.classList.remove("bf-dragging");
|
|
if (current.port) {
|
|
drag = null;
|
|
current.line.remove();
|
|
var target = commit && document.elementFromPoint(event.clientX, event.clientY);
|
|
var node = target && target.closest && target.closest("button[data-flow-node]");
|
|
if (node && root.contains(node)) {
|
|
connecting = current.port;
|
|
finishConnect(node.dataset.flowNode);
|
|
}
|
|
return;
|
|
}
|
|
if (!current.active) {
|
|
drag = null;
|
|
return;
|
|
}
|
|
// The click that follows this pointerup must not select: keep `drag`
|
|
// set until that click's task has run.
|
|
setTimeout(function () { drag = null; }, 0);
|
|
current.item.classList.remove("bf-flowchart__item--dragging");
|
|
var to = stepItems().indexOf(current.item);
|
|
if (!commit || to === model.indexOf(graph, current.id)) {
|
|
render(graph, state);
|
|
return;
|
|
}
|
|
var moved = result(hooks.move(current.id, to));
|
|
if (moved) say("Moved to step " + (to + 1) + ".");
|
|
else render(graph, state);
|
|
}
|
|
|
|
document.addEventListener("pointerup", function (event) { endDrag(true, event); });
|
|
document.addEventListener("pointercancel", function (event) { endDrag(false, event); });
|
|
document.addEventListener("keydown", function (event) {
|
|
if (event.key === "Escape" && drag && (drag.active || drag.port)) endDrag(false, event);
|
|
});
|
|
|
|
if (typeof ResizeObserver === "function") new ResizeObserver(function () { if (!drag) draw(); }).observe(root);
|
|
|
|
return { render: render, draw: draw, say: say, root: root };
|
|
}
|
|
|
|
window.FlareFlowView = { mount: mount };
|
|
}());
|