2020-06-15 22:23:08 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2020-07-16 14:28:42 +08:00
|
|
|
|
using System.Linq;
|
2020-06-15 22:23:08 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AntDesign.JsInterop;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using OneOf;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
using ColumnType = OneOf<int, Dictionary<string, int>>;
|
|
|
|
|
|
|
|
|
|
public partial class Descriptions : AntDomComponentBase
|
|
|
|
|
{
|
|
|
|
|
#region Parameters
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Bordered { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Layout { get; set; } = DescriptionsLayout.Horizontal;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public ColumnType Column { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Size { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-09-16 13:58:16 +08:00
|
|
|
|
public string Title { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment TitleTemplate { get; set; }
|
2020-06-15 22:23:08 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Colon { get; set; }
|
|
|
|
|
|
2021-03-12 17:02:11 +08:00
|
|
|
|
#endregion Parameters
|
2020-07-16 14:28:42 +08:00
|
|
|
|
|
2020-06-15 22:23:08 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
|
|
|
|
|
|
public IList<IDescriptionsItem> Items { get; } = new List<IDescriptionsItem>();
|
|
|
|
|
|
|
|
|
|
private List<List<(IDescriptionsItem item, int realSpan)>> _itemMatrix = new List<List<(IDescriptionsItem item, int realSpan)>>();
|
|
|
|
|
|
|
|
|
|
[Inject]
|
2021-09-09 12:56:11 +08:00
|
|
|
|
private IDomEventListener DomEventListener { get; set; }
|
2020-06-15 22:23:08 +08:00
|
|
|
|
|
|
|
|
|
private int _realColumn;
|
|
|
|
|
|
2021-09-22 22:08:32 +08:00
|
|
|
|
private static Dictionary<string, int> _defaultColumnMap = new Dictionary<string, int>
|
2020-06-17 14:26:47 +08:00
|
|
|
|
{
|
2021-09-22 22:08:32 +08:00
|
|
|
|
{ "Xxl", 3 },
|
|
|
|
|
{ "Xl", 3},
|
|
|
|
|
{ "Lg", 3},
|
|
|
|
|
{ "Md", 3},
|
|
|
|
|
{ "Sm", 2},
|
|
|
|
|
{ "Xs", 1}
|
2020-06-15 22:23:08 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-09-22 22:08:32 +08:00
|
|
|
|
private static readonly List<(int PixelWidth, BreakpointType Breakpoint)> _descriptionsResponsiveMap = new List<(int, BreakpointType)>()
|
2020-08-26 11:13:36 +08:00
|
|
|
|
{
|
2021-09-22 22:08:32 +08:00
|
|
|
|
(575,BreakpointType.Xs),
|
|
|
|
|
(576,BreakpointType.Sm),
|
|
|
|
|
(768,BreakpointType.Md),
|
|
|
|
|
(992,BreakpointType.Lg),
|
|
|
|
|
(1200,BreakpointType.Xl),
|
|
|
|
|
(1600,BreakpointType.Xxl)
|
2020-06-15 22:23:08 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private void SetClassMap()
|
|
|
|
|
{
|
2021-03-12 17:02:11 +08:00
|
|
|
|
ClassMapper
|
|
|
|
|
.Add("ant-descriptions")
|
|
|
|
|
.If("ant-descriptions", () => RTL)
|
2020-06-15 22:23:08 +08:00
|
|
|
|
.If("ant-descriptions-bordered", () => this.Bordered)
|
|
|
|
|
.If("ant-descriptions-middle", () => this.Size == DescriptionsSize.Middle)
|
|
|
|
|
.If("ant-descriptions-small", () => this.Size == DescriptionsSize.Small);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
SetClassMap();
|
2020-09-21 17:20:19 +08:00
|
|
|
|
await base.OnInitializedAsync();
|
|
|
|
|
}
|
2020-07-16 14:28:42 +08:00
|
|
|
|
|
2020-09-21 17:20:19 +08:00
|
|
|
|
protected override void OnAfterRender(bool firstRender)
|
|
|
|
|
{
|
|
|
|
|
if (firstRender && Column.IsT1)
|
2020-06-15 22:23:08 +08:00
|
|
|
|
{
|
2021-09-09 12:56:11 +08:00
|
|
|
|
DomEventListener.AddShared<object>("window", "resize", OnResize);
|
2020-06-15 22:23:08 +08:00
|
|
|
|
}
|
2020-09-21 17:20:19 +08:00
|
|
|
|
|
|
|
|
|
base.OnAfterRender(firstRender);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
2022-05-15 12:45:43 +08:00
|
|
|
|
DomEventListener?.Dispose();
|
2020-09-21 17:20:19 +08:00
|
|
|
|
base.Dispose(disposing);
|
2020-06-15 22:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 11:13:36 +08:00
|
|
|
|
private async void OnResize(object o)
|
|
|
|
|
{
|
|
|
|
|
await SetRealColumn();
|
|
|
|
|
PrepareMatrix();
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 22:23:08 +08:00
|
|
|
|
protected override async Task OnFirstAfterRenderAsync()
|
|
|
|
|
{
|
|
|
|
|
await SetRealColumn();
|
|
|
|
|
PrepareMatrix();
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
await base.OnFirstAfterRenderAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
|
|
|
{
|
|
|
|
|
PrepareMatrix();
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
await base.OnParametersSetAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PrepareMatrix()
|
|
|
|
|
{
|
|
|
|
|
List<List<(IDescriptionsItem item, int realSpan)>> itemMatrix = new List<List<(IDescriptionsItem item, int realSpan)>>();
|
|
|
|
|
|
|
|
|
|
List<(IDescriptionsItem item, int realSpan)> currentRow = new List<(IDescriptionsItem item, int realSpan)>();
|
|
|
|
|
var width = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < this.Items.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var item = this.Items[i];
|
|
|
|
|
width += item.Span;
|
|
|
|
|
|
|
|
|
|
if (width >= _realColumn)
|
|
|
|
|
{
|
|
|
|
|
currentRow.Add((item, _realColumn - (width - item.Span)));
|
|
|
|
|
FlushRow();
|
|
|
|
|
}
|
|
|
|
|
else if (i == this.Items.Count - 1)
|
|
|
|
|
{
|
|
|
|
|
currentRow.Add((item, _realColumn - (width - item.Span)));
|
|
|
|
|
FlushRow();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
currentRow.Add((item, item.Span));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this._itemMatrix = itemMatrix;
|
|
|
|
|
|
|
|
|
|
void FlushRow()
|
|
|
|
|
{
|
|
|
|
|
itemMatrix.Add(currentRow);
|
|
|
|
|
currentRow = new List<(IDescriptionsItem item, int realSpan)>();
|
|
|
|
|
width = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SetRealColumn()
|
|
|
|
|
{
|
|
|
|
|
if (Column.IsT0)
|
|
|
|
|
{
|
|
|
|
|
_realColumn = Column.AsT0 == 0 ? 3 : Column.AsT0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-17 22:14:01 +08:00
|
|
|
|
HtmlElement element = await JsInvokeAsync<HtmlElement>(JSInteropConstants.GetDomInfo, Ref);
|
|
|
|
|
var breakpointTuple = _descriptionsResponsiveMap.FirstOrDefault(x => x.PixelWidth > element.ClientWidth);
|
2021-09-22 22:08:32 +08:00
|
|
|
|
var bp = breakpointTuple == default ? BreakpointType.Xxl : breakpointTuple.Breakpoint;
|
2020-07-16 14:28:42 +08:00
|
|
|
|
_realColumn = Column.AsT1.ContainsKey(bp.ToString()) ? Column.AsT1[bp.ToString()] : _defaultColumnMap[bp.ToString()];
|
2020-06-15 22:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-16 14:28:42 +08:00
|
|
|
|
}
|