202 lines
6.2 KiB
C#
202 lines
6.2 KiB
C#
using System.Diagnostics;
|
|
using BlueWest.Views.Controllers;
|
|
using BlueWest.Views.Controllers.Data;
|
|
using BlueWest.Views.Languages;
|
|
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
|
|
|
namespace BlueWest.Views.Utils;
|
|
|
|
internal class LayoutCache
|
|
{
|
|
#region Route Tree
|
|
|
|
private static readonly RouteRecord NonLoggedInRoot = new RouteRecord(
|
|
AuthLoginRoute,
|
|
AuthLoginKeyName,
|
|
nameof(AuthController), new List<RouteRecord>());
|
|
|
|
|
|
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(
|
|
AccountKeyName,
|
|
AccountRouteLocation,
|
|
nameof(AccountController),
|
|
new List<RouteRecord>()
|
|
{
|
|
new RouteRecord(ChangePasswordKeyName, ChangePasswordRouteLocation, nameof(AccountController), new List<RouteRecord>())
|
|
},
|
|
ViewType.Account
|
|
),
|
|
}, 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 DataRouteRecord =
|
|
Root.Children.FirstOrDefault(x => x.ViewType == ViewType.Data)!;
|
|
|
|
internal static readonly RouteRecord AccountRouteRecord =
|
|
Root.Children.FirstOrDefault(x => x.ViewType == ViewType.Account)!;
|
|
|
|
#endregion Routing Utils
|
|
|
|
#region Internal Menus
|
|
|
|
internal static List<RouteView> GetDefaultFooterMenu(ViewDataDictionary dictionary)
|
|
{
|
|
var location = GetUserLanguage(dictionary);
|
|
|
|
var menu = LayoutCache
|
|
.Root
|
|
.Children;
|
|
|
|
if (dictionary[FooterMenuViewDataId] is List<RouteRecord> footerMenu)
|
|
{
|
|
menu = footerMenu;
|
|
}
|
|
|
|
return menu
|
|
.Select(x =>
|
|
{
|
|
if (SiteContent.RouteTitle[x.routeKey].ContainsKey(location))
|
|
{
|
|
return new RouteView(SiteContent.RouteTitle[x.routeKey][location],
|
|
location);
|
|
}
|
|
return new RouteView(SiteContent.RouteTitle[x.routeKey][DefaultCultureName],
|
|
x.location);
|
|
|
|
})
|
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
internal static List<RouteView> GetDefaultHeaderMenu(ViewDataDictionary dictionary, bool userAuthenticated = false)
|
|
{
|
|
if (!userAuthenticated)
|
|
{
|
|
var menuToShow = new List<RouteView>();
|
|
menuToShow.Add(new RouteView("Blue West", "/"));
|
|
return menuToShow;
|
|
}
|
|
|
|
var location = GetUserLanguage(dictionary);
|
|
|
|
var menu = LayoutCache
|
|
.Root
|
|
.Children;
|
|
|
|
if (dictionary[HeaderMenuId] is List<RouteRecord> footerMenu)
|
|
{
|
|
menu = footerMenu;
|
|
}
|
|
|
|
return menu
|
|
.Select(x =>
|
|
{
|
|
if (SiteContent.RouteTitle[x.routeKey].ContainsKey(location))
|
|
{
|
|
return new RouteView(SiteContent.RouteTitle[x.routeKey][location],
|
|
x.Location);
|
|
}
|
|
return new RouteView(SiteContent.RouteTitle[x.routeKey][DefaultCultureName],
|
|
x.location);
|
|
|
|
})
|
|
|
|
.ToList();
|
|
}
|
|
|
|
|
|
[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
|
|
|
|
} |