|
| 1 | +using Microsoft.AspNetCore.Razor.TagHelpers; |
| 2 | +using System.Text.Encodings.Web; |
| 3 | +using Umbraco.Cms.Core.Web; |
| 4 | +using Umbraco.Extensions; |
| 5 | +using Microsoft.AspNetCore.Mvc.TagHelpers; |
| 6 | +using System; |
| 7 | + |
| 8 | +namespace Our.Umbraco.TagHelpers |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// An attribute TagHelper that only works with <a> tags |
| 12 | + /// |
| 13 | + /// Applying the attribute our-is-active-page="navi-active" |
| 14 | + /// Will apply the CSS class name navi-active to the class attribute |
| 15 | + /// if the value in the href of the <a> compared to the page being rendered |
| 16 | + /// is the current page or part of an ancestor |
| 17 | + /// </summary>" |
| 18 | + [HtmlTargetElement("*", Attributes = tagHelperAttributes)] |
| 19 | + [HtmlTargetElement("a", Attributes = tagHelperAttributeName)] |
| 20 | + public class ActiveClassTagHelper : TagHelper |
| 21 | + { |
| 22 | + private const string tagHelperAttributeName = "our-active-class"; |
| 23 | + private const string tagHelperAttributeHrefName = "our-active-href"; |
| 24 | + private const string tagHelperAttributes = tagHelperAttributeName + ", " + tagHelperAttributeHrefName; |
| 25 | + |
| 26 | + private IUmbracoContextAccessor _umbracoContextAccessor; |
| 27 | + |
| 28 | + public ActiveClassTagHelper(IUmbracoContextAccessor umbracoContextAccessor) |
| 29 | + { |
| 30 | + _umbracoContextAccessor = umbracoContextAccessor; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// The CSS class name you wish to append to the class attribute |
| 35 | + /// on an <a> tag when the page is active/selected |
| 36 | + /// </summary> |
| 37 | + [HtmlAttributeName(tagHelperAttributeName)] |
| 38 | + public string ActiveClassName { get; set; } |
| 39 | + |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// If you wish to add an active CSS class on another DOM element than an <a> |
| 43 | + /// You can use this attribute to pass a link to the page to check in conjuction with `our-active-class` attribute |
| 44 | + /// </summary> |
| 45 | + [HtmlAttributeName("our-active-href")] |
| 46 | + public string? ActiveLink { get; set; } |
| 47 | + |
| 48 | + public override void Process(TagHelperContext context, TagHelperOutput output) |
| 49 | + { |
| 50 | + // Remove the attribute |
| 51 | + // We don't want it in the markup we send down to the page |
| 52 | + output.Attributes.RemoveAll(tagHelperAttributeName); |
| 53 | + |
| 54 | + var ctx = _umbracoContextAccessor.GetRequiredUmbracoContext(); |
| 55 | + |
| 56 | + // If we have active link prop set use that othewise try to find the href attribute on an <a> and its value |
| 57 | + var href = string.IsNullOrEmpty(ActiveLink) ? output.Attributes["href"]?.Value.ToString() : ActiveLink; |
| 58 | + if (string.IsNullOrEmpty(href)) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // Try & parse href as URI, as it could be relative or absolute |
| 64 | + // or contain a quersystring we only want the path part |
| 65 | + if (Uri.TryCreate(href, UriKind.Absolute, out Uri link) || Uri.TryCreate(ctx.PublishedRequest.Uri, href, out link)) |
| 66 | + { |
| 67 | + // Get the node based of the value in the HREF |
| 68 | + var nodeOfLink = ctx.Content.GetByRoute(link.AbsolutePath); |
| 69 | + if (nodeOfLink == null) |
| 70 | + { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // Get the current node of the page that is rendering |
| 75 | + var currentPageRendering = ctx.PublishedRequest.PublishedContent; |
| 76 | + |
| 77 | + // Check if thelink we are rendering is current page or an ancestor |
| 78 | + if (nodeOfLink.IsAncestorOrSelf(currentPageRendering)) |
| 79 | + { |
| 80 | + // Is active page |
| 81 | + output.AddClass(ActiveClassName, HtmlEncoder.Default); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments