yavsc/src/Yavsc.Org/Helpers/PageHelpers.cs
Paul Schneider df431e40af 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.
2026-06-20 20:17:36 +01:00

111 lines
4.8 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Localization;
namespace Yavsc.Server.Helpers
{
public static class PageHelpers
{
/// <summary>
/// Returns "active" aria-current="page" when the current route matches the given
/// controller/action, otherwise an empty string. Use as an attribute value
/// inside a &lt;a class="nav-link @..."&gt; element.
/// 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 false;
var currentController = route["controller"] as string;
var currentAction = route["action"] as string;
// Normalize: empty or "Home" + ("Index" or null) both target the site root.
// Compare case-insensitively because asp-controller tag helpers resolve
// controller names case-insensitively, while RouteData["controller"] holds
// the actual class name (e.g. "BlogSpot" vs an URL segment "Blogspot").
var cmp = StringComparison.OrdinalIgnoreCase;
bool isHome = string.IsNullOrEmpty(controller)
|| (string.Equals(controller, "Home", cmp)
&& (action == null || string.Equals(action, "Index", cmp)));
if (isHome)
{
return string.IsNullOrEmpty(currentController)
|| string.Equals(currentController, "Home", cmp)
&& (currentAction == null || string.Equals(currentAction, "Index", cmp));
}
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;
var items = new List<SelectListItem> ();
var names = enumType.GetEnumNames();
var values = enumType.GetEnumValues();
for (int index = 0; index < names.Length; index++)
{
var itemName = names[index];
items.Add(new SelectListItem() {
Value = values.GetValue(index).ToString(), Text = localisation[itemName], Selected = ( itemName == selectedName)
}) ;
}
var list = new SelectList(items);
return items;
}
public static List<SelectListItem> AddNull(this List<SelectListItem> selectList, string displayNull, object selectedValue = null)
{
selectList.Add(new SelectListItem { Text = displayNull, Value = "", Selected = selectedValue == null });
return selectList;
}
public static List<SelectListItem> CreateSelectListItems<T> (this IEnumerable<T>data,
Func<T,string> dataField,
Func<T,string> displayField = null, object selectedValue =null) where T : class
{
if (displayField == null) displayField = dataField;
var items = new List<SelectListItem> ();
foreach (var dataItem in data)
{
var itemVal = dataField(dataItem);
var itemName = displayField(dataItem);
items.Add(new SelectListItem() {
Value = itemVal, Text = itemName, Selected = ( selectedValue?.Equals(itemVal) ?? false )
}) ;
}
return items;
}
}
}