mirror of
https://github.com/unclshura/BlazorOpenApi.git
synced 2025-12-21 09:51:53 +00:00
Add ExpandoTree for hierarchical expandable nodes
This commit is contained in:
parent
dd22a91ca8
commit
878a7feded
@ -1,4 +1,5 @@
|
|||||||
<div class="expander @Class">
|
<CascadingValue Name="OpenAPIUI_ExpandoTree_Parent" Value="_anchor">
|
||||||
|
<div class="expander @Class">
|
||||||
<div class="ex-header @HeaderClass">
|
<div class="ex-header @HeaderClass">
|
||||||
@Title
|
@Title
|
||||||
<div onclick="@(() => Toggle())">
|
<div onclick="@(() => Toggle())">
|
||||||
@ -22,6 +23,7 @@
|
|||||||
@ChildContent
|
@ChildContent
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
</CascadingValue>
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
[Parameter]
|
[Parameter]
|
||||||
@ -35,9 +37,24 @@
|
|||||||
[Parameter]
|
[Parameter]
|
||||||
public string Class { get; set; } = "";
|
public string Class { get; set; } = "";
|
||||||
|
|
||||||
|
[CascadingParameter(Name = "OpenAPIUI_ExpandoTree")] public IExpandoTree Tree { get; set; } = null!;
|
||||||
|
[CascadingParameter(Name = "OpenAPIUI_ExpandoTree_Parent")] public string Parent { get; set; } = "";
|
||||||
|
|
||||||
|
private string _anchor = $"anc{Random.Shared.Next():X8}";
|
||||||
|
|
||||||
|
protected override void OnAfterRender(bool firstRender)
|
||||||
|
{
|
||||||
|
if (!firstRender)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Tree.Add(Title, _anchor, Parent, Collapsed);
|
||||||
|
Collapsed = Tree.IsCollapsed(Title);
|
||||||
|
}
|
||||||
|
|
||||||
void Toggle()
|
void Toggle()
|
||||||
{
|
{
|
||||||
Collapsed = !Collapsed;
|
Collapsed = !Collapsed;
|
||||||
|
Tree.Collapse(_anchor, Collapsed);
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,11 +48,19 @@
|
|||||||
<RequestBodyControl Value="@Value.RequestBody"/>
|
<RequestBodyControl Value="@Value.RequestBody"/>
|
||||||
<ResponsesControl Value="@Value.Responses" />
|
<ResponsesControl Value="@Value.Responses" />
|
||||||
|
|
||||||
|
@if (true)
|
||||||
|
{
|
||||||
|
var example = GenerateExampleData();
|
||||||
|
if ( !string.IsNullOrWhiteSpace(example) )
|
||||||
|
{
|
||||||
<div class="example-data">
|
<div class="example-data">
|
||||||
<h3>Example Data</h3>
|
<h3>Example Data</h3>
|
||||||
<pre>@GenerateExampleData()</pre>
|
<pre>@GenerateExampleData()</pre>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,7 +93,7 @@
|
|||||||
var exampleData = new Dictionary<string, object>();
|
var exampleData = new Dictionary<string, object>();
|
||||||
|
|
||||||
// Generate example data for parameters
|
// Generate example data for parameters
|
||||||
foreach (var parameter in Value.Parameters)
|
foreach (var parameter in Value.Parameters.Where(x => x.In == ParameterLocation.Query))
|
||||||
{
|
{
|
||||||
if (parameter.Example != null)
|
if (parameter.Example != null)
|
||||||
{
|
{
|
||||||
@ -121,7 +129,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonSerializer.Serialize(exampleData, new JsonSerializerOptions { WriteIndented = true });
|
return exampleData.Count == 0
|
||||||
|
? ""
|
||||||
|
: JsonSerializer.Serialize(exampleData, new JsonSerializerOptions { WriteIndented = true })
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
private object GenerateExampleFromSchema(OpenApiSchema? schema)
|
private object GenerateExampleFromSchema(OpenApiSchema? schema)
|
||||||
|
|||||||
54
BlazorOpenApi/ExpandoTree.cs
Normal file
54
BlazorOpenApi/ExpandoTree.cs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace BlazorOpenApi;
|
||||||
|
|
||||||
|
internal class ExpandoTree : IExpandoTree
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, ExpandoTreeNode> _nodes = new();
|
||||||
|
private readonly List<string> _order = new();
|
||||||
|
|
||||||
|
public void Add(string name, string anchor, string parentAnchor, bool collapsed)
|
||||||
|
{
|
||||||
|
if (_nodes.ContainsKey(anchor))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"A node with anchor '{anchor}' already exists.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var node = new ExpandoTreeNode
|
||||||
|
{
|
||||||
|
Name = name,
|
||||||
|
Anchor = anchor,
|
||||||
|
ParentAnchor = parentAnchor,
|
||||||
|
Collapsed = collapsed
|
||||||
|
};
|
||||||
|
|
||||||
|
_nodes[anchor] = node;
|
||||||
|
_order.Add(anchor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Collapse(string anchor, bool collapsed)
|
||||||
|
{
|
||||||
|
if (_nodes.TryGetValue(anchor, out var node))
|
||||||
|
{
|
||||||
|
node.Collapsed = collapsed;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new KeyNotFoundException($"No node found with anchor '{anchor}'.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsCollapsed(string anchor)
|
||||||
|
{
|
||||||
|
return _nodes.TryGetValue(anchor, out var node) && node.Collapsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Exists(string anchor) => _nodes.ContainsKey(anchor);
|
||||||
|
|
||||||
|
public ExpandoTreeNode[] GetChildren(string anchor) => _order
|
||||||
|
.Where(x => _nodes[x].ParentAnchor == anchor)
|
||||||
|
.Select(x => _nodes[x])
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
26
BlazorOpenApi/IExpandoTree.cs
Normal file
26
BlazorOpenApi/IExpandoTree.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BlazorOpenApi;
|
||||||
|
|
||||||
|
public class ExpandoTreeNode
|
||||||
|
{
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
public string Anchor { get; set; } = string.Empty;
|
||||||
|
public string ParentAnchor { get; set; } = string.Empty;
|
||||||
|
public bool Collapsed { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IExpandoTree
|
||||||
|
{
|
||||||
|
void Add(string name, string anchor, string parentAnchor, bool collapsed);
|
||||||
|
void Collapse(string anchor, bool collapsed);
|
||||||
|
bool IsCollapsed(string anchor);
|
||||||
|
bool Exists(string anchor);
|
||||||
|
ExpandoTreeNode[] GetChildren(string anchor);
|
||||||
|
}
|
||||||
|
|
||||||
|
//implement IExpandoTree
|
||||||
@ -2,13 +2,16 @@
|
|||||||
@using Microsoft.OpenApi.Models
|
@using Microsoft.OpenApi.Models
|
||||||
@using Microsoft.OpenApi.Readers
|
@using Microsoft.OpenApi.Readers
|
||||||
|
|
||||||
|
|
||||||
@if (Palette != null )
|
@if (Palette != null )
|
||||||
{
|
{
|
||||||
@PaletteStr
|
@PaletteStr
|
||||||
}
|
}
|
||||||
|
|
||||||
<div class="openapi-ui">
|
<div class="openapi-ui">
|
||||||
<CascadingValue Value="@_api">
|
<CascadingValue Value="@_api" IsFixed="true">
|
||||||
|
<CascadingValue Name="OpenAPIUI_ExpandoTree" Value="_tree" IsFixed="true">
|
||||||
|
|
||||||
<HeaderControl Value="@_api.Info" DownloadUrl="@Url" />
|
<HeaderControl Value="@_api.Info" DownloadUrl="@Url" />
|
||||||
<ServersControl Value="@_api.Servers" />
|
<ServersControl Value="@_api.Servers" />
|
||||||
@if (_api.Paths?.Count > 0)
|
@if (_api.Paths?.Count > 0)
|
||||||
@ -27,6 +30,8 @@
|
|||||||
<h2>Components</h2>
|
<h2>Components</h2>
|
||||||
<ComponentsControl Value="@_api.Components" />
|
<ComponentsControl Value="@_api.Components" />
|
||||||
}
|
}
|
||||||
|
|
||||||
|
</CascadingValue>
|
||||||
</CascadingValue>
|
</CascadingValue>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -35,8 +40,11 @@
|
|||||||
[Parameter] public string Json { get; set; } = "";
|
[Parameter] public string Json { get; set; } = "";
|
||||||
[Parameter] public OpenApiUiPalette? Palette { get; set; }
|
[Parameter] public OpenApiUiPalette? Palette { get; set; }
|
||||||
|
|
||||||
|
|
||||||
private string _loadedFor = "";
|
private string _loadedFor = "";
|
||||||
|
|
||||||
private OpenApiDocument _api = new();
|
private OpenApiDocument _api = new();
|
||||||
|
private IExpandoTree _tree = new ExpandoTree();
|
||||||
|
|
||||||
private MarkupString PaletteStr => Palette?.AsMarkupString ?? new();
|
private MarkupString PaletteStr => Palette?.AsMarkupString ?? new();
|
||||||
|
|
||||||
@ -98,4 +106,6 @@
|
|||||||
_api = new();
|
_api = new();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user