You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Components/Components/Drawer/DrawerContainer.razor.cs

109 lines
3.1 KiB

2 years ago
using Connected.Annotations;
using Connected.Extensions;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
namespace Connected.Components;
public partial class DrawerContainer : UIComponent
{
protected bool Fixed { get; set; } = false;
private List<Drawer> _drawers = new();
protected virtual string Classname =>
new CssBuilder()
.AddClass(GetDrawerClass(FindLeftDrawer()))
.AddClass(GetDrawerClass(FindRightDrawer()))
.AddClass(Class)
.Build();
protected string Stylename =>
new StyleBuilder()
.AddStyle("--mud-drawer-width-left", GetDrawerWidth(FindLeftDrawer()), !string.IsNullOrEmpty(GetDrawerWidth(FindLeftDrawer())))
.AddStyle("--mud-drawer-width-right", GetDrawerWidth(FindRightDrawer()), !string.IsNullOrEmpty(GetDrawerWidth(FindRightDrawer())))
.AddStyle("--mud-drawer-width-mini-left", GetMiniDrawerWidth(FindLeftMiniDrawer()), !string.IsNullOrEmpty(GetMiniDrawerWidth(FindLeftMiniDrawer())))
.AddStyle("--mud-drawer-width-mini-right", GetMiniDrawerWidth(FindRightMiniDrawer()), !string.IsNullOrEmpty(GetMiniDrawerWidth(FindRightMiniDrawer())))
.AddStyle(Style)
.Build();
[CascadingParameter(Name = "RightToLeft")] public bool RightToLeft { get; set; }
[Parameter]
[Category(CategoryTypes.Drawer.Behavior)]
public RenderFragment ChildContent { get; set; }
internal void FireDrawersChanged() => StateHasChanged();
internal void Add(Drawer drawer)
{
if (Fixed && !drawer.Fixed)
return;
_drawers.Add(drawer);
StateHasChanged();
}
internal void Remove(Drawer drawer)
{
_drawers.Remove(drawer);
StateHasChanged();
}
private string GetDrawerClass(Drawer drawer)
{
if (drawer == null)
return string.Empty;
var className = $"mud-drawer-{(drawer.Open ? "open" : "close")}-{drawer.Variant.ToDescriptionString()}";
if (drawer.Variant is DrawerVariant.Responsive or DrawerVariant.Mini)
{
className += $"-{drawer.Breakpoint.ToDescriptionString()}";
}
className += $"-{drawer.GetPosition()}";
className += $" mud-drawer-{drawer.GetPosition()}-clipped-{drawer.ClipMode.ToDescriptionString()}";
return className;
}
private string GetDrawerWidth(Drawer drawer)
{
if (drawer == null)
return string.Empty;
return drawer.Width;
}
private string GetMiniDrawerWidth(Drawer drawer)
{
if (drawer == null)
return string.Empty;
return drawer.MiniWidth;
}
private Drawer FindLeftDrawer()
{
var anchor = RightToLeft ? Anchor.End : Anchor.Start;
return _drawers.FirstOrDefault(d => d.Anchor == anchor || d.Anchor == Anchor.Left);
}
private Drawer FindRightDrawer()
{
var anchor = RightToLeft ? Anchor.Start : Anchor.End;
return _drawers.FirstOrDefault(d => d.Anchor == anchor || d.Anchor == Anchor.Right);
}
private Drawer FindLeftMiniDrawer()
{
var anchor = RightToLeft ? Anchor.End : Anchor.Start;
return _drawers.FirstOrDefault(d => d.Variant == DrawerVariant.Mini && (d.Anchor == anchor || d.Anchor == Anchor.Left));
}
private Drawer FindRightMiniDrawer()
{
var anchor = RightToLeft ? Anchor.Start : Anchor.End;
return _drawers.FirstOrDefault(d => d.Variant == DrawerVariant.Mini && (d.Anchor == anchor || d.Anchor == Anchor.Right));
}
}