Merge pull request #3178 from sbwalker/dev

move icon loading reflection logic to server
This commit is contained in:
Shaun Walker 2023-08-23 15:25:54 -04:00 committed by GitHub
commit f5ce48a7c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 57 additions and 40 deletions

View File

@ -45,7 +45,7 @@
private string _passwordType = "password"; private string _passwordType = "password";
private string _togglePassword = string.Empty; private string _togglePassword = string.Empty;
protected override async Task OnInitializedAsync() protected override void OnInitialized()
{ {
_togglePassword = SharedLocalizer["ShowPassword"]; _togglePassword = SharedLocalizer["ShowPassword"];
} }

View File

@ -59,7 +59,7 @@
private string _passwordType = "password"; private string _passwordType = "password";
private string _togglePassword = string.Empty; private string _togglePassword = string.Empty;
protected override async Task OnInitializedAsync() protected override void OnInitialized()
{ {
_togglePassword = SharedLocalizer["ShowPassword"]; _togglePassword = SharedLocalizer["ShowPassword"];
} }

View File

@ -75,7 +75,7 @@
private string _encryption = "false"; private string _encryption = "false";
private string _trustservercertificate = "false"; private string _trustservercertificate = "false";
protected override async Task OnInitializedAsync() protected override void OnInitialized()
{ {
_togglePassword = SharedLocalizer["ShowPassword"]; _togglePassword = SharedLocalizer["ShowPassword"];
} }

View File

@ -3,6 +3,7 @@
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IPageService PageService @inject IPageService PageService
@inject IThemeService ThemeService @inject IThemeService ThemeService
@inject ISystemService SystemService
@inject IStringLocalizer<Add> Localizer @inject IStringLocalizer<Add> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer @inject IStringLocalizer<SharedResources> SharedLocalizer
@ -112,7 +113,7 @@
<div class="row mb-1 align-items-center"> <div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="icon" HelpText="Optionally provide an icon class name for this page which will be displayed in the site navigation" ResourceKey="Icon">Icon: </Label> <Label Class="col-sm-3" For="icon" HelpText="Optionally provide an icon class name for this page which will be displayed in the site navigation" ResourceKey="Icon">Icon: </Label>
<div class="col-sm-8"> <div class="col-sm-8">
<InputList Value="@_icon" ValueChanged="IconChanged" InputValues="@IconList" /> <InputList Value="@_icon" ValueChanged="IconChanged" InputValues="@_icons" />
</div> </div>
<div class="col-sm-1"> <div class="col-sm-1">
<i class="@_icon"></i> <i class="@_icon"></i>
@ -218,7 +219,7 @@
private string _url; private string _url;
private string _ispersonalizable = "False"; private string _ispersonalizable = "False";
private string _title; private string _title;
private string _icon { get; set; } = string.Empty; private string _icon = string.Empty;
private string _themetype = string.Empty; private string _themetype = string.Empty;
private string _containertype = string.Empty; private string _containertype = string.Empty;
private string _headcontent; private string _headcontent;
@ -230,7 +231,7 @@
private RenderFragment ThemeSettingsComponent { get; set; } private RenderFragment ThemeSettingsComponent { get; set; }
private bool _refresh = false; private bool _refresh = false;
protected Page _parent = null; protected Page _parent = null;
protected Dictionary<string, string> IconList = new(); protected Dictionary<string, string> _icons;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
@ -245,20 +246,7 @@
_parentid = _parent.PageId.ToString(); _parentid = _parent.PageId.ToString();
} }
} }
_icons = await SystemService.GetIconsAsync();
Type iconsType = typeof(Icons);
System.Reflection.FieldInfo[] fields = iconsType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField);
foreach (System.Reflection.FieldInfo field in fields)
{
if (field.FieldType == typeof(string))
{
string fieldName = field.Name;
string fieldValue = (string)field.GetValue(null);
IconList.Add(fieldName, fieldValue);
}
}
// if admin or user has edit access to parent page // if admin or user has edit access to parent page
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Admin) || (_parent != null && UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, _parent.PermissionList))) if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Admin) || (_parent != null && UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, _parent.PermissionList)))

View File

@ -5,6 +5,7 @@
@inject IPageService PageService @inject IPageService PageService
@inject IPageModuleService PageModuleService @inject IPageModuleService PageModuleService
@inject IThemeService ThemeService @inject IThemeService ThemeService
@inject ISystemService SystemService
@inject IStringLocalizer<Edit> Localizer @inject IStringLocalizer<Edit> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer @inject IStringLocalizer<SharedResources> SharedLocalizer
@ -124,7 +125,7 @@
<div class="row mb-1 align-items-center"> <div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="icon" HelpText="Optionally provide an icon class name for this page which will be displayed in the site navigation" ResourceKey="Icon">Icon: </Label> <Label Class="col-sm-3" For="icon" HelpText="Optionally provide an icon class name for this page which will be displayed in the site navigation" ResourceKey="Icon">Icon: </Label>
<div class="col-sm-8"> <div class="col-sm-8">
<InputList Value="@_icon" ValueChanged="IconChanged" InputValues="@IconList" /> <InputList Value="@_icon" ValueChanged="IconChanged" InputValues="@_icons" />
</div> </div>
<div class="col-sm-1"> <div class="col-sm-1">
<i class="@_icon"></i> <i class="@_icon"></i>
@ -295,7 +296,7 @@
private string _url; private string _url;
private string _ispersonalizable; private string _ispersonalizable;
private string _title; private string _title;
private string _icon { get; set; } private string _icon;
private string _themetype; private string _themetype;
private string _containertype = "-"; private string _containertype = "-";
private Type _themeSettingsType; private Type _themeSettingsType;
@ -315,7 +316,8 @@
private bool _refresh = false; private bool _refresh = false;
protected Page _page = null; protected Page _page = null;
protected Page _parent = null; protected Page _parent = null;
protected Dictionary<string, string> IconList = new(); protected Dictionary<string, string> _icons;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
try try
@ -323,6 +325,7 @@
_children = PageState.Pages.Where(item => item.ParentId == null).ToList(); _children = PageState.Pages.Where(item => item.ParentId == null).ToList();
_pageId = Int32.Parse(PageState.QueryString["id"]); _pageId = Int32.Parse(PageState.QueryString["id"]);
_page = await PageService.GetPageAsync(_pageId); _page = await PageService.GetPageAsync(_pageId);
_icons = await SystemService.GetIconsAsync();
if (_page != null && UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, _page.PermissionList)) if (_page != null && UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, _page.PermissionList))
{ {
@ -355,20 +358,6 @@
_icon = _page.Icon; _icon = _page.Icon;
_ispersonalizable = _page.IsPersonalizable.ToString(); _ispersonalizable = _page.IsPersonalizable.ToString();
Type iconsType = typeof(Icons);
System.Reflection.FieldInfo[] fields = iconsType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField);
foreach (System.Reflection.FieldInfo field in fields)
{
if (field.FieldType == typeof(string))
{
string fieldName = field.Name;
string fieldValue = (string)field.GetValue(null);
IconList.Add(fieldName, fieldValue);
}
}
// appearance // appearance
_title = _page.Title; _title = _page.Title;
_themetype = _page.ThemeType; _themetype = _page.ThemeType;

View File

@ -9,13 +9,13 @@ namespace Oqtane.Services
public interface ISystemService public interface ISystemService
{ {
/// <summary> /// <summary>
/// returns a key-value directory with the current system configuration information /// returns a key-value dictionary with the current system configuration information
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
Task<Dictionary<string, object>> GetSystemInfoAsync(); Task<Dictionary<string, object>> GetSystemInfoAsync();
/// <summary> /// <summary>
/// returns a key-value directory with the current system information - "environment" or "configuration" /// returns a key-value dictionary with the current system information - "environment" or "configuration"
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
Task<Dictionary<string, object>> GetSystemInfoAsync(string type); Task<Dictionary<string, object>> GetSystemInfoAsync(string type);
@ -32,5 +32,11 @@ namespace Oqtane.Services
/// <param name="settings"></param> /// <param name="settings"></param>
/// <returns></returns> /// <returns></returns>
Task UpdateSystemInfoAsync(Dictionary<string, object> settings); Task UpdateSystemInfoAsync(Dictionary<string, object> settings);
/// <summary>
/// returns a key-value dictionary with default system icons
/// </summary>
/// <returns></returns>
Task<Dictionary<string, string>> GetIconsAsync();
} }
} }

View File

@ -33,5 +33,10 @@ namespace Oqtane.Services
{ {
await PostJsonAsync(Apiurl, settings); await PostJsonAsync(Apiurl, settings);
} }
public async Task<Dictionary<string, string>> GetIconsAsync()
{
return await GetJsonAsync<Dictionary<string, string>>($"{Apiurl}/icons");
}
} }
} }

View File

@ -74,7 +74,6 @@ namespace Oqtane.Controllers
return systeminfo; return systeminfo;
} }
// GET: api/<controller> // GET: api/<controller>
[HttpGet("{key}/{value}")] [HttpGet("{key}/{value}")]
[Authorize(Roles = RoleNames.Host)] [Authorize(Roles = RoleNames.Host)]
@ -94,6 +93,36 @@ namespace Oqtane.Controllers
} }
} }
// GET: api/<controller>/icons
[HttpGet("icons")]
public Dictionary<string, string> Get()
{
var icons = new Dictionary<string, string>();
// use reflection to get list of icons from Icons class
Type iconsType = typeof(Icons);
System.Reflection.FieldInfo[] fields = iconsType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField);
foreach (System.Reflection.FieldInfo field in fields)
{
if (field.FieldType == typeof(string))
{
// add spacing between words based on capitalization
var name = "";
for (int index = 0; index < field.Name.Length; index++)
{
name += ((index > 0 && field.Name[index] == Char.ToUpper(field.Name[index])) ? " " : "") + field.Name[index];
}
string fieldName = name;
string fieldValue = (string)field.GetValue(null);
icons.Add(fieldName, fieldValue);
}
}
return icons;
}
private void UpdateSetting(string key, object value) private void UpdateSetting(string key, object value)
{ {
switch (key.ToLower()) switch (key.ToLower())