Added text area support and DI support for modifier functions
This commit is contained in:
@@ -12,13 +12,15 @@ public class PropertyConfig(PropertyInfo info, TableConfig table, int nthPropert
|
||||
public bool Sortable { get; set; } = true;
|
||||
public bool Searchable { get; set; } = true;
|
||||
public PropertyInfo? DisplayedProperty { get; set; }
|
||||
public Func<object, string>? Formatter { get; set; }
|
||||
public Func<object, string>? EnumerableFormatter { get; set; }
|
||||
public Func<string, object>? Parser { get; set; }
|
||||
public Func<object?, Task<IEnumerable<string>>>? Validator { get; set; }
|
||||
public Func<object, IServiceProvider, string>? Formatter { get; set; }
|
||||
public Func<object, IServiceProvider, string>? EnumerableFormatter { get; set; }
|
||||
public Func<string, IServiceProvider, object>? Parser { get; set; }
|
||||
public Func<object?, IServiceProvider, Task<IEnumerable<string>>>? Validator { get; set; }
|
||||
public bool Editable { get; set; } = true;
|
||||
public bool Creatable { get; set; } = true;
|
||||
public bool DisplayValue { get; set; } = true;
|
||||
public bool TextArea { get; set; }
|
||||
public int TextAreaRows { get; set; } = 16;
|
||||
public bool IsRelation { get; set; }
|
||||
public bool IsRequired { get; set; }
|
||||
public bool IsEnumerable { get; set; }
|
||||
@@ -36,46 +38,46 @@ public class PropertyConfig<TProp>(PropertyConfig config) {
|
||||
|
||||
public PropertyConfig<TProp> List(bool list) {
|
||||
InnerConfig.List = list;
|
||||
InnerConfig.Searchable = false;
|
||||
InnerConfig.Searchable = !list;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> Sortable(bool sortable) {
|
||||
public PropertyConfig<TProp> IsSortable(bool sortable) {
|
||||
InnerConfig.Sortable = sortable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> Searchable(bool searchable) {
|
||||
public PropertyConfig<TProp> IsSearchable(bool searchable) {
|
||||
InnerConfig.Searchable = searchable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> DisplayedProperty<TInnerProp>(Expression<Func<TProp, TInnerProp>> propertyExpression) {
|
||||
public PropertyConfig<TProp> SetDisplayedProperty<TInnerProp>(Expression<Func<TProp, TInnerProp>> propertyExpression) {
|
||||
InnerConfig.DisplayedProperty = TableConfig<TProp>.GetPropertyInfo(propertyExpression);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> Format(Func<TProp, string> formatter) {
|
||||
InnerConfig.Formatter = obj => formatter.Invoke((TProp)obj);
|
||||
public PropertyConfig<TProp> Format(Func<TProp, IServiceProvider, string> formatter) {
|
||||
InnerConfig.Formatter = (obj, provider) => formatter.Invoke((TProp)obj, provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> FormatEach<TInnerProp>(Func<TInnerProp, string> formatter) {
|
||||
InnerConfig.EnumerableFormatter = obj => formatter.Invoke((TInnerProp)obj);
|
||||
public PropertyConfig<TProp> FormatEach<TInnerProp>(Func<TInnerProp, IServiceProvider, string> formatter) {
|
||||
InnerConfig.EnumerableFormatter = (obj, provider) => formatter.Invoke((TInnerProp)obj, provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> ValueParser(Func<string, TProp> parser) {
|
||||
InnerConfig.Parser = str => parser.Invoke(str)!;
|
||||
public PropertyConfig<TProp> SetParser(Func<string, IServiceProvider, TProp> parser) {
|
||||
InnerConfig.Parser = (str, provider) => parser.Invoke(str, provider)!;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> Editable(bool editable) {
|
||||
public PropertyConfig<TProp> SetEditable(bool editable) {
|
||||
InnerConfig.Editable = editable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> Creatable(bool creatable) {
|
||||
public PropertyConfig<TProp> SetCreatable(bool creatable) {
|
||||
InnerConfig.Creatable = creatable;
|
||||
return this;
|
||||
}
|
||||
@@ -85,17 +87,27 @@ public class PropertyConfig<TProp>(PropertyConfig config) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> Validator(Func<TProp?, IEnumerable<string>> validator) {
|
||||
InnerConfig.Validator = obj => Task.FromResult(validator.Invoke((TProp?)obj));
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> Validator(Func<TProp?, Task<IEnumerable<string>>> validator) {
|
||||
InnerConfig.Validator = obj => validator.Invoke((TProp?)obj);
|
||||
public PropertyConfig<TProp> IsTextArea(bool textField) {
|
||||
InnerConfig.TextArea = textField;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> OrderIndex(int index) {
|
||||
public PropertyConfig<TProp> SetTextAreaRows(int rows) {
|
||||
InnerConfig.TextAreaRows = rows;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetValidator(Func<TProp?, IServiceProvider, IEnumerable<string>> validator) {
|
||||
InnerConfig.Validator = (obj, provider) => Task.FromResult(validator.Invoke((TProp?)obj, provider));
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetValidator(Func<TProp?, IServiceProvider, Task<IEnumerable<string>>> validator) {
|
||||
InnerConfig.Validator = (obj, provider) => validator.Invoke((TProp?)obj, provider);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<TProp> SetOrderIndex(int index) {
|
||||
InnerConfig.Order = index;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -69,16 +69,16 @@ public class TableConfig<TModel>(TableConfig config) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public PropertyConfig<string> AddListingProperty(string name, Func<TModel, string> template) {
|
||||
public PropertyConfig<string> AddListingProperty(string name, Func<TModel, IServiceProvider, string> template) {
|
||||
var prop = new PropertyConfig(InnerConfig.Properties.First().Info, InnerConfig, InnerConfig.Properties.Count);
|
||||
prop.Name = name;
|
||||
prop.IsListingProperty = true;
|
||||
prop.Formatter = obj => template.Invoke((TModel)obj);
|
||||
prop.Formatter = (obj, provider) => template.Invoke((TModel)obj, provider);
|
||||
InnerConfig.Properties.Add(prop);
|
||||
return new PropertyConfig<string>(prop);
|
||||
}
|
||||
|
||||
public TableConfig<TModel> AddListingProperty(string name, Func<TModel, string> template, Action<PropertyConfig<string>> configurator) {
|
||||
public TableConfig<TModel> AddListingProperty(string name, Func<TModel, IServiceProvider, string> template, Action<PropertyConfig<string>> configurator) {
|
||||
var prop = AddListingProperty(name, template);
|
||||
configurator.Invoke(prop);
|
||||
return this;
|
||||
@@ -94,7 +94,7 @@ public class TableConfig<TModel>(TableConfig config) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public TableConfig<TModel> OrderIndex(int index) {
|
||||
public TableConfig<TModel> SetOrderIndex(int index) {
|
||||
InnerConfig.Order = index;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider pr
|
||||
if (dbContext is null) return null;
|
||||
|
||||
var type = typeof(TableManager<>).MakeGenericType(table.TableType);
|
||||
return Activator.CreateInstance(type, dbContext, table, this) as ITableManager;
|
||||
return Activator.CreateInstance(type, dbContext, table, this, provider) as ITableManager;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
using HopFrame.Core.Config;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HopFrame.Core.Services.Implementations;
|
||||
|
||||
internal sealed class TableManager<TModel>(DbContext context, TableConfig config, IContextExplorer explorer) : ITableManager where TModel : class {
|
||||
internal sealed class TableManager<TModel>(DbContext context, TableConfig config, IContextExplorer explorer, IServiceProvider provider) : ITableManager where TModel : class {
|
||||
|
||||
public IQueryable<object> LoadPage(int page, int perPage = 20) {
|
||||
var table = context.Set<TModel>();
|
||||
@@ -72,20 +70,20 @@ internal sealed class TableManager<TModel>(DbContext context, TableConfig config
|
||||
if (item is null) return string.Empty;
|
||||
|
||||
if (prop.IsListingProperty)
|
||||
return prop.Formatter!.Invoke(item);
|
||||
return prop.Formatter!.Invoke(item, provider);
|
||||
|
||||
var propValue = value ?? prop.Info.GetValue(item);
|
||||
if (propValue is null)
|
||||
return string.Empty;
|
||||
|
||||
if (prop.Formatter is not null) {
|
||||
return prop.Formatter.Invoke(propValue);
|
||||
return prop.Formatter.Invoke(propValue, provider);
|
||||
}
|
||||
|
||||
if (prop.IsEnumerable) {
|
||||
if (value is not null) {
|
||||
if (prop.EnumerableFormatter is not null) {
|
||||
return prop.EnumerableFormatter.Invoke(value);
|
||||
return prop.EnumerableFormatter.Invoke(value, provider);
|
||||
}
|
||||
|
||||
return value.ToString() ?? string.Empty;
|
||||
|
||||
@@ -36,11 +36,11 @@
|
||||
</div>
|
||||
<div style="display: flex; gap: 5px; margin-bottom: 4px">
|
||||
@if (!property.IsRequired) {
|
||||
<FluentButton OnClick="async () => await SetPropertyValue(property, null, InputType.Relation)" Disabled="@(!property.Editable)">
|
||||
<FluentButton OnClick="async () => await SetPropertyValue(property, null, InputType.Relation)" Disabled="@(_currentlyEditing && !property.Editable)">
|
||||
<FluentIcon Value="@(new Icons.Regular.Size20.Dismiss())" Color="Color.Neutral" />
|
||||
</FluentButton>
|
||||
}
|
||||
<FluentButton OnClick="async () => await OpenRelationalPicker(property)" Disabled="@(!property.Editable)">
|
||||
<FluentButton OnClick="async () => await OpenRelationalPicker(property)" Disabled="@(_currentlyEditing && !property.Editable)">
|
||||
<FluentIcon Value="@(new Icons.Regular.Size20.Open())" Color="Color.Neutral" />
|
||||
</FluentButton>
|
||||
</div>
|
||||
@@ -52,7 +52,7 @@
|
||||
Label="@property.Name"
|
||||
Value="GetPropertyValue<double>(property)"
|
||||
Style="width: 100%;"
|
||||
Disabled="@(!property.Editable)"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Number))" />
|
||||
}
|
||||
@@ -60,7 +60,7 @@
|
||||
<FluentSwitch
|
||||
Label="@property.Name"
|
||||
Value="GetPropertyValue<bool>(property)"
|
||||
Disabled="@(!property.Editable)"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Switch))" />
|
||||
}
|
||||
@@ -70,14 +70,14 @@
|
||||
<FluentDatePicker
|
||||
Label="@property.Name"
|
||||
Value="GetPropertyValue<DateTime>(property)"
|
||||
Disabled="@(!property.Editable)"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Date))" />
|
||||
</div>
|
||||
<div style="display: flex; flex-direction: column; justify-content: flex-end">
|
||||
<FluentTimePicker
|
||||
Value="GetPropertyValue<DateTime>(property)"
|
||||
Disabled="@(!property.Editable)"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Time))" />
|
||||
</div>
|
||||
@@ -88,7 +88,7 @@
|
||||
Label="@property.Name"
|
||||
Value="GetPropertyValue<DateOnly>(property).ToDateTime(TimeOnly.MinValue)"
|
||||
Style="width: 100%"
|
||||
Disabled="@(!property.Editable)"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Date))" />
|
||||
}
|
||||
@@ -97,7 +97,7 @@
|
||||
Label="@property.Name"
|
||||
Value="GetPropertyValue<TimeOnly>(property).ToDateTime()"
|
||||
Style="width: 100%"
|
||||
Disabled="@(!property.Editable)"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Time))" />
|
||||
}
|
||||
@@ -109,7 +109,7 @@
|
||||
Value="@(GetPropertyValue<string>(property))"
|
||||
Style="width: 100%"
|
||||
Height="250px"
|
||||
Disabled="@(!property.Editable)"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Enum))" />
|
||||
}
|
||||
@@ -123,23 +123,33 @@
|
||||
Value="@(GetPropertyValue<string>(property))"
|
||||
Style="width: 100%"
|
||||
Height="250px"
|
||||
Disabled="@(!property.Editable)"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Enum))" />
|
||||
</div>
|
||||
<div style="display: flex; gap: 5px">
|
||||
<FluentButton OnClick="async () => await SetPropertyValue(property, null, InputType.Enum)" Disabled="@(!property.Editable)">
|
||||
<FluentButton OnClick="async () => await SetPropertyValue(property, null, InputType.Enum)" Disabled="@(_currentlyEditing && !property.Editable)">
|
||||
<FluentIcon Value="@(new Icons.Regular.Size20.Dismiss())" Color="Color.Neutral" />
|
||||
</FluentButton>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
else if (property.TextArea) {
|
||||
<FluentTextArea
|
||||
Label="@property.Name"
|
||||
Value="@(GetPropertyValue<string>(property))"
|
||||
Style="width: 100%;"
|
||||
Rows="@property.TextAreaRows"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Text))" />
|
||||
}
|
||||
else {
|
||||
<FluentTextField
|
||||
Label="@property.Name"
|
||||
Value="@(GetPropertyValue<string>(property))"
|
||||
Style="width: 100%;"
|
||||
Disabled="@(!property.Editable)"
|
||||
Disabled="@(_currentlyEditing && !property.Editable)"
|
||||
Required="@property.IsRequired"
|
||||
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Text))" />
|
||||
}
|
||||
@@ -151,9 +161,13 @@
|
||||
}
|
||||
</FluentDialogBody>
|
||||
|
||||
<FluentToastProvider MaxToastCount="10" />
|
||||
|
||||
@inject IContextExplorer Explorer
|
||||
@inject IDialogService Dialogs
|
||||
@inject IHopFrameAuthHandler Handler
|
||||
@inject IToastService Toasts
|
||||
@inject IServiceProvider Provider
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
@@ -217,6 +231,13 @@
|
||||
break;
|
||||
|
||||
case InputType.Text:
|
||||
if (config.Info.PropertyType == typeof(Guid)) {
|
||||
var success = Guid.TryParse((string)value, out var guid);
|
||||
if (success) result = guid;
|
||||
else Toasts.ShowError($"'{value}' is not a valid guid");
|
||||
break;
|
||||
}
|
||||
|
||||
result = Convert.ToString(value);
|
||||
break;
|
||||
|
||||
@@ -254,6 +275,11 @@
|
||||
result = ((IEnumerable)value).OfType<object>().FirstOrDefault();
|
||||
else {
|
||||
needsOverride = false;
|
||||
|
||||
if (!typeof(IList).IsAssignableFrom(config.Info.PropertyType)) {
|
||||
throw new ArgumentException($"Invalid type of '{config.Name}' property in '{config.Table.DisplayName}' table, only list types are supported on enumerable relations.");
|
||||
}
|
||||
|
||||
var asList = (IList)config.Info.GetValue(Content.CurrentObject)!;
|
||||
asList.Clear();
|
||||
foreach (var element in (IEnumerable)value) {
|
||||
@@ -268,7 +294,7 @@
|
||||
}
|
||||
|
||||
if (config.Parser is not null && result is not null) {
|
||||
result = config.Parser(result.ToString()!);
|
||||
result = config.Parser(result.ToString()!, Provider);
|
||||
}
|
||||
|
||||
if (needsOverride)
|
||||
@@ -319,7 +345,7 @@
|
||||
var value = property.Info.GetValue(Content.CurrentObject);
|
||||
|
||||
if (property.Validator is not null) {
|
||||
errorList.AddRange(await property.Validator.Invoke(value));
|
||||
errorList.AddRange(await property.Validator.Invoke(value, Provider));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -273,8 +273,8 @@
|
||||
else DialogData?.SelectedObjects.Add(item);
|
||||
}
|
||||
|
||||
private void SelectAll(bool? selected) {
|
||||
selected = _currentlyDisplayedModels.Any(obj => DialogData?.SelectedObjects.Contains(obj) != true);
|
||||
private void SelectAll() {
|
||||
var selected = _currentlyDisplayedModels.Any(obj => DialogData?.SelectedObjects.Contains(obj) != true);
|
||||
foreach (var displayedModel in _currentlyDisplayedModels) {
|
||||
SelectItem(displayedModel, selected == true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user