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

202 lines
6.2 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.Controllers.Data;
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),
new List<RouteRecord>()
{
new RouteRecord(
DataKeyName,
DataLocation,
nameof(DataController),
new List<RouteRecord>()
{
new RouteRecord(
CompaniesKeyName,
CompaniesLocation,
nameof(CompaniesController),
new List<RouteRecord>()
),
new RouteRecord(
CountriesKeyName,
CountriesLocation,
nameof(CountriesController),
new List<RouteRecord>()
),
new RouteRecord(
CurrenciesKeyName,
CurrenciesLocation,
nameof(CurrenciesController),
new List<RouteRecord>()
),
new RouteRecord(
BanksKeyName,
BanksLocation,
nameof(BanksController),
new List<RouteRecord>()
),
new RouteRecord(
DataUsersKeyName,
DataUsersLocation,
nameof(DataUsersController),
new List<RouteRecord>()
),
},
ViewType.Data
),
new RouteRecord(
SystemKeyName,
SystemRouteLocation,
nameof(SystemController),
new List<RouteRecord>()
{
new RouteRecord(
RolesKeyName,
RolesLocation,
nameof(RolesController),
new List<RouteRecord>()
),
new RouteRecord(
ApplicationUsersKeyName,
ApplicationUsersLocation,
nameof(ApplicationUsersController),
new List<RouteRecord>()
),
new RouteRecord(
LogsKeyName,
LogsLocation,
nameof(LogsController),
new List<RouteRecord>()
)
},
ViewType.System
),
new RouteRecord(
2022-10-27 20:13:02 +03:00
AccountKeyName,
AccountRouteLocation,
nameof(AccountController),
new List<RouteRecord>()
{
new RouteRecord(ChangePasswordKeyName, ChangePasswordRouteLocation, nameof(AccountController), new List<RouteRecord>())
},
ViewType.Account
2022-09-26 04:05:23 +03:00
),
}, ViewType.Root);
#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
}