|
| 1 | +using System.Globalization; |
| 2 | +using System.Text.Json; |
| 3 | +using Elsa.Studio.Components.DataPanelRenderers; |
| 4 | +using Elsa.Studio.DomInterop.Contracts; |
| 5 | +using Elsa.Studio.Localization; |
| 6 | +using Elsa.Studio.Models; |
| 7 | +using Microsoft.AspNetCore.Components; |
| 8 | +using MudBlazor; |
| 9 | + |
| 10 | +namespace Elsa.Studio.Components; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// A component that displays a panel of data items in a tabular format. |
| 14 | +/// </summary> |
| 15 | +public partial class DataPanel : ComponentBase |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// The default maximum length for truncating displayed content in data panel components. |
| 19 | + /// </summary> |
| 20 | + public const int DefaultTruncationLength = 300; |
| 21 | + private static readonly JsonSerializerOptions JsonSerializerOptions = new() { WriteIndented = true }; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The data to display. |
| 25 | + /// </summary> |
| 26 | + [Parameter] public DataPanelModel Data { get; set; } = new(); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// If true, empty values will be hidden. |
| 30 | + /// </summary> |
| 31 | + [Parameter] public bool HideEmptyValues { get; set; } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// If true, a message will be displayed when there is no data. |
| 35 | + /// </summary> |
| 36 | + [Parameter] public bool ShowNoDataAlert { get; set; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// The title of the data panel |
| 40 | + /// </summary> |
| 41 | + [Parameter] public string Title { get; set; } = null!; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// The message to display when there is no data. |
| 45 | + /// </summary> |
| 46 | + [Parameter] public string? NoDataMessage { get; set; } |
| 47 | + |
| 48 | + [Inject] private ILocalizer Localizer { get; set; } = null!; |
| 49 | + [Inject] private IClipboard Clipboard { get; set; } = null!; |
| 50 | + [Inject] private ISnackbar Snackbar { get; set; } = null!; |
| 51 | + [Inject] private IDialogService DialogService { get; set; } = null!; |
| 52 | + |
| 53 | + /// <inheritdoc /> |
| 54 | + protected override void OnInitialized() |
| 55 | + { |
| 56 | + NoDataMessage ??= Localizer["No data available."]; |
| 57 | + } |
| 58 | + |
| 59 | + private RenderFragment RenderValue(DataPanelItem item) => builder => |
| 60 | + { |
| 61 | + // Priority 1: ValueTemplate (custom render fragment with context) |
| 62 | + if (item.ValueTemplate != null) |
| 63 | + { |
| 64 | + var context = new DataPanelItemContext |
| 65 | + { |
| 66 | + Label = item.Label, |
| 67 | + Value = item.Value, |
| 68 | + Item = item |
| 69 | + }; |
| 70 | + builder.AddContent(0, item.ValueTemplate(context)); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // Priority 2: ValueComponentType (reusable custom component) |
| 75 | + if (item.ValueComponentType != null) |
| 76 | + { |
| 77 | + builder.OpenComponent(0, item.ValueComponentType); |
| 78 | + builder.AddAttribute(1, "Item", item); |
| 79 | + builder.CloseComponent(); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // Priority 3: Format-based rendering (new system using dedicated components) |
| 84 | + var displayText = GetFormattedValue(item); |
| 85 | + builder.OpenComponent<DataPanelValueRenderer>(0); |
| 86 | + builder.AddAttribute(1, "Item", item); |
| 87 | + builder.AddAttribute(2, "DisplayText", displayText); |
| 88 | + builder.AddAttribute(3, "TruncationLength", DefaultTruncationLength); |
| 89 | + builder.AddAttribute(4, "OnViewClicked", EventCallback.Factory.Create(this, () => OnViewClicked(item))); |
| 90 | + builder.CloseComponent(); |
| 91 | + }; |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Gets the formatted value for a data panel item. |
| 95 | + /// </summary> |
| 96 | + public string GetFormattedValue(DataPanelItem item) |
| 97 | + { |
| 98 | + // Use Text if explicitly provided (backward compatibility) |
| 99 | + if (!string.IsNullOrWhiteSpace(item.Text)) |
| 100 | + return item.Text; |
| 101 | + |
| 102 | + // No value to format |
| 103 | + if (item.Value == null) |
| 104 | + return string.Empty; |
| 105 | + |
| 106 | + // Apply format based on Format property |
| 107 | + return item.Format switch |
| 108 | + { |
| 109 | + DataPanelItemFormat.Text => item.Value.ToString() ?? string.Empty, |
| 110 | + DataPanelItemFormat.Timestamp => FormatTimestamp(item.Value, item.FormatString), |
| 111 | + DataPanelItemFormat.Number => FormatNumber(item.Value), |
| 112 | + DataPanelItemFormat.Boolean => FormatBoolean(item.Value), |
| 113 | + DataPanelItemFormat.Json => FormatJson(item.Value), |
| 114 | + DataPanelItemFormat.Code => item.Value.ToString() ?? string.Empty, |
| 115 | + DataPanelItemFormat.Markdown => item.Value.ToString() ?? string.Empty, |
| 116 | + DataPanelItemFormat.Auto => FormatAuto(item.Value), |
| 117 | + _ => item.Value.ToString() ?? string.Empty |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + private string FormatTimestamp(object value, string? formatString) |
| 122 | + { |
| 123 | + return value switch |
| 124 | + { |
| 125 | + DateTime dt => string.IsNullOrWhiteSpace(formatString) |
| 126 | + ? dt.ToLocalTime().ToString(CultureInfo.CurrentUICulture) |
| 127 | + : dt.ToLocalTime().ToString(formatString, CultureInfo.CurrentUICulture), |
| 128 | + DateTimeOffset dto => string.IsNullOrWhiteSpace(formatString) |
| 129 | + ? dto.ToLocalTime().ToString(CultureInfo.CurrentUICulture) |
| 130 | + : dto.ToLocalTime().ToString(formatString, CultureInfo.CurrentUICulture), |
| 131 | + _ => value.ToString() ?? string.Empty |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + private string FormatNumber(object value) |
| 136 | + { |
| 137 | + return value switch |
| 138 | + { |
| 139 | + int or long or short or byte => $"{value:N0}", |
| 140 | + double or float or decimal => $"{value:N2}", |
| 141 | + _ => value.ToString() ?? string.Empty |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + private string FormatBoolean(object value) |
| 146 | + { |
| 147 | + return value is bool b ? (b ? Localizer["Yes"] : Localizer["No"]) : value.ToString() ?? string.Empty; |
| 148 | + } |
| 149 | + |
| 150 | + private string FormatJson(object value) |
| 151 | + { |
| 152 | + return value as string ?? JsonSerializer.Serialize(value, JsonSerializerOptions); |
| 153 | + } |
| 154 | + |
| 155 | + private string FormatAuto(object value) |
| 156 | + { |
| 157 | + // Auto-detect based on type |
| 158 | + return value switch |
| 159 | + { |
| 160 | + DateTime or DateTimeOffset => FormatTimestamp(value, null), |
| 161 | + bool => FormatBoolean(value), |
| 162 | + int or long or short or byte or double or float or decimal => FormatNumber(value), |
| 163 | + _ => value.ToString() ?? string.Empty |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + private async Task OnCopyClicked(DataPanelItem item) |
| 168 | + { |
| 169 | + var textToCopy = !string.IsNullOrWhiteSpace(item.Text) ? item.Text : GetFormattedValue(item); |
| 170 | + await Clipboard.CopyText(textToCopy); |
| 171 | + Snackbar.Add(Localizer["{0} copied", item.Label], Severity.Success); |
| 172 | + } |
| 173 | + |
| 174 | + private async Task OnViewClicked(DataPanelItem item) |
| 175 | + { |
| 176 | + var options = new DialogOptions |
| 177 | + { |
| 178 | + CloseOnEscapeKey = true, |
| 179 | + Position = DialogPosition.Center, |
| 180 | + FullWidth = true, |
| 181 | + MaxWidth = MaxWidth.Large |
| 182 | + }; |
| 183 | + |
| 184 | + // Create a copy with the formatted text for viewing |
| 185 | + var itemToView = item with { Text = GetFormattedValue(item) }; |
| 186 | + |
| 187 | + var parameters = new DialogParameters |
| 188 | + { |
| 189 | + { nameof(DataPanelItem), itemToView } |
| 190 | + }; |
| 191 | + await DialogService.ShowAsync<ContentVisualizer>(Localizer["View content"], parameters, options); |
| 192 | + } |
| 193 | +} |
0 commit comments