Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Shared/Acls/AclScopeTree.razor
T
2026-11-13 21:52:00 -05:00

119 lines
4.7 KiB
Plaintext

@* #520 stage 7 — recursive, single-select renderer for the ACL scope picker.
Deliberately a separate component from RawTree rather than a third mode on it. RawTree already carries
the /raw editing tree AND the equipment reference picker; a third mode would put the scope picker's
bugs on the same page as device authoring, and this repo has no bUnit to catch that. This renders the
unified AclScopeNode, so ONE component covers both the raw and the UNS hierarchy. *@
@using ZB.MOM.WW.OtOpcUa.AdminUI.Acls
@foreach (var node in Nodes)
{
@RenderNode(node, 0)
}
@code {
/// <summary>The rows to render at this level.</summary>
[Parameter, EditorRequired] public IReadOnlyList<AclScopeNode> Nodes { get; set; } = default!;
/// <summary>The <see cref="AclScopeNode.Key"/> of the currently-selected row, if any.</summary>
[Parameter] public string? SelectedKey { get; set; }
/// <summary>Raised when a selectable row is chosen. Single-select: a grant has exactly one scope.</summary>
[Parameter] public EventCallback<AclScopeNode> OnSelect { get; set; }
/// <summary>
/// Loads one lazy level (the raw side). Wired by the host to <c>IAclScopeService.LoadChildrenAsync</c>;
/// when absent, every node is treated as already materialised.
/// </summary>
[Parameter] public Func<AclScopeNode, Task<IReadOnlyList<AclScopeNode>>>? LoadChildren { get; set; }
private RenderFragment RenderNode(AclScopeNode node, int depth) => __builder =>
{
var indent = $"padding-left:{depth * 18}px";
var hasExpander = node.HasLazyChildren || node.Children.Count > 0;
var selected = node.Selectable && node.Key == SelectedKey;
<div @key="node.Key" class="d-flex align-items-center gap-1 py-1" style="@indent">
@if (hasExpander)
{
<button type="button" class="btn btn-sm btn-link p-0 tree-twist" aria-label="@((node.Expanded ? "Collapse " : "Expand ") + node.DisplayName)" aria-expanded="@(node.Expanded ? "true" : "false")"
@onclick="@(() => ToggleAsync(node))">
@(node.Expanded ? "▼" : "▶")
</button>
}
else
{
<span class="tree-twist"></span>
}
@if (node.Selectable)
{
<button type="button" aria-pressed="@(selected ? "true" : "false")"
class="btn btn-sm p-0 px-1 border-0 d-inline-flex align-items-center gap-1 @(selected ? "bg-primary text-white rounded" : "btn-link text-decoration-none")"
@onclick="@(() => OnSelect.InvokeAsync(node))">
<span aria-hidden="true">@node.Icon</span>
<span class="mono small">@node.DisplayName</span>
<span class="chip chip-idle ms-1" style="font-size:0.65rem">@node.ScopeKind</span>
</button>
}
else
{
@* Label only — an enterprise heading, or a level with no ACL scope kind behind it. *@
<span class="d-inline-flex align-items-center gap-1 text-muted">
<span aria-hidden="true">@node.Icon</span>
<span class="mono small">@node.DisplayName</span>
</span>
}
</div>
@if (node.Expanded && node.Loading)
{
<div class="small text-muted" style="@($"padding-left:{(depth + 1) * 18}px")">
<span class="spinner-border spinner-border-sm me-1"></span>Loading&hellip;
</div>
}
else if (node.Expanded && node.Error is not null)
{
<div class="small text-danger" style="@($"padding-left:{(depth + 1) * 18}px")">@node.Error</div>
}
else if (node.Expanded)
{
@foreach (var child in node.Children)
{
@RenderNode(child, depth + 1)
}
}
};
private async Task ToggleAsync(AclScopeNode node)
{
if (node.Loading) { return; }
node.Expanded = !node.Expanded;
if (node.Expanded && node.HasLazyChildren && !node.Loaded && LoadChildren is not null)
{
node.Loading = true;
node.Error = null;
StateHasChanged();
try
{
var children = await LoadChildren(node);
node.Children.Clear();
node.Children.AddRange(children);
node.Loaded = true;
}
catch (Exception ex)
{
node.Error = ex.Message;
}
finally
{
node.Loading = false;
}
}
StateHasChanged();
}
}