|
| 1 | +@implements IAsyncDisposable |
| 2 | +@using System.Globalization |
| 3 | +@using Microsoft.AspNetCore.Components.Web |
| 4 | +@using Microsoft.JSInterop |
| 5 | +@inject IJSRuntime JS |
| 6 | + |
| 7 | +<button type="button" |
| 8 | + class="btn btn-link p-0 border-0 fancy-filter-button" |
| 9 | + aria-label="@FormatColumnLabel(Texts.FilterColumnFormat, ColumnTitle)" |
| 10 | + aria-haspopup="dialog" |
| 11 | + aria-expanded="@(isOpen ? "true" : "false")" |
| 12 | + @ref="anchorElement" |
| 13 | + @onclick="OpenAsync"> |
| 14 | + <i class="bi @(ActiveFilter is null ? "bi-funnel" : "bi-funnel-fill")" aria-hidden="true"></i> |
| 15 | +</button> |
| 16 | + |
| 17 | +<div popover="auto" id="@popupId" class="fancy-filter-popup" @ref="popupElement"> |
| 18 | + <div class="d-flex flex-column gap-2 p-2"> |
| 19 | + <select class="form-select form-select-sm" @bind="draftOperator"> |
| 20 | + @foreach (FancyColumnFilterOperator op in Operators) |
| 21 | + { |
| 22 | + <option value="@op.ToString()">@GetOperatorLabel(op)</option> |
| 23 | + } |
| 24 | + </select> |
| 25 | + |
| 26 | + @if (draftOperator.RequiresValue()) |
| 27 | + { |
| 28 | + <input type="text" |
| 29 | + class="form-control form-control-sm" |
| 30 | + placeholder="@Texts.FilterValuePlaceholder" |
| 31 | + value="@draftValue" |
| 32 | + @oninput="OnValueInput" |
| 33 | + @onkeydown="OnValueKeyDown" /> |
| 34 | + } |
| 35 | + |
| 36 | + <div class="d-flex justify-content-end gap-2"> |
| 37 | + <button type="button" |
| 38 | + class="btn btn-sm btn-outline-secondary" |
| 39 | + disabled="@(ActiveFilter is null)" |
| 40 | + @onclick="ClearAsync"> |
| 41 | + @Texts.ClearFilterLabel |
| 42 | + </button> |
| 43 | + <button type="button" |
| 44 | + class="btn btn-sm btn-primary" |
| 45 | + disabled="@(!CanApply)" |
| 46 | + @onclick="ApplyAsync"> |
| 47 | + @Texts.ApplyFilterLabel |
| 48 | + </button> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | +</div> |
| 52 | + |
| 53 | +@code { |
| 54 | + private static readonly FancyColumnFilterOperator[] Operators = Enum.GetValues<FancyColumnFilterOperator>(); |
| 55 | + |
| 56 | + private readonly string popupId = $"fancy-filter-{Guid.NewGuid():N}"; |
| 57 | + private ElementReference anchorElement; |
| 58 | + private ElementReference popupElement; |
| 59 | + private IJSObjectReference? module; |
| 60 | + private DotNetObjectReference<FancyColumnFilterPopup>? dotNetReference; |
| 61 | + private FancyColumnFilterOperator draftOperator = FancyColumnFilterOperator.Contains; |
| 62 | + private string draftValue = string.Empty; |
| 63 | + private bool isOpen; |
| 64 | + |
| 65 | + [Parameter, EditorRequired] |
| 66 | + public string ColumnKey { get; set; } = string.Empty; |
| 67 | + |
| 68 | + [Parameter, EditorRequired] |
| 69 | + public string ColumnTitle { get; set; } = string.Empty; |
| 70 | + |
| 71 | + [Parameter] |
| 72 | + public FancyColumnFilter? ActiveFilter { get; set; } |
| 73 | + |
| 74 | + [Parameter, EditorRequired] |
| 75 | + public FancyTableTexts Texts { get; set; } = FancyTableTexts.Default; |
| 76 | + |
| 77 | + [Parameter] |
| 78 | + public EventCallback<FancyColumnFilter> OnApply { get; set; } |
| 79 | + |
| 80 | + [Parameter] |
| 81 | + public EventCallback OnClear { get; set; } |
| 82 | + |
| 83 | + private FancyColumnFilter Draft => new( |
| 84 | + ColumnKey, |
| 85 | + draftOperator, |
| 86 | + draftOperator.RequiresValue() ? draftValue : null); |
| 87 | + |
| 88 | + private bool CanApply => Draft.IsEffective(); |
| 89 | + |
| 90 | + private async Task OpenAsync() |
| 91 | + { |
| 92 | + draftOperator = ActiveFilter?.Operator ?? FancyColumnFilterOperator.Contains; |
| 93 | + draftValue = ActiveFilter?.Value ?? string.Empty; |
| 94 | + isOpen = true; |
| 95 | + |
| 96 | + module ??= await JS.InvokeAsync<IJSObjectReference>("import", "./_content/SoPro.FancyTable/fancy-table-filter.js"); |
| 97 | + dotNetReference ??= DotNetObjectReference.Create(this); |
| 98 | + await module.InvokeVoidAsync("show", anchorElement, popupElement, dotNetReference); |
| 99 | + } |
| 100 | + |
| 101 | + private void OnValueInput(ChangeEventArgs args) |
| 102 | + { |
| 103 | + draftValue = args.Value?.ToString() ?? string.Empty; |
| 104 | + } |
| 105 | + |
| 106 | + private async Task OnValueKeyDown(KeyboardEventArgs args) |
| 107 | + { |
| 108 | + if (args.Key == "Enter" && CanApply) |
| 109 | + { |
| 110 | + await ApplyAsync(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private async Task ApplyAsync() |
| 115 | + { |
| 116 | + if (!CanApply) |
| 117 | + { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + await OnApply.InvokeAsync(Draft); |
| 122 | + await HideAsync(); |
| 123 | + } |
| 124 | + |
| 125 | + private async Task ClearAsync() |
| 126 | + { |
| 127 | + await OnClear.InvokeAsync(); |
| 128 | + await HideAsync(); |
| 129 | + } |
| 130 | + |
| 131 | + private async Task HideAsync() |
| 132 | + { |
| 133 | + if (module is not null) |
| 134 | + { |
| 135 | + await module.InvokeVoidAsync("hide", popupElement); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + [JSInvokable] |
| 140 | + public Task OnPopoverClosed() |
| 141 | + { |
| 142 | + isOpen = false; |
| 143 | + StateHasChanged(); |
| 144 | + return Task.CompletedTask; |
| 145 | + } |
| 146 | + |
| 147 | + private string GetOperatorLabel(FancyColumnFilterOperator op) => op switch |
| 148 | + { |
| 149 | + FancyColumnFilterOperator.Equals => Texts.FilterOperatorEqualsLabel, |
| 150 | + FancyColumnFilterOperator.NotEquals => Texts.FilterOperatorNotEqualsLabel, |
| 151 | + FancyColumnFilterOperator.Contains => Texts.FilterOperatorContainsLabel, |
| 152 | + FancyColumnFilterOperator.NotContains => Texts.FilterOperatorNotContainsLabel, |
| 153 | + FancyColumnFilterOperator.StartsWith => Texts.FilterOperatorStartsWithLabel, |
| 154 | + FancyColumnFilterOperator.EndsWith => Texts.FilterOperatorEndsWithLabel, |
| 155 | + FancyColumnFilterOperator.IsEmpty => Texts.FilterOperatorIsEmptyLabel, |
| 156 | + FancyColumnFilterOperator.IsNotEmpty => Texts.FilterOperatorIsNotEmptyLabel, |
| 157 | + _ => op.ToString() |
| 158 | + }; |
| 159 | + |
| 160 | + private static string FormatColumnLabel(string format, string title) => |
| 161 | + string.Format(CultureInfo.CurrentCulture, format, title); |
| 162 | + |
| 163 | + public async ValueTask DisposeAsync() |
| 164 | + { |
| 165 | + if (module is not null) |
| 166 | + { |
| 167 | + try |
| 168 | + { |
| 169 | + await module.InvokeVoidAsync("dispose", popupElement); |
| 170 | + await module.DisposeAsync(); |
| 171 | + } |
| 172 | + catch (JSDisconnectedException) |
| 173 | + { |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + dotNetReference?.Dispose(); |
| 178 | + } |
| 179 | +} |
0 commit comments