mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding the ability to add a menu item under another item directly, #4009
This commit is contained in:
@@ -34,7 +34,6 @@ namespace Orchard.Core.Navigation.Controllers {
|
||||
_menuService = menuService;
|
||||
_menuManager = menuManager;
|
||||
_navigationManager = navigationManager;
|
||||
|
||||
Services = orchardServices;
|
||||
T = NullLocalizer.Instance;
|
||||
Logger = NullLogger.Instance;
|
||||
@@ -155,7 +154,7 @@ namespace Orchard.Core.Navigation.Controllers {
|
||||
ModelState.AddModelError(key, errorMessage.ToString());
|
||||
}
|
||||
|
||||
public ActionResult CreateMenuItem(string id, int menuId, string returnUrl) {
|
||||
public ActionResult CreateMenuItem(string id, int menuId, string returnUrl, string parentMenuItemPosition = null) {
|
||||
if (!Services.Authorizer.Authorize(Permissions.ManageMenus, _menuService.GetMenu(menuId), T("Couldn't manage the main menu")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
@@ -164,25 +163,31 @@ namespace Orchard.Core.Navigation.Controllers {
|
||||
|
||||
if (menuPart == null)
|
||||
return HttpNotFound();
|
||||
|
||||
|
||||
// load the menu
|
||||
var menu = Services.ContentManager.Get(menuId);
|
||||
|
||||
if (menu == null)
|
||||
return HttpNotFound();
|
||||
|
||||
try {
|
||||
// filter the content items for this specific menu
|
||||
menuPart.MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu));
|
||||
|
||||
|
||||
try {
|
||||
if (!String.IsNullOrEmpty(parentMenuItemPosition)) {
|
||||
var menuEntries = _menuService.GetMenuParts(menuId).Select(CreateMenuItemEntries);
|
||||
menuPart.MenuPosition = GetNextChildPosition(menuEntries, parentMenuItemPosition);
|
||||
}
|
||||
else {
|
||||
// filter the content items for this specific menu
|
||||
menuPart.MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu));
|
||||
}
|
||||
|
||||
var model = Services.ContentManager.BuildEditor(menuPart);
|
||||
|
||||
|
||||
return View(model);
|
||||
}
|
||||
catch (Exception exception) {
|
||||
if (exception.IsFatal()) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Error(T("Creating menu item failed: {0}", exception.Message).Text);
|
||||
Services.Notifier.Error(T("Creating menu item failed: {0}", exception.Message));
|
||||
@@ -191,7 +196,7 @@ namespace Orchard.Core.Navigation.Controllers {
|
||||
}
|
||||
|
||||
[HttpPost, ActionName("CreateMenuItem")]
|
||||
public ActionResult CreateMenuItemPost(string id, int menuId, string returnUrl) {
|
||||
public ActionResult CreateMenuItemPost(string id, int menuId, string returnUrl, string parentMenuItemPosition = null) {
|
||||
if (!Services.Authorizer.Authorize(Permissions.ManageMenus, _menuService.GetMenu(menuId), T("Couldn't manage the main menu")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
@@ -205,12 +210,18 @@ namespace Orchard.Core.Navigation.Controllers {
|
||||
|
||||
if (menu == null)
|
||||
return HttpNotFound();
|
||||
|
||||
|
||||
var model = Services.ContentManager.UpdateEditor(menuPart, this);
|
||||
|
||||
menuPart.MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu));
|
||||
if (!String.IsNullOrEmpty(parentMenuItemPosition)) {
|
||||
var menuEntries = _menuService.GetMenuParts(menuId).Select(CreateMenuItemEntries);
|
||||
menuPart.MenuPosition = GetNextChildPosition(menuEntries, parentMenuItemPosition);
|
||||
}
|
||||
else {
|
||||
menuPart.MenuPosition = Position.GetNext( _navigationManager.BuildMenu(menu));
|
||||
}
|
||||
|
||||
menuPart.Menu = menu;
|
||||
|
||||
Services.ContentManager.Create(menuPart);
|
||||
|
||||
if (!ModelState.IsValid) {
|
||||
@@ -222,5 +233,23 @@ namespace Orchard.Core.Navigation.Controllers {
|
||||
|
||||
return this.RedirectLocal(returnUrl, () => RedirectToAction("Index"));
|
||||
}
|
||||
|
||||
private static string GetNextChildPosition(IEnumerable<MenuItemEntry> menuItems, string parentMenuItemPosition) {
|
||||
var parentMenuItemPositionPlusDot = parentMenuItemPosition + ".";
|
||||
var childElements = menuItems.Where(childElement => childElement.Position.StartsWith(parentMenuItemPositionPlusDot));
|
||||
if (childElements.Any()) {
|
||||
var result = childElements
|
||||
.Select(childElement => {
|
||||
var positionParts = childElement.Position.Substring(parentMenuItemPositionPlusDot.Length).Split(new[] { '.' }, 2, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
return positionParts.Any() ? int.Parse(positionParts[0]) : 0;
|
||||
})
|
||||
.Max();
|
||||
|
||||
return parentMenuItemPositionPlusDot + (result + 1);
|
||||
}
|
||||
|
||||
return parentMenuItemPositionPlusDot + "1";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -52,6 +52,27 @@
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
$(function () {
|
||||
$(".navigation-menu-item > div").on("click", function () {
|
||||
if ($(".navigation-menu-item > div.menu-item-selected").length) {
|
||||
if ($(this).hasClass("menu-item-selected")) {
|
||||
$(this).removeClass("menu-item-selected");
|
||||
}
|
||||
else {
|
||||
$(".navigation-menu-item > div").removeClass("menu-item-selected");
|
||||
$(this).addClass("menu-item-selected")
|
||||
}
|
||||
}
|
||||
else {
|
||||
$(this).addClass("menu-item-selected");
|
||||
}
|
||||
});
|
||||
|
||||
$(".menu-item-actions > .button").on("click", function (e) {
|
||||
if ($(".navigation-menu-item > div.menu-item-selected").length) {
|
||||
e.originalEvent.currentTarget.href = $(this).attr("href") + "&parentMenuItemPosition=" + $(".navigation-menu-item > div.menu-item-selected > .navigation-position > input").val();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
})(jQuery);
|
||||
|
@@ -172,4 +172,13 @@
|
||||
.dir-rtl div.menu-item-actions {
|
||||
left:10px;
|
||||
right: inherit;
|
||||
}
|
||||
}
|
||||
|
||||
.navigation-menu li div.menu-item-selected {
|
||||
background-color: #6a7b42;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.navigation-menu li div.menu-item-selected a {
|
||||
color: #edf9f5;
|
||||
}
|
@@ -46,6 +46,8 @@
|
||||
<div class="container">
|
||||
<div class="navigation-menu">
|
||||
@if (Model.MenuItemEntries.Any()) {
|
||||
@Html.Hint(T("If you'd like to add a new menu item under another one directly then select the parent by clicking on it."))
|
||||
|
||||
@RenderMenuItems(Model.MenuItemEntries, 0)
|
||||
}
|
||||
else {
|
||||
@@ -77,8 +79,8 @@
|
||||
}
|
||||
|
||||
|
||||
@helper RenderMenuItems(IList<MenuItemEntry> menuItems, int index) {
|
||||
|
||||
@helper RenderMenuItems(IList<MenuItemEntry> menuItems, int index) {
|
||||
|
||||
@:<ol>
|
||||
|
||||
// store current level to detect lowerb or upper level
|
||||
|
Reference in New Issue
Block a user