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:
parent
1addc3039b
commit
df431e40af
2 changed files with 34 additions and 15 deletions
|
|
@ -16,9 +16,31 @@ namespace Yavsc.Server.Helpers
|
||||||
/// Pass an empty controller (or "Home" with action "Index") to match the site root.
|
/// Pass an empty controller (or "Home" with action "Index") to match the site root.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static IHtmlContent ActivePage(this ViewContext viewContext, string controller, string action = null)
|
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;
|
var route = viewContext?.RouteData.Values;
|
||||||
if (route == null) return HtmlString.Empty;
|
if (route == null) return false;
|
||||||
|
|
||||||
var currentController = route["controller"] as string;
|
var currentController = route["controller"] as string;
|
||||||
var currentAction = route["action"] as string;
|
var currentAction = route["action"] as string;
|
||||||
|
|
@ -32,24 +54,18 @@ namespace Yavsc.Server.Helpers
|
||||||
|| (string.Equals(controller, "Home", cmp)
|
|| (string.Equals(controller, "Home", cmp)
|
||||||
&& (action == null || string.Equals(action, "Index", cmp)));
|
&& (action == null || string.Equals(action, "Index", cmp)));
|
||||||
|
|
||||||
bool isCurrent;
|
|
||||||
if (isHome)
|
if (isHome)
|
||||||
{
|
{
|
||||||
isCurrent = string.IsNullOrEmpty(currentController)
|
return string.IsNullOrEmpty(currentController)
|
||||||
|| string.Equals(currentController, "Home", cmp)
|
|| string.Equals(currentController, "Home", cmp)
|
||||||
&& (currentAction == null || string.Equals(currentAction, "Index", cmp));
|
&& (currentAction == null || string.Equals(currentAction, "Index", cmp));
|
||||||
}
|
}
|
||||||
else
|
return string.Equals(currentController, controller, cmp)
|
||||||
{
|
&& (action == null || string.Equals(currentAction, action, cmp));
|
||||||
isCurrent = string.Equals(currentController, controller, cmp)
|
|
||||||
&& (action == null || string.Equals(currentAction, action, cmp));
|
|
||||||
}
|
|
||||||
|
|
||||||
return isCurrent
|
|
||||||
? (IHtmlContent)new HtmlString("active\" aria-current=\"page")
|
|
||||||
: HtmlString.Empty;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
public static List<SelectListItem> CreateSelectListItems<T> (this IStringLocalizer<T> localisation, Type enumType, object selectedValue =null)
|
||||||
{
|
{
|
||||||
string selectedName = (selectedValue != null) ? enumType.GetEnumName(selectedValue) : null;
|
string selectedName = (selectedValue != null) ? enumType.GetEnumName(selectedValue) : null;
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,12 @@
|
||||||
@if (Context.User?.Identity?.IsAuthenticated ?? false)
|
@if (Context.User?.Identity?.IsAuthenticated ?? false)
|
||||||
{
|
{
|
||||||
string userName = User.GetUserName();
|
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">
|
<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">
|
<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, "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>
|
<li><a class="dropdown-item @PageHelpers.ActivePage(ViewContext, "HyperLink")" asp-controller="HyperLink" asp-action="Index">HyperLink</a></li>
|
||||||
|
|
@ -16,7 +19,7 @@
|
||||||
</li>
|
</li>
|
||||||
@if (User.IsInMsRole(YavscConstants.AdminGroupName)) {
|
@if (User.IsInMsRole(YavscConstants.AdminGroupName)) {
|
||||||
<li class="nav-item dropdown">
|
<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
|
Administration
|
||||||
</a>
|
</a>
|
||||||
<ul class="dropdown-menu" aria-labelledby="dropdown05">
|
<ul class="dropdown-menu" aria-labelledby="dropdown05">
|
||||||
|
|
@ -31,7 +34,7 @@
|
||||||
</ul>
|
</ul>
|
||||||
</li>}
|
</li>}
|
||||||
<li class="nav-item dropdown">
|
<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">
|
<ul class="dropdown-menu">
|
||||||
<li>
|
<li>
|
||||||
<a class="dropdown-item @PageHelpers.ActivePage(ViewContext, "Manage")" asp-controller="Manage" asp-action="Index" title="Manage">
|
<a class="dropdown-item @PageHelpers.ActivePage(ViewContext, "Manage")" asp-controller="Manage" asp-action="Index" title="Manage">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue