From 3bddd64d914f2d909512195a0c86cf9c5e1b1efe Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Fri, 20 Feb 2026 16:25:01 +0100 Subject: [PATCH] Added page hooks for main layout --- .../Configuration/LayoutPageOptions.cs | 33 ++++++++++++++++ .../UI/Shared/Partials/MainLayout.razor | 39 ++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 Moonlight.Frontend/Configuration/LayoutPageOptions.cs diff --git a/Moonlight.Frontend/Configuration/LayoutPageOptions.cs b/Moonlight.Frontend/Configuration/LayoutPageOptions.cs new file mode 100644 index 00000000..bc83800e --- /dev/null +++ b/Moonlight.Frontend/Configuration/LayoutPageOptions.cs @@ -0,0 +1,33 @@ +using System.Diagnostics.CodeAnalysis; +using Microsoft.AspNetCore.Components; + +namespace Moonlight.Frontend.Configuration; + +public class LayoutPageOptions +{ + public IReadOnlyList Components => InnerComponents; + + private readonly List InnerComponents = new(); + + public void Add<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(LayoutPageSlot slot, int order) + where T : ComponentBase + => Add(typeof(T), slot, order); + + public void Add(Type componentType, LayoutPageSlot slot, int order) + => InnerComponents.Add(new LayoutPageComponent(componentType, order, slot)); + + public void Remove<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>() + where T : ComponentBase + => Remove(typeof(T)); + + public void Remove(Type componentType) + => InnerComponents.RemoveAll(x => x.ComponentType == componentType); +} + +public record LayoutPageComponent(Type ComponentType, int Order, LayoutPageSlot Slot); + +public enum LayoutPageSlot +{ + Header = 0, + Footer = 1 +} \ No newline at end of file diff --git a/Moonlight.Frontend/UI/Shared/Partials/MainLayout.razor b/Moonlight.Frontend/UI/Shared/Partials/MainLayout.razor index 7f59c3aa..57a113c6 100644 --- a/Moonlight.Frontend/UI/Shared/Partials/MainLayout.razor +++ b/Moonlight.Frontend/UI/Shared/Partials/MainLayout.razor @@ -1,18 +1,53 @@ -@using ShadcnBlazor.Extras.Alerts +@using Microsoft.Extensions.Options +@using Moonlight.Frontend.Configuration +@using ShadcnBlazor.Extras.Alerts @using ShadcnBlazor.Sidebars @inherits LayoutComponentBase +@inject IOptions LayoutPageOptions + + @foreach (var headerComponent in HeaderComponents) + { + + } +
@Body
+ + @foreach (var footerComponent in FooterComponents) + { + + }
-
\ No newline at end of file + + +@code +{ + private Type[] HeaderComponents; + private Type[] FooterComponents; + + protected override void OnInitialized() + { + HeaderComponents = LayoutPageOptions.Value.Components + .Where(x => x.Slot == LayoutPageSlot.Header) + .OrderBy(x => x.Order) + .Select(x => x.ComponentType) + .ToArray(); + + FooterComponents = LayoutPageOptions.Value.Components + .Where(x => x.Slot == LayoutPageSlot.Footer) + .OrderBy(x => x.Order) + .Select(x => x.ComponentType) + .ToArray(); + } +} \ No newline at end of file