61 lines
1.9 KiB
Plaintext
61 lines
1.9 KiB
Plaintext
@using Microsoft.AspNetCore.Components.Routing
|
|
@using GPW.CORE.Services
|
|
@inject NavigationManager Nav
|
|
@inject RouteModeService RouteModeSvc
|
|
|
|
<a href="@Href" class="nav-link @CssClass" @onclick="OnClick" @onclick:preventDefault="true">
|
|
@ChildContent
|
|
</a>
|
|
|
|
@code {
|
|
[Parameter] public string Href { get; set; } = string.Empty;
|
|
[Parameter] public RenderFragment? ChildContent { get; set; }
|
|
[Parameter] public NavLinkMatch Match { get; set; } = NavLinkMatch.Prefix;
|
|
[Parameter] public bool ServerOnly { get; set; } = false;
|
|
[Parameter] public bool ClientOnly { get; set; } = false;
|
|
|
|
// Utility per active class
|
|
private string CssClass
|
|
{
|
|
get
|
|
{
|
|
var baseClass = ""; // puoi passare classi esterne concatenandole se vuoi
|
|
if (IsActive()) baseClass = string.IsNullOrEmpty(baseClass) ? "active" : $"{baseClass} active";
|
|
return baseClass;
|
|
}
|
|
}
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
var mode = ServerOnly ? RouteMode.ServerOnly : (ClientOnly ? RouteMode.ClientOnly : RouteMode.Hybrid);
|
|
RouteModeSvc.SetMode(Href, mode);
|
|
}
|
|
|
|
|
|
private bool IsActive()
|
|
{
|
|
var absoluteHref = Nav.ToAbsoluteUri(Href).AbsoluteUri;
|
|
var current = Nav.Uri;
|
|
if (Match == NavLinkMatch.All)
|
|
return string.Equals(current.TrimEnd('/'), absoluteHref.TrimEnd('/'), StringComparison.OrdinalIgnoreCase);
|
|
return current.StartsWith(absoluteHref, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private void OnClick()
|
|
{
|
|
if (ServerOnly && OperatingSystem.IsBrowser())
|
|
{
|
|
Nav.NavigateTo(Href, forceLoad: true);
|
|
return;
|
|
}
|
|
if (ClientOnly && !OperatingSystem.IsBrowser())
|
|
{
|
|
Nav.NavigateTo(Href, forceLoad: true);
|
|
return;
|
|
}
|
|
|
|
// Navigazione client-side normale
|
|
Nav.NavigateTo(Href);
|
|
}
|
|
}
|