props/Props/TagHelpers/NavLinkTagHelper.cs
Harrison Deng 38ffb3c7e1 Added logging to module framework
Implemented logging to Adafruit and changed database loading behavior.
2021-08-07 17:20:46 -05:00

44 lines
1.5 KiB
C#

using System;
using System.Linq;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Props.TagHelpers
{
public class NavLinkTagHelper : AnchorTagHelper
{
private IUrlHelperFactory urlHelperFactory;
private IUrlHelper urlHelper;
public NavLinkTagHelper(IHtmlGenerator generator, IUrlHelperFactory urlHelperFactory) : base(generator)
{
this.urlHelperFactory = urlHelperFactory;
}
public override void Init(TagHelperContext context)
{
this.urlHelper = urlHelperFactory.GetUrlHelper(ViewContext);
base.Init(context);
}
public string ActiveClass { get; set; } = "active";
public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);
TagHelperAttribute dest;
if (output.Attributes.TryGetAttribute("href", out dest) && urlHelper.RouteUrl(ViewContext.RouteData.Values).Equals(dest.Value))
{
output.AddClass(ActiveClass, HtmlEncoder.Default);
output.Attributes.Add("aria-current", "page");
}
output.TagName = "a";
}
}
}