Bjornmamman / Our.Umbraco.SimpleTreeMenu

3 stars 6 forks source link

ISimpleTreeItem.Level is always equal to 1 #56

Closed hfloyd closed 4 months ago

hfloyd commented 7 months ago

I noticed that if calling Item.Level, the value returned is always "1", where I would expect it to match the actual level of the item in relation to the whole Tree.

A test example:

Here is a menu with a "DisplayTitle" property shown (manually added by myself to each menu item)

image

Notice that the tagged level information is shown correctly here.

The raw data stored for this property:

{
    "items": [
        {
            "id": 1,
            "key": "101aac22-3656-4c4a-affe-0d0e89a8d73c",
            "contentTypeAlias": "ElementMenuItem",
            "name": "Item 1",
            "level": 0,
            "selected": false,
            "properties": {
                "DisplayTitle": "Item 1",
                "Link": [],
                "ManualMegaMenu": ""
            },
            "items": [
                {
                    "id": 2,
                    "key": "a6a2e577-5f24-4b74-9035-9708ead58d8e",
                    "contentTypeAlias": "ElementMenuItem",
                    "name": "Child 1.1",
                    "level": 1,
                    "selected": false,
                    "properties": {
                        "DisplayTitle": "Child 1.1",
                        "Link": [],
                        "ManualMegaMenu": ""
                    }
                },
                {
                    "id": 3,
                    "key": "fdaa594e-1bb8-44f7-8c9f-17bc3c843b3c",
                    "contentTypeAlias": "ElementMenuItem",
                    "name": "Child 1.2",
                    "level": 1,
                    "selected": false,
                    "properties": {
                        "DisplayTitle": "Child 1.2",
                        "Link": [],
                        "ManualMegaMenu": null
                    }
                },
                {
                    "id": 4,
                    "key": "f7992427-71ee-467e-b97d-4cfd2b80f19f",
                    "contentTypeAlias": "ElementMenuItem",
                    "name": "Child 1.3",
                    "level": 1,
                    "selected": false,
                    "properties": {
                        "DisplayTitle": "Child 1.3",
                        "Link": [],
                        "ManualMegaMenu": ""
                    }
                }
            ]
        },
        {
            "id": 5,
            "key": "f4c32979-d69f-4fc4-89de-2b0ade5e96e6",
            "contentTypeAlias": "ElementMenuItem",
            "name": "Item 2",
            "level": 0,
            "selected": false,
            "properties": {
                "DisplayTitle": "Item 2",
                "Link": [],
                "ManualMegaMenu": ""
            },
            "items": [
                {
                    "id": 6,
                    "key": "b2b49166-e12e-44b4-bec9-b0e4d24cc77d",
                    "contentTypeAlias": "ElementMenuItem",
                    "name": "Child 2.1",
                    "level": 1,
                    "selected": false,
                    "properties": {
                        "DisplayTitle": "Child 2.1",
                        "Link": [],
                        "ManualMegaMenu": ""
                    },
                    "items": [
                        {
                            "id": 8,
                            "key": "ec85793e-a400-4d44-994b-401834b976e2",
                            "contentTypeAlias": "ElementMenuItem",
                            "name": "Grandchild 2.1.1",
                            "level": 2,
                            "selected": false,
                            "properties": {
                                "DisplayTitle": "Grandchild 2.1.1",
                                "Link": [],
                                "ManualMegaMenu": ""
                            }
                        },
                        {
                            "id": 9,
                            "key": "2257a4af-3cd2-413e-ac74-6c02cf64f5e0",
                            "contentTypeAlias": "ElementMenuItem",
                            "name": "Grandchild 2.1.2",
                            "level": 2,
                            "selected": false,
                            "properties": {
                                "DisplayTitle": "Grandchild 2.1.2",
                                "Link": [],
                                "ManualMegaMenu": ""
                            }
                        },
                        {
                            "id": 10,
                            "key": "91b8a9f8-8294-4502-92e0-10a78725d5ef",
                            "contentTypeAlias": "ElementMenuItem",
                            "name": "Grandchild 2.1.3",
                            "level": 2,
                            "selected": false,
                            "properties": {
                                "DisplayTitle": "Grandchild 2.1.3",
                                "Link": [],
                                "ManualMegaMenu": ""
                            }
                        }
                    ]
                },
                {
                    "id": 7,
                    "key": "cbff6942-a06a-4f1c-a1f6-95166acc28e5",
                    "contentTypeAlias": "ElementMenuItem",
                    "name": "Child 2.2",
                    "level": 1,
                    "selected": false,
                    "properties": {
                        "DisplayTitle": "Child 2.2",
                        "Link": [],
                        "ManualMegaMenu": ""
                    }
                }
            ]
        }
    ]
}

The "level" is recorded properly here.

I am using ModelsBuilder to return strongly-typed models:

    [PublishedModel("NavMenu")]
    public partial class NavMenu : PublishedContentModel {

        #region Helpers

        public new const string ModelTypeAlias = "NavMenu";

        public new const PublishedItemType ModelItemType = PublishedItemType.Content;

        [return: global::System.Diagnostics.CodeAnalysis.MaybeNull]
        public new static IPublishedContentType GetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor)
            => PublishedModelUtility.GetModelContentType(publishedSnapshotAccessor, ModelItemType, ModelTypeAlias);

        [return: global::System.Diagnostics.CodeAnalysis.MaybeNull]
        public static IPublishedPropertyType GetModelPropertyType<TValue>(IPublishedSnapshotAccessor publishedSnapshotAccessor, Expression<Func<NavMenu, TValue>> selector)
            => PublishedModelUtility.GetModelPropertyType(GetModelContentType(publishedSnapshotAccessor), selector);

        #endregion

        #region Constructors

        public NavMenu(IPublishedContent content, IPublishedValueFallback publishedValueFallback) : base(content, publishedValueFallback) { }

        #endregion

        #region Properties

        [global::System.Diagnostics.CodeAnalysis.MaybeNull]
        [ImplementPropertyType("Items")]
        public new global::System.Collections.Generic.IEnumerable<global::Our.Umbraco.SimpleTreeMenu.SimpleTreeItem> Items
            => this.Value<global::System.Collections.Generic.IEnumerable<global::Our.Umbraco.SimpleTreeMenu.SimpleTreeItem>>("Items");

        #endregion

    }

   [PublishedModel("ElementMenuItem")]
   public partial class ElementMenuItem : PublishedElementModel {

       #region Helpers

       public new const string ModelTypeAlias = "ElementMenuItem";

       public new const PublishedItemType ModelItemType = PublishedItemType.Element;

       [return: global::System.Diagnostics.CodeAnalysis.MaybeNull]
       public new static IPublishedContentType GetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor)
           => PublishedModelUtility.GetModelContentType(publishedSnapshotAccessor, ModelItemType, ModelTypeAlias);

       [return: global::System.Diagnostics.CodeAnalysis.MaybeNull]
       public static IPublishedPropertyType GetModelPropertyType<TValue>(IPublishedSnapshotAccessor publishedSnapshotAccessor, Expression<Func<ElementMenuItem, TValue>> selector)
           => PublishedModelUtility.GetModelPropertyType(GetModelContentType(publishedSnapshotAccessor), selector);

       #endregion

       #region Constructors

       public ElementMenuItem(IPublishedElement content, IPublishedValueFallback publishedValueFallback) : base(content, publishedValueFallback) { }

       #endregion

       #region Properties

       [global::System.Diagnostics.CodeAnalysis.MaybeNull]
       [ImplementPropertyType("DisplayTitle")]
       public new string DisplayTitle
           => this.Value<string>("DisplayTitle");

       [global::System.Diagnostics.CodeAnalysis.MaybeNull]
       [ImplementPropertyType("Link")]
       public new global::Umbraco.Cms.Core.Models.Link Link
           => this.Value<global::Umbraco.Cms.Core.Models.Link>("Link");

       #endregion

   }

In a front-end View file, I created some code to render out the Menu values:

...

<div class="navigation">
    @if (mainMenu != null && mainMenu.Items.Any())
    {
        TestMenuItems(mainMenu);
    }
</div>

@functions {
    public void TestMenuItems(NavMenu Menu)
    {
        <!-- MENU: @Menu.Name -->
        foreach (var item in Menu.Items)
        {
            var menuItem = new ElementMenuItem(item.Item, null);
            <!-- @menuItem.DisplayTitle [Level: @item.Level] -->
            TestLoopChildren(item);
        }
    }

    public void TestLoopChildren(ISimpleTreeItem Item)
    {
        foreach (var child in Item.Children)
        {
            var menuItem = new ElementMenuItem(child.Item, null);
            <!-- @menuItem.DisplayTitle [Level: @child.Level] -->
            TestLoopChildren(child);
        }
    }
}

And this is what is rendered: image

Notice that "Level" is ALWAYS 1.

hfloyd commented 7 months ago

It looks like this issue is that the converter never sets the Level value:

https://github.com/Bjornmamman/Our.Umbraco.SimpleTreeMenu/blob/396c365e5439dcdc2e7b81f6f8acf3101c4be3c1/src/TreeMenu/ValueConverters/SimpleTreeMenuConverter.cs#L92