mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-12-03 12:03:51 +08:00
Generating hierarchies based on the position property
--HG-- branch : 1.x
This commit is contained in:
@@ -26,7 +26,7 @@ namespace Orchard.UI.Navigation {
|
|||||||
|
|
||||||
public IEnumerable<MenuItem> BuildMenu(string menuName) {
|
public IEnumerable<MenuItem> BuildMenu(string menuName) {
|
||||||
var sources = GetSources(menuName);
|
var sources = GetSources(menuName);
|
||||||
return FinishMenu(Reduce(Merge(sources)).ToArray());
|
return FinishMenu(Reduce(Arrange(Merge(sources))).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<string> BuildImageSets(string menuName) {
|
public IEnumerable<string> BuildImageSets(string menuName) {
|
||||||
@@ -139,6 +139,34 @@ namespace Orchard.UI.Navigation {
|
|||||||
.SelectMany(positionGroup => positionGroup.OrderBy(item => item.Text == null ? "" : item.Text.TextHint));
|
.SelectMany(positionGroup => positionGroup.OrderBy(item => item.Text == null ? "" : item.Text.TextHint));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static IEnumerable<MenuItem> Arrange(IEnumerable<MenuItem> items) {
|
||||||
|
var index = new Dictionary<string, MenuItem>();
|
||||||
|
var result = new List<MenuItem>();
|
||||||
|
|
||||||
|
foreach (var item in items) {
|
||||||
|
MenuItem parent = null;
|
||||||
|
var position = item.Position;
|
||||||
|
|
||||||
|
while (parent == null && !String.IsNullOrEmpty(position)) {
|
||||||
|
if (index.TryGetValue(position, out parent)) {
|
||||||
|
parent.Items = parent.Items.Concat(new [] { item });
|
||||||
|
}
|
||||||
|
|
||||||
|
position = position.Substring(0, position.Length - 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
index.Add(item.Position, item);
|
||||||
|
|
||||||
|
// if the current element has no parent, it's a top level item
|
||||||
|
if (parent == null) {
|
||||||
|
result.Add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
static MenuItem Join(IEnumerable<MenuItem> items) {
|
static MenuItem Join(IEnumerable<MenuItem> items) {
|
||||||
if (items.Count() < 2)
|
if (items.Count() < 2)
|
||||||
return items.Single();
|
return items.Single();
|
||||||
|
|||||||
Reference in New Issue
Block a user