Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Shared/Uns/AddReferenceModal.razor
T

148 lines
7.3 KiB
Plaintext

@* v3 Batch 3 "+ Add reference" picker: reuses the Raw project tree (RawTree) in picker mode to
multi-select raw tags, scoped to the equipment's own cluster. The cluster scope is structural —
IUnsTreeService.LoadReferencePickerRootAsync hands back a SINGLE cluster root, so raw tags of any
other cluster are simply unreachable through the tree, not merely hidden. On commit the selected
tag ids are added as UnsTagReference rows via AddReferencesAsync; the host reloads its Tags list. *@
@using ZB.MOM.WW.OtOpcUa.AdminUI.Uns
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Raw
@inject IUnsTreeService Svc
@implements IDisposable
@{
var presentation = _generation;
var completedAction = OnCommitted;
var cancelAction = OnCancel;
}
<ModalFrame DialogClass="modal-lg modal-dialog-scrollable" Visible="@Visible" Title='Add tag references' Target="@presentation" Busy="@Pending" OnClose="() => CancelAsync(presentation, cancelAction)">
<ChildContent>
<fieldset disabled="@Pending">
<p class="text-muted small mb-2">
Pick raw tags to reference under this equipment. The tree is scoped to the
equipment's cluster. Tick individual tags, or use a device / tag-group's
<em>“Select all tags below”</em> menu to pull many at once.
</p>
@if (_loading)
{
<p class="text-muted"><span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span>Loading…</p>
}
else if (_roots.Count == 0)
{
<p class="text-muted">This equipment does not resolve to a cluster, so there are no raw tags to reference.</p>
}
else
{
<div class="border rounded p-2" style="max-height:50vh; overflow:auto">
<RawTree Roots="_roots" PickerMode="true" SelectedTagIds="_selected"
@key="presentation" OnSelectionChanged="() => OnSelectionChanged(presentation)"
ResolveDescendantTagIds="node => ResolveDescendantsAsync(presentation, node)" />
</div>
}
</fieldset>
</ChildContent>
<Footer>
<div id='@FieldId("service-error")' role="alert" hidden="@string.IsNullOrWhiteSpace(_error)" class="text-danger small w-100">@_error</div>
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" @onclick="() => CancelAsync(presentation, cancelAction)" disabled="@Pending">Cancel</button>
<button type="button" class="btn btn-primary" @onclick="() => CommitAsync(presentation, completedAction)" disabled="@(Pending || _loading || _selected.Count == 0)">
@if (_busy) { <span class="spinner-border spinner-border-sm me-1" aria-hidden="true"></span> }
Add @_selected.Count reference@(_selected.Count == 1 ? "" : "s")
</button>
</div>
</Footer>
</ModalFrame>
@code {
/// <summary>Optional nonconfidential identity for successive presentations.</summary>
[Parameter] public object? Target { get; set; }
private readonly string _fieldScope = "ot-uns-" + Guid.NewGuid().ToString("N");
private string FieldId(string suffix) => _fieldScope + "-" + suffix;
private string FieldIds(params string[] suffixes) => string.Join(" ", suffixes.Select(FieldId));
private bool _disposed, _closing;
private long _generation;
private object? _lastTarget;
private bool _lastVisible;
private bool Current(long generation) => !_disposed && Visible && generation == _generation;
private bool Pending => _busy || _closing;
/// <summary>Retires UI continuations without cancelling a dispatched operation.</summary>
public void Dispose() { _disposed = true; ++_generation; }
/// <summary>Whether the modal is shown. The host owns this flag.</summary>
[Parameter] public bool Visible { get; set; }
/// <summary>The equipment the selected raw tags are referenced under.</summary>
[Parameter] public string? EquipmentId { get; set; }
/// <summary>Raised after references are committed so the host can reload its Tags list and close.</summary>
[Parameter] public EventCallback OnCommitted { get; set; }
/// <summary>Raised when the user cancels so the host can close.</summary>
[Parameter] public EventCallback OnCancel { get; set; }
private IReadOnlyList<RawNode> _roots = Array.Empty<RawNode>();
private HashSet<string> _selected = new(StringComparer.Ordinal);
private bool _busy;
private bool _loading;
private string? _error;
protected override async Task OnParametersSetAsync()
{
object identity = (Target, EquipmentId);
if (_disposed || (_lastVisible == Visible && Equals(_lastTarget, identity))) return;
_lastVisible = Visible; _lastTarget = identity;
var generation = ++_generation;
if (!Visible) return;
_selected = new(StringComparer.Ordinal);
_error = null; _roots = Array.Empty<RawNode>(); _loading = true;
var equipmentId = EquipmentId;
try
{
var root = string.IsNullOrEmpty(equipmentId) ? null : await Svc.LoadReferencePickerRootAsync(equipmentId);
if (!Current(generation)) return;
_roots = root is null ? Array.Empty<RawNode>() : new[] { root };
_loading = false;
}
catch when (!Current(generation)) { /* A superseded root load cannot fault the replacement. */ }
}
private async Task<IReadOnlyList<string>> ResolveDescendantsAsync(long generation, RawNode node)
{
if (!Current(generation) || Pending) return Array.Empty<string>();
try
{
var ids = await Svc.LoadDescendantTagIdsAsync(node);
return Current(generation) && !Pending ? ids : Array.Empty<string>();
}
catch when (!Current(generation)) { return Array.Empty<string>(); }
}
// Each presentation owns its set: a retired child can never mutate its replacement's selection.
private void OnSelectionChanged(long generation) { if (Current(generation)) StateHasChanged(); }
private async Task CommitAsync(long generation, EventCallback completedAction)
{
if (!Current(generation) || Pending || _loading || _selected.Count == 0) return;
var equipmentId = EquipmentId!;
var selected = _selected.ToList();
_busy = true; _error = null;
try
{
var result = await Svc.AddReferencesAsync(equipmentId, selected);
if (!Current(generation)) return;
if (result.Ok) await completedAction.InvokeAsync();
else _error = result.Error;
}
catch when (!Current(generation)) { /* A retired operation cannot surface an error in its replacement. */ }
finally { _busy = false; }
}
private async Task CancelAsync(long generation, EventCallback cancelAction)
{
if (!Current(generation) || Pending) return;
_closing = true;
try { await cancelAction.InvokeAsync(); }
finally { _closing = false; }
}
}