mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Refactoring some Menu concerns
Simplify navigation admin controller for inserting a new menu item Removed null top-level main menu item Avoid use of content part in view model for post Enable HasText to recognize LocalizedString Flatten TextHint comparison for deduplicating menu items Remove L1 Crop from NavigationManager --HG-- branch : dev
This commit is contained in:
@@ -85,23 +85,26 @@ namespace Orchard.Core.Navigation.Controllers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Create(CreateMenuItemViewModel model) {
|
public ActionResult Create(NavigationManagementViewModel model) {
|
||||||
if (!_services.Authorizer.Authorize(Permissions.ManageMainMenu, T("Couldn't manage the main menu")))
|
if (!_services.Authorizer.Authorize(Permissions.ManageMainMenu, T("Couldn't manage the main menu")))
|
||||||
return new HttpUnauthorizedResult();
|
return new HttpUnauthorizedResult();
|
||||||
|
|
||||||
var menuPart = _services.ContentManager.New<MenuPart>("MenuItem");
|
var menuPart = _services.ContentManager.New<MenuPart>("MenuItem");
|
||||||
model.MenuItem = _services.ContentManager.UpdateEditor(menuPart, this);
|
menuPart.OnMainMenu = true;
|
||||||
|
menuPart.MenuText = model.NewMenuItem.Text;
|
||||||
|
menuPart.MenuPosition = model.NewMenuItem.Position;
|
||||||
|
if (string.IsNullOrEmpty(menuPart.MenuPosition))
|
||||||
|
menuPart.MenuPosition = Position.GetNext(_navigationManager.BuildMenu("main"));
|
||||||
|
|
||||||
|
var menuItem = menuPart.As<MenuItemPart>();
|
||||||
|
menuItem.Url = model.NewMenuItem.Url;
|
||||||
|
|
||||||
if (!ModelState.IsValid) {
|
if (!ModelState.IsValid) {
|
||||||
_services.TransactionManager.Cancel();
|
_services.TransactionManager.Cancel();
|
||||||
return Index(new NavigationManagementViewModel { NewMenuItem = model });
|
return View("Index", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(menuPart.MenuPosition))
|
_services.ContentManager.Create(menuPart);
|
||||||
menuPart.MenuPosition = Position.GetNext(_navigationManager.BuildMenu("main"));
|
|
||||||
menuPart.OnMainMenu = true;
|
|
||||||
|
|
||||||
_services.ContentManager.Create(model.MenuItem);
|
|
||||||
|
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,18 +19,13 @@ namespace Orchard.Core.Navigation.Services {
|
|||||||
public void GetNavigation(NavigationBuilder builder) {
|
public void GetNavigation(NavigationBuilder builder) {
|
||||||
var menuParts = _contentManager.Query<MenuPart, MenuPartRecord>().Where(x => x.OnMainMenu).List();
|
var menuParts = _contentManager.Query<MenuPart, MenuPartRecord>().Where(x => x.OnMainMenu).List();
|
||||||
foreach (var menuPart in menuParts) {
|
foreach (var menuPart in menuParts) {
|
||||||
if (menuPart != null ) {
|
if (menuPart != null) {
|
||||||
var part = menuPart;
|
var part = menuPart;
|
||||||
|
|
||||||
if (part.Is<MenuItemPart>())
|
if (part.Is<MenuItemPart>())
|
||||||
builder.Add(
|
builder.Add(new LocalizedString(HttpUtility.HtmlEncode(part.MenuText)), part.MenuPosition, item => item.Url(part.As<MenuItemPart>().Url));
|
||||||
menu => menu.Add(new LocalizedString(HttpUtility.HtmlEncode(part.MenuText)), part.MenuPosition, nib => nib.Url(part.As<MenuItemPart>().Url)));
|
|
||||||
else
|
else
|
||||||
builder.Add(
|
builder.Add(new LocalizedString(HttpUtility.HtmlEncode(part.MenuText)), part.MenuPosition, item => item.Action(_contentManager.GetItemMetadata(part.ContentItem).DisplayRouteValues));
|
||||||
menu =>
|
|
||||||
menu.Add(new LocalizedString(HttpUtility.HtmlEncode(part.MenuText)), part.MenuPosition,
|
|
||||||
nib =>
|
|
||||||
nib.Action(_contentManager.GetItemMetadata(part.ContentItem).DisplayRouteValues)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ using Orchard.ContentManagement;
|
|||||||
|
|
||||||
namespace Orchard.Core.Navigation.ViewModels {
|
namespace Orchard.Core.Navigation.ViewModels {
|
||||||
public class CreateMenuItemViewModel {
|
public class CreateMenuItemViewModel {
|
||||||
public IContent MenuItem { get; set; }
|
public MenuItemEntry MenuItem { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ namespace Orchard.Core.Navigation.ViewModels {
|
|||||||
MenuItemEntries = Enumerable.Empty<MenuItemEntry>().ToList();
|
MenuItemEntries = Enumerable.Empty<MenuItemEntry>().ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CreateMenuItemViewModel NewMenuItem { get; set; }
|
public MenuItemEntry NewMenuItem { get; set; }
|
||||||
public IList<MenuItemEntry> MenuItemEntries { get; set; }
|
public IList<MenuItemEntry> MenuItemEntries { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,15 +52,15 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<label for="MenuText">@T("Text")</label>
|
<label for="MenuText">@T("Text")</label>
|
||||||
@Html.EditorFor(nmvm => nmvm.NewMenuItem.MenuItem.As<MenuPart>().MenuText)
|
@Html.EditorFor(m => m.NewMenuItem.Text)
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<label for="MenuPosition">@T("Position")</label>
|
<label for="MenuPosition">@T("Position")</label>
|
||||||
@Html.EditorFor(nmvm => nmvm.NewMenuItem.MenuItem.As<MenuPart>().MenuPosition)
|
@Html.EditorFor(m => m.NewMenuItem.Position)
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<label for="Url">@T("Url")</label>
|
<label for="Url">@T("Url")</label>
|
||||||
@Html.EditorFor(nmvm => nmvm.NewMenuItem.MenuItem.As<MenuItemPart>().Url)
|
@Html.EditorFor(m => m.NewMenuItem.Url)
|
||||||
</td>
|
</td>
|
||||||
<td><button class="add" type="submit">@T("Add")</button></td>
|
<td><button class="add" type="submit">@T("Add")</button></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ namespace Orchard.Mvc.ViewEngines.Razor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public bool HasText(object thing) {
|
public bool HasText(object thing) {
|
||||||
return !string.IsNullOrWhiteSpace(thing as string);
|
return !string.IsNullOrWhiteSpace(Convert.ToString(thing));
|
||||||
}
|
}
|
||||||
|
|
||||||
public OrchardTagBuilder Tag(dynamic shape, string tagName) {
|
public OrchardTagBuilder Tag(dynamic shape, string tagName) {
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ namespace Orchard.Mvc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public bool HasText(object thing) {
|
public bool HasText(object thing) {
|
||||||
return !string.IsNullOrWhiteSpace(thing as string);
|
return !string.IsNullOrWhiteSpace(Convert.ToString(thing));
|
||||||
}
|
}
|
||||||
|
|
||||||
public OrchardTagBuilder Tag(dynamic shape, string tagName) {
|
public OrchardTagBuilder Tag(dynamic shape, string tagName) {
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ namespace Orchard.Mvc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public bool HasText(object thing) {
|
public bool HasText(object thing) {
|
||||||
return !string.IsNullOrWhiteSpace(thing as string);
|
return !string.IsNullOrWhiteSpace(Convert.ToString(thing));
|
||||||
}
|
}
|
||||||
|
|
||||||
public OrchardTagBuilder Tag(dynamic shape, string tagName) {
|
public OrchardTagBuilder Tag(dynamic shape, string tagName) {
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ using System.Linq;
|
|||||||
namespace Orchard.UI.Navigation {
|
namespace Orchard.UI.Navigation {
|
||||||
public class MenuItemComparer : IEqualityComparer<MenuItem> {
|
public class MenuItemComparer : IEqualityComparer<MenuItem> {
|
||||||
public bool Equals(MenuItem x, MenuItem y) {
|
public bool Equals(MenuItem x, MenuItem y) {
|
||||||
if (x.Text != null && y.Text != null) {
|
var xTextHint = x.Text == null ? null : x.Text.TextHint;
|
||||||
if (!string.Equals(x.Text.TextHint, y.Text.TextHint)) {
|
var yTextHint = y.Text == null ? null : y.Text.TextHint;
|
||||||
return false;
|
if (!string.Equals(xTextHint, yTextHint)) {
|
||||||
}
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(x.Url) && !string.IsNullOrWhiteSpace(y.Url)) {
|
if (!string.IsNullOrWhiteSpace(x.Url) && !string.IsNullOrWhiteSpace(y.Url)) {
|
||||||
|
|||||||
@@ -21,7 +21,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(Crop(Reduce(Merge(sources))).ToArray());
|
return FinishMenu(Reduce(Merge(sources)).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<string> BuildImageSets(string menuName) {
|
public IEnumerable<string> BuildImageSets(string menuName) {
|
||||||
@@ -57,9 +57,6 @@ namespace Orchard.UI.Navigation {
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<MenuItem> Crop(IEnumerable<MenuItem> items) {
|
|
||||||
return items.Where(item => item.Items.Any() || item.RouteValues != null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<MenuItem> Reduce(IEnumerable<MenuItem> items) {
|
private IEnumerable<MenuItem> Reduce(IEnumerable<MenuItem> items) {
|
||||||
var hasDebugShowAllMenuItems = _authorizationService.TryCheckAccess(Permission.Named("DebugShowAllMenuItems"), _orchardServices.WorkContext.CurrentUser, null);
|
var hasDebugShowAllMenuItems = _authorizationService.TryCheckAccess(Permission.Named("DebugShowAllMenuItems"), _orchardServices.WorkContext.CurrentUser, null);
|
||||||
|
|||||||
@@ -7,19 +7,17 @@ using Orchard.UI.Navigation;
|
|||||||
namespace Orchard.Utility {
|
namespace Orchard.Utility {
|
||||||
public static class Position {
|
public static class Position {
|
||||||
public static string GetNext(IEnumerable<MenuItem> menuItems) {
|
public static string GetNext(IEnumerable<MenuItem> menuItems) {
|
||||||
var topMenuItem = menuItems.FirstOrDefault();
|
|
||||||
|
|
||||||
if (topMenuItem != null) {
|
var maxMenuItem = menuItems.Where(PositionHasMojorNumber).OrderByDescending(mi => mi.Position, new FlatPositionComparer()).FirstOrDefault();
|
||||||
var maxMenuItem = topMenuItem.Items.Where(PositionHasMojorNumber).OrderByDescending(mi => mi.Position, new FlatPositionComparer()).FirstOrDefault();
|
var positionParts = maxMenuItem.Position.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Where(s => s.Trim().Length > 0);
|
||||||
var positionParts = maxMenuItem.Position.Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries).Where(s => s.Trim().Length > 0);
|
if (positionParts.Count() > 0) {
|
||||||
if (positionParts.Count() > 0) {
|
int result;
|
||||||
int result;
|
if (int.TryParse(positionParts.ElementAt(0), out result)) {
|
||||||
if (int.TryParse(positionParts.ElementAt(0), out result)) {
|
return (result + 1).ToString();
|
||||||
return (result + 1).ToString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return "1";
|
return "1";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user