2020-05-10 15:42:02 +08:00
|
|
|
|
using System;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-05-29 00:33:49 +08:00
|
|
|
|
using AntDesign.JsInterop;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-06-03 23:11:02 +08:00
|
|
|
|
public partial class Sider
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-06-03 23:11:02 +08:00
|
|
|
|
private static string _prefixCls = "ant-layout-sider";
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
[Parameter] public bool Collapsible { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
[Parameter] public RenderFragment ChildContent { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-08-18 18:05:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// When Trigger is null, `OnCollapse` won't be invoked after `Collapsed` was changed.
|
|
|
|
|
/// </summary>
|
2020-06-03 23:11:02 +08:00
|
|
|
|
[Parameter] public RenderFragment Trigger { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-06-04 14:48:13 +08:00
|
|
|
|
[Parameter] public bool NoTrigger { get; set; }
|
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
[CascadingParameter] public Layout Parent { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
[Parameter] public BreakpointType Breakpoint { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
[Parameter] public SiderTheme Theme { get; set; } = SiderTheme.Dark;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
[Parameter] public int Width { get; set; } = 200;
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
[Parameter] public int CollapsedWidth { get; set; } = 80;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Collapsed
|
|
|
|
|
{
|
2020-08-18 18:05:44 +08:00
|
|
|
|
get => _isCollapsed;
|
2020-06-03 23:11:02 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2020-08-18 18:05:44 +08:00
|
|
|
|
if (_isCollapsed == value)
|
2020-06-03 23:11:02 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2020-08-18 18:05:44 +08:00
|
|
|
|
_isCollapsed = value;
|
|
|
|
|
OnCollapsed?.Invoke(_isCollapsed);
|
2020-06-03 23:11:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-05-22 00:05:26 +08:00
|
|
|
|
public EventCallback<bool> OnCollapse { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<bool> OnBreakpoint { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
[Inject] public DomEventService DomEventService { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-08-18 18:05:44 +08:00
|
|
|
|
private int ComputedWidth => _isCollapsed ? CollapsedWidth : Width;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// param1: collapsed or not
|
|
|
|
|
/// param2: call inside sider or not
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event Action<bool> OnCollapsed;
|
|
|
|
|
|
|
|
|
|
private bool _isCollapsed;
|
|
|
|
|
|
|
|
|
|
private string SiderStyles =>
|
|
|
|
|
$"flex: 0 0 {ComputedWidth}px;" +
|
|
|
|
|
$"max-width: {ComputedWidth}px;" +
|
|
|
|
|
$"min-width: {ComputedWidth}px;" +
|
|
|
|
|
$"width: {ComputedWidth}px;" +
|
|
|
|
|
(_isCollapsed ? "overflow:initial;" : "");
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-08-18 18:05:44 +08:00
|
|
|
|
private RenderFragment DefaultTrigger => builder =>
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-06-07 19:41:00 +08:00
|
|
|
|
builder.OpenComponent<Icon>(1);
|
2020-08-18 18:05:44 +08:00
|
|
|
|
builder.AddAttribute(2, "Type", _isCollapsed ? "right" : "left");
|
2020-06-03 23:11:02 +08:00
|
|
|
|
builder.AddAttribute(3, "Theme", "outline");
|
|
|
|
|
builder.CloseComponent();
|
2020-04-23 17:13:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-22 00:05:26 +08:00
|
|
|
|
private void SetClass()
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-06-03 23:11:02 +08:00
|
|
|
|
ClassMapper.Clear()
|
|
|
|
|
.Add(_prefixCls)
|
2020-08-12 14:59:56 +08:00
|
|
|
|
.If($"{_prefixCls}-dark", () => Theme == SiderTheme.Dark)
|
|
|
|
|
.If($"{_prefixCls}-light", () => Theme == SiderTheme.Light)
|
2020-06-03 23:11:02 +08:00
|
|
|
|
.If($"{_prefixCls}-has-trigger", () => Collapsible)
|
2020-08-18 18:05:44 +08:00
|
|
|
|
.If($"{_prefixCls}-collapsed", () => _isCollapsed)
|
|
|
|
|
.If($"{_prefixCls}-zero-width", () => CollapsedWidth == 0 && _isCollapsed);
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
protected override void OnInitialized()
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-06-03 23:11:02 +08:00
|
|
|
|
base.OnInitialized();
|
|
|
|
|
Parent?.HasSider();
|
2020-08-18 18:05:44 +08:00
|
|
|
|
|
2020-06-04 14:48:13 +08:00
|
|
|
|
if (Trigger == null && !NoTrigger)
|
2020-06-03 23:11:02 +08:00
|
|
|
|
{
|
2020-08-18 18:05:44 +08:00
|
|
|
|
Trigger = DefaultTrigger;
|
2020-06-03 23:11:02 +08:00
|
|
|
|
}
|
2020-07-15 13:26:40 +08:00
|
|
|
|
|
2020-08-18 18:05:44 +08:00
|
|
|
|
SetClass();
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-06-03 23:11:02 +08:00
|
|
|
|
await base.OnAfterRenderAsync(firstRender);
|
2020-08-18 18:05:44 +08:00
|
|
|
|
if (firstRender && Breakpoint != null)
|
2020-06-03 23:11:02 +08:00
|
|
|
|
{
|
|
|
|
|
var dimensions = await JsInvokeAsync<Window>(JSInteropConstants.getWindow);
|
2020-08-26 11:13:36 +08:00
|
|
|
|
DomEventService.AddEventListener<Window>("window", "resize", OnResize, false);
|
2020-06-03 23:11:02 +08:00
|
|
|
|
OptimizeSize(dimensions.innerWidth);
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 11:13:36 +08:00
|
|
|
|
private void OnResize(Window window)
|
|
|
|
|
{
|
|
|
|
|
OptimizeSize(window.innerWidth);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
private void ToggleCollapsed()
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-08-18 18:05:44 +08:00
|
|
|
|
_isCollapsed = !_isCollapsed;
|
|
|
|
|
OnCollapsed?.Invoke(_isCollapsed);
|
|
|
|
|
|
|
|
|
|
if (OnCollapse.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
OnCollapse.InvokeAsync(_isCollapsed);
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 23:11:02 +08:00
|
|
|
|
private void OptimizeSize(decimal windowWidth)
|
2020-04-23 17:13:56 +08:00
|
|
|
|
{
|
2020-06-03 23:11:02 +08:00
|
|
|
|
if (windowWidth < Breakpoint?.Width)
|
|
|
|
|
{
|
2020-08-18 18:05:44 +08:00
|
|
|
|
_isCollapsed = true;
|
2020-06-03 23:11:02 +08:00
|
|
|
|
OnBreakpoint.InvokeAsync(true);
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-08-18 18:05:44 +08:00
|
|
|
|
_isCollapsed = false;
|
|
|
|
|
|
|
|
|
|
OnCollapsed?.Invoke(_isCollapsed);
|
|
|
|
|
if (OnCollapse.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
OnCollapse.InvokeAsync(_isCollapsed);
|
|
|
|
|
}
|
2020-06-03 23:11:02 +08:00
|
|
|
|
|
|
|
|
|
StateHasChanged();
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
2020-08-26 11:13:36 +08:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
|
|
|
|
|
DomEventService.RemoveEventListerner<Window>("window", "resize", OnResize);
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|
2020-07-15 13:26:40 +08:00
|
|
|
|
}
|