blob: 759b701c37916874948d280b6b85f4c1bc2c6a2f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
namespace marlin.web.TagHelpers;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
[HtmlTargetElement("a", Attributes = "active-when")]
public class ActiveLinkTagHelper : TagHelper
{
[HtmlAttributeName("asp-controller")]
public string Controller { get; set; }
[HtmlAttributeName("asp-action")]
public string Action { get; set; }
[HtmlAttributeName("active-when")]
public ActiveLinkWhen ActiveWhen { get; set; }
[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var currentController = ViewContext.RouteData.Values["controller"]?.ToString();
var currentAction = ViewContext.RouteData.Values["action"]?.ToString();
if (!string.Equals(currentController, Controller, StringComparison.OrdinalIgnoreCase))
return;
if (!string.Equals(currentAction, Action, StringComparison.OrdinalIgnoreCase)
&& this.ActiveWhen == ActiveLinkWhen.ControllerAndAction)
return;
var existingClasses = output.Attributes["class"]?.Value?.ToString() ?? "";
output.Attributes.SetAttribute("class", $"{existingClasses} active".Trim());
}
}
public enum ActiveLinkWhen
{
Controller,
ControllerAndAction
}
|