Skip to content

Commit

Permalink
Removing workaround.
Browse files Browse the repository at this point in the history
  • Loading branch information
Psichorex committed Sep 7, 2023
1 parent 6d777e2 commit 68c74c5
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions Lombiq.BaseTheme/Services/MainMenuNavigationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
using AngleSharp.Html.Parser;
using Lombiq.HelpfulLibraries.OrchardCore.Navigation;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentManagement;
using OrchardCore.Menu.Models;
using OrchardCore.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -21,8 +19,8 @@ public class MainMenuNavigationProvider : MainMenuNavigationProviderBase
{
private readonly IContentHandleManager _contentHandleManager;
private readonly IContentManager _contentManager;

private readonly Lazy<IUrlHelper> _urlHelperLazy;
private readonly IUrlHelperFactory _urlHelperFactory;
private readonly IActionContextAccessor _actionContextAccessor;

public MainMenuNavigationProvider(
IHttpContextAccessor hca,
Expand All @@ -35,8 +33,8 @@ public MainMenuNavigationProvider(
{
_contentHandleManager = contentHandleManager;
_contentManager = contentManager;

_urlHelperLazy = new(() => urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext!));
_urlHelperFactory = urlHelperFactory;
_actionContextAccessor = actionContextAccessor;
}

protected override async Task BuildAsync(NavigationBuilder builder)
Expand All @@ -60,7 +58,7 @@ private async Task AddAsync(NavigationBuilder builder, ContentItem menuItem)

if (menuItem.As<LinkMenuItemPart>() is { } linkMenuItemPart)
{
builder.Add(text, menu => menu.Url(GetUrl(linkMenuItemPart.Url)));
builder.Add(text, menu => menu.Url(linkMenuItemPart.Url));
}
else if (menuItem.As<ContentMenuItemPart>() is { } contentMenuItemPart)
{
Expand All @@ -75,9 +73,7 @@ private async Task AddAsync(NavigationBuilder builder, ContentItem menuItem)
{
var nodeList = new HtmlParser().ParseFragment($"<div>{htmlMenuItemPart.Html}</div>", contextElement: null!);
var textContent = string.Concat(nodeList.Select(x => x.Text()));
var url = string.IsNullOrEmpty(htmlMenuItemPart.Url) ? "#" : GetUrl(htmlMenuItemPart.Url);

builder.Add(T[textContent], menu => menu.Url(url).LocalNav());
builder.Add(new LocalizedString(textContent, textContent), menu => menu.Url("#").LocalNav());
}
else if (menuItem.As<MenuItemsListPart>() is { } menuItemsListPart)
{
Expand All @@ -89,32 +85,26 @@ await builder.AddAsync(text, menu =>
private async Task AddContentMenuItemPartAsync(NavigationBuilder builder, LocalizedString text, IEnumerable<string> ids)
{
var contentItems = (await _contentManager.GetAsync(ids)).AsList();
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext!);

if (contentItems.Count == 1)
{
var contentItem = contentItems.Single();
if (string.IsNullOrEmpty(text.Value)) text = GetTitle(contentItem);
builder.Add(text, menu => UseDisplayUrl(menu, contentItem));
builder.Add(text, menu => menu.Url(urlHelper.DisplayContentItem(contentItem)));
}
else
{
builder.Add(text, menu =>
{
foreach (var contentItem in contentItems)
{
menu.Add(GetTitle(contentItem), child => UseDisplayUrl(child, contentItem));
menu.Add(GetTitle(contentItem), child => child.Url(urlHelper.DisplayContentItem(contentItem)));
}
});
}
}

// This and UseDisplayUrl(), and the other changes to this class under
// https://github.com/Lombiq/Orchard-Base-Theme/pull/74/files#diff-6dca81c4abae780b06f901d2ab84eb1e6d369e8d842da264a89199dfbaf11071
// may be reverted once https://github.com/OrchardCMS/OrchardCore/issues/13943 is fixed by an Orchard Core upgrade.
private string GetUrl(string contentPath) => _urlHelperLazy.Value.Content(contentPath);

private void UseDisplayUrl(NavigationItemBuilder menu, IContent content) =>
menu.Url(_urlHelperLazy.Value.DisplayContentItem(content));

private static LocalizedString GetTitle(ContentItem contentItem) =>
new(contentItem.DisplayText, contentItem.DisplayText);
}

0 comments on commit 68c74c5

Please sign in to comment.