diff --git a/TestSiteV10/Views/Partials/Navigation/TopNavigation.cshtml b/TestSiteV10/Views/Partials/Navigation/TopNavigation.cshtml index a3fe709..c240bf7 100644 --- a/TestSiteV10/Views/Partials/Navigation/TopNavigation.cshtml +++ b/TestSiteV10/Views/Partials/Navigation/TopNavigation.cshtml @@ -1,4 +1,5 @@ @using UmbNav.Core.Models +@using UmbNav.Core.Extensions @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage @{ var site = Model.Root(); @@ -12,5 +13,6 @@ }*@ @foreach (var item in umbNav) { - + @**@ + @item.Name } \ No newline at end of file diff --git a/src/UmbNav.Core/Extensions/UmbNavItemExtensions.cs b/src/UmbNav.Core/Extensions/UmbNavItemExtensions.cs index cdbf2df..ea625b4 100644 --- a/src/UmbNav.Core/Extensions/UmbNavItemExtensions.cs +++ b/src/UmbNav.Core/Extensions/UmbNavItemExtensions.cs @@ -121,5 +121,23 @@ public static string Url(this UmbNavItem item, string culture = null, UrlMode mo return item.Url; } + + public static bool IsActive(this UmbNavItem item, IPublishedContent currentPage, bool checkAncestors = false) + { + if (item.Key == currentPage.Key) + { + return true; + } + + if (checkAncestors) + { + if (item.Content.IsAncestorOrSelf(currentPage) && item.Content != currentPage.Root()) + { + return true; + } + } + + return false; + } } } \ No newline at end of file diff --git a/src/UmbNav.Core/Models/UmbNavItem.cs b/src/UmbNav.Core/Models/UmbNavItem.cs index 203456f..ab0aec0 100644 --- a/src/UmbNav.Core/Models/UmbNavItem.cs +++ b/src/UmbNav.Core/Models/UmbNavItem.cs @@ -75,6 +75,7 @@ public class UmbNavItem [JsonProperty("itemType")] internal string MenuItemType { get; set; } = "link"; [JsonIgnore] + [Obsolete("If you need to check ancestors, use the IsActive() extension instead")] public bool IsActive { get; set; } [JsonProperty("displayAsLabel")] diff --git a/src/UmbNav.Core/TagHelpers/UmbnavItemTagHelper.cs b/src/UmbNav.Core/TagHelpers/UmbnavItemTagHelper.cs index c9a1b3f..141d2e9 100644 --- a/src/UmbNav.Core/TagHelpers/UmbnavItemTagHelper.cs +++ b/src/UmbNav.Core/TagHelpers/UmbnavItemTagHelper.cs @@ -16,6 +16,8 @@ public class UmbnavitemTagHelper : TagHelper public string Culture { get; set; } public string LabelTagName { get; set; } = "span"; public string ActiveClass { get; set; } + public bool IsActiveAncestorCheck { get; set; } = false; + public IPublishedContent CurrentPage { get; set; } = null; private bool IsLabel { get { return MenuItem.ItemType == UmbNavItemType.Label; } } public override void Process(TagHelperContext context, TagHelperOutput output) @@ -33,7 +35,14 @@ public override void Process(TagHelperContext context, TagHelperOutput output) output.AddClass(MenuItem.CustomClasses, HtmlEncoder.Default); } - if (!string.IsNullOrEmpty(ActiveClass) && MenuItem.IsActive) + if (!string.IsNullOrEmpty(ActiveClass) && CurrentPage != null && IsActiveAncestorCheck) + { + if (MenuItem.IsActive(CurrentPage, true)) + { + output.AddClass(ActiveClass, HtmlEncoder.Default); + } + } + else if (!string.IsNullOrEmpty(ActiveClass) && MenuItem.IsActive) { output.AddClass(ActiveClass, HtmlEncoder.Default); } diff --git a/src/UmbNav.Core/UmbNav.Core.csproj b/src/UmbNav.Core/UmbNav.Core.csproj index 287bf6b..6a3f9cb 100644 --- a/src/UmbNav.Core/UmbNav.Core.csproj +++ b/src/UmbNav.Core/UmbNav.Core.csproj @@ -19,10 +19,10 @@ 2023 UmbHost Limited MIT true - 2.0.2 + 2.0.3 $(AssemblyVersion) README.md - Fixes member visiblity + Added IsActive extension with ancestor check diff --git a/src/UmbNav.Web/UmbNav.Web.csproj b/src/UmbNav.Web/UmbNav.Web.csproj index 3e49dac..d0a2035 100644 --- a/src/UmbNav.Web/UmbNav.Web.csproj +++ b/src/UmbNav.Web/UmbNav.Web.csproj @@ -18,10 +18,10 @@ 2023 UmbHost Limited MIT true - 2.0.7 + 2.0.8 $(AssemblyVersion) README.md - Fixes member visiblity + Added IsActive extension with ancestor check