CodeLiturgy.Dashboard/CodeLiturgy.Views/Utils/LayoutCache.cs

122 lines
3.4 KiB
C#
Raw Normal View History

2022-10-27 20:13:02 +03:00
using System.Diagnostics;
2022-10-30 19:48:24 +03:00
using CodeLiturgy.Views.Controllers;
using CodeLiturgy.Views.Languages;
2022-09-26 04:05:23 +03:00
using Microsoft.AspNetCore.Mvc.ViewFeatures;
2022-10-30 19:48:24 +03:00
namespace CodeLiturgy.Views.Utils;
2022-09-26 04:05:23 +03:00
internal class LayoutCache
{
#region Route Tree
2022-10-27 20:13:02 +03:00
private static readonly RouteRecord NonLoggedInRoot = new RouteRecord(
AuthLoginRoute,
AuthLoginKeyName,
nameof(AuthController), new List<RouteRecord>());
2022-09-26 04:05:23 +03:00
private static readonly RouteRecord Root = new RouteRecord(
RootKeyName,
RootLocation,
nameof(HomeController),
2022-11-12 21:50:32 +03:00
new List<RouteRecord>(), ViewType.Root);
2022-09-26 04:05:23 +03:00
#endregion Route Tree
#region Routing Utils
internal static readonly RouteRecord SystemRoute =
Root.Children.FirstOrDefault(x => x.ViewType == ViewType.System)!;
2022-10-27 20:13:02 +03:00
internal static readonly RouteRecord DataRouteRecord =
2022-09-26 04:05:23 +03:00
Root.Children.FirstOrDefault(x => x.ViewType == ViewType.Data)!;
2022-10-27 20:13:02 +03:00
internal static readonly RouteRecord AccountRouteRecord =
Root.Children.FirstOrDefault(x => x.ViewType == ViewType.Account)!;
2022-09-26 04:05:23 +03:00
#endregion Routing Utils
#region Internal Menus
internal static List<RouteView> GetDefaultFooterMenu(ViewDataDictionary dictionary)
{
2022-10-27 20:13:02 +03:00
var location = GetUserLanguage(dictionary);
2022-09-26 04:05:23 +03:00
var menu = LayoutCache
.Root
.Children;
2022-10-27 20:13:02 +03:00
if (dictionary[FooterMenuViewDataId] is List<RouteRecord> footerMenu)
2022-09-26 04:05:23 +03:00
{
menu = footerMenu;
}
return menu
.Select(x =>
{
2022-10-27 20:13:02 +03:00
if (SiteContent.RouteTitle[x.routeKey].ContainsKey(location))
2022-09-26 04:05:23 +03:00
{
2022-10-27 20:13:02 +03:00
return new RouteView(SiteContent.RouteTitle[x.routeKey][location],
2022-09-26 04:05:23 +03:00
location);
}
2022-10-27 20:13:02 +03:00
return new RouteView(SiteContent.RouteTitle[x.routeKey][DefaultCultureName],
2022-09-26 04:05:23 +03:00
x.location);
})
.ToList();
}
2022-10-27 20:13:02 +03:00
internal static List<RouteView> GetDefaultHeaderMenu(ViewDataDictionary dictionary, bool userAuthenticated = false)
2022-09-26 04:05:23 +03:00
{
2022-10-27 20:13:02 +03:00
if (!userAuthenticated)
{
var menuToShow = new List<RouteView>();
menuToShow.Add(new RouteView("Blue West", "/"));
return menuToShow;
}
var location = GetUserLanguage(dictionary);
2022-09-26 04:05:23 +03:00
var menu = LayoutCache
.Root
.Children;
if (dictionary[HeaderMenuId] is List<RouteRecord> footerMenu)
{
menu = footerMenu;
}
return menu
.Select(x =>
{
2022-10-27 20:13:02 +03:00
if (SiteContent.RouteTitle[x.routeKey].ContainsKey(location))
2022-09-26 04:05:23 +03:00
{
2022-10-27 20:13:02 +03:00
return new RouteView(SiteContent.RouteTitle[x.routeKey][location],
2022-09-26 04:05:23 +03:00
x.Location);
}
2022-10-27 20:13:02 +03:00
return new RouteView(SiteContent.RouteTitle[x.routeKey][DefaultCultureName],
2022-09-26 04:05:23 +03:00
x.location);
})
.ToList();
}
2022-10-27 20:13:02 +03:00
[Conditional("DEBUG")]
internal static void IsDevMode(ref bool isDebug)
{
isDebug = true;
}
internal static string GetUserLanguage(ViewDataDictionary dictionary)
2022-09-26 04:05:23 +03:00
{
return (dictionary[LanguageViewStorage] as string)!;
}
#endregion Internal Menus
}