Keep driver address builders inline within their owning dialog
This commit is contained in:
+88
-34
@@ -1,36 +1,34 @@
|
||||
@* Shared modal shell for per-driver address pickers. Parent page toggles `Visible`;
|
||||
the child fragment renders the per-driver picker body. The shell handles modal
|
||||
chrome, the "Use this address" button, and dismisses on close. *@
|
||||
@* Inline address picker within the owning form/dialog. The parent keeps modal focus/background
|
||||
ownership; selecting an address updates the caller's working draft, never saves the parent form. *@
|
||||
@implements IDisposable
|
||||
|
||||
@{
|
||||
var presentation = _generation;
|
||||
var selection = CurrentAddress;
|
||||
var pick = OnPickAddress;
|
||||
var close = VisibleChanged;
|
||||
var returnFocus = ReturnFocus;
|
||||
}
|
||||
@if (Visible)
|
||||
{
|
||||
<div class="modal-backdrop fade show" style="display:block"></div>
|
||||
<div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
|
||||
<div class="modal-dialog modal-lg" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">@Title</h5>
|
||||
<button type="button" class="btn-close" aria-label="Close" @onclick="OnCloseAsync"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
@ChildContent
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
@if (!string.IsNullOrEmpty(CurrentAddress))
|
||||
{
|
||||
<code class="me-auto mono" title="@CurrentAddress">@DisplayText.Abbreviate(CurrentAddress, 60)</code>
|
||||
}
|
||||
<div class="btn-group" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="OnCloseAsync">Close</button>
|
||||
<button type="button" class="btn btn-primary" disabled="@string.IsNullOrEmpty(CurrentAddress)"
|
||||
@onclick="OnUseAsync">
|
||||
Use this address
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<section class="panel mt-3" role="group" aria-labelledby="@_headingId" aria-busy="@(_busy ? "true" : "false")">
|
||||
<h3 id="@_headingId" class="panel-head">@Title</h3>
|
||||
<div style="padding:1rem">
|
||||
<fieldset @key="presentation" disabled="@_busy">
|
||||
@ChildContent
|
||||
</fieldset>
|
||||
<div class="d-flex flex-wrap gap-2 align-items-center mt-3">
|
||||
@if (!string.IsNullOrEmpty(selection))
|
||||
{
|
||||
<code class="mono text-break me-auto" title="@selection">@DisplayText.Abbreviate(selection, 60)</code>
|
||||
}
|
||||
<button @ref="_closeButton" type="button" class="btn btn-outline-secondary" disabled="@_busy"
|
||||
@onclick="() => CloseAsync(presentation, close, returnFocus)">Close</button>
|
||||
<button type="button" class="btn btn-primary" disabled="@(_busy || string.IsNullOrEmpty(selection))"
|
||||
@onclick="() => UseAsync(presentation, selection, pick, close, returnFocus)">Use this address</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
@@ -40,15 +38,71 @@
|
||||
[Parameter] public string CurrentAddress { get; set; } = "";
|
||||
[Parameter] public EventCallback<string> OnPickAddress { get; set; }
|
||||
[Parameter] public RenderFragment? ChildContent { get; set; }
|
||||
/// <summary>Optional caller-owned identity when a picker instance is reused for another working draft.</summary>
|
||||
[Parameter] public object? Target { get; set; }
|
||||
/// <summary>Stable originating Build control in the owning dialog; focused before this inline picker is removed.</summary>
|
||||
[Parameter] public ElementReference? ReturnFocus { get; set; }
|
||||
|
||||
private async Task OnCloseAsync()
|
||||
private readonly string _headingId = "driver-tag-picker-" + Guid.NewGuid().ToString("N");
|
||||
private ElementReference _closeButton;
|
||||
private long _generation;
|
||||
private long? _focusRequest;
|
||||
private bool _initialized, _lastVisible, _busy, _disposed;
|
||||
private string? _lastTitle;
|
||||
private object? _lastTarget;
|
||||
private EventCallback<string> _lastPick;
|
||||
private bool Current(long generation) => !_disposed && Visible && generation == _generation;
|
||||
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
await VisibleChanged.InvokeAsync(false);
|
||||
if (_disposed) return;
|
||||
if (_initialized && _lastVisible == Visible && _lastTitle == Title && Equals(_lastTarget, Target) && _lastPick.Equals(OnPickAddress)) return;
|
||||
_initialized = true; _lastVisible = Visible; _lastTitle = Title; _lastTarget = Target; _lastPick = OnPickAddress;
|
||||
++_generation; _busy = false;
|
||||
_focusRequest = Visible ? _generation : null;
|
||||
}
|
||||
|
||||
private async Task OnUseAsync()
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
await OnPickAddress.InvokeAsync(CurrentAddress);
|
||||
await VisibleChanged.InvokeAsync(false);
|
||||
var request = _focusRequest; _focusRequest = null;
|
||||
if (request is not { } generation || !Current(generation) || _busy) return;
|
||||
try { await _closeButton.FocusAsync(); }
|
||||
catch (JSDisconnectedException) { }
|
||||
catch (JSException) { }
|
||||
catch (InvalidOperationException) { } // Static SSR has no browser element runtime.
|
||||
}
|
||||
private Task CloseAsync(long generation, EventCallback<bool> close, ElementReference? returnFocus)
|
||||
=> Current(generation) && !_busy ? CompleteCloseAsync(generation, close, returnFocus) : Task.CompletedTask;
|
||||
private async Task CompleteCloseAsync(long generation, EventCallback<bool> close, ElementReference? returnFocus)
|
||||
{
|
||||
if (!Current(generation) || _busy) return;
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
if (returnFocus is { } trigger && !string.IsNullOrEmpty(trigger.Id))
|
||||
{
|
||||
try { await trigger.FocusAsync(); }
|
||||
catch (JSDisconnectedException) { }
|
||||
catch (JSException) { }
|
||||
catch (InvalidOperationException) { }
|
||||
}
|
||||
if (!Current(generation)) return;
|
||||
Visible = false; _lastVisible = false; _busy = false; _focusRequest = null; ++_generation;
|
||||
await close.InvokeAsync(false);
|
||||
}
|
||||
finally { if (Current(generation)) _busy = false; }
|
||||
}
|
||||
private async Task UseAsync(long generation, string selection, EventCallback<string> pick, EventCallback<bool> close, ElementReference? returnFocus)
|
||||
{
|
||||
if (!Current(generation) || _busy || string.IsNullOrEmpty(selection)) return;
|
||||
_busy = true;
|
||||
try { await pick.InvokeAsync(selection); }
|
||||
catch when (!Current(generation)) { } // Retired callback failures cannot affect a replacement draft.
|
||||
finally { if (Current(generation)) _busy = false; }
|
||||
if (Current(generation)) await CompleteCloseAsync(generation, close, returnFocus);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true; ++_generation; _focusRequest = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
@implements IDisposable
|
||||
@{ var presentation = _openGeneration; var savedAction = OnSaved; var closeAction = VisibleChanged; }
|
||||
<ModalFrame Visible="@Visible" Title="Edit tag" DialogClass="modal-lg" Target="@presentation" Busy="@Pending" OnClose="() => ClosePresentation(presentation, closeAction)">
|
||||
<ModalFrame Visible="@Visible" Title="Edit tag" DialogClass="modal-lg modal-dialog-scrollable" Target="@presentation" Busy="@Pending" OnClose="() => ClosePresentation(presentation, closeAction)">
|
||||
<ChildContent>
|
||||
<EditForm @ref="_editForm" id='@FieldId("form")' Model="_form" OnValidSubmit="() => SavePresentation(presentation, savedAction, closeAction)" OnInvalidSubmit="() => FocusInvalidOwnedField(presentation)" FormName="rawTagModal" aria-describedby='@FieldId("save-error")'>
|
||||
<DataAnnotationsValidator />
|
||||
|
||||
+3
-2
@@ -10,7 +10,7 @@
|
||||
<label for="@FieldId("field-02")" class="form-label">Tag path</label>
|
||||
<div class="input-group input-group-sm">
|
||||
<input id="@FieldId("field-02")" class="form-control form-control-sm mono" value="@_m.TagPath" placeholder="e.g. Motor1.Speed" @onchange="@(e => Update(() => _m.TagPath = e.Value?.ToString() ?? string.Empty))" />
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
<button @ref="_pickerTrigger" type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3"><label for="@FieldId("field-03")" class="form-label">Data type</label>
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
@if (_showPicker)
|
||||
{
|
||||
<DriverTagPicker @bind-Visible="_showPicker"
|
||||
<DriverTagPicker ReturnFocus="@_pickerTrigger" @bind-Visible="_showPicker"
|
||||
Title="AB CIP address builder"
|
||||
CurrentAddress="@_pickerAddress"
|
||||
OnPickAddress="@OnAddressPicked">
|
||||
@@ -53,6 +53,7 @@
|
||||
|
||||
private AbCipTagConfigModel _m = new();
|
||||
private string? _lastConfigJson;
|
||||
private ElementReference _pickerTrigger;
|
||||
private bool _showPicker;
|
||||
private string _pickerAddress = "";
|
||||
|
||||
|
||||
+3
-2
@@ -10,7 +10,7 @@
|
||||
<label for="@FieldId("field-02")" class="form-label">Address</label>
|
||||
<div class="input-group input-group-sm">
|
||||
<input id="@FieldId("field-02")" class="form-control form-control-sm mono" value="@_m.Address" placeholder="e.g. N7:0, B3:0/0" @onchange="@(e => Update(() => _m.Address = e.Value?.ToString() ?? string.Empty))" />
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
<button @ref="_pickerTrigger" type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3"><label for="@FieldId("field-03")" class="form-label">Data type</label>
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
@if (_showPicker)
|
||||
{
|
||||
<DriverTagPicker @bind-Visible="_showPicker"
|
||||
<DriverTagPicker ReturnFocus="@_pickerTrigger" @bind-Visible="_showPicker"
|
||||
Title="AB Legacy address builder"
|
||||
CurrentAddress="@_pickerAddress"
|
||||
OnPickAddress="@OnAddressPicked">
|
||||
@@ -51,6 +51,7 @@
|
||||
|
||||
private AbLegacyTagConfigModel _m = new();
|
||||
private string? _lastConfigJson;
|
||||
private ElementReference _pickerTrigger;
|
||||
private bool _showPicker;
|
||||
private string _pickerAddress = "";
|
||||
|
||||
|
||||
+3
-2
@@ -10,7 +10,7 @@
|
||||
<label for="@FieldId("field-02")" class="form-label">Address</label>
|
||||
<div class="input-group input-group-sm">
|
||||
<input aria-describedby="@FieldId("type-mismatch")" aria-invalid="@(FocasTagDefinitionFactory.DescribeDataTypeMismatch(_m.DataType, _m.Address) is not null ? "true" : null)" id="@FieldId("field-02")" class="form-control form-control-sm mono" value="@_m.Address" placeholder="e.g. X0.0, R100, PARAM:1815/0, MACRO:500" @onchange="@(e => Update(() => _m.Address = e.Value?.ToString() ?? string.Empty))" />
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
<button @ref="_pickerTrigger" type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3"><label for="@FieldId("field-03")" class="form-label">Data type</label>
|
||||
@@ -43,7 +43,7 @@
|
||||
|
||||
@if (_showPicker)
|
||||
{
|
||||
<DriverTagPicker @bind-Visible="_showPicker"
|
||||
<DriverTagPicker ReturnFocus="@_pickerTrigger" @bind-Visible="_showPicker"
|
||||
Title="FOCAS address builder"
|
||||
CurrentAddress="@_pickerAddress"
|
||||
OnPickAddress="@OnAddressPicked">
|
||||
@@ -67,6 +67,7 @@
|
||||
|
||||
private FocasTagConfigModel _m = new();
|
||||
private string? _lastConfigJson;
|
||||
private ElementReference _pickerTrigger;
|
||||
private bool _showPicker;
|
||||
private string _pickerAddress = "";
|
||||
|
||||
|
||||
+3
-2
@@ -61,7 +61,7 @@
|
||||
<div class="col-md-2"><label for="@FieldId("field-07")" class="form-label">String len</label>
|
||||
<input id="@FieldId("field-07")" type="number" class="form-control form-control-sm" value="@_m.StringLength" @onchange="@(e => UpdateAddress(() => _m.StringLength = ParseInt(e.Value)))" /></div>
|
||||
<div class="col-12 mt-1">
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build address</button>
|
||||
<button @ref="_pickerTrigger" type="button" class="btn btn-sm btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build address</button>
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<div class="form-check">
|
||||
@@ -75,7 +75,7 @@
|
||||
|
||||
@if (_showPicker)
|
||||
{
|
||||
<DriverTagPicker @bind-Visible="_showPicker"
|
||||
<DriverTagPicker ReturnFocus="@_pickerTrigger" @bind-Visible="_showPicker"
|
||||
Title="Modbus address builder"
|
||||
CurrentAddress="@_pickerAddress"
|
||||
OnPickAddress="@OnAddressPicked">
|
||||
@@ -97,6 +97,7 @@
|
||||
|
||||
private ModbusTagConfigModel _m = new();
|
||||
private string? _lastConfigJson;
|
||||
private ElementReference _pickerTrigger;
|
||||
private bool _showPicker;
|
||||
private string _pickerAddress = "";
|
||||
|
||||
|
||||
+3
-2
@@ -8,7 +8,7 @@
|
||||
<label for="@FieldId("field-01")" class="form-label">Address</label>
|
||||
<div class="input-group input-group-sm">
|
||||
<input id="@FieldId("field-01")" class="form-control form-control-sm mono" value="@_m.Address" placeholder="e.g. DB1.DBW0, M0.0, I0.0, QD4" @onchange="@(e => Update(() => _m.Address = e.Value?.ToString() ?? string.Empty))" />
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
<button @ref="_pickerTrigger" type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4"><label for="@FieldId("field-02")" class="form-label">Data type</label>
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
@if (_showPicker)
|
||||
{
|
||||
<DriverTagPicker @bind-Visible="_showPicker"
|
||||
<DriverTagPicker ReturnFocus="@_pickerTrigger" @bind-Visible="_showPicker"
|
||||
Title="S7 address builder"
|
||||
CurrentAddress="@_pickerAddress"
|
||||
OnPickAddress="@OnAddressPicked">
|
||||
@@ -51,6 +51,7 @@
|
||||
|
||||
private S7TagConfigModel _m = new();
|
||||
private string? _lastConfigJson;
|
||||
private ElementReference _pickerTrigger;
|
||||
private bool _showPicker;
|
||||
private string _pickerAddress = "";
|
||||
|
||||
|
||||
+3
-2
@@ -107,13 +107,13 @@ else
|
||||
<input aria-describedby="@FieldIds("field-17-hint")" id="@FieldId("field-17")" class="form-control form-control-sm mono" value="@_m.TimestampColumn" placeholder="sample_ts" @onchange="@(e => Update(() => _m.TimestampColumn = NullIfBlank(e.Value)))" />
|
||||
<div id="@FieldId("field-17-hint")" class="form-text">Source-timestamp column; blank ⇒ the driver stamps the poll time.</div></div>
|
||||
<div class="col-12 mt-1">
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build address</button>
|
||||
<button @ref="_pickerTrigger" type="button" class="btn btn-sm btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build address</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (_showPicker)
|
||||
{
|
||||
<DriverTagPicker @bind-Visible="_showPicker"
|
||||
<DriverTagPicker ReturnFocus="@_pickerTrigger" @bind-Visible="_showPicker"
|
||||
Title="Sql tag builder"
|
||||
CurrentAddress="@_pickerAddress"
|
||||
OnPickAddress="@OnAddressPicked">
|
||||
@@ -142,6 +142,7 @@ else
|
||||
|
||||
private SqlTagConfigModel _m = new();
|
||||
private string? _lastConfigJson;
|
||||
private ElementReference _pickerTrigger;
|
||||
private bool _showPicker;
|
||||
private string _pickerAddress = "";
|
||||
|
||||
|
||||
+3
-2
@@ -10,7 +10,7 @@
|
||||
<label for="@FieldId("field-02")" class="form-label">Symbol path</label>
|
||||
<div class="input-group input-group-sm">
|
||||
<input id="@FieldId("field-02")" class="form-control form-control-sm mono" value="@_m.SymbolPath" placeholder="e.g. MAIN.bStart, GVL.Counter" @onchange="@(e => Update(() => _m.SymbolPath = e.Value?.ToString() ?? string.Empty))" />
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
<button @ref="_pickerTrigger" type="button" class="btn btn-outline-secondary" @onclick="@(() => _showPicker = true)">Build</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3"><label for="@FieldId("field-03")" class="form-label">Data type</label>
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
@if (_showPicker)
|
||||
{
|
||||
<DriverTagPicker @bind-Visible="_showPicker"
|
||||
<DriverTagPicker ReturnFocus="@_pickerTrigger" @bind-Visible="_showPicker"
|
||||
Title="TwinCAT address builder"
|
||||
CurrentAddress="@_pickerAddress"
|
||||
OnPickAddress="@OnAddressPicked">
|
||||
@@ -53,6 +53,7 @@
|
||||
|
||||
private TwinCATTagConfigModel _m = new();
|
||||
private string? _lastConfigJson;
|
||||
private ElementReference _pickerTrigger;
|
||||
private bool _showPicker;
|
||||
private string _pickerAddress = "";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user