From 21f538e54df3d8e3817cbb1107b4b26cad09cac7 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Mon, 4 Mar 2013 16:21:51 +0100 Subject: [PATCH] #19222: Fixing MenuWidgetPartDriver to correctly start and cut-off menu item hierarchies. Work Item: 19222 --HG-- branch : 1.x --- .../Drivers/MenuWidgetPartDriver.cs | 74 +++++++------------ 1 file changed, 27 insertions(+), 47 deletions(-) diff --git a/src/Orchard.Web/Core/Navigation/Drivers/MenuWidgetPartDriver.cs b/src/Orchard.Web/Core/Navigation/Drivers/MenuWidgetPartDriver.cs index a8b978ee8..2fcc80e4c 100644 --- a/src/Orchard.Web/Core/Navigation/Drivers/MenuWidgetPartDriver.cs +++ b/src/Orchard.Web/Core/Navigation/Drivers/MenuWidgetPartDriver.cs @@ -56,9 +56,7 @@ namespace Orchard.Core.Navigation.Drivers { var menuName = menu.As().Title.HtmlClassify(); var currentCulture = _workContextAccessor.GetContext().CurrentCulture; - - IEnumerable menuItems = _navigationManager.BuildMenu(menu); - + var menuItems = _navigationManager.BuildMenu(menu); var localized = new List(); foreach(var menuItem in menuItems) { // if there is no associated content, it as culture neutral @@ -75,10 +73,8 @@ namespace Orchard.Core.Navigation.Drivers { menuItems = localized; var routeData = _workContextAccessor.GetContext().HttpContext.Request.RequestContext.RouteData; - - var selectedPath = NavigationHelper.SetSelectedPath(menuItems, routeData); - - dynamic menuShape = shapeHelper.Menu(); + var selectedPath = NavigationHelper.SetSelectedPath(menuItems, routeData); + var menuShape = shapeHelper.Menu(); if (part.Breadcrumb) { menuItems = selectedPath ?? new Stack(); @@ -113,57 +109,41 @@ namespace Orchard.Core.Navigation.Drivers { } menuItems = result; - menuShape = shapeHelper.Breadcrumb(); } else { - IEnumerable topLevelItems = menuItems.ToList(); + var topLevelItems = menuItems.ToList(); - if (part.StartLevel > 1) { + // apply start level by pushing children as top level items. When the start level is + // greater than 1 (ie. below the top level), only menu items along the selected path + // will be displayed. + for (var i = 0; topLevelItems.Any() && i < part.StartLevel - 1; i++) { + var temp = new List(); if (selectedPath != null) { - // the selected path will return the whole selected hierarchy - // intersecting will return the root selected menu item topLevelItems = topLevelItems.Intersect(selectedPath.Where(x => x.Selected)).ToList(); - } - else { - topLevelItems = new List(); - menuItems = topLevelItems; - } - } - - if (topLevelItems.Any()) { - // apply start level by pushing childrens as top level items - int i = 0; - for (; i < part.StartLevel - 1; i++) { - var temp = new List(); foreach (var menuItem in topLevelItems) { temp.AddRange(menuItem.Items); } - topLevelItems = temp; } - - // apply display level ? - if(part.Levels > 0) { - var current = topLevelItems.ToList(); - for (int j=1; j < part.Levels; j++ ) { - var temp = new List(); - foreach (var menuItem in current) { - temp.AddRange(menuItem.Items); - } - current = temp; - } - - topLevelItems = current; - - // cut the sub-levels of any selected menu item - foreach (var menuItem in topLevelItems) { - menuItem.Items = Enumerable.Empty(); - } - - } - - menuItems = topLevelItems; + topLevelItems = temp; } + + // limit the number of levels to display (down from and including the start level) + if(part.Levels > 0) { + var current = topLevelItems.ToList(); + for (var i = 1; current.Any() && i < part.Levels; i++ ) { + var temp = new List(); + foreach (var menuItem in current) { + temp.AddRange(menuItem.Items); + } + current = temp; + } + // cut the sub-levels beneath any menu items that are at the lowest level being displayed + foreach (var menuItem in current) { + menuItem.Items = Enumerable.Empty(); + } + } + menuItems = topLevelItems; } menuShape.MenuName(menuName);