ant-design-blazor/components/grid/Row.razor.cs

183 lines
6.4 KiB
C#
Raw Normal View History

2020-03-15 23:54:48 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using AntDesign.JsInterop;
2020-03-15 23:54:48 +08:00
using Microsoft.AspNetCore.Components;
using OneOf;
namespace AntDesign
2020-03-15 23:54:48 +08:00
{
/*
* Possible values and meaning
* int - horizontal gutter
* Dictionary<string, int> - horizontal gutters for different screen sizes
* (int, int) - horizontal gutter, vertical gutter
* (Dictionary<string, int>, int) - horizontal gutters for different screen sizes, vertical gutter
* (int, Dictionary<string, int>) - horizontal gutter, vertical gutter for different screen sizes
* (Dictionary<string, int>, Dictionary<string, int>) - horizontal gutters for different screen sizes, vertical gutter for different screen sizes
*/
using GutterType = OneOf<int, Dictionary<string, int>, (int, int), (Dictionary<string, int>, int), (int, Dictionary<string, int>), (Dictionary<string, int>, Dictionary<string, int>)>;
2020-03-15 23:54:48 +08:00
public partial class Row : AntDomComponentBase
2020-03-15 23:54:48 +08:00
{
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter]
public string Type { get; set; }
2020-03-15 23:54:48 +08:00
/// <summary>
/// 'top' | 'middle' | 'bottom'
/// </summary>
[Parameter]
public string Align { get; set; }
2020-03-15 23:54:48 +08:00
/// <summary>
/// 'start' | 'end' | 'center' | 'space-around' | 'space-between'
/// </summary>
[Parameter]
public string Justify { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public bool Wrap { get; set; } = true;
[Parameter]
public GutterType Gutter { get; set; }
2020-03-15 23:54:48 +08:00
[Parameter]
public EventCallback<BreakpointType> OnBreakpoint { get; set; }
/// <summary>
/// Used to set gutter during pre-rendering
/// </summary>
[Parameter]
public BreakpointType DefaultBreakpoint { get; set; }
[Inject]
public DomEventService DomEventService { get; set; }
2020-03-15 23:54:48 +08:00
private string GutterStyle { get; set; }
2020-03-15 23:54:48 +08:00
public IList<Col> Cols { get; } = new List<Col>();
2020-03-15 23:54:48 +08:00
private static BreakpointType[] _breakpoints = new[] {
BreakpointType.Xs,
BreakpointType.Sm,
BreakpointType.Md,
BreakpointType.Lg,
BreakpointType.Xl,
BreakpointType.Xxl
2020-03-15 23:54:48 +08:00
};
protected override async Task OnInitializedAsync()
{
var prefixCls = "ant-row";
ClassMapper.Add(prefixCls)
.If($"{prefixCls}-top", () => Align == "top")
.If($"{prefixCls}-middle", () => Align == "middle")
.If($"{prefixCls}-bottom", () => Align == "bottom")
.If($"{prefixCls}-start", () => Justify == "start")
.If($"{prefixCls}-end", () => Justify == "end")
.If($"{prefixCls}-center", () => Justify == "center")
.If($"{prefixCls}-space-around", () => Justify == "space-around")
.If($"{prefixCls}-space-between", () => Justify == "space-between")
.If($"{prefixCls}-no-wrap", () => !Wrap)
.If($"{prefixCls}-rtl", () => RTL)
2020-03-15 23:54:48 +08:00
;
if (DefaultBreakpoint != null)
{
SetGutterStyle(DefaultBreakpoint.Name);
}
2020-03-15 23:54:48 +08:00
await base.OnInitializedAsync();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
var dimensions = await JsInvokeAsync<Window>(JSInteropConstants.GetWindow);
DomEventService.AddEventListener<Window>("window", "resize", OnResize, false);
OptimizeSize(dimensions.innerWidth);
}
await base.OnAfterRenderAsync(firstRender);
}
private async void OnResize(Window window)
{
OptimizeSize(window.innerWidth);
}
private void OptimizeSize(decimal windowWidth)
2020-03-15 23:54:48 +08:00
{
BreakpointType actualBreakpoint = _breakpoints[_breakpoints.Length - 1];
for (int i = 0; i < _breakpoints.Length; i++)
2020-03-15 23:54:48 +08:00
{
if (windowWidth <= _breakpoints[i].Width && (windowWidth >= (i > 0 ? _breakpoints[i - 1].Width : 0)))
2020-03-15 23:54:48 +08:00
{
actualBreakpoint = _breakpoints[i];
2020-03-15 23:54:48 +08:00
}
}
SetGutterStyle(actualBreakpoint.Name);
if (OnBreakpoint.HasDelegate)
{
OnBreakpoint.InvokeAsync(actualBreakpoint);
}
StateHasChanged();
}
private void SetGutterStyle(string breakPoint)
{
var gutter = this.GetGutter(breakPoint);
2020-03-15 23:54:48 +08:00
Cols.ForEach(x => x.RowGutterChanged(gutter));
GutterStyle = "";
2020-03-15 23:54:48 +08:00
if (gutter.horizontalGutter > 0)
{
GutterStyle = $"margin-left: -{gutter.horizontalGutter / 2}px; margin-right: -{gutter.horizontalGutter / 2}px; ";
2020-03-15 23:54:48 +08:00
}
GutterStyle += $"row-gap: {gutter.verticalGutter}px; ";
2020-03-15 23:54:48 +08:00
StateHasChanged();
2020-03-15 23:54:48 +08:00
}
private (int horizontalGutter, int verticalGutter) GetGutter(string breakPoint)
2020-03-15 23:54:48 +08:00
{
GutterType gutter = 0;
if (this.Gutter.Value != null)
gutter = this.Gutter;
2020-03-15 23:54:48 +08:00
return gutter.Match(
num => (num, 0),
dic => breakPoint != null && dic.ContainsKey(breakPoint) ? (dic[breakPoint], 0) : (0, 0),
tuple => tuple,
tupleDicInt => (tupleDicInt.Item1.ContainsKey(breakPoint) ? tupleDicInt.Item1[breakPoint] : 0, tupleDicInt.Item2),
tupleIntDic => (tupleIntDic.Item1, tupleIntDic.Item2.ContainsKey(breakPoint) ? tupleIntDic.Item2[breakPoint] : 0),
tupleDicDic => (tupleDicDic.Item1.ContainsKey(breakPoint) ? tupleDicDic.Item1[breakPoint] : 0, tupleDicDic.Item2.ContainsKey(breakPoint) ? tupleDicDic.Item2[breakPoint] : 0)
2020-03-15 23:54:48 +08:00
);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
DomEventService.RemoveEventListerner<Window>("window", "resize", OnResize);
}
2020-03-15 23:54:48 +08:00
}
public enum BreakpointEnum
{
xxl,
xl,
lg,
md,
sm,
xs
}
2021-04-08 22:32:48 +08:00
}