fix(centralui): grouped bindings review fixes — Apply with the header at none/mixed keeps each member's connection (never unbinds; upsert Save could not persist it), object inference runs over effective references (bare-default members make an overridden group mixed, default-only prefixes infer), bindings to a deleted connection count as unbound, 'n changed · unsaved' diffs against last-saved state; bUnit behaviour tests for fallback banner, grouping and mixed-group Apply+Save
This commit is contained in:
+31
-14
@@ -177,7 +177,7 @@
|
||||
{
|
||||
<span class="badge text-bg-secondary" data-test="binding-group-mixed">mixed</span>
|
||||
}
|
||||
@if (_groupChangedCounts.TryGetValue(g.Key, out var changed) && changed > 0)
|
||||
@if (GroupChangedCount(g) is var changed && changed > 0)
|
||||
{
|
||||
<span class="badge border text-muted">@changed changed · unsaved</span>
|
||||
}
|
||||
@@ -981,7 +981,9 @@
|
||||
{
|
||||
_flattenedForBindings = flat.Value.Configuration;
|
||||
source = flat.Value.Configuration.Attributes
|
||||
.Where(a => !string.IsNullOrEmpty(a.DefaultDataSourceReference) || !string.IsNullOrEmpty(a.DataSourceReference))
|
||||
// Same predicate as the deploy gate (ValidateConnectionBindingCompleteness:
|
||||
// DataSourceReference != null), so every row the gate will check has a row here.
|
||||
.Where(a => a.DefaultDataSourceReference is not null || a.DataSourceReference is not null)
|
||||
.Select(a => (a.CanonicalName, a.DefaultDataSourceReference ?? a.DataSourceReference));
|
||||
}
|
||||
}
|
||||
@@ -998,10 +1000,11 @@
|
||||
return new BindingRow(s.Name, s.Default, null, null);
|
||||
}).ToList();
|
||||
RebuildBindingGroups();
|
||||
SnapshotSavedBindingState();
|
||||
}
|
||||
|
||||
private static IEnumerable<(string Name, string? Default)> DirectAttributeRows(IReadOnlyList<TemplateAttribute> directAttrs)
|
||||
=> directAttrs.Where(a => !string.IsNullOrEmpty(a.DataSourceReference)).Select(a => (a.Name, a.DataSourceReference));
|
||||
=> directAttrs.Where(a => a.DataSourceReference is not null).Select(a => (a.Name, a.DataSourceReference));
|
||||
|
||||
/// <summary>Re-projects the current selections/overrides into the rows, then regroups.</summary>
|
||||
private void RebuildBindingGroups()
|
||||
@@ -1013,9 +1016,29 @@
|
||||
Override = _bindingOverrides.GetValueOrDefault(r.CanonicalName)
|
||||
})
|
||||
.ToList();
|
||||
_bindingGroups = BindingGroupBuilder.Build(_bindingRows);
|
||||
// A binding to a connection that no longer exists at the site renders as
|
||||
// "— none —" and the deploy gate rejects it — count it as unbound.
|
||||
_bindingGroups = BindingGroupBuilder.Build(_bindingRows, _siteConnections.Select(c => c.Id).ToHashSet());
|
||||
}
|
||||
|
||||
/// <summary>Per-row (connection, override) as last loaded/saved — the baseline for the "n changed · unsaved" pill.</summary>
|
||||
private Dictionary<string, (int? Conn, string? Override)> _savedBindingState = new(StringComparer.Ordinal);
|
||||
|
||||
private void SnapshotSavedBindingState()
|
||||
=> _savedBindingState = _bindingRows.ToDictionary(
|
||||
r => r.CanonicalName,
|
||||
r => (_bindingSelections.TryGetValue(r.CanonicalName, out var c) ? c : (int?)null, _bindingOverrides.GetValueOrDefault(r.CanonicalName)),
|
||||
StringComparer.Ordinal);
|
||||
|
||||
/// <summary>Members whose current state differs from the last loaded/saved state (per-row edits and group Applies alike).</summary>
|
||||
private int GroupChangedCount(BindingGroup g)
|
||||
=> g.Members.Count(m =>
|
||||
{
|
||||
var name = m.Row.CanonicalName;
|
||||
var current = (_bindingSelections.TryGetValue(name, out var c) ? c : (int?)null, _bindingOverrides.GetValueOrDefault(name));
|
||||
return !_savedBindingState.TryGetValue(name, out var saved) || saved != current;
|
||||
});
|
||||
|
||||
private int TotalUnbound => _bindingGroups.Sum(g => g.UnboundCount);
|
||||
private int MixedGroups => _bindingGroups.Count(g => g.IsObjectMixed || g.ConnectionState == BindingConnectionState.Mixed);
|
||||
|
||||
@@ -1023,7 +1046,6 @@
|
||||
private readonly HashSet<string> _expandedGroups = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<string, string?> _groupObjectInput = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<string, int?> _groupConnectionInput = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<string, int> _groupChangedCounts = new(StringComparer.Ordinal);
|
||||
private bool _showOnlyUnbound;
|
||||
|
||||
private void ToggleGroup(string key) { if (!_expandedGroups.Remove(key)) _expandedGroups.Add(key); }
|
||||
@@ -1040,26 +1062,21 @@
|
||||
/// <summary>
|
||||
/// Per-module bulk apply: rewrites every member of the group to
|
||||
/// <c>{object}.{memberTail}</c> on the header's connection (blank object clears
|
||||
/// the overrides). Edits only the in-memory selection/override maps — Save
|
||||
/// Bindings persists them — and counts rows that actually changed for the
|
||||
/// "n changed · unsaved" pill.
|
||||
/// the overrides; a header left at "— none —"/"— mixed —" keeps each member's
|
||||
/// current connection — Apply never unbinds a group). Edits only the in-memory
|
||||
/// selection/override maps; Save Bindings persists them.
|
||||
/// </summary>
|
||||
private void ApplyGroupObject(BindingGroup g)
|
||||
{
|
||||
var connId = GroupConnectionInput(g);
|
||||
var changes = BindingGroupBuilder.ApplyObject(g, connId > 0 ? connId : null, GroupObjectInput(g));
|
||||
var changed = 0;
|
||||
foreach (var c in changes)
|
||||
{
|
||||
var before = (_bindingSelections.GetValueOrDefault(c.CanonicalName), _bindingOverrides.GetValueOrDefault(c.CanonicalName));
|
||||
if (c.ConnectionId is > 0) _bindingSelections[c.CanonicalName] = c.ConnectionId.Value;
|
||||
else _bindingSelections.Remove(c.CanonicalName);
|
||||
if (!string.IsNullOrEmpty(c.Override)) _bindingOverrides[c.CanonicalName] = c.Override;
|
||||
else _bindingOverrides.Remove(c.CanonicalName);
|
||||
if (before != (_bindingSelections.GetValueOrDefault(c.CanonicalName), _bindingOverrides.GetValueOrDefault(c.CanonicalName)))
|
||||
changed++;
|
||||
}
|
||||
_groupChangedCounts[g.Key] = _groupChangedCounts.GetValueOrDefault(g.Key) + changed;
|
||||
_groupObjectInput.Remove(g.Key);
|
||||
_groupConnectionInput.Remove(g.Key);
|
||||
_expandedGroups.Add(g.Key);
|
||||
@@ -1199,7 +1216,7 @@
|
||||
var result = await InstanceService.SetConnectionBindingsAsync(Id, bindings, user);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
_groupChangedCounts.Clear();
|
||||
SnapshotSavedBindingState();
|
||||
_toast.ShowSuccess($"Saved {bindings.Count} connection binding(s).");
|
||||
}
|
||||
else
|
||||
|
||||
@@ -44,8 +44,20 @@ public static class BindingGroupBuilder
|
||||
{
|
||||
public const string InstanceGroupKey = "(instance)";
|
||||
|
||||
public static IReadOnlyList<BindingGroup> Build(IEnumerable<BindingRow> rows)
|
||||
/// <param name="rows">The data-sourced flattened attributes with their current binding state.</param>
|
||||
/// <param name="knownConnectionIds">
|
||||
/// The site's data connection ids. When supplied, a row whose connection id is not
|
||||
/// in the set is treated as UNBOUND — a binding to a deleted connection renders as
|
||||
/// "— none —" and the deploy gate rejects it, so the pills must agree.
|
||||
/// </param>
|
||||
public static IReadOnlyList<BindingGroup> Build(IEnumerable<BindingRow> rows, IReadOnlyCollection<int>? knownConnectionIds = null)
|
||||
{
|
||||
if (knownConnectionIds is not null)
|
||||
{
|
||||
var known = knownConnectionIds as ISet<int> ?? new HashSet<int>(knownConnectionIds);
|
||||
rows = rows.Select(r => r.ConnectionId is int id && !known.Contains(id) ? r with { ConnectionId = null } : r);
|
||||
}
|
||||
|
||||
var byKey = new Dictionary<string, List<BindingGroupMember>>(StringComparer.Ordinal);
|
||||
var nested = new Dictionary<string, SortedSet<string>>(StringComparer.Ordinal);
|
||||
|
||||
@@ -93,26 +105,40 @@ public static class BindingGroupBuilder
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Longest object prefix shared by every member that has an override: each override
|
||||
/// must be <c>{prefix}.{memberTail}</c>. Returns (null,false) when no member has an
|
||||
/// override; (null,true) when members disagree or an override doesn't end in its tail.
|
||||
/// Object prefix shared by every member's EFFECTIVE reference (override if set, else
|
||||
/// the default): each must be <c>{prefix}.{memberTail}</c>.
|
||||
/// <list type="bullet">
|
||||
/// <item>No member has an override: infer from the defaults alone — (prefix,false) when
|
||||
/// they all agree, otherwise (null,false). Nothing has been edited yet, so a bare
|
||||
/// composed default is "unset", not "mixed".</item>
|
||||
/// <item>Some member has an override: (prefix,false) only when EVERY member's effective
|
||||
/// reference shares it; (null,true) when members disagree, an override doesn't end in
|
||||
/// its tail, or a member still sits on a bare default the gate would reject.</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
private static (string? Object, bool Mixed) InferObject(IReadOnlyList<BindingGroupMember> members)
|
||||
{
|
||||
var anyOverride = members.Any(m => !string.IsNullOrWhiteSpace(m.Row.Override));
|
||||
string? candidate = null;
|
||||
var any = false;
|
||||
foreach (var m in members)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(m.Row.Override)) continue;
|
||||
any = true;
|
||||
var suffix = "." + m.MemberTail;
|
||||
if (!m.Row.Override.EndsWith(suffix, StringComparison.Ordinal)) return (null, true);
|
||||
var prefix = m.Row.Override[..^suffix.Length];
|
||||
if (prefix.Length == 0) return (null, true);
|
||||
if (candidate is null) candidate = prefix;
|
||||
else if (!string.Equals(candidate, prefix, StringComparison.Ordinal)) return (null, true);
|
||||
var effective = m.EffectiveReference;
|
||||
var prefix = PrefixOf(effective, m.MemberTail);
|
||||
if (prefix is null || (candidate is not null && !string.Equals(candidate, prefix, StringComparison.Ordinal)))
|
||||
return (null, anyOverride);
|
||||
candidate = prefix;
|
||||
}
|
||||
return any ? (candidate, false) : (null, false);
|
||||
return (candidate, false);
|
||||
}
|
||||
|
||||
/// <summary>Returns <c>prefix</c> when <paramref name="reference"/> is <c>{prefix}.{tail}</c>, else null.</summary>
|
||||
private static string? PrefixOf(string? reference, string tail)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(reference)) return null;
|
||||
var suffix = "." + tail;
|
||||
if (!reference.EndsWith(suffix, StringComparison.Ordinal)) return null;
|
||||
var prefix = reference[..^suffix.Length];
|
||||
return prefix.Length == 0 ? null : prefix;
|
||||
}
|
||||
|
||||
/// <summary>Result of a bulk apply for one member.</summary>
|
||||
@@ -121,14 +147,19 @@ public static class BindingGroupBuilder
|
||||
/// <summary>
|
||||
/// Rewrites every member of <paramref name="group"/> to
|
||||
/// <c>{objectPath}.{memberTail}</c> on <paramref name="connectionId"/>. A blank
|
||||
/// <paramref name="objectPath"/> clears the overrides (template default applies) and
|
||||
/// leaves the connection as given. Surrounding whitespace and a trailing '.' are tolerated.
|
||||
/// <paramref name="objectPath"/> clears the overrides (template default applies).
|
||||
/// A null <paramref name="connectionId"/> (header reading "— mixed —"/"— none —")
|
||||
/// KEEPS each member's current connection — it never unbinds a group, since the
|
||||
/// Save path is an upsert and a removal could not persist anyway. Surrounding
|
||||
/// whitespace and a trailing '.' are tolerated.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<BindingChange> ApplyObject(BindingGroup group, int? connectionId, string? objectPath)
|
||||
{
|
||||
var obj = (objectPath ?? string.Empty).Trim().TrimEnd('.');
|
||||
return group.Members
|
||||
.Select(m => new BindingChange(m.Row.CanonicalName, connectionId,
|
||||
.Select(m => new BindingChange(
|
||||
m.Row.CanonicalName,
|
||||
connectionId ?? (m.Row.ConnectionId is > 0 ? m.Row.ConnectionId : null),
|
||||
obj.Length == 0 ? null : obj + "." + m.MemberTail))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user