56 lines
2.4 KiB
C#
56 lines
2.4 KiB
C#
using Elternbeirat.Contracts;
|
|
|
|
namespace Elternbeirat.Web.Features.Layout;
|
|
|
|
/// <summary>
|
|
/// Builds the header and footer navigation from the public pages. Every page
|
|
/// carries a required <see cref="Page.Location"/> ("header" or "footer") and an
|
|
/// <see cref="Page.Order"/>; this splits them into the two menus, each ordered.
|
|
/// </summary>
|
|
public static class NavBuilder
|
|
{
|
|
/// <summary>Header links, ordered, built from pages with location "header".</summary>
|
|
public static IReadOnlyList<NavLinkItem> Header(IReadOnlyList<Page> pages) =>
|
|
ToLinks(PagesAt(pages, "header"));
|
|
|
|
/// <summary>Footer links, ordered, built from pages with location "footer".</summary>
|
|
public static IReadOnlyList<NavLinkItem> Footer(IReadOnlyList<Page> pages) =>
|
|
ToLinks(PagesAt(pages, "footer"));
|
|
|
|
/// <summary>
|
|
/// Returns the pages of the header menu themselves, in menu order.
|
|
/// </summary>
|
|
/// <param name="pages">All public pages.</param>
|
|
/// <returns>The pages with location <c>header</c>, ordered by <see cref="Page.Order"/>.</returns>
|
|
/// <remarks>
|
|
/// For callers that need more than a link, such as the tiles on the home page,
|
|
/// which also show part of each page's body. Using the same selection as
|
|
/// <see cref="Header"/> keeps tiles and menu in step.
|
|
/// </remarks>
|
|
public static IReadOnlyList<Page> HeaderPages(IReadOnlyList<Page> pages) =>
|
|
PagesAt(pages, "header");
|
|
|
|
/// <summary>
|
|
/// Returns the URL a page is reached at.
|
|
/// </summary>
|
|
/// <param name="slug">The page's slug.</param>
|
|
/// <returns><c>/</c> for the home page, otherwise <c>/{slug}</c>.</returns>
|
|
/// <remarks>
|
|
/// The home page owns "/" (served by the Home component); every other slug is
|
|
/// reached at "/{slug}", where either a literal route or the catch-all content
|
|
/// page renders it.
|
|
/// </remarks>
|
|
public static string HrefFor(string slug) =>
|
|
Page.IsHomeSlug(slug) ? "/" : $"/{slug}";
|
|
|
|
private static List<Page> PagesAt(
|
|
IReadOnlyList<Page> pages,
|
|
string location) =>
|
|
[.. pages
|
|
.Where(page => string.Equals(page.Location, location, StringComparison.Ordinal))
|
|
.OrderBy(page => page.Order)];
|
|
|
|
private static List<NavLinkItem> ToLinks(IEnumerable<Page> pages) =>
|
|
[.. pages.Select(page => new NavLinkItem(HrefFor(page.Slug), page.Title))];
|
|
}
|