2018-06-24 00:21:15 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2026-06-20 19:48:20 +01:00
|
|
|
|
using Microsoft.AspNetCore.Html;
|
2023-03-19 17:57:55 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
2026-06-20 19:48:20 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
2019-09-27 06:59:20 +01:00
|
|
|
|
using Microsoft.Extensions.Localization;
|
2018-06-24 00:21:15 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Yavsc.Server.Helpers
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class PageHelpers
|
|
|
|
|
|
{
|
2026-06-20 19:48:20 +01:00
|
|
|
|
/// <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 <a class="nav-link @..."> 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)
|
2026-06-20 20:17:36 +01:00
|
|
|
|
{
|
|
|
|
|
|
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)
|
2026-06-20 19:48:20 +01:00
|
|
|
|
{
|
|
|
|
|
|
var route = viewContext?.RouteData.Values;
|
2026-06-20 20:17:36 +01:00
|
|
|
|
if (route == null) return false;
|
2026-06-20 19:48:20 +01:00
|
|
|
|
|
|
|
|
|
|
var currentController = route["controller"] as string;
|
|
|
|
|
|
var currentAction = route["action"] as string;
|
|
|
|
|
|
|
|
|
|
|
|
// Normalize: empty or "Home" + ("Index" or null) both target the site root.
|
2026-06-20 19:58:58 +01:00
|
|
|
|
// 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;
|
2026-06-20 19:48:20 +01:00
|
|
|
|
bool isHome = string.IsNullOrEmpty(controller)
|
2026-06-20 19:58:58 +01:00
|
|
|
|
|| (string.Equals(controller, "Home", cmp)
|
|
|
|
|
|
&& (action == null || string.Equals(action, "Index", cmp)));
|
2026-06-20 19:48:20 +01:00
|
|
|
|
|
|
|
|
|
|
if (isHome)
|
|
|
|
|
|
{
|
2026-06-20 20:17:36 +01:00
|
|
|
|
return string.IsNullOrEmpty(currentController)
|
2026-06-20 19:58:58 +01:00
|
|
|
|
|| string.Equals(currentController, "Home", cmp)
|
|
|
|
|
|
&& (currentAction == null || string.Equals(currentAction, "Index", cmp));
|
2026-06-20 19:48:20 +01:00
|
|
|
|
}
|
2026-06-20 20:17:36 +01:00
|
|
|
|
return string.Equals(currentController, controller, cmp)
|
|
|
|
|
|
&& (action == null || string.Equals(currentAction, action, cmp));
|
2026-06-20 19:48:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-20 20:17:36 +01:00
|
|
|
|
private static IHtmlContent ActiveMarker() => new HtmlString("active\" aria-current=\"page");
|
|
|
|
|
|
|
2019-09-27 06:59:20 +01:00
|
|
|
|
public static List<SelectListItem> CreateSelectListItems<T> (this IStringLocalizer<T> localisation, Type enumType, object selectedValue =null)
|
2018-06-24 00:21:15 +02:00
|
|
|
|
{
|
|
|
|
|
|
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() {
|
2019-09-27 06:59:20 +01:00
|
|
|
|
Value = values.GetValue(index).ToString(), Text = localisation[itemName], Selected = ( itemName == selectedName)
|
2018-06-24 00:21:15 +02:00
|
|
|
|
}) ;
|
|
|
|
|
|
}
|
|
|
|
|
|
var list = new SelectList(items);
|
|
|
|
|
|
return items;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-01 16:28:47 +00:00
|
|
|
|
public static List<SelectListItem> AddNull(this List<SelectListItem> selectList, string displayNull, object selectedValue = null)
|
2018-12-27 23:53:27 +00:00
|
|
|
|
{
|
2019-01-01 16:28:47 +00:00
|
|
|
|
selectList.Add(new SelectListItem { Text = displayNull, Value = "", Selected = selectedValue == null });
|
2018-12-27 23:53:27 +00:00
|
|
|
|
return selectList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-25 15:18:37 +02:00
|
|
|
|
public static List<SelectListItem> CreateSelectListItems<T> (this IEnumerable<T>data,
|
2018-06-24 00:21:15 +02:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2018-12-27 23:53:27 +00:00
|
|
|
|
}
|