@implements IDialogContentComponent @rendermode InteractiveServer @using HopFrame.Core.Config @using HopFrame.Core.Services @using HopFrame.Web.Models @using HopFrame.Web.Helpers @using Microsoft.EntityFrameworkCore.Internal @foreach (var property in Content.Config.Properties) { if (!_currentlyEditing && !property.Creatable) continue;
@if (property.IsRelation) {
} else if (property.Info.PropertyType.IsNumeric()) { } else if (Type.GetTypeCode(property.Info.PropertyType) == TypeCode.Boolean) { } else if (Type.GetTypeCode(property.Info.PropertyType) == TypeCode.DateTime) {
} else if (property.Info.PropertyType == typeof(DateOnly)) { } else if (property.Info.PropertyType == typeof(TimeOnly)) { } else if (property.Info.PropertyType.IsEnum) { } else { }
}
@inject IContextExplorer Explorer @inject IDialogService Dialogs @code { [Parameter] public required EditorDialogData Content { get; set; } [CascadingParameter] public required FluentDialog Dialog { get; set; } private bool _currentlyEditing; private ITableManager? _manager; protected override void OnInitialized() { _currentlyEditing = Content.CurrentObject is not null; Dialog.Instance.Parameters.Title = (_currentlyEditing ? "Edit " : "Add ") + Content.Config.TableType.Name; Dialog.Instance.Parameters.Width = "500px"; Dialog.Instance.Parameters.PrimaryAction = "Save"; _manager = Explorer.GetTableManager(Content.Config.PropertyName); Content.CurrentObject ??= Activator.CreateInstance(Content.Config.TableType); } private TValue? GetPropertyValue(PropertyConfig config) { if (!config.DisplayValue) return default; if (Content.CurrentObject is null) return default; var value = config.Info.GetValue(Content.CurrentObject); var newlyGenerated = false; if (config.Info.PropertyType.IsDefaultValue(value) && config.Template is not null) { value = config.Template.Invoke(); newlyGenerated = true; } if (value is null) return default; if (config.Info.PropertyType == typeof(TValue)) return (TValue)value; if (typeof(TValue) == typeof(string)) { if (!newlyGenerated) return (TValue)(object)_manager!.DisplayProperty(Content.CurrentObject, config.Info, Content.Config); return (TValue)(object)value.ToString()!; } return (TValue)Convert.ChangeType(value, typeof(TValue)); } private void SetPropertyValue(PropertyConfig config, object? value, InputType senderType) { object? result = null; if (value is not null) { switch (senderType) { case InputType.Number: result = Convert.ChangeType(value, config.Info.PropertyType); break; case InputType.Text: result = Convert.ToString(value); break; case InputType.Switch: result = Convert.ToBoolean(value); break; case InputType.Enum: result = Enum.Parse(config.Info.PropertyType, (string)value); break; case InputType.Date: if (config.Info.PropertyType == typeof(DateTime)) { var newDate = (DateOnly)value; var dateTime = GetPropertyValue(config); result = new DateTime(newDate.Year, newDate.Month, newDate.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond, dateTime.Microsecond); } else result = (DateOnly)value; break; case InputType.Time: if (config.Info.PropertyType == typeof(DateTime)) { var newTime = (TimeOnly)value; var dateTime = GetPropertyValue(config); result = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, newTime.Hour, newTime.Minute, newTime.Second, newTime.Millisecond, newTime.Microsecond); } else result = (TimeOnly)value; break; case InputType.Relation: result = value; break; default: throw new ArgumentOutOfRangeException(nameof(senderType), senderType, null); } } if (config.Parser is not null && result is not null) { result = config.Parser(result.ToString()!); } config.Info.SetValue(Content.CurrentObject, result); } private async Task OpenRelationalPicker(PropertyConfig config) { var relationTable = Explorer.GetTable(config.Info.PropertyType); if (relationTable is null) return; var currentValue = config.Info.GetValue(Content.CurrentObject); var dialog = await Dialogs.ShowDialogAsync(new RelationPickerDialogData(relationTable, currentValue), new DialogParameters()); var result = await dialog.Result; if (result.Cancelled) return; var data = (RelationPickerDialogData)result.Data!; SetPropertyValue(config, data.Object, InputType.Relation); } private enum InputType { Number, Switch, Date, Time, Enum, Text, Relation } }