Skip to content

Commit

Permalink
Add boolean to get or set sidebar(2) collapsed state
Browse files Browse the repository at this point in the history
  • Loading branch information
Mischa Spelt committed Aug 5, 2024
1 parent 75ab692 commit 0267598
Show file tree
Hide file tree
Showing 2 changed files with 383 additions and 349 deletions.
365 changes: 191 additions & 174 deletions blazorbootstrap/Components/Sidebar/Sidebar.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,207 +4,224 @@ namespace BlazorBootstrap;

public partial class Sidebar : BlazorBootstrapComponentBase
{
#region Fields and Constants
#region Fields and Constants

private bool collapseNavMenu = true;
private bool collapseNavMenu = true;

private bool collapseSidebar = false;
private bool collapseSidebar = false;

private bool isMobile = false;
private bool isMobile = false;

private IEnumerable<NavItem>? items = null;
private IEnumerable<NavItem>? items = null;

private DotNetObjectReference<Sidebar> objRef = default!;
private DotNetObjectReference<Sidebar> objRef = default!;

private bool requestInProgress = false;
private bool requestInProgress = false;

#endregion
#endregion

#region Methods
#region Methods

protected override async Task OnAfterRenderAsync(bool firstRender)
protected override async Task OnAfterRenderAsync( bool firstRender )
{
if( firstRender )
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("window.blazorBootstrap.sidebar.initialize", Id, objRef);
await JSRuntime.InvokeVoidAsync( "window.blazorBootstrap.sidebar.initialize", Id, objRef );

var width = await JSRuntime.InvokeAsync<int>("window.blazorBootstrap.sidebar.windowSize");
var width = await JSRuntime.InvokeAsync<int>( "window.blazorBootstrap.sidebar.windowSize" );

bsWindowResize(width);
bsWindowResize( width );

await RefreshDataAsync(firstRender);
}

await base.OnAfterRenderAsync(firstRender);
}

protected override async Task OnInitializedAsync()
{
objRef ??= DotNetObjectReference.Create(this);

AdditionalAttributes ??= new Dictionary<string, object>();

await base.OnInitializedAsync();
await RefreshDataAsync( firstRender );
}

[JSInvokable]
public void bsWindowResize(int width)
{
if (width < 641) // mobile
isMobile = true;
else
isMobile = false;
}
await base.OnAfterRenderAsync( firstRender );
}

/// <summary>
/// Refresh the sidebar data.
/// </summary>
/// <returns>Task</returns>
public async Task RefreshDataAsync(bool firstRender = false)
{
if (requestInProgress)
return;
protected override async Task OnInitializedAsync()
{
objRef ??= DotNetObjectReference.Create( this );

requestInProgress = true;
AdditionalAttributes ??= new Dictionary<string, object>();

if (DataProvider != null)
{
var request = new SidebarDataProviderRequest();
var result = await DataProvider.Invoke(request);
items = result != null ? result.Data : new List<NavItem>();
}
await base.OnInitializedAsync();
}

requestInProgress = false;
[JSInvokable]
public void bsWindowResize( int width )
{
if( width < 641 ) // mobile
isMobile = true;
else
isMobile = false;
}

await InvokeAsync(StateHasChanged);
}
/// <summary>
/// Refresh the sidebar data.
/// </summary>
/// <returns>Task</returns>
public async Task RefreshDataAsync( bool firstRender = false )
{
if( requestInProgress )
return;

/// <summary>
/// Toggles sidebar.
/// </summary>
public void ToggleSidebar()
{
collapseSidebar = !collapseSidebar;
StateHasChanged();
}
requestInProgress = true;

internal void HideNavMenuOnMobile()
if( DataProvider != null )
{
if (isMobile && !collapseNavMenu)
collapseNavMenu = true;
var request = new SidebarDataProviderRequest();
var result = await DataProvider.Invoke( request );
items = result != null ? result.Data : new List<NavItem>();
}

private string GetNavMenuCssClass()
{
var classList = new HashSet<string>();

if (collapseNavMenu)
classList.Add("collapse");
requestInProgress = false;

classList.Add("bb-sidebar-content nav-scrollable bb-scrollbar");
await InvokeAsync( StateHasChanged );
}

if (collapseSidebar)
classList.Add("bb-scrollbar-hidden");
/// <summary>
/// Toggles sidebar.
/// </summary>
public void ToggleSidebar()
{
ToggleTo( !collapseSidebar );
}

return string.Join(" ", classList);
private void ToggleTo( bool isCollapsed )
{
if( collapseSidebar != isCollapsed )
{
collapseSidebar = isCollapsed;
StateHasChanged();
}

private void ToggleNavMenu() => collapseNavMenu = !collapseNavMenu;

#endregion

#region Properties, Indexers

protected override string? ClassNames =>
BuildClassNames(Class,
("bb-sidebar", true),
("collapsed", collapseSidebar),
("expanded", !collapseSidebar));

protected override string? StyleNames =>
BuildStyleNames(Style,
($"--bb-sidebar-width: {Width.ToString(CultureInfo.InvariantCulture)}{WidthUnit.ToCssString()};", Width > 0));

/// <summary>
/// Gets or sets the badge text.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string? BadgeText { get; set; }

/// <summary>
/// Gets or sets the custom icon name.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string? CustomIconName { get; set; }

/// <summary>
/// Gets or sets the data provider.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
[EditorRequired]
public SidebarDataProviderDelegate? DataProvider { get; set; } = default!;

/// <summary>
/// Gets or sets the Href.
/// </summary>
/// <remarks>
/// Default value is <see cref="string.Empty" />.
/// </remarks>
[Parameter]
public string? Href { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the IconName.
/// </summary>
/// <remarks>
/// Default value is <see cref="IconName.None" />.
/// </remarks>
[Parameter]
public IconName IconName { get; set; } = IconName.None;

/// <summary>
/// Gets or sets the sidebar logo.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string? ImageSrc { get; set; }

private string? navMenuCssClass => GetNavMenuCssClass();

/// <summary>
/// Gets or sets the sidebar title.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
[EditorRequired]
public string? Title { get; set; } = default!;

/// <summary>
/// Gets or sets the sidebar width.
/// </summary>
/// <remarks>Default value is 270.</remarks>
[Parameter]
public float Width { get; set; } = 270;

/// <summary>
/// Gets or sets the sidebar width unit.
/// </summary>
/// <remarks>Default value is <see cref="Unit.Px" />.</remarks>
[Parameter]
public Unit WidthUnit { get; set; } = Unit.Px;

#endregion
}

internal void HideNavMenuOnMobile()
{
if( isMobile && !collapseNavMenu )
collapseNavMenu = true;
}

private string GetNavMenuCssClass()
{
var classList = new HashSet<string>();

if( collapseNavMenu )
classList.Add( "collapse" );

classList.Add( "bb-sidebar-content nav-scrollable bb-scrollbar" );

if( collapseSidebar )
classList.Add( "bb-scrollbar-hidden" );

return string.Join( " ", classList );
}

private void ToggleNavMenu() => collapseNavMenu = !collapseNavMenu;

#endregion

#region Properties, Indexers

protected override string? ClassNames =>
BuildClassNames( Class,
("bb-sidebar", true),
("collapsed", collapseSidebar),
("expanded", !collapseSidebar) );

protected override string? StyleNames =>
BuildStyleNames( Style,
($"--bb-sidebar-width: {Width.ToString( CultureInfo.InvariantCulture )}{WidthUnit.ToCssString()};", Width > 0) );

/// <summary>
/// Gets or sets the badge text.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string? BadgeText { get; set; }

/// <summary>
/// Gets or sets the custom icon name.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string? CustomIconName { get; set; }

/// <summary>
/// Gets or sets the data provider.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
[EditorRequired]
public SidebarDataProviderDelegate? DataProvider { get; set; } = default!;

/// <summary>
/// Gets or sets the Href.
/// </summary>
/// <remarks>
/// Default value is <see cref="string.Empty" />.
/// </remarks>
[Parameter]
public string? Href { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the IconName.
/// </summary>
/// <remarks>
/// Default value is <see cref="IconName.None" />.
/// </remarks>
[Parameter]
public IconName IconName { get; set; } = IconName.None;

/// <summary>
/// Gets or sets the sidebar logo.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
public string? ImageSrc { get; set; }

/// <summary>
/// Gets or sets the current collapsed state of the sidebar.
/// </summary>
public bool IsCollapsed
{
get => collapseSidebar;
set => ToggleTo( value );
}

private string? navMenuCssClass => GetNavMenuCssClass();

/// <summary>
/// Gets or sets the sidebar title.
/// </summary>
/// <remarks>
/// Default value is null.
/// </remarks>
[Parameter]
[EditorRequired]
public string? Title { get; set; } = default!;

/// <summary>
/// Gets or sets the sidebar width.
/// </summary>
/// <remarks>Default value is 270.</remarks>
[Parameter]
public float Width { get; set; } = 270;

/// <summary>
/// Gets or sets the sidebar width unit.
/// </summary>
/// <remarks>Default value is <see cref="Unit.Px" />.</remarks>
[Parameter]
public Unit WidthUnit { get; set; } = Unit.Px;

#endregion
}
Loading

0 comments on commit 0267598

Please sign in to comment.