CodeLiturgy.Dashboard/CodeLiturgy.Views/RazorUtils/LayoutCache.cs

112 lines
2.8 KiB
C#

using System.Diagnostics;
using CodeLiturgy.Views.Controllers;
using CodeLiturgy.Views.Languages;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
namespace CodeLiturgy.Views.Utils;
internal class LayoutCache
{
#region Route Tree
private static readonly RouteRecord NonLoggedInRoot = new RouteRecord(
"Login",
AuthLoginRoute,
AuthLoginKeyName,
nameof(AuthController), new List<RouteRecord>());
internal static readonly RouteRecord Root = new RouteRecord(
"Code Liturgy - Dashboard",
RootKeyName,
RootLocation,
nameof(HomeController),
new List<RouteRecord>()
{
new RouteRecord("Environments", "envs0", "environments", nameof(EnvironmentsApiController), new List<RouteRecord>()),
new RouteRecord("Standalone Sites", "sites0", "sites", nameof(SitesApiController), new List<RouteRecord>()),
}
, ViewType.Root);
#endregion Route Tree
#region Routing Utils
internal static readonly RouteRecord SystemRoute =
Root.Children.FirstOrDefault(x => x.ViewType == ViewType.System)!;
internal static readonly RouteRecord AccountRouteRecord =
Root.Children.FirstOrDefault(x => x.ViewType == ViewType.Account)!;
#endregion Routing Utils
#region Internal Menus
internal static List<Url> GetDefaultHeaderMenu(ViewDataDictionary dictionary, bool userAuthenticated = false)
{
var menuToShow = new List<Url>();
// Add Root URL
menuToShow.Add(Root.ToUrl());
if (!userAuthenticated)
{
return menuToShow;
}
var rootChildrenUrls = Root.Children.Select(x => x.ToUrl()).ToList();
menuToShow = menuToShow
.Concat(rootChildrenUrls)
.ToList();
if (dictionary[HeaderMenuId] is List<Url> headerMenu)
{
menuToShow = menuToShow
.Concat(headerMenu)
.ToList();
}
return menuToShow;
}
internal static List<Url> GetDefaultFooterMenu(ViewDataDictionary dictionary, bool userAuthenticated)
{
var menuToShow = new List<Url>();
menuToShow.Add(Root.ToUrl());
if (!userAuthenticated)
{
return menuToShow;
}
if (dictionary[FooterMenuViewDataId] is List<Url> footerMenu)
{
menuToShow = menuToShow
.Concat(footerMenu)
.ToList();
}
return menuToShow;
}
[Conditional("DEBUG")]
internal static void IsDevMode(ref bool isDebug)
{
isDebug = true;
}
internal static string GetUserLanguage(ViewDataDictionary dictionary)
{
return (dictionary[LanguageViewStorage] as string)!;
}
#endregion Internal Menus
}