| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Chronicis.Client.Models; |
| | | 4 | | using Chronicis.Shared.DTOs; |
| | | 5 | | using Microsoft.AspNetCore.Components; |
| | | 6 | | |
| | | 7 | | namespace Chronicis.Client.Components.Shared; |
| | | 8 | | |
| | | 9 | | public partial class ExternalLinkDetailPanel : ComponentBase |
| | | 10 | | { |
| | 0 | 11 | | [Parameter] public ExternalLinkContentDto? Content { get; set; } |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// When provided, bypasses the render definition service and uses this definition directly. |
| | | 15 | | /// Used by the admin render definition generator for live preview. |
| | | 16 | | /// </summary> |
| | 0 | 17 | | [Parameter] public RenderDefinition? DefinitionOverride { get; set; } |
| | | 18 | | |
| | | 19 | | private bool _useStructuredRendering; |
| | | 20 | | private bool _isLoading; |
| | | 21 | | private JsonElement? _parsedJson; |
| | | 22 | | private RenderDefinition? _definition; |
| | | 23 | | private string? _lastContentId; |
| | | 24 | | private RenderDefinition? _lastDefinitionOverride; |
| | | 25 | | |
| | | 26 | | protected override async Task OnParametersSetAsync() |
| | | 27 | | { |
| | 0 | 28 | | var contentId = Content?.Id; |
| | 0 | 29 | | var overrideChanged = DefinitionOverride != _lastDefinitionOverride; |
| | | 30 | | |
| | 0 | 31 | | if (contentId == _lastContentId && !overrideChanged) |
| | 0 | 32 | | return; |
| | 0 | 33 | | _lastContentId = contentId; |
| | 0 | 34 | | _lastDefinitionOverride = DefinitionOverride; |
| | | 35 | | |
| | 0 | 36 | | if (Content == null || string.IsNullOrWhiteSpace(Content.JsonData)) |
| | | 37 | | { |
| | 0 | 38 | | _useStructuredRendering = false; |
| | 0 | 39 | | _isLoading = false; |
| | 0 | 40 | | _parsedJson = null; |
| | 0 | 41 | | _definition = null; |
| | 0 | 42 | | return; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | try |
| | | 46 | | { |
| | 0 | 47 | | _isLoading = true; |
| | | 48 | | |
| | 0 | 49 | | var doc = JsonDocument.Parse(Content.JsonData); |
| | 0 | 50 | | var parsedJson = doc.RootElement.Clone(); |
| | | 51 | | |
| | 0 | 52 | | var lastSlash = Content.Id.LastIndexOf('/'); |
| | 0 | 53 | | var categoryPath = lastSlash > 0 ? Content.Id[..lastSlash] : null; |
| | | 54 | | |
| | 0 | 55 | | Logger.LogInformation( |
| | 0 | 56 | | "ExternalLinkDetailPanel: Parsed JSON for {Id}. Category={Category}, RootKind={Kind}", |
| | 0 | 57 | | Content.Id, categoryPath, parsedJson.ValueKind); |
| | | 58 | | |
| | 0 | 59 | | var definition = DefinitionOverride |
| | 0 | 60 | | ?? await RenderDefinitionService.ResolveAsync(Content.Source, categoryPath); |
| | | 61 | | |
| | 0 | 62 | | _parsedJson = parsedJson; |
| | 0 | 63 | | _definition = definition; |
| | 0 | 64 | | _useStructuredRendering = true; |
| | 0 | 65 | | _isLoading = false; |
| | | 66 | | |
| | 0 | 67 | | Logger.LogInformation( |
| | 0 | 68 | | "ExternalLinkDetailPanel: Rendering with CatchAll={CatchAll}, Sections={Sections}", |
| | 0 | 69 | | _definition.CatchAll, _definition.Sections.Count); |
| | | 70 | | |
| | 0 | 71 | | StateHasChanged(); |
| | 0 | 72 | | } |
| | 0 | 73 | | catch (JsonException ex) |
| | | 74 | | { |
| | 0 | 75 | | Logger.LogWarning(ex, "Failed to parse JsonData for {Id}, falling back to markdown", Content.Id); |
| | 0 | 76 | | _useStructuredRendering = false; |
| | 0 | 77 | | _isLoading = false; |
| | 0 | 78 | | StateHasChanged(); |
| | 0 | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | // ================================================================================== |
| | | 83 | | // Rendering Methods |
| | | 84 | | // ================================================================================== |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the 'fields' object from the root, which is the primary data container. |
| | | 88 | | /// </summary> |
| | | 89 | | private static JsonElement? GetFieldsElement(JsonElement root) |
| | | 90 | | { |
| | 0 | 91 | | if (root.ValueKind == JsonValueKind.Object && |
| | 0 | 92 | | root.TryGetProperty("fields", out var fields) && |
| | 0 | 93 | | fields.ValueKind == JsonValueKind.Object) |
| | | 94 | | { |
| | 0 | 95 | | return fields; |
| | | 96 | | } |
| | 0 | 97 | | return null; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Main render method — dispatches to definition-driven or generic rendering. |
| | | 102 | | /// </summary> |
| | 0 | 103 | | private RenderFragment RenderStructured(JsonElement root, RenderDefinition definition) => builder => |
| | 0 | 104 | | { |
| | 0 | 105 | | var fields = GetFieldsElement(root); |
| | 0 | 106 | | var dataSource = fields ?? root; |
| | 0 | 107 | | |
| | 0 | 108 | | var renderedPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 0 | 109 | | // Always mark hidden fields as rendered so they don't appear in catch-all |
| | 0 | 110 | | foreach (var hidden in definition.Hidden) |
| | 0 | 111 | | { |
| | 0 | 112 | | renderedPaths.Add(hidden); |
| | 0 | 113 | | } |
| | 0 | 114 | | // Title field is rendered by the drawer header, not the panel |
| | 0 | 115 | | renderedPaths.Add(definition.TitleField); |
| | 0 | 116 | | |
| | 0 | 117 | | var seq = 0; |
| | 0 | 118 | | |
| | 0 | 119 | | // Render defined sections |
| | 0 | 120 | | foreach (var section in definition.Sections) |
| | 0 | 121 | | { |
| | 0 | 122 | | RenderSection(builder, ref seq, dataSource, section, renderedPaths); |
| | 0 | 123 | | } |
| | 0 | 124 | | |
| | 0 | 125 | | // Catch-all: render any remaining fields not covered by sections |
| | 0 | 126 | | if (definition.CatchAll) |
| | 0 | 127 | | { |
| | 0 | 128 | | RenderCatchAll(builder, ref seq, dataSource, renderedPaths); |
| | 0 | 129 | | } |
| | 0 | 130 | | }; |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Renders a single defined section as an expansion panel. |
| | | 134 | | /// Skips sections where no fields would be visible (all null/empty with omitNull). |
| | | 135 | | /// </summary> |
| | | 136 | | private void RenderSection( |
| | | 137 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 138 | | ref int seq, |
| | | 139 | | JsonElement dataSource, |
| | | 140 | | RenderSection section, |
| | | 141 | | HashSet<string> renderedPaths) |
| | | 142 | | { |
| | | 143 | | // If the section targets a specific path (e.g., an array), resolve it |
| | 0 | 144 | | JsonElement sectionData = dataSource; |
| | 0 | 145 | | if (!string.IsNullOrWhiteSpace(section.Path)) |
| | | 146 | | { |
| | 0 | 147 | | if (!dataSource.TryGetProperty(section.Path, out var pathElement)) |
| | 0 | 148 | | return; // Path doesn't exist in data — skip section |
| | | 149 | | |
| | 0 | 150 | | sectionData = pathElement; |
| | 0 | 151 | | renderedPaths.Add(section.Path); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | // Pre-check: would any fields actually render? |
| | | 155 | | // If all fields are null/empty and omitNull, skip the entire section. |
| | 0 | 156 | | if (section.Fields != null && section.Fields.Count > 0 && section.Render != "stat-row") |
| | | 157 | | { |
| | 0 | 158 | | var hasVisibleField = false; |
| | 0 | 159 | | foreach (var field in section.Fields) |
| | | 160 | | { |
| | | 161 | | // Multi-path: check if any path has a value |
| | 0 | 162 | | if (field.Paths.Count > 1) |
| | | 163 | | { |
| | 0 | 164 | | if (field.Paths.Any(p => sectionData.TryGetProperty(p, out var v) && !IsNullOrEmpty(v))) |
| | | 165 | | { |
| | 0 | 166 | | hasVisibleField = true; |
| | 0 | 167 | | break; |
| | | 168 | | } |
| | | 169 | | continue; |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | if (!sectionData.TryGetProperty(field.Path, out var val)) |
| | | 173 | | continue; |
| | 0 | 174 | | if (field.OmitNull && IsNullOrEmpty(val)) |
| | | 175 | | continue; |
| | 0 | 176 | | hasVisibleField = true; |
| | 0 | 177 | | break; |
| | | 178 | | } |
| | 0 | 179 | | if (!hasVisibleField) |
| | | 180 | | { |
| | | 181 | | // Still track paths as rendered so they don't leak into catch-all |
| | 0 | 182 | | foreach (var field in section.Fields) |
| | 0 | 183 | | foreach (var p in field.Paths) |
| | 0 | 184 | | renderedPaths.Add(p); |
| | 0 | 185 | | return; |
| | | 186 | | } |
| | | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | builder.OpenElement(seq++, "details"); |
| | 0 | 190 | | builder.AddAttribute(seq++, "class", "elp-section"); |
| | 0 | 191 | | if (!section.Collapsed) |
| | | 192 | | { |
| | 0 | 193 | | builder.AddAttribute(seq++, "open", true); |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | // Summary (section header) |
| | 0 | 197 | | builder.OpenElement(seq++, "summary"); |
| | 0 | 198 | | builder.AddAttribute(seq++, "class", "elp-section-header"); |
| | 0 | 199 | | builder.AddContent(seq++, section.Label); |
| | 0 | 200 | | builder.CloseElement(); // summary |
| | | 201 | | |
| | | 202 | | // Section content |
| | 0 | 203 | | builder.OpenElement(seq++, "div"); |
| | 0 | 204 | | builder.AddAttribute(seq++, "class", "elp-section-body"); |
| | | 205 | | |
| | 0 | 206 | | if (section.Render == "stat-row" && section.Fields != null) |
| | | 207 | | { |
| | 0 | 208 | | RenderStatRow(builder, ref seq, sectionData, section.Fields); |
| | 0 | 209 | | foreach (var field in section.Fields) |
| | 0 | 210 | | renderedPaths.Add(field.Path); |
| | | 211 | | } |
| | 0 | 212 | | else if (section.Render == "list" && sectionData.ValueKind == JsonValueKind.Array) |
| | | 213 | | { |
| | 0 | 214 | | RenderArrayAsList(builder, ref seq, sectionData, section.ItemFields); |
| | | 215 | | } |
| | 0 | 216 | | else if (section.Render == "table" && sectionData.ValueKind == JsonValueKind.Array) |
| | | 217 | | { |
| | 0 | 218 | | RenderArrayAsTable(builder, ref seq, sectionData, section.ItemFields); |
| | | 219 | | } |
| | 0 | 220 | | else if (section.Fields != null) |
| | | 221 | | { |
| | 0 | 222 | | foreach (var field in section.Fields) |
| | | 223 | | { |
| | 0 | 224 | | RenderDefinedField(builder, ref seq, sectionData, field); |
| | 0 | 225 | | foreach (var p in field.Paths) |
| | 0 | 226 | | renderedPaths.Add(p); |
| | | 227 | | } |
| | | 228 | | } |
| | | 229 | | |
| | 0 | 230 | | builder.CloseElement(); // div.elp-section-body |
| | 0 | 231 | | builder.CloseElement(); // details.elp-section |
| | 0 | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Renders all fields not already covered by defined sections. |
| | | 236 | | /// Auto-discovers structure: nested objects with 'fields' become subsections. |
| | | 237 | | /// </summary> |
| | | 238 | | private void RenderCatchAll( |
| | | 239 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 240 | | ref int seq, |
| | | 241 | | JsonElement dataSource, |
| | | 242 | | HashSet<string> renderedPaths) |
| | | 243 | | { |
| | 0 | 244 | | if (dataSource.ValueKind != JsonValueKind.Object) |
| | 0 | 245 | | return; |
| | | 246 | | |
| | 0 | 247 | | var unrenderedFields = new List<(string Name, JsonElement Value)>(); |
| | 0 | 248 | | foreach (var prop in dataSource.EnumerateObject()) |
| | | 249 | | { |
| | 0 | 250 | | if (renderedPaths.Contains(prop.Name)) |
| | | 251 | | continue; |
| | 0 | 252 | | unrenderedFields.Add((prop.Name, prop.Value)); |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | if (unrenderedFields.Count == 0) |
| | 0 | 256 | | return; |
| | | 257 | | |
| | | 258 | | // Separate scalar fields from complex ones (objects/arrays) |
| | 0 | 259 | | var scalarFields = new List<(string Name, JsonElement Value)>(); |
| | 0 | 260 | | var complexFields = new List<(string Name, JsonElement Value)>(); |
| | | 261 | | |
| | 0 | 262 | | foreach (var (name, value) in unrenderedFields) |
| | | 263 | | { |
| | 0 | 264 | | if (value.ValueKind == JsonValueKind.Object || value.ValueKind == JsonValueKind.Array) |
| | 0 | 265 | | complexFields.Add((name, value)); |
| | | 266 | | else |
| | 0 | 267 | | scalarFields.Add((name, value)); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | // Render scalars as a simple properties section |
| | 0 | 271 | | if (scalarFields.Count > 0) |
| | | 272 | | { |
| | 0 | 273 | | builder.OpenElement(seq++, "details"); |
| | 0 | 274 | | builder.AddAttribute(seq++, "class", "elp-section"); |
| | 0 | 275 | | builder.AddAttribute(seq++, "open", true); |
| | | 276 | | |
| | 0 | 277 | | builder.OpenElement(seq++, "summary"); |
| | 0 | 278 | | builder.AddAttribute(seq++, "class", "elp-section-header"); |
| | 0 | 279 | | builder.AddContent(seq++, "Properties"); |
| | 0 | 280 | | builder.CloseElement(); |
| | | 281 | | |
| | 0 | 282 | | builder.OpenElement(seq++, "div"); |
| | 0 | 283 | | builder.AddAttribute(seq++, "class", "elp-section-body"); |
| | | 284 | | |
| | 0 | 285 | | foreach (var (name, value) in scalarFields) |
| | | 286 | | { |
| | | 287 | | // Skip null/empty values in catch-all to reduce noise |
| | 0 | 288 | | if (IsNullOrEmpty(value)) |
| | | 289 | | continue; |
| | 0 | 290 | | RenderKeyValue(builder, ref seq, FormatFieldName(name), FormatScalarValue(value)); |
| | | 291 | | } |
| | | 292 | | |
| | 0 | 293 | | builder.CloseElement(); // div |
| | 0 | 294 | | builder.CloseElement(); // details |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | // Render complex fields as individual subsections |
| | 0 | 298 | | foreach (var (name, value) in complexFields) |
| | | 299 | | { |
| | 0 | 300 | | builder.OpenElement(seq++, "details"); |
| | 0 | 301 | | builder.AddAttribute(seq++, "class", "elp-section"); |
| | 0 | 302 | | builder.AddAttribute(seq++, "open", true); |
| | | 303 | | |
| | 0 | 304 | | builder.OpenElement(seq++, "summary"); |
| | 0 | 305 | | builder.AddAttribute(seq++, "class", "elp-section-header"); |
| | 0 | 306 | | builder.AddContent(seq++, FormatFieldName(name)); |
| | 0 | 307 | | builder.CloseElement(); |
| | | 308 | | |
| | 0 | 309 | | builder.OpenElement(seq++, "div"); |
| | 0 | 310 | | builder.AddAttribute(seq++, "class", "elp-section-body"); |
| | | 311 | | |
| | 0 | 312 | | RenderGenericValue(builder, ref seq, value, 0); |
| | | 313 | | |
| | 0 | 314 | | builder.CloseElement(); // div |
| | 0 | 315 | | builder.CloseElement(); // details |
| | | 316 | | } |
| | 0 | 317 | | } |
| | | 318 | | |
| | | 319 | | // ================================================================================== |
| | | 320 | | // Field Rendering Helpers |
| | | 321 | | // ================================================================================== |
| | | 322 | | |
| | | 323 | | /// <summary> |
| | | 324 | | /// Renders a field defined in a RenderField specification. |
| | | 325 | | /// </summary> |
| | | 326 | | private void RenderDefinedField( |
| | | 327 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 328 | | ref int seq, |
| | | 329 | | JsonElement dataSource, |
| | | 330 | | RenderField field) |
| | | 331 | | { |
| | 0 | 332 | | if (field.Render == "hidden") |
| | 0 | 333 | | return; |
| | | 334 | | |
| | | 335 | | // Multi-path support: resolve all paths and concatenate values |
| | 0 | 336 | | if (field.Paths.Count > 1) |
| | | 337 | | { |
| | 0 | 338 | | var parts = new List<string>(); |
| | 0 | 339 | | foreach (var path in field.Paths) |
| | | 340 | | { |
| | 0 | 341 | | if (dataSource.TryGetProperty(path, out var partValue) && !IsNullOrEmpty(partValue)) |
| | 0 | 342 | | parts.Add(FormatScalarValue(partValue)); |
| | | 343 | | } |
| | | 344 | | |
| | 0 | 345 | | if (parts.Count == 0 && field.OmitNull) |
| | 0 | 346 | | return; |
| | | 347 | | |
| | 0 | 348 | | var label = field.Label ?? FormatFieldName(field.Paths[0]); |
| | 0 | 349 | | var combined = parts.Count > 0 ? string.Join(" ", parts) : "—"; |
| | 0 | 350 | | RenderKeyValue(builder, ref seq, label, combined); |
| | 0 | 351 | | return; |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | // Single-path rendering (original behavior) |
| | 0 | 355 | | if (!dataSource.TryGetProperty(field.Path, out var value)) |
| | 0 | 356 | | return; |
| | | 357 | | |
| | | 358 | | // OmitNull: skip fields with null/empty/dash values |
| | 0 | 359 | | if (field.OmitNull && IsNullOrEmpty(value)) |
| | 0 | 360 | | return; |
| | | 361 | | |
| | 0 | 362 | | var fieldLabel = field.Label ?? FormatFieldName(field.Path); |
| | | 363 | | |
| | 0 | 364 | | switch (field.Render) |
| | | 365 | | { |
| | | 366 | | case "heading": |
| | 0 | 367 | | builder.OpenElement(seq++, "h4"); |
| | 0 | 368 | | builder.AddAttribute(seq++, "class", "elp-subheading"); |
| | 0 | 369 | | builder.AddContent(seq++, FormatScalarValue(value)); |
| | 0 | 370 | | builder.CloseElement(); |
| | 0 | 371 | | break; |
| | | 372 | | |
| | | 373 | | case "richtext": |
| | 0 | 374 | | var text = value.GetString() ?? string.Empty; |
| | 0 | 375 | | if (!string.IsNullOrWhiteSpace(text)) |
| | | 376 | | { |
| | 0 | 377 | | builder.OpenElement(seq++, "div"); |
| | 0 | 378 | | builder.AddAttribute(seq++, "class", "elp-richtext"); |
| | 0 | 379 | | builder.AddMarkupContent(seq++, MarkdownService.ToHtml(text)); |
| | 0 | 380 | | builder.CloseElement(); |
| | | 381 | | } |
| | 0 | 382 | | break; |
| | | 383 | | |
| | 0 | 384 | | case "chips" when value.ValueKind == JsonValueKind.Array: |
| | 0 | 385 | | builder.OpenElement(seq++, "div"); |
| | 0 | 386 | | builder.AddAttribute(seq++, "class", "elp-field"); |
| | | 387 | | |
| | 0 | 388 | | builder.OpenElement(seq++, "span"); |
| | 0 | 389 | | builder.AddAttribute(seq++, "class", "elp-field-label"); |
| | 0 | 390 | | builder.AddContent(seq++, fieldLabel); |
| | 0 | 391 | | builder.CloseElement(); |
| | | 392 | | |
| | 0 | 393 | | builder.OpenElement(seq++, "div"); |
| | 0 | 394 | | builder.AddAttribute(seq++, "class", "elp-chips"); |
| | 0 | 395 | | foreach (var item in value.EnumerateArray()) |
| | | 396 | | { |
| | 0 | 397 | | builder.OpenElement(seq++, "span"); |
| | 0 | 398 | | builder.AddAttribute(seq++, "class", "elp-chip"); |
| | 0 | 399 | | builder.AddContent(seq++, FormatScalarValue(item)); |
| | 0 | 400 | | builder.CloseElement(); |
| | | 401 | | } |
| | 0 | 402 | | builder.CloseElement(); // div.elp-chips |
| | 0 | 403 | | builder.CloseElement(); // div.elp-field |
| | 0 | 404 | | break; |
| | | 405 | | |
| | | 406 | | default: |
| | | 407 | | // Default: key-value pair |
| | 0 | 408 | | if (value.ValueKind == JsonValueKind.Object || value.ValueKind == JsonValueKind.Array) |
| | | 409 | | { |
| | 0 | 410 | | RenderGenericValue(builder, ref seq, value, 0); |
| | | 411 | | } |
| | | 412 | | else |
| | | 413 | | { |
| | 0 | 414 | | RenderKeyValue(builder, ref seq, fieldLabel, FormatScalarValue(value)); |
| | | 415 | | } |
| | | 416 | | break; |
| | | 417 | | } |
| | 0 | 418 | | } |
| | | 419 | | |
| | | 420 | | /// <summary> |
| | | 421 | | /// Renders an array as a list of items, each rendered with itemFields or generically. |
| | | 422 | | /// </summary> |
| | | 423 | | private void RenderArrayAsList( |
| | | 424 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 425 | | ref int seq, |
| | | 426 | | JsonElement array, |
| | | 427 | | List<RenderField>? itemFields) |
| | | 428 | | { |
| | 0 | 429 | | foreach (var item in array.EnumerateArray()) |
| | | 430 | | { |
| | 0 | 431 | | builder.OpenElement(seq++, "div"); |
| | 0 | 432 | | builder.AddAttribute(seq++, "class", "elp-list-item"); |
| | | 433 | | |
| | 0 | 434 | | if (item.ValueKind == JsonValueKind.Object) |
| | | 435 | | { |
| | | 436 | | // Check for nested fields pattern |
| | 0 | 437 | | var innerData = GetFieldsElement(item) ?? item; |
| | | 438 | | |
| | 0 | 439 | | if (itemFields != null && itemFields.Count > 0) |
| | | 440 | | { |
| | 0 | 441 | | foreach (var field in itemFields) |
| | | 442 | | { |
| | 0 | 443 | | RenderDefinedField(builder, ref seq, innerData, field); |
| | | 444 | | } |
| | | 445 | | } |
| | | 446 | | else |
| | | 447 | | { |
| | 0 | 448 | | RenderGenericObject(builder, ref seq, innerData, 0); |
| | | 449 | | } |
| | | 450 | | } |
| | | 451 | | else |
| | | 452 | | { |
| | 0 | 453 | | builder.OpenElement(seq++, "span"); |
| | 0 | 454 | | builder.AddContent(seq++, FormatScalarValue(item)); |
| | 0 | 455 | | builder.CloseElement(); |
| | | 456 | | } |
| | | 457 | | |
| | 0 | 458 | | builder.CloseElement(); // div.elp-list-item |
| | | 459 | | } |
| | 0 | 460 | | } |
| | | 461 | | |
| | | 462 | | /// <summary> |
| | | 463 | | /// Renders an array as a table (for flat, uniform arrays). |
| | | 464 | | /// </summary> |
| | | 465 | | private void RenderArrayAsTable( |
| | | 466 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 467 | | ref int seq, |
| | | 468 | | JsonElement array, |
| | | 469 | | List<RenderField>? itemFields) |
| | | 470 | | { |
| | | 471 | | // Determine columns from itemFields or first array item |
| | 0 | 472 | | var columns = new List<(string Path, string Label)>(); |
| | 0 | 473 | | if (itemFields != null && itemFields.Count > 0) |
| | | 474 | | { |
| | 0 | 475 | | columns = itemFields |
| | 0 | 476 | | .Where(f => f.Render != "hidden") |
| | 0 | 477 | | .Select(f => (f.Path, Label: f.Label ?? FormatFieldName(f.Path))) |
| | 0 | 478 | | .ToList(); |
| | | 479 | | } |
| | | 480 | | else |
| | | 481 | | { |
| | | 482 | | // Auto-detect from first item |
| | 0 | 483 | | foreach (var item in array.EnumerateArray()) |
| | | 484 | | { |
| | 0 | 485 | | if (item.ValueKind == JsonValueKind.Object) |
| | | 486 | | { |
| | 0 | 487 | | var source = GetFieldsElement(item) ?? item; |
| | 0 | 488 | | foreach (var prop in source.EnumerateObject()) |
| | | 489 | | { |
| | 0 | 490 | | if (prop.Value.ValueKind != JsonValueKind.Object && |
| | 0 | 491 | | prop.Value.ValueKind != JsonValueKind.Array) |
| | | 492 | | { |
| | 0 | 493 | | columns.Add((prop.Name, FormatFieldName(prop.Name))); |
| | | 494 | | } |
| | | 495 | | } |
| | | 496 | | } |
| | | 497 | | break; // Only inspect first item |
| | | 498 | | } |
| | | 499 | | } |
| | | 500 | | |
| | 0 | 501 | | if (columns.Count == 0) |
| | 0 | 502 | | return; |
| | | 503 | | |
| | 0 | 504 | | builder.OpenElement(seq++, "table"); |
| | 0 | 505 | | builder.AddAttribute(seq++, "class", "elp-table"); |
| | | 506 | | |
| | | 507 | | // Header row |
| | 0 | 508 | | builder.OpenElement(seq++, "thead"); |
| | 0 | 509 | | builder.OpenElement(seq++, "tr"); |
| | 0 | 510 | | foreach (var (_, label) in columns) |
| | | 511 | | { |
| | 0 | 512 | | builder.OpenElement(seq++, "th"); |
| | 0 | 513 | | builder.AddContent(seq++, label); |
| | 0 | 514 | | builder.CloseElement(); |
| | | 515 | | } |
| | 0 | 516 | | builder.CloseElement(); // tr |
| | 0 | 517 | | builder.CloseElement(); // thead |
| | | 518 | | |
| | | 519 | | // Data rows |
| | 0 | 520 | | builder.OpenElement(seq++, "tbody"); |
| | 0 | 521 | | foreach (var item in array.EnumerateArray()) |
| | | 522 | | { |
| | 0 | 523 | | if (item.ValueKind != JsonValueKind.Object) |
| | | 524 | | continue; |
| | 0 | 525 | | var source = GetFieldsElement(item) ?? item; |
| | | 526 | | |
| | 0 | 527 | | builder.OpenElement(seq++, "tr"); |
| | 0 | 528 | | foreach (var (path, _) in columns) |
| | | 529 | | { |
| | 0 | 530 | | builder.OpenElement(seq++, "td"); |
| | 0 | 531 | | if (source.TryGetProperty(path, out var cellValue)) |
| | | 532 | | { |
| | 0 | 533 | | builder.AddContent(seq++, FormatScalarValue(cellValue)); |
| | | 534 | | } |
| | 0 | 535 | | builder.CloseElement(); |
| | | 536 | | } |
| | 0 | 537 | | builder.CloseElement(); // tr |
| | | 538 | | } |
| | 0 | 539 | | builder.CloseElement(); // tbody |
| | 0 | 540 | | builder.CloseElement(); // table |
| | 0 | 541 | | } |
| | | 542 | | |
| | | 543 | | // ================================================================================== |
| | | 544 | | // Generic Rendering (no definition) |
| | | 545 | | // ================================================================================== |
| | | 546 | | |
| | | 547 | | /// <summary> |
| | | 548 | | /// Renders any JSON value generically. Handles recursive structure. |
| | | 549 | | /// </summary> |
| | | 550 | | private void RenderGenericValue( |
| | | 551 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 552 | | ref int seq, |
| | | 553 | | JsonElement value, |
| | | 554 | | int depth) |
| | | 555 | | { |
| | 0 | 556 | | if (depth > 10) |
| | 0 | 557 | | return; // Safety limit |
| | | 558 | | |
| | 0 | 559 | | switch (value.ValueKind) |
| | | 560 | | { |
| | | 561 | | case JsonValueKind.Object: |
| | | 562 | | // Check for the { fields: {...} } pattern — unwrap it |
| | 0 | 563 | | var inner = GetFieldsElement(value) ?? value; |
| | 0 | 564 | | RenderGenericObject(builder, ref seq, inner, depth); |
| | 0 | 565 | | break; |
| | | 566 | | |
| | | 567 | | case JsonValueKind.Array: |
| | 0 | 568 | | RenderGenericArray(builder, ref seq, value, depth); |
| | 0 | 569 | | break; |
| | | 570 | | |
| | | 571 | | default: |
| | 0 | 572 | | builder.OpenElement(seq++, "span"); |
| | 0 | 573 | | builder.AddContent(seq++, FormatScalarValue(value)); |
| | 0 | 574 | | builder.CloseElement(); |
| | | 575 | | break; |
| | | 576 | | } |
| | 0 | 577 | | } |
| | | 578 | | |
| | | 579 | | /// <summary> |
| | | 580 | | /// Renders an object's properties as key-value pairs or nested subsections. |
| | | 581 | | /// </summary> |
| | | 582 | | private void RenderGenericObject( |
| | | 583 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 584 | | ref int seq, |
| | | 585 | | JsonElement obj, |
| | | 586 | | int depth) |
| | | 587 | | { |
| | 0 | 588 | | foreach (var prop in obj.EnumerateObject()) |
| | | 589 | | { |
| | 0 | 590 | | if (prop.Value.ValueKind == JsonValueKind.Null) |
| | | 591 | | continue; |
| | | 592 | | |
| | 0 | 593 | | var label = FormatFieldName(prop.Name); |
| | | 594 | | |
| | 0 | 595 | | if (prop.Value.ValueKind == JsonValueKind.Object || |
| | 0 | 596 | | prop.Value.ValueKind == JsonValueKind.Array) |
| | | 597 | | { |
| | | 598 | | // Nested complex value — render as a collapsible subsection |
| | 0 | 599 | | builder.OpenElement(seq++, "details"); |
| | 0 | 600 | | builder.AddAttribute(seq++, "class", "elp-subsection"); |
| | 0 | 601 | | builder.AddAttribute(seq++, "open", true); |
| | | 602 | | |
| | 0 | 603 | | builder.OpenElement(seq++, "summary"); |
| | 0 | 604 | | builder.AddAttribute(seq++, "class", "elp-subsection-header"); |
| | 0 | 605 | | builder.AddContent(seq++, label); |
| | 0 | 606 | | builder.CloseElement(); |
| | | 607 | | |
| | 0 | 608 | | builder.OpenElement(seq++, "div"); |
| | 0 | 609 | | builder.AddAttribute(seq++, "class", "elp-subsection-body"); |
| | 0 | 610 | | RenderGenericValue(builder, ref seq, prop.Value, depth + 1); |
| | 0 | 611 | | builder.CloseElement(); |
| | | 612 | | |
| | 0 | 613 | | builder.CloseElement(); // details |
| | | 614 | | } |
| | | 615 | | else |
| | | 616 | | { |
| | 0 | 617 | | RenderKeyValue(builder, ref seq, label, FormatScalarValue(prop.Value)); |
| | | 618 | | } |
| | | 619 | | } |
| | 0 | 620 | | } |
| | | 621 | | |
| | | 622 | | /// <summary> |
| | | 623 | | /// Renders an array generically — simple values as a comma list, |
| | | 624 | | /// objects as individual items with dividers. |
| | | 625 | | /// </summary> |
| | | 626 | | private void RenderGenericArray( |
| | | 627 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 628 | | ref int seq, |
| | | 629 | | JsonElement array, |
| | | 630 | | int depth) |
| | | 631 | | { |
| | | 632 | | // Check if all items are scalars |
| | 0 | 633 | | var allScalar = true; |
| | 0 | 634 | | foreach (var item in array.EnumerateArray()) |
| | | 635 | | { |
| | 0 | 636 | | if (item.ValueKind == JsonValueKind.Object || item.ValueKind == JsonValueKind.Array) |
| | | 637 | | { |
| | 0 | 638 | | allScalar = false; |
| | 0 | 639 | | break; |
| | | 640 | | } |
| | | 641 | | } |
| | | 642 | | |
| | 0 | 643 | | if (allScalar) |
| | | 644 | | { |
| | | 645 | | // Render as chips/tags |
| | 0 | 646 | | builder.OpenElement(seq++, "div"); |
| | 0 | 647 | | builder.AddAttribute(seq++, "class", "elp-chips"); |
| | 0 | 648 | | foreach (var item in array.EnumerateArray()) |
| | | 649 | | { |
| | 0 | 650 | | builder.OpenElement(seq++, "span"); |
| | 0 | 651 | | builder.AddAttribute(seq++, "class", "elp-chip"); |
| | 0 | 652 | | builder.AddContent(seq++, FormatScalarValue(item)); |
| | 0 | 653 | | builder.CloseElement(); |
| | | 654 | | } |
| | 0 | 655 | | builder.CloseElement(); |
| | | 656 | | } |
| | | 657 | | else |
| | | 658 | | { |
| | | 659 | | // Render each item as a block |
| | 0 | 660 | | foreach (var item in array.EnumerateArray()) |
| | | 661 | | { |
| | 0 | 662 | | builder.OpenElement(seq++, "div"); |
| | 0 | 663 | | builder.AddAttribute(seq++, "class", "elp-list-item"); |
| | 0 | 664 | | RenderGenericValue(builder, ref seq, item, depth + 1); |
| | 0 | 665 | | builder.CloseElement(); |
| | | 666 | | } |
| | | 667 | | } |
| | 0 | 668 | | } |
| | | 669 | | |
| | | 670 | | // ================================================================================== |
| | | 671 | | // Primitives |
| | | 672 | | // ================================================================================== |
| | | 673 | | |
| | | 674 | | /// <summary> |
| | | 675 | | /// Renders a simple label: value pair. |
| | | 676 | | /// Values longer than 50 characters stack below the label for readability. |
| | | 677 | | /// </summary> |
| | | 678 | | private static void RenderKeyValue( |
| | | 679 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 680 | | ref int seq, |
| | | 681 | | string label, |
| | | 682 | | string value) |
| | | 683 | | { |
| | 0 | 684 | | var isBlock = value.Length > 50; |
| | 0 | 685 | | builder.OpenElement(seq++, "div"); |
| | 0 | 686 | | builder.AddAttribute(seq++, "class", isBlock ? "elp-field elp-field--block" : "elp-field"); |
| | | 687 | | |
| | 0 | 688 | | builder.OpenElement(seq++, "span"); |
| | 0 | 689 | | builder.AddAttribute(seq++, "class", "elp-field-label"); |
| | 0 | 690 | | builder.AddContent(seq++, label); |
| | 0 | 691 | | builder.CloseElement(); |
| | | 692 | | |
| | 0 | 693 | | builder.OpenElement(seq++, "span"); |
| | 0 | 694 | | builder.AddAttribute(seq++, "class", "elp-field-value"); |
| | 0 | 695 | | builder.AddContent(seq++, value); |
| | 0 | 696 | | builder.CloseElement(); |
| | | 697 | | |
| | 0 | 698 | | builder.CloseElement(); // div.elp-field |
| | 0 | 699 | | } |
| | | 700 | | |
| | | 701 | | // ================================================================================== |
| | | 702 | | // Formatting Utilities |
| | | 703 | | // ================================================================================== |
| | | 704 | | |
| | | 705 | | /// <summary> |
| | | 706 | | /// Resolves the display value for a field, handling both single-path and multi-path fields. |
| | | 707 | | /// For multi-path, concatenates non-null values with a space separator. |
| | | 708 | | /// Returns (resolvedValue, found). When not found, resolvedValue is default. |
| | | 709 | | /// </summary> |
| | | 710 | | private static (JsonElement value, bool found) ResolveFieldValue(JsonElement dataSource, RenderField field) |
| | | 711 | | { |
| | 0 | 712 | | if (field.Paths.Count <= 1) |
| | | 713 | | { |
| | | 714 | | // Single path — standard lookup |
| | 0 | 715 | | if (dataSource.TryGetProperty(field.Path, out var val)) |
| | 0 | 716 | | return (val, true); |
| | 0 | 717 | | return (default, false); |
| | | 718 | | } |
| | | 719 | | |
| | | 720 | | // Multi-path: concatenate non-null text values |
| | 0 | 721 | | var parts = new List<string>(); |
| | 0 | 722 | | foreach (var path in field.Paths) |
| | | 723 | | { |
| | 0 | 724 | | if (dataSource.TryGetProperty(path, out var val) && !IsNullOrEmpty(val)) |
| | 0 | 725 | | parts.Add(FormatScalarValue(val)); |
| | | 726 | | } |
| | | 727 | | |
| | 0 | 728 | | if (parts.Count == 0) |
| | 0 | 729 | | return (default, false); |
| | | 730 | | |
| | | 731 | | // Return as a synthetic string JsonElement via round-trip |
| | 0 | 732 | | var combined = string.Join(" ", parts); |
| | 0 | 733 | | using var doc = JsonDocument.Parse($"\"{combined.Replace("\"", "\\\"")}\""); |
| | 0 | 734 | | return (doc.RootElement.Clone(), true); |
| | 0 | 735 | | } |
| | | 736 | | |
| | | 737 | | /// <summary> |
| | | 738 | | /// Returns all JSON field paths referenced by a RenderField (for rendered-path tracking). |
| | | 739 | | /// </summary> |
| | 0 | 740 | | private static IEnumerable<string> GetAllPaths(RenderField field) => field.Paths; |
| | | 741 | | |
| | | 742 | | /// <summary> |
| | | 743 | | /// Formats a JSON field name for display: snake_case/camelCase → Title Case. |
| | | 744 | | /// </summary> |
| | | 745 | | private static string FormatFieldName(string fieldName) |
| | | 746 | | { |
| | 0 | 747 | | if (string.IsNullOrWhiteSpace(fieldName)) |
| | 0 | 748 | | return fieldName; |
| | | 749 | | |
| | | 750 | | // Replace underscores with spaces |
| | 0 | 751 | | var withSpaces = fieldName.Replace('_', ' '); |
| | | 752 | | |
| | | 753 | | // Insert spaces before capitals (camelCase) |
| | 0 | 754 | | var chars = new List<char>(); |
| | 0 | 755 | | for (int i = 0; i < withSpaces.Length; i++) |
| | | 756 | | { |
| | 0 | 757 | | if (i > 0 && char.IsUpper(withSpaces[i]) && char.IsLower(withSpaces[i - 1])) |
| | 0 | 758 | | chars.Add(' '); |
| | 0 | 759 | | chars.Add(withSpaces[i]); |
| | | 760 | | } |
| | | 761 | | |
| | | 762 | | // Title case |
| | 0 | 763 | | var words = new string(chars.ToArray()) |
| | 0 | 764 | | .Split(' ', StringSplitOptions.RemoveEmptyEntries); |
| | 0 | 765 | | var titleCased = words.Select(w => |
| | 0 | 766 | | w.Length > 0 |
| | 0 | 767 | | ? char.ToUpper(w[0], CultureInfo.InvariantCulture) + w[1..].ToLower(CultureInfo.InvariantCulture) |
| | 0 | 768 | | : w); |
| | | 769 | | |
| | 0 | 770 | | return string.Join(" ", titleCased); |
| | | 771 | | } |
| | | 772 | | |
| | | 773 | | /// <summary> |
| | | 774 | | /// Formats a scalar JSON value for display. |
| | | 775 | | /// </summary> |
| | | 776 | | private static string FormatScalarValue(JsonElement value) |
| | | 777 | | { |
| | 0 | 778 | | return value.ValueKind switch |
| | 0 | 779 | | { |
| | 0 | 780 | | JsonValueKind.String => value.GetString() ?? string.Empty, |
| | 0 | 781 | | JsonValueKind.Number => value.GetRawText(), |
| | 0 | 782 | | JsonValueKind.True => "Yes", |
| | 0 | 783 | | JsonValueKind.False => "No", |
| | 0 | 784 | | JsonValueKind.Null => "—", |
| | 0 | 785 | | _ => value.GetRawText() |
| | 0 | 786 | | }; |
| | | 787 | | } |
| | | 788 | | |
| | | 789 | | /// <summary> |
| | | 790 | | /// Returns true if the JSON value is null, empty string, or the em-dash placeholder. |
| | | 791 | | /// </summary> |
| | | 792 | | private static bool IsNullOrEmpty(JsonElement value) |
| | | 793 | | { |
| | 0 | 794 | | if (value.ValueKind == JsonValueKind.Null || value.ValueKind == JsonValueKind.Undefined) |
| | 0 | 795 | | return true; |
| | | 796 | | |
| | 0 | 797 | | if (value.ValueKind == JsonValueKind.String) |
| | | 798 | | { |
| | 0 | 799 | | var str = value.GetString(); |
| | 0 | 800 | | return string.IsNullOrWhiteSpace(str) || str == "—" || str == "-"; |
| | | 801 | | } |
| | | 802 | | |
| | 0 | 803 | | if (value.ValueKind == JsonValueKind.Array && value.GetArrayLength() == 0) |
| | 0 | 804 | | return true; |
| | | 805 | | |
| | 0 | 806 | | return false; |
| | | 807 | | } |
| | | 808 | | |
| | | 809 | | /// <summary> |
| | | 810 | | /// Renders fields as a compact horizontal stat row (labels on top, values below). |
| | | 811 | | /// Used for D&D ability scores and similar compact stat groups. |
| | | 812 | | /// </summary> |
| | | 813 | | private static void RenderStatRow( |
| | | 814 | | Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder, |
| | | 815 | | ref int seq, |
| | | 816 | | JsonElement dataSource, |
| | | 817 | | List<RenderField> fields) |
| | | 818 | | { |
| | 0 | 819 | | builder.OpenElement(seq++, "table"); |
| | 0 | 820 | | builder.AddAttribute(seq++, "class", "elp-stat-row"); |
| | | 821 | | |
| | | 822 | | // Header row: labels |
| | 0 | 823 | | builder.OpenElement(seq++, "thead"); |
| | 0 | 824 | | builder.OpenElement(seq++, "tr"); |
| | 0 | 825 | | foreach (var field in fields) |
| | | 826 | | { |
| | 0 | 827 | | builder.OpenElement(seq++, "th"); |
| | 0 | 828 | | builder.AddContent(seq++, field.Label ?? FormatFieldName(field.Path)); |
| | 0 | 829 | | builder.CloseElement(); |
| | | 830 | | } |
| | 0 | 831 | | builder.CloseElement(); // tr |
| | 0 | 832 | | builder.CloseElement(); // thead |
| | | 833 | | |
| | | 834 | | // Value row |
| | 0 | 835 | | builder.OpenElement(seq++, "tbody"); |
| | 0 | 836 | | builder.OpenElement(seq++, "tr"); |
| | 0 | 837 | | foreach (var field in fields) |
| | | 838 | | { |
| | 0 | 839 | | builder.OpenElement(seq++, "td"); |
| | 0 | 840 | | if (dataSource.TryGetProperty(field.Path, out var value)) |
| | | 841 | | { |
| | 0 | 842 | | builder.AddContent(seq++, FormatScalarValue(value)); |
| | | 843 | | } |
| | | 844 | | else |
| | | 845 | | { |
| | 0 | 846 | | builder.AddContent(seq++, "—"); |
| | | 847 | | } |
| | 0 | 848 | | builder.CloseElement(); |
| | | 849 | | } |
| | 0 | 850 | | builder.CloseElement(); // tr |
| | 0 | 851 | | builder.CloseElement(); // tbody |
| | | 852 | | |
| | 0 | 853 | | builder.CloseElement(); // table |
| | 0 | 854 | | } |
| | | 855 | | } |