|
| 1 | +using Microsoft.AspNetCore.Razor.TagHelpers; |
| 2 | +using Umbraco.Cms.Core.Web; |
| 3 | +using Umbraco.Extensions; |
| 4 | + |
| 5 | +namespace Our.Umbraco.TagHelpers |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// An attribute TagHelper that only works with <a> tags |
| 9 | + /// |
| 10 | + /// Applying the attribute our-is-active-page="navi-active" |
| 11 | + /// Will apply the CSS class name navi-active to the class attribute |
| 12 | + /// if the value in the href of the <a> compared to the page being rendered |
| 13 | + /// is the current page or part of an ancestor |
| 14 | + /// </summary> |
| 15 | + [HtmlTargetElement("a", Attributes = "our-is-active-page")] |
| 16 | + public class IsActivePageTagHelper : TagHelper |
| 17 | + { |
| 18 | + private IUmbracoContextAccessor _umbracoContextAccessor; |
| 19 | + |
| 20 | + public IsActivePageTagHelper(IUmbracoContextAccessor umbracoContextAccessor) |
| 21 | + { |
| 22 | + _umbracoContextAccessor = umbracoContextAccessor; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The CSS class name you wish to append to the class attribute |
| 27 | + /// on an <a> tag when the page is active/selected |
| 28 | + /// </summary> |
| 29 | + [HtmlAttributeName("our-is-active-page")] |
| 30 | + public string ActiveClassName { get; set; } |
| 31 | + |
| 32 | + public override void Process(TagHelperContext context, TagHelperOutput output) |
| 33 | + { |
| 34 | + // Remove the attribute |
| 35 | + // We don't want it in the markup we send down to the page |
| 36 | + if(output.Attributes.TryGetAttribute("our-is-active-page", out TagHelperAttribute tagHelperAttr)) |
| 37 | + { |
| 38 | + output.Attributes.Remove(tagHelperAttr); |
| 39 | + } |
| 40 | + |
| 41 | + var ctx = _umbracoContextAccessor.GetRequiredUmbracoContext(); |
| 42 | + |
| 43 | + // On the <a> try to find the href attribute and its value |
| 44 | + var href = output.Attributes["href"]?.Value.ToString(); |
| 45 | + if (string.IsNullOrEmpty(href)) |
| 46 | + { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Clean href values of any querystrings |
| 51 | + // As we won't be able to query GetByRoute for an Umbraco node with them |
| 52 | + href = href.Substring(0, href.IndexOf("?") > 0 ? href.IndexOf("?") : href.Length); |
| 53 | + |
| 54 | + // Get the node based of the value in the HREF |
| 55 | + var nodeOfLink = ctx.Content.GetByRoute(href); |
| 56 | + if(nodeOfLink == null) |
| 57 | + { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // Get the current node of the page that is rendering |
| 62 | + var currentPageRendering = ctx.PublishedRequest.PublishedContent; |
| 63 | + |
| 64 | + // Check if thelink we are rendering is current page or an ancestor |
| 65 | + if(nodeOfLink.IsAncestorOrSelf(currentPageRendering)) |
| 66 | + { |
| 67 | + // Is active page |
| 68 | + // Check if the <a> has a class attribute already |
| 69 | + if(output.Attributes.TryGetAttribute("class", out TagHelperAttribute classAttribute)) |
| 70 | + { |
| 71 | + var existingClasses = classAttribute.Value.ToString(); |
| 72 | + output.Attributes.SetAttribute("class", existingClasses + " nav-link--active"); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + // No class attribute exists so add a new one |
| 77 | + output.Attributes.SetAttribute("class", "nav-link--active"); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments