Initial commit
This commit is contained in:
@@ -0,0 +1,314 @@
|
||||
@using MudBlazor
|
||||
@using RobotNet10.ScriptEditor.Models
|
||||
@using RobotNet10.ScriptEngine.Shared
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">Create Mission: @MissionName</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
@if (Parameters == null || Parameters.Length == 0)
|
||||
{
|
||||
<MudText Typo="Typo.body2">This mission has no parameters.</MudText>
|
||||
}
|
||||
else
|
||||
{
|
||||
@foreach (var param in Parameters)
|
||||
{
|
||||
@switch (param.Type)
|
||||
{
|
||||
case "System.Boolean":
|
||||
<MudSwitch T="bool" Color="Color.Primary" @bind-Value="@param.BoolValue" />
|
||||
break;
|
||||
case "System.Byte":
|
||||
<MudNumericField T="byte"
|
||||
@bind-Value="@param.ByteValue"
|
||||
Min="@System.Byte.MinValue"
|
||||
Max="@System.Byte.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.SByte":
|
||||
<MudNumericField T="sbyte"
|
||||
@bind-Value="@param.SByteValue"
|
||||
Min="@System.SByte.MinValue"
|
||||
Max="@System.SByte.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Int16":
|
||||
<MudNumericField T="short"
|
||||
@bind-Value="@param.ShortValue"
|
||||
Min="@System.Int16.MinValue"
|
||||
Max="@System.Int16.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.UInt16":
|
||||
<MudNumericField T="ushort"
|
||||
@bind-Value="@param.UShortValue"
|
||||
Min="@System.UInt16.MinValue"
|
||||
Max="@System.UInt16.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Int32":
|
||||
<MudNumericField T="int"
|
||||
@bind-Value="@param.IntValue"
|
||||
Min="@System.Int32.MinValue"
|
||||
Max="@System.Int32.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.UInt32":
|
||||
<MudNumericField T="uint"
|
||||
@bind-Value="@param.UIntValue"
|
||||
Min="@System.UInt32.MinValue"
|
||||
Max="@System.UInt32.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Int64":
|
||||
<MudNumericField T="long"
|
||||
@bind-Value="@param.LongValue"
|
||||
Min="@System.Int64.MinValue"
|
||||
Max="@System.Int64.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.UInt64":
|
||||
<MudNumericField T="ulong"
|
||||
@bind-Value="@param.ULongValue"
|
||||
Min="@System.UInt64.MinValue"
|
||||
Max="@System.UInt64.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Single":
|
||||
<MudNumericField T="float"
|
||||
@bind-Value="@param.FloatValue"
|
||||
Min="@System.Single.MinValue"
|
||||
Max="@System.Single.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Double":
|
||||
<MudNumericField T="double"
|
||||
@bind-Value="@param.DoubleValue"
|
||||
Min="@System.Double.MinValue"
|
||||
Max="@System.Double.MaxValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Decimal":
|
||||
<MudNumericField T="double"
|
||||
@bind-Value="@param.DecimalValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.String":
|
||||
<MudTextField @bind-Value="@param.StringValue"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Char":
|
||||
<MudTextField @bind-Value="@param.CharValue"
|
||||
MaxLength="1"
|
||||
Label="@param.Type"
|
||||
Error="@(!string.IsNullOrEmpty(param.Errors))"
|
||||
ErrorText="@param.Errors"
|
||||
ShrinkLabel="true"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense" />
|
||||
break;
|
||||
case "System.Threading.CancellationToken":
|
||||
<span>
|
||||
<CancellationToken>
|
||||
</span>
|
||||
break;
|
||||
default:
|
||||
<MudAlert Severity="Severity.Error" Dense>Unsupport parameter with type @param.Type</MudAlert>
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="Cancel">Cancel</MudButton>
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="Submit" Disabled="@(!IsValid)">Create</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter] IMudDialogInstance Dialog { get; set; } = null!;
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public ScriptMissionDto Model { get; set; } = default!;
|
||||
|
||||
public string MissionName => Model.Name;
|
||||
private ScriptMissionParameterValueModel[] Parameters = [];
|
||||
private bool IsValid => Parameters.All(p => string.IsNullOrEmpty(p.Errors));
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
base.OnAfterRender(firstRender);
|
||||
if (firstRender)
|
||||
{
|
||||
Parameters = [.. Model.Parameters.Select(p => new ScriptMissionParameterValueModel(p.Name, p.Type, p.Default ?? ""))];
|
||||
|
||||
for (int i = 0; i < Parameters.Length; i++)
|
||||
{
|
||||
ValidateParameter(i);
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private bool HasDefaultValue(int index)
|
||||
{
|
||||
if (index < 0 || index >= Parameters.Length) return false;
|
||||
return !string.IsNullOrEmpty(Parameters[index].Default);
|
||||
}
|
||||
|
||||
private string GetHelperText(string typeName)
|
||||
{
|
||||
var type = ScriptHelpers.ResolveTypeFromString(typeName);
|
||||
if (type == null) return "";
|
||||
|
||||
return type switch
|
||||
{
|
||||
_ when type == typeof(bool) => "Enter 'true' or 'false'",
|
||||
_ when type == typeof(int) => "Enter an integer value",
|
||||
_ when type == typeof(long) => "Enter a long integer value",
|
||||
_ when type == typeof(float) => "Enter a float value (e.g., 3.14f)",
|
||||
_ when type == typeof(double) => "Enter a double value (e.g., 3.14)",
|
||||
_ when type == typeof(decimal) => "Enter a decimal value (e.g., 3.14)",
|
||||
_ when type == typeof(char) => "Enter a single character",
|
||||
_ when type == typeof(string) => "Enter a string value",
|
||||
_ when type.IsEnum => $"Enter one of: {string.Join(", ", Enum.GetNames(type))}",
|
||||
_ => "Enter a valid value"
|
||||
};
|
||||
}
|
||||
|
||||
private void ValidateParameter(int index)
|
||||
{
|
||||
if (index < 0 || index >= Parameters.Length) return;
|
||||
if(Parameters[index].Type == "System.Threading.CancellationToken")
|
||||
{
|
||||
// No validation needed
|
||||
return;
|
||||
}
|
||||
|
||||
Parameters[index].Errors = string.Empty;
|
||||
var param = Parameters[index];
|
||||
var value = Parameters[index].ToString();
|
||||
|
||||
// Allow empty if has default value
|
||||
if (string.IsNullOrWhiteSpace(value) && !string.IsNullOrEmpty(param.Default))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(value) && string.IsNullOrEmpty(param.Default))
|
||||
{
|
||||
Parameters[index].Errors = "Value is required";
|
||||
return;
|
||||
}
|
||||
|
||||
var type = ScriptHelpers.ResolveTypeFromString(param.Type);
|
||||
if (type == null)
|
||||
{
|
||||
Parameters[index].Errors = "Unknown type";
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (type.IsEnum)
|
||||
{
|
||||
Enum.Parse(type, value.Trim(), ignoreCase: true);
|
||||
}
|
||||
else if (!ScriptHelpers.ResolveValueFromString(value.Trim(), type, out _))
|
||||
{
|
||||
Parameters[index].Errors = $"Invalid value for type {param.Type}";
|
||||
}
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
Parameters[index].Errors = $"Invalid value for type {param.Type}";
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Parameters[index].Errors = $"Failed to validate value for type {param.Type}";
|
||||
}
|
||||
}
|
||||
|
||||
private void Cancel() => Dialog.Cancel();
|
||||
|
||||
private void Submit()
|
||||
{
|
||||
if (!IsValid) return;
|
||||
|
||||
var parameters = new Dictionary<string, string>();
|
||||
for (int i = 0; i < Parameters.Length; i++)
|
||||
{
|
||||
var value = Parameters[i].ToString();
|
||||
// Use default if empty and default exists
|
||||
if (string.IsNullOrWhiteSpace(value) && !string.IsNullOrEmpty(Parameters[i].Default))
|
||||
{
|
||||
value = Parameters[i].Default;
|
||||
}
|
||||
parameters[Parameters[i].Name] = value ?? string.Empty;
|
||||
}
|
||||
|
||||
Dialog.Close(DialogResult.Ok(parameters));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user