Lists need one component for the whole app (SPEC §10.6-§10.7): a list query model that turns search, filters, sort and paging into SQL or runs over rows in memory, a KhGrid that pages 100,000 rows while scrolling (or in pages) with sort, selection, keyboard, inline edit, totals, row menu, saved column layout and CSV/XLSX export, the KhRadar around it with search, quick filters, a filter builder and saved filters, a radar window for F4 lookups, and KhLinesGrid for document lines typed from the keyboard. Checked in the browser on the component catalog and the sample document. Fixed on the way: the grid height lost to the flex basis, a new search kept the old scroll position, Save of a filter needed a second click, page shortcuts (Ctrl+Canc) reached the record behind a modal window, Escape did nothing once focus left a dialog, totals did not follow an inline edit, and amounts exported as 0 instead of 0,00. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
6.2 KiB
C#
157 lines
6.2 KiB
C#
using System.Globalization;
|
|
|
|
namespace Khaemhat.Application.Querying;
|
|
|
|
/// <summary>How a filter compares a field with its values (SPEC §10.7 <c>KhGrid</c>, <c>KhRadar</c>).</summary>
|
|
public enum ListFilterOperator
|
|
{
|
|
/// <summary>Equal to the value.</summary>
|
|
Equals = 0,
|
|
|
|
/// <summary>Different from the value.</summary>
|
|
NotEquals = 1,
|
|
|
|
/// <summary>Text containing the value, ignoring case.</summary>
|
|
Contains = 2,
|
|
|
|
/// <summary>Text starting with the value, ignoring case.</summary>
|
|
StartsWith = 3,
|
|
|
|
/// <summary>Greater than the value.</summary>
|
|
GreaterThan = 4,
|
|
|
|
/// <summary>Greater than or equal to the value.</summary>
|
|
GreaterOrEqual = 5,
|
|
|
|
/// <summary>Less than the value.</summary>
|
|
LessThan = 6,
|
|
|
|
/// <summary>Less than or equal to the value.</summary>
|
|
LessOrEqual = 7,
|
|
|
|
/// <summary>Between the first and the second value, both included.</summary>
|
|
Between = 8,
|
|
|
|
/// <summary>One of the values.</summary>
|
|
In = 9,
|
|
|
|
/// <summary>Empty (null, or an empty text).</summary>
|
|
IsEmpty = 10,
|
|
|
|
/// <summary>Not empty.</summary>
|
|
IsNotEmpty = 11,
|
|
}
|
|
|
|
/// <summary>
|
|
/// A filter on a field. The values are invariant texts (<c>2026-11-30</c>, <c>1234.5</c>, <c>true</c>, the name of an
|
|
/// enum value), so a filter can be saved in the user settings and read back unchanged.
|
|
/// </summary>
|
|
/// <param name="Field">The field id.</param>
|
|
/// <param name="Operator">The comparison.</param>
|
|
/// <param name="Values">The values: none for the empty checks, two for <see cref="ListFilterOperator.Between"/>.</param>
|
|
public sealed record ListFilter(string Field, ListFilterOperator Operator, IReadOnlyList<string> Values)
|
|
{
|
|
/// <summary>A filter with its values.</summary>
|
|
/// <param name="field">The field id.</param>
|
|
/// <param name="op">The comparison.</param>
|
|
/// <param name="values">The values.</param>
|
|
/// <returns>The filter.</returns>
|
|
public static ListFilter Of(string field, ListFilterOperator op, params string[] values) => new(field, op, values);
|
|
|
|
/// <summary>The invariant text of a value, as filters keep it.</summary>
|
|
/// <param name="value">The value.</param>
|
|
/// <returns>The text.</returns>
|
|
public static string Text(object? value) => value switch
|
|
{
|
|
null => string.Empty,
|
|
DateOnly d => d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
|
|
DateTime d => d.ToString("O", CultureInfo.InvariantCulture),
|
|
DateTimeOffset d => d.ToString("O", CultureInfo.InvariantCulture),
|
|
bool b => b ? "true" : "false",
|
|
IFormattable f => f.ToString(null, CultureInfo.InvariantCulture),
|
|
_ => value.ToString() ?? string.Empty,
|
|
};
|
|
|
|
/// <inheritdoc />
|
|
public bool Equals(ListFilter? other) =>
|
|
other is not null && Field == other.Field && Operator == other.Operator && Values.SequenceEqual(other.Values);
|
|
|
|
/// <inheritdoc />
|
|
public override int GetHashCode()
|
|
{
|
|
var hash = new HashCode();
|
|
hash.Add(Field);
|
|
hash.Add(Operator);
|
|
foreach (var value in Values)
|
|
{
|
|
hash.Add(value);
|
|
}
|
|
|
|
return hash.ToHashCode();
|
|
}
|
|
}
|
|
|
|
/// <summary>An ordering on a field.</summary>
|
|
/// <param name="Field">The field id.</param>
|
|
/// <param name="Descending">Whether from the largest.</param>
|
|
public sealed record ListSort(string Field, bool Descending = false);
|
|
|
|
/// <summary>
|
|
/// What a list or radar asks its source: the text typed in the search box, the filters, the ordering and the page.
|
|
/// The source answers with a <see cref="ListPage{T}"/>; with <see cref="ListFields{T}"/> the same request becomes SQL for
|
|
/// a repository or runs on rows in memory.
|
|
/// </summary>
|
|
public sealed record ListQuery
|
|
{
|
|
/// <summary>The largest page a source returns.</summary>
|
|
public const int MaxLimit = 1000;
|
|
|
|
/// <summary>The text of the search box: every searchable field is checked.</summary>
|
|
public string? Search { get; init; }
|
|
|
|
/// <summary>The filters, all of which must hold.</summary>
|
|
public IReadOnlyList<ListFilter> Filters { get; init; } = [];
|
|
|
|
/// <summary>The ordering, first field first.</summary>
|
|
public IReadOnlyList<ListSort> Sort { get; init; } = [];
|
|
|
|
/// <summary>The rows to skip.</summary>
|
|
public int Offset { get; init; }
|
|
|
|
/// <summary>The rows to return, at most <see cref="MaxLimit"/>.</summary>
|
|
public int Limit { get; init; } = 50;
|
|
|
|
/// <summary>The same request for another page.</summary>
|
|
/// <param name="offset">The rows to skip.</param>
|
|
/// <param name="limit">The rows to return.</param>
|
|
/// <returns>The request.</returns>
|
|
public ListQuery WithPage(int offset, int limit) => this with { Offset = Math.Max(0, offset), Limit = Math.Clamp(limit, 1, MaxLimit) };
|
|
|
|
/// <summary>Whether two requests select and order the same rows, whatever the page.</summary>
|
|
/// <param name="other">The other request.</param>
|
|
/// <returns>Whether they match.</returns>
|
|
public bool SameRows(ListQuery? other) =>
|
|
other is not null && string.Equals(Search?.Trim(), other.Search?.Trim(), StringComparison.OrdinalIgnoreCase)
|
|
&& Filters.SequenceEqual(other.Filters) && Sort.SequenceEqual(other.Sort);
|
|
}
|
|
|
|
/// <summary>A page of rows with the count and the totals of every row the request selects (not only the page).</summary>
|
|
/// <typeparam name="T">The row type.</typeparam>
|
|
/// <param name="Items">The rows of the page.</param>
|
|
/// <param name="Total">The rows selected.</param>
|
|
/// <param name="Totals">The sums by field id, for the totals row of the grid.</param>
|
|
public sealed record ListPage<T>(IReadOnlyList<T> Items, long Total, IReadOnlyDictionary<string, decimal> Totals)
|
|
{
|
|
/// <summary>No rows.</summary>
|
|
#pragma warning disable CA1000 // An empty page of the same row type is the natural default.
|
|
public static ListPage<T> Empty { get; } = new([], 0, new Dictionary<string, decimal>());
|
|
#pragma warning restore CA1000
|
|
}
|
|
|
|
/// <summary>Where a grid or radar reads its rows.</summary>
|
|
/// <typeparam name="T">The row type.</typeparam>
|
|
/// <param name="query">The request.</param>
|
|
/// <param name="cancellationToken">Cancellation, when a newer request replaces this one.</param>
|
|
/// <returns>The page.</returns>
|
|
public delegate Task<ListPage<T>> ListSource<T>(ListQuery query, CancellationToken cancellationToken);
|