200 lines
7.9 KiB
Plaintext
200 lines
7.9 KiB
Plaintext
@page "/scripts"
|
|
@attribute [Authorize(Policy = AdminUiPolicies.ConfigEditor)]
|
|
@rendermode RenderMode.InteractiveServer
|
|
@using Microsoft.EntityFrameworkCore
|
|
@using ZB.MOM.WW.OtOpcUa.Configuration
|
|
@using ZB.MOM.WW.OtOpcUa.AdminUI.ScriptList
|
|
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
|
|
@implements IDisposable
|
|
|
|
<PageErrorBoundary>
|
|
<PageTitle>Scripts · OtOpcUa</PageTitle>
|
|
<div class="page-head">
|
|
<h1>Scripts</h1>
|
|
<span class="spacer"></span>
|
|
<a href="/scripts/new" class="btn btn-primary btn-sm">New script</a>
|
|
</div>
|
|
|
|
@if (_rows is null)
|
|
{
|
|
<p>Loading…</p>
|
|
}
|
|
else
|
|
{
|
|
<section class="panel rise mt-3" style="animation-delay:.08s">
|
|
<h2 class="panel-head">@_rows.Count script@(_rows.Count == 1 ? "" : "s")</h2>
|
|
@if (_rows.Count == 0)
|
|
{
|
|
<div style="padding:1rem" class="text-muted">No scripts defined.</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="table-wrap" role="region" aria-label="Scripts" tabindex="0">
|
|
<table class="data-table">
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">Script</th>
|
|
<th scope="col"><span class="visually-hidden">Actions</span></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (var s in _rows)
|
|
{
|
|
<tr>
|
|
<th scope="row">
|
|
<div>
|
|
<span>@(string.IsNullOrWhiteSpace(s.Name) ? s.ScriptId : s.Name)</span>
|
|
<span class="chip chip-idle ms-1">@s.Language</span>
|
|
</div>
|
|
<div class="mono small text-muted">
|
|
@s.ScriptId · hash=@DisplayText.Abbreviate(s.SourceHash, 12)
|
|
</div>
|
|
</th>
|
|
<td class="text-end">
|
|
<div class="btn-group btn-group-sm" role="group">
|
|
<button type="button" class="btn btn-sm btn-outline-secondary"
|
|
@onclick="() => ShowSource(s.ScriptId)">Source</button>
|
|
<a href="/scripts/@s.ScriptId" class="btn btn-sm btn-outline-primary">Edit</a>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
}
|
|
</section>
|
|
}
|
|
|
|
@* The dialog's EXISTENCE is gated on the held id (operator intent); the resolve drives only its
|
|
CONTENT. Gating existence on FindSourceScript() would make the modal a function of _rows, and any
|
|
render where the row is momentarily unresolvable (a future reload/poll setting _rows null or
|
|
replacing the list) would unmount the subtree and dispose the Close handler's binding id — a
|
|
click already in flight then throws GetRequiredEventBindingEntry. Latent today (_rows is written
|
|
once, in OnInitializedAsync) but the shape bit ScadaBridge live the moment a refresh landed. *@
|
|
@{
|
|
var sourceId = _sourceScriptId;
|
|
var sourceGeneration = _sourceGeneration;
|
|
var src = FindSourceScript();
|
|
var sourceTitle = src is null ? sourceId ?? "Script source" : string.IsNullOrWhiteSpace(src.Name) ? src.ScriptId : src.Name;
|
|
}
|
|
<ModalFrame Visible="@(sourceId is not null)" Target="@sourceGeneration" Title="@sourceTitle"
|
|
DialogClass="modal-lg modal-dialog-scrollable" OnClose="() => CloseSource(sourceGeneration, sourceId)">
|
|
<TitleContent>
|
|
<span class="text-break">@sourceTitle</span>
|
|
@if (src is not null)
|
|
{
|
|
<span class="mono small text-muted text-break ms-2">@src.ScriptId</span>
|
|
}
|
|
</TitleContent>
|
|
<ChildContent>
|
|
@if (_sourceError is not null)
|
|
{
|
|
<div class="notice notice-alert mb-0" role="alert">@_sourceError</div>
|
|
}
|
|
else if (src is null || (_sourceLoaded && _sourceCode is null))
|
|
{
|
|
<p class="text-muted mb-0" role="status">This script is no longer loaded.</p>
|
|
}
|
|
else if (!_sourceLoaded)
|
|
{
|
|
<p class="text-muted mb-0" role="status">Loading source…</p>
|
|
}
|
|
else
|
|
{
|
|
<pre class="mono small mb-0" role="region" aria-label="@($"Source for {sourceTitle}")" tabindex="0"
|
|
style="background:var(--zebra-bg);padding:1rem;border-radius:4px;overflow:auto">@_sourceCode</pre>
|
|
}
|
|
</ChildContent>
|
|
<Footer>
|
|
<button type="button" class="btn btn-outline-secondary" data-initial-focus="true" @onclick="() => CloseSource(sourceGeneration, sourceId)">Close</button>
|
|
</Footer>
|
|
</ModalFrame>
|
|
</PageErrorBoundary>
|
|
|
|
@code {
|
|
// A projection without SourceCode (R04 P8); the source loads per dialog open.
|
|
private List<ScriptListRow>? _rows;
|
|
|
|
// Only the id of the script whose source dialog is open — never the record itself, so a reload
|
|
// that drops or replaces the row cannot leave a stale object on screen.
|
|
private string? _sourceScriptId;
|
|
|
|
// The open dialog's source, fetched on open. _sourceGeneration discards a superseded fetch (the
|
|
// operator opened another row, or closed the dialog, while the first one was in flight).
|
|
private string? _sourceCode;
|
|
private bool _sourceLoaded;
|
|
private string? _sourceError;
|
|
private int _sourceGeneration;
|
|
private bool _disposed;
|
|
private bool CurrentSource(int generation, string? scriptId)
|
|
=> !_disposed && scriptId is not null && generation == _sourceGeneration && scriptId == _sourceScriptId;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await using var db = await DbFactory.CreateDbContextAsync();
|
|
var rows = await ScriptListQueries.LoadRowsAsync(db);
|
|
if (!_disposed) _rows = rows;
|
|
}
|
|
|
|
/// <summary>Re-resolves the open dialog's script from the current list on every render. Drives
|
|
/// the dialog's CONTENT only — never its existence (see the comment at the markup): a script
|
|
/// that is no longer loaded renders a notice with the frame and Close still mounted.</summary>
|
|
private ScriptListRow? FindSourceScript()
|
|
=> _sourceScriptId is null
|
|
? null
|
|
: _rows?.FirstOrDefault(s => s.ScriptId == _sourceScriptId);
|
|
|
|
// One dialog at a time: opening another row replaces the id.
|
|
private async Task ShowSource(string scriptId)
|
|
{
|
|
if (_disposed) return;
|
|
var generation = ++_sourceGeneration;
|
|
_sourceScriptId = scriptId;
|
|
_sourceCode = null;
|
|
_sourceLoaded = false;
|
|
|
|
_sourceError = null;
|
|
string? source;
|
|
try
|
|
{
|
|
await using var db = await DbFactory.CreateDbContextAsync();
|
|
source = await ScriptListQueries.LoadSourceAsync(db, scriptId);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// A ConfigDb hiccup must not escape the click handler into the page's error boundary (review r0 M7).
|
|
if (!CurrentSource(generation, scriptId)) return;
|
|
_sourceError = $"The script source could not be loaded: {ex.Message}";
|
|
_sourceLoaded = true;
|
|
return;
|
|
}
|
|
|
|
if (!CurrentSource(generation, scriptId)) return;
|
|
_sourceCode = source;
|
|
_sourceLoaded = true;
|
|
}
|
|
|
|
private void CloseSource(int generation, string? scriptId)
|
|
{
|
|
if (!CurrentSource(generation, scriptId)) return;
|
|
ClearSource();
|
|
}
|
|
|
|
private void ClearSource()
|
|
{
|
|
_sourceGeneration++;
|
|
_sourceScriptId = null;
|
|
_sourceCode = null;
|
|
_sourceLoaded = false;
|
|
_sourceError = null;
|
|
}
|
|
|
|
/// <summary>Retires pending read results and captured close handlers without changing the database query.</summary>
|
|
public void Dispose()
|
|
{
|
|
_disposed = true;
|
|
ClearSource();
|
|
}
|
|
}
|