298 lines
15 KiB
C#
298 lines
15 KiB
C#
using DevExpress.Blazor;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq.Expressions;
|
|
using TIAM.Entities.TransferDestinations;
|
|
using TIAMWebApp.Shared.Application.Utility;
|
|
using TIAMWebApp.Shared.Application.Models.ClientSide.UI.WizardModels;
|
|
using TIAMWebApp.Shared.Application.Models.PageModels;
|
|
using DevExpress.Pdf.Native.BouncyCastle.Asn1.X509.Qualified;
|
|
|
|
|
|
namespace TIAMSharedUI.Pages.Components
|
|
{
|
|
public partial class InputWizard : ComponentBase
|
|
{
|
|
[Inject]
|
|
public required LogToBrowserConsole LogToBrowserConsole { get; set; }
|
|
|
|
public Dictionary<int, Guid> FormSteps { get; set; } = new Dictionary<int, Guid>();
|
|
public int CurrentStep { get; set; } = 0;
|
|
|
|
|
|
//TestUserData Data { get; set; } = new TestUserData();
|
|
|
|
[Parameter]
|
|
public object Data { get; set; } = new object();
|
|
|
|
[Parameter]
|
|
public EventCallback<object> OnSubmit { get; set; }
|
|
|
|
|
|
string _phoneMask = "(999)000-0000";
|
|
string _formSubmitResult = "";
|
|
private string _spinnerClass = "";
|
|
|
|
async Task HandleValidSubmit()
|
|
{
|
|
_spinnerClass = "spinner-border spinner-border-sm";
|
|
await Task.Delay(500);
|
|
_formSubmitResult = "You have been registred successully.";
|
|
_spinnerClass = "";
|
|
|
|
await OnSubmit.InvokeAsync(Data);
|
|
|
|
}
|
|
void HandleInvalidSubmit()
|
|
{
|
|
_formSubmitResult = "Please correct all errors";
|
|
}
|
|
|
|
public void OnNext(MouseEventArgs args)
|
|
{
|
|
LogToBrowserConsole.LogToBC("OnNext called");
|
|
CurrentStep++;
|
|
}
|
|
|
|
public void OnPrevious(MouseEventArgs args)
|
|
{
|
|
LogToBrowserConsole.LogToBC("OnPrev called");
|
|
CurrentStep--;
|
|
}
|
|
|
|
public RenderFragment CreateEditFormFields() => formLayoutBuilder =>
|
|
{
|
|
|
|
var _type = Data.GetType();
|
|
LogToBrowserConsole.LogToBC("Hellooooo " + _type.AssemblyQualifiedName);
|
|
|
|
var propertyList = _type.GetProperties();
|
|
//var propertyList = typeof(TestUserData).GetProperties();
|
|
formLayoutBuilder.OpenComponent<DxFormLayout>(0);
|
|
|
|
formLayoutBuilder.AddAttribute(1, "ChildContent", (RenderFragment)((layoutItemBuilder) =>
|
|
{
|
|
int i = 0;
|
|
int j = 0;
|
|
foreach (var property in propertyList)
|
|
{
|
|
|
|
//if (property.Name == "Id" || property.Name == "Latitude" || property.Name == "Longitude" || property.Name == "Created" || property.Name == "Modified")
|
|
if (property.Name == "Id" || property.Name == "Created" || property.Name == "Modified")
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Guid _stepID = Guid.Empty;
|
|
|
|
if (!FormSteps.ContainsKey(j))
|
|
{
|
|
_stepID = Guid.NewGuid();
|
|
FormSteps.Add(j, _stepID);
|
|
}
|
|
else
|
|
{
|
|
_stepID = FormSteps[j];
|
|
}
|
|
|
|
var access = Expression.Property(Expression.Constant(Data), property.Name);
|
|
var lambda = Expression.Lambda(typeof(Func<>).MakeGenericType(property.PropertyType), access);
|
|
|
|
LogToBrowserConsole.LogToBC(lambda.ToString());
|
|
|
|
layoutItemBuilder.OpenElement(i++, "div");//open div
|
|
layoutItemBuilder.AddAttribute(i++, "id", _stepID.ToString());
|
|
layoutItemBuilder.AddAttribute(i++, "class", "disply-flex align-items-center");
|
|
layoutItemBuilder.AddAttribute(i++, "style", "width: 100%;");
|
|
if (j != CurrentStep)
|
|
layoutItemBuilder.AddAttribute(i++, "hidden", "true");
|
|
|
|
DataTypeAttribute attrList = (DataTypeAttribute)property.GetCustomAttributes(typeof(DataTypeAttribute), false).First();
|
|
DisplayAttribute displayLabel = (DisplayAttribute)property.GetCustomAttributes(typeof(DisplayAttribute), false).First();
|
|
layoutItemBuilder.OpenComponent<DxFormLayoutItem>(i++); //open dxformlayoutitem
|
|
layoutItemBuilder.AddAttribute(i++, "Caption", displayLabel.Name);
|
|
layoutItemBuilder.AddAttribute(i++, "ColSpanMd", 12);
|
|
//layoutItemBuilder.AddAttribute(i++, "CssClass", "form-field");
|
|
|
|
layoutItemBuilder.AddAttribute(i++, "Template", (RenderFragment<object>)((context) => ((editor) =>
|
|
{
|
|
var j = 0;
|
|
switch (attrList.DataType)
|
|
{
|
|
|
|
case DataType.Text:
|
|
{
|
|
editor.OpenComponent<DxTextBox>(j++);
|
|
LogToBrowserConsole.LogToBC($"{property.Name}, {property.PropertyType}");
|
|
editor.AddAttribute(j++, "Text", property.GetValue(Data));
|
|
editor.AddAttribute(j++, "TextExpression", lambda);
|
|
editor.AddAttribute(j++, "CssClass", "form-field");
|
|
editor.AddAttribute(j++, "TextChanged", EventCallback.Factory.Create<string>(this, str => { property.SetValue(Data, str); }));
|
|
editor.CloseComponent();
|
|
break;
|
|
}
|
|
case DataType.Password:
|
|
{
|
|
|
|
editor.OpenComponent<DxTextBox>(j++);
|
|
editor.AddAttribute(j++, "Password", true);
|
|
editor.AddAttribute(j++, "NullText", "Password");
|
|
editor.AddAttribute(j++, "Text", property.GetValue(Data));
|
|
editor.AddAttribute(j++, "TextExpression", lambda);
|
|
editor.AddAttribute(j++, "TextChanged", EventCallback.Factory.Create<string>(this, str => { property.SetValue(Data, str); }));
|
|
editor.CloseComponent();
|
|
break;
|
|
}
|
|
case DataType.PhoneNumber:
|
|
{
|
|
editor.OpenComponent<DxMaskedInput<string>>(j++);
|
|
editor.AddAttribute(j++, "Value", property.GetValue(Data));
|
|
editor.AddAttribute(j++, "Mask", _phoneMask);
|
|
editor.AddAttribute(j++, "ValueExpression", lambda);
|
|
editor.AddAttribute(j++, "ValueChanged", EventCallback.Factory.Create<string>(this, str => { property.SetValue(Data, str); }));
|
|
editor.CloseComponent();
|
|
break;
|
|
}
|
|
case DataType.Date:
|
|
{
|
|
editor.OpenComponent<DxDateEdit<DateTime>>(j);
|
|
editor.AddAttribute(j++, "Date", property.GetValue(Data));
|
|
editor.AddAttribute(j++, "DateExpression", lambda);
|
|
editor.AddAttribute(j++, "DateChanged", EventCallback.Factory.Create<DateTime>(this, str => { property.SetValue(Data, str); }));
|
|
editor.CloseComponent();
|
|
break;
|
|
|
|
}
|
|
case DataType.Custom:
|
|
{
|
|
if (property.PropertyType == typeof(double))
|
|
{
|
|
|
|
editor.OpenComponent<DxMaskedInput<double>>(j);
|
|
|
|
editor.AddAttribute(j++, "Value", property.GetValue(Data));
|
|
|
|
editor.AddAttribute(j++, "Mask", "n6");
|
|
editor.AddAttribute(j++, "ValueExpression", lambda);
|
|
editor.AddAttribute(j++, "CssClass", "form-field");
|
|
editor.AddAttribute(j++, "ValueChanged", EventCallback.Factory.Create<double>(this, str => { property.SetValue(Data, str); }));
|
|
editor.CloseComponent();
|
|
break;
|
|
}
|
|
else if (property.PropertyType == typeof(int))
|
|
{
|
|
|
|
editor.OpenComponent<DxMaskedInput<int>>(j);
|
|
|
|
editor.AddAttribute(j++, "Value", property.GetValue(Data));
|
|
|
|
editor.AddAttribute(j++, "Mask", "n0");
|
|
editor.AddAttribute(j++, "ValueExpression", lambda);
|
|
editor.AddAttribute(j++, "CssClass", "form-field");
|
|
editor.AddAttribute(j++, "ValueChanged", EventCallback.Factory.Create<int>(this, str => { property.SetValue(Data, str); }));
|
|
editor.CloseComponent();
|
|
break;
|
|
}
|
|
else if (property.PropertyType == typeof(string) && property.Name == "Occupation")
|
|
{
|
|
|
|
editor.OpenComponent<DxComboBox<string, string>>(j);
|
|
editor.AddAttribute(j++, "Data", AdditionalData.Occupations);
|
|
editor.AddAttribute(j++, "Value", property.GetValue(Data));
|
|
editor.AddAttribute(j++, "ValueExpression", lambda);
|
|
editor.AddAttribute(j++, "ValueChanged", EventCallback.Factory.Create<string>(this, str => { property.SetValue(Data, str); }));
|
|
editor.CloseComponent();
|
|
break;
|
|
}
|
|
else if (property.PropertyType == typeof(TransferDestination))
|
|
{
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
case DataType.MultilineText:
|
|
{
|
|
editor.OpenComponent<DxMemo>(j);
|
|
editor.AddAttribute(j++, "Text", property.GetValue(Data));
|
|
editor.AddAttribute(j++, "TextExpression", lambda);
|
|
editor.AddAttribute(j++, "CssClass", "form-field");
|
|
editor.AddAttribute(j++, "TextChanged", EventCallback.Factory.Create<string>(this, str => { property.SetValue(Data, str); }));
|
|
editor.CloseComponent();
|
|
break;
|
|
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
})));
|
|
|
|
layoutItemBuilder.CloseComponent(); //close dxformlayoutitem
|
|
if (j < propertyList.Length - 1)
|
|
{
|
|
layoutItemBuilder.OpenComponent<DxButton>(i++);
|
|
layoutItemBuilder.AddAttribute(i++, "Click", EventCallback.Factory.Create<MouseEventArgs>(this, OnNext));
|
|
layoutItemBuilder.AddAttribute(i++, "SubmitFormOnClick", false);
|
|
layoutItemBuilder.AddAttribute(i++, "CssClass", "btn btn-primary mt-3");
|
|
layoutItemBuilder.AddAttribute(i++, "Text", "Next");
|
|
layoutItemBuilder.CloseComponent();
|
|
}
|
|
if (j > 0)
|
|
{
|
|
layoutItemBuilder.OpenComponent<DxButton>(i++);
|
|
layoutItemBuilder.AddAttribute(i++, "Click", EventCallback.Factory.Create<MouseEventArgs>(this, OnPrevious));
|
|
layoutItemBuilder.AddAttribute(i++, "SubmitFormOnClick", false);
|
|
layoutItemBuilder.AddAttribute(i++, "CssClass", "btn btn-secondary mt-3");
|
|
layoutItemBuilder.AddAttribute(i++, "Text", "Previous");
|
|
layoutItemBuilder.CloseComponent();
|
|
}
|
|
layoutItemBuilder.CloseElement(); //close div
|
|
|
|
|
|
layoutItemBuilder.OpenElement(i++, "div");
|
|
layoutItemBuilder.AddAttribute(i++, "class", "text-danger");
|
|
layoutItemBuilder.OpenComponent(i++, typeof(ValidationMessage<>).MakeGenericType(property.PropertyType));
|
|
layoutItemBuilder.AddAttribute(i++, "For", lambda);
|
|
layoutItemBuilder.CloseComponent();
|
|
layoutItemBuilder.CloseElement();
|
|
|
|
|
|
LogToBrowserConsole.LogToBC($"loop {j}, {propertyList.Length}");
|
|
j++;
|
|
|
|
}
|
|
layoutItemBuilder.OpenComponent<DxFormLayoutItem>(i++);
|
|
layoutItemBuilder.AddAttribute(i++, "Template", (RenderFragment<object>)((context) => ((editor) =>
|
|
{
|
|
LogToBrowserConsole.LogToBC($"Submit button {CurrentStep}, {propertyList.Length}");
|
|
editor.OpenComponent<DxButton>(i++);
|
|
editor.AddAttribute(i++, "SubmitFormOnClick", true);
|
|
editor.AddAttribute(i++, "Text", "Submit");
|
|
editor.AddAttribute(i++, "CssClass", "btn btn-primary mt-3");
|
|
if (CurrentStep == propertyList.Length-1)
|
|
{
|
|
editor.AddAttribute(i++, "Visible", true);
|
|
}
|
|
else
|
|
{
|
|
editor.AddAttribute(i++, "Visible", false);
|
|
}
|
|
editor.OpenElement(i++, "span");
|
|
editor.AddAttribute(i++, "class", _spinnerClass);
|
|
editor.CloseElement();
|
|
editor.CloseComponent();
|
|
})));
|
|
|
|
layoutItemBuilder.CloseComponent();
|
|
}));
|
|
formLayoutBuilder.CloseComponent();
|
|
};
|
|
|
|
|
|
|
|
}
|
|
}
|