nav: highlight active dropdown toggle in _LoginPartial

Add ActivePageAny(ViewContext, IEnumerable<string>) so a dropdown
toggle gets the active class + aria-current="page" whenever any of
its children is the current route. Apply it to the Plateforme,
Administration, and account menu toggles.
This commit is contained in:
Paul Schneider 2026-06-20 20:17:36 +01:00
commit df431e40af
2 changed files with 34 additions and 15 deletions

View file

@ -16,9 +16,31 @@ namespace Yavsc.Server.Helpers
/// Pass an empty controller (or "Home" with action "Index") to match the site root.
/// </summary>
public static IHtmlContent ActivePage(this ViewContext viewContext, string controller, string action = null)
{
return MatchesCurrent(viewContext, controller, action)
? ActiveMarker()
: HtmlString.Empty;
}
/// <summary>
/// Returns "active" aria-current="page" when the current route targets any of
/// the given controllers. Useful for highlighting a dropdown toggle whose
/// children span several controllers.
/// </summary>
public static IHtmlContent ActivePageAny(this ViewContext viewContext, IEnumerable<string> controllers)
{
if (controllers == null) return HtmlString.Empty;
foreach (var c in controllers)
{
if (MatchesCurrent(viewContext, c, null)) return ActiveMarker();
}
return HtmlString.Empty;
}
private static bool MatchesCurrent(ViewContext viewContext, string controller, string action)
{
var route = viewContext?.RouteData.Values;
if (route == null) return HtmlString.Empty;
if (route == null) return false;
var currentController = route["controller"] as string;
var currentAction = route["action"] as string;
@ -32,24 +54,18 @@ namespace Yavsc.Server.Helpers
|| (string.Equals(controller, "Home", cmp)
&& (action == null || string.Equals(action, "Index", cmp)));
bool isCurrent;
if (isHome)
{
isCurrent = string.IsNullOrEmpty(currentController)
return string.IsNullOrEmpty(currentController)
|| string.Equals(currentController, "Home", cmp)
&& (currentAction == null || string.Equals(currentAction, "Index", cmp));
}
else
{
isCurrent = string.Equals(currentController, controller, cmp)
&& (action == null || string.Equals(currentAction, action, cmp));
}
return isCurrent
? (IHtmlContent)new HtmlString("active\" aria-current=\"page")
: HtmlString.Empty;
return string.Equals(currentController, controller, cmp)
&& (action == null || string.Equals(currentAction, action, cmp));
}
private static IHtmlContent ActiveMarker() => new HtmlString("active\" aria-current=\"page");
public static List<SelectListItem> CreateSelectListItems<T> (this IStringLocalizer<T> localisation, Type enumType, object selectedValue =null)
{
string selectedName = (selectedValue != null) ? enumType.GetEnumName(selectedValue) : null;

View file

@ -5,9 +5,12 @@
@if (Context.User?.Identity?.IsAuthenticated ?? false)
{
string userName = User.GetUserName();
var plateformeControllers = new[] { "Bug", "HyperLink", "Feature" };
var administrationControllers = new[] { "Administration", "Announces", "Activity", "CommandForms", "Notifications", "SIRENExceptions", "Client", "MailingTemplate" };
var accountMenuControllers = new[] { "Manage", "Grants", "Device", "Diagnostics", "Account" };
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="dropdown04" data-bs-toggle="dropdown" aria-expanded="false">Plateforme</a>
<a class="nav-link dropdown-toggle @PageHelpers.ActivePageAny(ViewContext, plateformeControllers)" href="#" id="dropdown04" data-bs-toggle="dropdown" aria-expanded="false">Plateforme</a>
<ul class="dropdown-menu" aria-labelledby="dropdown04">
<li><a class="dropdown-item @PageHelpers.ActivePage(ViewContext, "Bug")" asp-controller="Bug" asp-action="Index">Bugs</a></li>
<li><a class="dropdown-item @PageHelpers.ActivePage(ViewContext, "HyperLink")" asp-controller="HyperLink" asp-action="Index">HyperLink</a></li>
@ -16,7 +19,7 @@
</li>
@if (User.IsInMsRole(YavscConstants.AdminGroupName)) {
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="dropdown05" data-bs-toggle="dropdown" aria-expanded="false">
<a class="nav-link dropdown-toggle @PageHelpers.ActivePageAny(ViewContext, administrationControllers)" href="#" id="dropdown05" data-bs-toggle="dropdown" aria-expanded="false">
Administration
</a>
<ul class="dropdown-menu" aria-labelledby="dropdown05">
@ -31,7 +34,7 @@
</ul>
</li>}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="dropdown04" data-bs-toggle="dropdown" aria-expanded="false">Hello @userName!</a>
<a class="nav-link dropdown-toggle @PageHelpers.ActivePageAny(ViewContext, accountMenuControllers)" href="#" id="dropdown04" data-bs-toggle="dropdown" aria-expanded="false">Hello @userName!</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item @PageHelpers.ActivePage(ViewContext, "Manage")" asp-controller="Manage" asp-action="Index" title="Manage">