125 lines
3.2 KiB
C#
125 lines
3.2 KiB
C#
using Microsoft.UI.Xaml;
|
|
using SQLVision.Core.Enums;
|
|
using SQLVision.Core.Interfaces;
|
|
using SQLVision.Core.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace SQLVision.UI.ViewModels;
|
|
|
|
public class ParameterViewModel : ObservableObject
|
|
{
|
|
private readonly IControlFactory _controlFactory;
|
|
private FrameworkElement _control;
|
|
private object _value;
|
|
private bool _isEnabled = true;
|
|
private string _validationError;
|
|
|
|
public ScriptParameter Parameter { get; }
|
|
public string Name => Parameter.Name;
|
|
public string DisplayName => Parameter.DisplayName ?? Parameter.Name;
|
|
public string Description => Parameter.Description;
|
|
public ParameterType Type => Parameter.Type;
|
|
|
|
public FrameworkElement Control
|
|
{
|
|
get
|
|
{
|
|
if (_control == null)
|
|
{
|
|
_control = _controlFactory.CreateControl(Parameter, OnValueChanged);
|
|
}
|
|
return _control;
|
|
}
|
|
}
|
|
|
|
public object Value
|
|
{
|
|
get => _value ?? Parameter.DefaultValue;
|
|
set
|
|
{
|
|
if (SetProperty(ref _value, value))
|
|
{
|
|
ValidateValue();
|
|
ValueChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsEnabled
|
|
{
|
|
get => _isEnabled;
|
|
set => SetProperty(ref _isEnabled, value);
|
|
}
|
|
|
|
public string ValidationError
|
|
{
|
|
get => _validationError;
|
|
private set => SetProperty(ref _validationError, value);
|
|
}
|
|
|
|
public bool HasError => !string.IsNullOrEmpty(ValidationError);
|
|
|
|
public event EventHandler ValueChanged;
|
|
|
|
public ParameterViewModel(ScriptParameter parameter, IControlFactory controlFactory)
|
|
{
|
|
Parameter = parameter ?? throw new ArgumentNullException(nameof(parameter));
|
|
_controlFactory = controlFactory ?? throw new ArgumentNullException(nameof(controlFactory));
|
|
|
|
_value = parameter.DefaultValue;
|
|
}
|
|
|
|
private void OnValueChanged(object value)
|
|
{
|
|
Value = value;
|
|
}
|
|
|
|
private void ValidateValue()
|
|
{
|
|
if (Parameter.IsValid(Value))
|
|
{
|
|
ValidationError = null;
|
|
}
|
|
else
|
|
{
|
|
ValidationError = "Неверное значение параметра";
|
|
}
|
|
}
|
|
|
|
public void UpdateDependencies(Dictionary<string, object> currentValues)
|
|
{
|
|
if (string.IsNullOrEmpty(Parameter.DependsOn))
|
|
{
|
|
IsEnabled = true;
|
|
return;
|
|
}
|
|
|
|
if (!currentValues.TryGetValue(Parameter.DependsOn, out var dependencyValue))
|
|
{
|
|
IsEnabled = false;
|
|
return;
|
|
}
|
|
|
|
// Проверка условий зависимости
|
|
if (Parameter.DependencyValues != null)
|
|
{
|
|
var isEnabled = Parameter.DependencyValues
|
|
.Any(kvp => object.Equals(kvp.Value, dependencyValue));
|
|
|
|
IsEnabled = isEnabled;
|
|
}
|
|
else
|
|
{
|
|
IsEnabled = dependencyValue != null &&
|
|
!string.IsNullOrWhiteSpace(dependencyValue.ToString());
|
|
}
|
|
|
|
if (!IsEnabled)
|
|
{
|
|
Value = null;
|
|
}
|
|
}
|
|
}
|