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)
|
|
|
|
|
|
{
|
|
|
|
|
|
var route = viewContext?.RouteData.Values;
|
|
|
|
|
|
if (route == null) return HtmlString.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
var currentController = route["controller"] as string;
|
|
|
|
|
|
var currentAction = route["action"] as string;
|
|
|
|
|
|
|
|
|
|
|
|
// Normalize: empty or "Home" + ("Index" or null) both target the site root.
|
|
|
|
|
|
bool isHome = string.IsNullOrEmpty(controller)
|
|
|
|
|
|
|| (string.Equals(controller, "Home", StringComparison.Ordinal)
|
|
|
|
|
|
&& (action == null || string.Equals(action, "Index", StringComparison.Ordinal)));
|
|
|
|
|
|
|
|
|
|
|
|
bool isCurrent;
|
|
|
|
|
|
if (isHome)
|
|
|
|
|
|
{
|
|
|
|
|
|
isCurrent = string.IsNullOrEmpty(currentController)
|
|
|
|
|
|
|| string.Equals(currentController, "Home", StringComparison.Ordinal)
|
|
|
|
|
|
&& (currentAction == null || string.Equals(currentAction, "Index", StringComparison.Ordinal));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
isCurrent = string.Equals(currentController, controller, StringComparison.Ordinal)
|
|
|
|
|
|
&& (action == null || string.Equals(currentAction, action, StringComparison.Ordinal));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return isCurrent
|
|
|
|
|
|
? (IHtmlContent)new HtmlString("active\" aria-current=\"page")
|
|
|
|
|
|
: HtmlString.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
}
|