mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
Instead of an isBreadcrumb parameter we now use BreadcrumbMenuItemPart to distinguish breadcrumb-only menu items
This commit is contained in:
parent
cce9b3172e
commit
f44ddc623c
@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Web;
|
||||||
|
using Orchard.ContentManagement.Drivers;
|
||||||
|
using Orchard.Core.Navigation.Models;
|
||||||
|
|
||||||
|
namespace Orchard.Core.Navigation.Drivers {
|
||||||
|
public class BreadcrumbMenuItemPartDriver : ContentPartDriver<BreadcrumbMenuItemPart> {
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,7 @@ using Orchard.Core.Title.Models;
|
|||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
using Orchard.UI.Navigation;
|
using Orchard.UI.Navigation;
|
||||||
using Orchard.Utility.Extensions;
|
using Orchard.Utility.Extensions;
|
||||||
|
using Orchard.ContentManagement.Utilities;
|
||||||
|
|
||||||
namespace Orchard.Core.Navigation.Drivers {
|
namespace Orchard.Core.Navigation.Drivers {
|
||||||
public class MenuWidgetPartDriver : ContentPartDriver<MenuWidgetPart> {
|
public class MenuWidgetPartDriver : ContentPartDriver<MenuWidgetPart> {
|
||||||
@ -49,7 +50,12 @@ namespace Orchard.Core.Navigation.Drivers {
|
|||||||
|
|
||||||
var menuName = menu.As<TitlePart>().Title.HtmlClassify();
|
var menuName = menu.As<TitlePart>().Title.HtmlClassify();
|
||||||
var currentCulture = _workContextAccessor.GetContext().CurrentCulture;
|
var currentCulture = _workContextAccessor.GetContext().CurrentCulture;
|
||||||
var menuItems = _navigationManager.BuildMenu(menu, part.Breadcrumb);
|
var menuItems = _navigationManager.BuildMenu(menu);
|
||||||
|
|
||||||
|
if (!part.Breadcrumb) {
|
||||||
|
menuItems = menuItems.Where(x => !x.Content.Has<BreadcrumbMenuItemPart>());
|
||||||
|
}
|
||||||
|
|
||||||
var localized = new List<MenuItem>();
|
var localized = new List<MenuItem>();
|
||||||
foreach(var menuItem in menuItems) {
|
foreach(var menuItem in menuItems) {
|
||||||
// if there is no associated content, it as culture neutral
|
// if there is no associated content, it as culture neutral
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
using Orchard.ContentManagement;
|
||||||
|
|
||||||
|
namespace Orchard.Core.Navigation.Models {
|
||||||
|
public class BreadcrumbMenuItemPart : ContentPart {
|
||||||
|
}
|
||||||
|
}
|
@ -44,29 +44,21 @@ namespace Orchard.Core.Navigation.Services {
|
|||||||
public ILogger Logger { get; set; }
|
public ILogger Logger { get; set; }
|
||||||
|
|
||||||
public IEnumerable<MenuItem> BuildMenu(string menuName) {
|
public IEnumerable<MenuItem> BuildMenu(string menuName) {
|
||||||
return BuildMenu(menuName, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<MenuItem> BuildMenu(string menuName, bool isBreadcrumb) {
|
|
||||||
var sources = GetSources(menuName);
|
var sources = GetSources(menuName);
|
||||||
var hasDebugShowAllMenuItems = _authorizationService.TryCheckAccess(Permission.Named("DebugShowAllMenuItems"), _orchardServices.WorkContext.CurrentUser, null);
|
var hasDebugShowAllMenuItems = _authorizationService.TryCheckAccess(Permission.Named("DebugShowAllMenuItems"), _orchardServices.WorkContext.CurrentUser, null);
|
||||||
return FinishMenu(Reduce(Filter(Merge(sources), isBreadcrumb), menuName == "admin", hasDebugShowAllMenuItems).ToArray());
|
return FinishMenu(Reduce(Filter(Merge(sources)), menuName == "admin", hasDebugShowAllMenuItems).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<MenuItem> BuildMenu(IContent menu) {
|
public IEnumerable<MenuItem> BuildMenu(IContent menu) {
|
||||||
return BuildMenu(menu,false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<MenuItem> BuildMenu(IContent menu, bool isBreadcrumb) {
|
|
||||||
var sources = GetSources(menu);
|
var sources = GetSources(menu);
|
||||||
var hasDebugShowAllMenuItems = _authorizationService.TryCheckAccess(Permission.Named("DebugShowAllMenuItems"), _orchardServices.WorkContext.CurrentUser, null);
|
var hasDebugShowAllMenuItems = _authorizationService.TryCheckAccess(Permission.Named("DebugShowAllMenuItems"), _orchardServices.WorkContext.CurrentUser, null);
|
||||||
return FinishMenu(Reduce(Arrange(Filter(Merge(sources), isBreadcrumb)), false, hasDebugShowAllMenuItems).ToArray());
|
return FinishMenu(Reduce(Arrange(Filter(Merge(sources))), false, hasDebugShowAllMenuItems).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNextPosition(IContent menu) {
|
public string GetNextPosition(IContent menu) {
|
||||||
var sources = GetSources(menu);
|
var sources = GetSources(menu);
|
||||||
var hasDebugShowAllMenuItems = _authorizationService.TryCheckAccess(Permission.Named("DebugShowAllMenuItems"), _orchardServices.WorkContext.CurrentUser, null);
|
var hasDebugShowAllMenuItems = _authorizationService.TryCheckAccess(Permission.Named("DebugShowAllMenuItems"), _orchardServices.WorkContext.CurrentUser, null);
|
||||||
return Position.GetNext(Reduce(Arrange(Filter(Merge(sources), false)), false, hasDebugShowAllMenuItems).ToArray());
|
return Position.GetNext(Reduce(Arrange(Filter(Merge(sources))), false, hasDebugShowAllMenuItems).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<string> BuildImageSets(string menuName) {
|
public IEnumerable<string> BuildImageSets(string menuName) {
|
||||||
@ -82,10 +74,10 @@ namespace Orchard.Core.Navigation.Services {
|
|||||||
return menuItems;
|
return menuItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> menuItems, bool isBreadcrumb) {
|
private IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> menuItems) {
|
||||||
IEnumerable<MenuItem> result = menuItems;
|
IEnumerable<MenuItem> result = menuItems;
|
||||||
foreach (var filter in _navigationFilters) {
|
foreach (var filter in _navigationFilters) {
|
||||||
result = filter.Filter(result, isBreadcrumb);
|
result = filter.Filter(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -172,6 +172,7 @@
|
|||||||
<Compile Include="Dashboard\Services\CompilationErrorBanner.cs" />
|
<Compile Include="Dashboard\Services\CompilationErrorBanner.cs" />
|
||||||
<Compile Include="Navigation\Commands\MenuCommands.cs" />
|
<Compile Include="Navigation\Commands\MenuCommands.cs" />
|
||||||
<Compile Include="Navigation\Drivers\AdminMenuPartDriver.cs" />
|
<Compile Include="Navigation\Drivers\AdminMenuPartDriver.cs" />
|
||||||
|
<Compile Include="Navigation\Drivers\BreadcrumbMenuItemPartDriver.cs" />
|
||||||
<Compile Include="Navigation\Drivers\MenuItemPartDriver.cs" />
|
<Compile Include="Navigation\Drivers\MenuItemPartDriver.cs" />
|
||||||
<Compile Include="Navigation\Drivers\MenuWidgetPartDriver.cs" />
|
<Compile Include="Navigation\Drivers\MenuWidgetPartDriver.cs" />
|
||||||
<Compile Include="Navigation\Drivers\ShapeMenuItemPartDriver.cs" />
|
<Compile Include="Navigation\Drivers\ShapeMenuItemPartDriver.cs" />
|
||||||
@ -182,6 +183,7 @@
|
|||||||
<Compile Include="Navigation\Handlers\MenuWidgetPartHandler.cs" />
|
<Compile Include="Navigation\Handlers\MenuWidgetPartHandler.cs" />
|
||||||
<Compile Include="Navigation\Models\AdminMenuPart.cs" />
|
<Compile Include="Navigation\Models\AdminMenuPart.cs" />
|
||||||
<Compile Include="Navigation\Models\AdminMenuPartRecord.cs" />
|
<Compile Include="Navigation\Models\AdminMenuPartRecord.cs" />
|
||||||
|
<Compile Include="Navigation\Models\BreadcrumbMenuItemPart.cs" />
|
||||||
<Compile Include="Navigation\Models\MenuWidgetPart.cs" />
|
<Compile Include="Navigation\Models\MenuWidgetPart.cs" />
|
||||||
<Compile Include="Navigation\Models\ShapeMenuItemPart.cs" />
|
<Compile Include="Navigation\Models\ShapeMenuItemPart.cs" />
|
||||||
<Compile Include="Navigation\Security\AuthorizationEventHandler.cs" />
|
<Compile Include="Navigation\Security\AuthorizationEventHandler.cs" />
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using Orchard.Data.Migration;
|
using Orchard.Data.Migration;
|
||||||
using Orchard.ContentManagement.MetaData;
|
using Orchard.ContentManagement.MetaData;
|
||||||
using Orchard.Environment.Extensions;
|
using Orchard.Environment.Extensions;
|
||||||
|
using Orchard.Core.Navigation.Models;
|
||||||
|
|
||||||
namespace Orchard.Alias {
|
namespace Orchard.Alias {
|
||||||
[OrchardFeature("Orchard.Alias.BreadcrumbLink")]
|
[OrchardFeature("Orchard.Alias.BreadcrumbLink")]
|
||||||
@ -8,6 +9,7 @@ namespace Orchard.Alias {
|
|||||||
public int Create() {
|
public int Create() {
|
||||||
ContentDefinitionManager.AlterTypeDefinition("AliasBreadcrumbMenuItem",
|
ContentDefinitionManager.AlterTypeDefinition("AliasBreadcrumbMenuItem",
|
||||||
cfg => cfg
|
cfg => cfg
|
||||||
|
.WithPart("BreadcrumbMenuItemPart")
|
||||||
.WithPart("MenuPart")
|
.WithPart("MenuPart")
|
||||||
.WithPart("CommonPart")
|
.WithPart("CommonPart")
|
||||||
.WithIdentity()
|
.WithIdentity()
|
||||||
|
@ -27,93 +27,94 @@ namespace Orchard.Alias.Navigation {
|
|||||||
_hca = hca;
|
_hca = hca;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> items, bool isBreadcrumb = false) {
|
public IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> items) {
|
||||||
foreach (var item in items) {
|
foreach (var item in items) {
|
||||||
if (item.Content != null && item.Content.ContentItem.ContentType == "AliasBreadcrumbMenuItem") {
|
if (item.Content != null && item.Content.ContentItem.ContentType == "AliasBreadcrumbMenuItem") {
|
||||||
if (isBreadcrumb) {
|
var request = _hca.Current().Request;
|
||||||
var request = _hca.Current().Request;
|
var path = request.Path;
|
||||||
var path = request.Path;
|
var appPath = request.ApplicationPath ?? "/";
|
||||||
var appPath = request.ApplicationPath ?? "/";
|
var requestUrl = (path.StartsWith(appPath) ? path.Substring(appPath.Length) : path).TrimStart('/');
|
||||||
var requestUrl = (path.StartsWith(appPath) ? path.Substring(appPath.Length) : path).TrimStart('/');
|
var endsWithSlash = requestUrl.EndsWith("/");
|
||||||
var endsWithSlash = requestUrl.EndsWith("/");
|
|
||||||
|
|
||||||
var menuPosition = item.Position;
|
var menuPosition = item.Position;
|
||||||
|
|
||||||
var urlLevel = endsWithSlash ? requestUrl.Count(l => l == '/') - 1 : requestUrl.Count(l => l == '/');
|
var urlLevel = endsWithSlash ? requestUrl.Count(l => l == '/') - 1 : requestUrl.Count(l => l == '/');
|
||||||
var menuLevel = menuPosition.Count(l => l == '.');
|
var menuLevel = menuPosition.Count(l => l == '.');
|
||||||
|
|
||||||
// Checking menu item if it's the leaf element or it's an intermediate element
|
// Checking menu item if it's the leaf element or it's an intermediate element
|
||||||
// or it's an unneccessary element according to the url.
|
// or it's an unneccessary element according to the url.
|
||||||
RouteValueDictionary contentRoute;
|
RouteValueDictionary contentRoute;
|
||||||
if (menuLevel == urlLevel) {
|
if (menuLevel == urlLevel) {
|
||||||
|
contentRoute = request.RequestContext.RouteData.Values;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// If menuLevel doesn't equal with urlLevel it can mean that this menu item is
|
||||||
|
// an intermediate element (the difference is a positive value) or this menu
|
||||||
|
// item is lower in the navigation hierarchy according to the url (negative
|
||||||
|
// value). If the value is negative, removing the menu item, if the value
|
||||||
|
// is positive finding its place in the hierarchy.
|
||||||
|
var levelDifference = urlLevel - menuLevel;
|
||||||
|
if (levelDifference > 0) {
|
||||||
|
if (endsWithSlash) {
|
||||||
|
levelDifference += levelDifference;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < levelDifference; i++) {
|
||||||
|
requestUrl = requestUrl.Remove(requestUrl.LastIndexOf('/'));
|
||||||
|
path = path.Remove(path.LastIndexOf('/'));
|
||||||
|
}
|
||||||
contentRoute = _aliasService.Get(requestUrl);
|
contentRoute = _aliasService.Get(requestUrl);
|
||||||
|
if (contentRoute == null) {
|
||||||
|
// After the exact number of segments is cut out from the url and the
|
||||||
|
// currentRoute is still null, trying another check with the added slash,
|
||||||
|
// because we don't know if the alias was created with a slash at the end or not.
|
||||||
|
contentRoute = _aliasService.Get(requestUrl.Insert(requestUrl.Length, "/"));
|
||||||
|
path = path.Insert(path.Length, "/");
|
||||||
|
if (contentRoute == null) {
|
||||||
|
contentRoute = new RouteValueDictionary();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// If menuLevel doesn't equal with urlLevel it can mean that this menu item is
|
contentRoute = new RouteValueDictionary();
|
||||||
// an intermediate element (the difference is a positive value) or this menu
|
|
||||||
// item is lower in the navigation hierarchy according to the url (negative
|
|
||||||
// value). If the value is negative, removing the menu item, if the value
|
|
||||||
// is positive finding its place in the hierarchy.
|
|
||||||
var levelDifference = urlLevel - menuLevel;
|
|
||||||
if (levelDifference > 0) {
|
|
||||||
if (endsWithSlash) {
|
|
||||||
levelDifference += levelDifference;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < levelDifference; i++) {
|
|
||||||
requestUrl = requestUrl.Remove(requestUrl.LastIndexOf('/'));
|
|
||||||
path = path.Remove(path.LastIndexOf('/'));
|
|
||||||
}
|
|
||||||
contentRoute = _aliasService.Get(requestUrl);
|
|
||||||
if (contentRoute == null) {
|
|
||||||
// After the exact number of segments is cut out from the url and the
|
|
||||||
// currentRoute is still null, trying another check with the added slash,
|
|
||||||
// because we don't know if the alias was created with a slash at the end or not.
|
|
||||||
contentRoute = _aliasService.Get(requestUrl.Insert(requestUrl.Length, "/"));
|
|
||||||
path = path.Insert(path.Length, "/");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
contentRoute = new RouteValueDictionary();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
object id;
|
object id;
|
||||||
contentRoute.TryGetValue("Id", out id);
|
contentRoute.TryGetValue("Id", out id);
|
||||||
int contentId;
|
int contentId;
|
||||||
int.TryParse(id as string, out contentId);
|
int.TryParse(id as string, out contentId);
|
||||||
if (contentId == 0) {
|
if (contentId == 0) {
|
||||||
// If failed to get the Id's value from currentRoute, transform the alias to the virtual path
|
// If failed to get the Id's value from currentRoute, transform the alias to the virtual path
|
||||||
// and try to get the content item's id from there. E.g. "Blogs/Blog/Item?blogId=12" where
|
// and try to get the content item's id from there. E.g. "Blogs/Blog/Item?blogId=12" where
|
||||||
// the last digits represents the content item's id. If there is a match in the path we get
|
// the last digits represents the content item's id. If there is a match in the path we get
|
||||||
// the digits after the equality sign.
|
// the digits after the equality sign.
|
||||||
// There is an another type of the routes: like "Contents/Item/Display/13", but when the
|
// There is an another type of the routes: like "Contents/Item/Display/13", but when the
|
||||||
// content item's route is in this form we already have the id from contentRoute.TryGetValue("Id", out id).
|
// content item's route is in this form we already have the id from contentRoute.TryGetValue("Id", out id).
|
||||||
var virtualPath = _aliasService.LookupVirtualPaths(contentRoute, _hca.Current()).FirstOrDefault();
|
var virtualPath = _aliasService.LookupVirtualPaths(contentRoute, _hca.Current()).FirstOrDefault();
|
||||||
int.TryParse(virtualPath != null ? virtualPath.VirtualPath.Substring(virtualPath.VirtualPath.LastIndexOf('=') + 1) : "0", out contentId);
|
int.TryParse(virtualPath != null ? virtualPath.VirtualPath.Substring(virtualPath.VirtualPath.LastIndexOf('=') + 1) : "0", out contentId);
|
||||||
}
|
}
|
||||||
if (contentId != 0) {
|
if (contentId != 0) {
|
||||||
var currentContentItem = _contentManager.Get(contentId);
|
var currentContentItem = _contentManager.Get(contentId);
|
||||||
if (currentContentItem != null) {
|
if (currentContentItem != null) {
|
||||||
var menuText = _contentManager.GetItemMetadata(currentContentItem).DisplayText;
|
var menuText = _contentManager.GetItemMetadata(currentContentItem).DisplayText;
|
||||||
var routes = _contentManager.GetItemMetadata(currentContentItem).DisplayRouteValues;
|
var routes = _contentManager.GetItemMetadata(currentContentItem).DisplayRouteValues;
|
||||||
|
|
||||||
var inserted = new MenuItem {
|
var inserted = new MenuItem {
|
||||||
Text = new LocalizedString(menuText),
|
Text = new LocalizedString(menuText),
|
||||||
IdHint = item.IdHint,
|
IdHint = item.IdHint,
|
||||||
Classes = item.Classes,
|
Classes = item.Classes,
|
||||||
Url = path,
|
Url = path,
|
||||||
Href = item.Href,
|
Href = item.Href,
|
||||||
LinkToFirstChild = false,
|
LinkToFirstChild = false,
|
||||||
RouteValues = routes,
|
RouteValues = routes,
|
||||||
LocalNav = item.LocalNav,
|
LocalNav = item.LocalNav,
|
||||||
Items = Enumerable.Empty<MenuItem>(),
|
Items = Enumerable.Empty<MenuItem>(),
|
||||||
Position = menuPosition,
|
Position = menuPosition,
|
||||||
Permissions = item.Permissions,
|
Permissions = item.Permissions,
|
||||||
Content = currentContentItem
|
Content = item.Content
|
||||||
};
|
};
|
||||||
|
|
||||||
yield return inserted;
|
yield return inserted;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ namespace Orchard.Projections.Navigation {
|
|||||||
_projectionManager = projectionManager;
|
_projectionManager = projectionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> items, bool isBreadcrumb) {
|
public IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> items) {
|
||||||
|
|
||||||
foreach (var item in items) {
|
foreach (var item in items) {
|
||||||
if (item.Content != null && item.Content.ContentItem.ContentType == "NavigationQueryMenuItem") {
|
if (item.Content != null && item.Content.ContentItem.ContentType == "NavigationQueryMenuItem") {
|
||||||
|
@ -24,7 +24,7 @@ namespace Orchard.Taxonomies.Navigation {
|
|||||||
_taxonomyService = taxonomyService;
|
_taxonomyService = taxonomyService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> items, bool isBreadcrumb) {
|
public IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> items) {
|
||||||
|
|
||||||
foreach (var item in items) {
|
foreach (var item in items) {
|
||||||
if (item.Content != null && item.Content.ContentItem.ContentType == "TaxonomyNavigationMenuItem") {
|
if (item.Content != null && item.Content.ContentItem.ContentType == "TaxonomyNavigationMenuItem") {
|
||||||
|
@ -5,6 +5,6 @@ namespace Orchard.UI.Navigation {
|
|||||||
/// Provides a way to alter the main navigation, for instance by dynamically injecting new items
|
/// Provides a way to alter the main navigation, for instance by dynamically injecting new items
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface INavigationFilter : IDependency {
|
public interface INavigationFilter : IDependency {
|
||||||
IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> menuItems, bool isBreadcrumb = false);
|
IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> menuItems);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,7 @@ using Orchard.ContentManagement;
|
|||||||
namespace Orchard.UI.Navigation {
|
namespace Orchard.UI.Navigation {
|
||||||
public interface INavigationManager : IDependency {
|
public interface INavigationManager : IDependency {
|
||||||
IEnumerable<MenuItem> BuildMenu(string menuName);
|
IEnumerable<MenuItem> BuildMenu(string menuName);
|
||||||
IEnumerable<MenuItem> BuildMenu(string menuName, bool isBreadcrumbMenu);
|
|
||||||
IEnumerable<MenuItem> BuildMenu(IContent menu);
|
IEnumerable<MenuItem> BuildMenu(IContent menu);
|
||||||
IEnumerable<MenuItem> BuildMenu(IContent menu, bool isBreadcrumbMenu);
|
|
||||||
IEnumerable<string> BuildImageSets(string menuName);
|
IEnumerable<string> BuildImageSets(string menuName);
|
||||||
string GetUrl(string menuItemUrl, RouteValueDictionary routeValueDictionary);
|
string GetUrl(string menuItemUrl, RouteValueDictionary routeValueDictionary);
|
||||||
string GetNextPosition(IContent menu);
|
string GetNextPosition(IContent menu);
|
||||||
|
Loading…
Reference in New Issue
Block a user