using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
///
/// A base class for configuration options.
///
public abstract partial class ConfigurationBase : MSGComponentBase
{
///
/// The description of the option, i.e., the name. Should be
/// as short as possible.
///
[Parameter]
public string OptionDescription { get; set; } = string.Empty;
///
/// A helpful text that explains the option in more detail.
///
[Parameter]
public string OptionHelp { get; set; } = string.Empty;
///
/// Is the option disabled?
///
[Parameter]
public Func Disabled { get; set; } = () => false;
///
/// Is the option locked by a configuration plugin?
///
[Parameter]
public Func IsLocked { get; set; } = () => false;
///
/// Should the option be stretched to fill the available space?
///
protected abstract bool Stretch { get; }
///
/// The CSS class to apply to the component.
///
protected virtual string GetClassForBase => string.Empty;
///
/// The visual variant of the option.
///
protected virtual Variant Variant => Variant.Text;
///
/// The label to display for the option.
///
protected virtual string Label => string.Empty;
private StretchItems StretchItems => this.Stretch ? StretchItems.End : StretchItems.None;
protected bool IsDisabled => this.Disabled() || this.IsLocked();
private string Classes => $"{this.GetClassForBase} {MARGIN_CLASS}";
private protected virtual RenderFragment? Body => null;
private const string MARGIN_CLASS = "mb-6";
protected static readonly Dictionary SPELLCHECK_ATTRIBUTES = new();
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
this.SettingsManager.InjectSpellchecking(SPELLCHECK_ATTRIBUTES);
this.ApplyFilters([], [ Event.CONFIGURATION_CHANGED ]);
await base.OnInitializedAsync();
}
#endregion
private string TB(string fallbackEN) => this.T(fallbackEN, typeof(ConfigurationBase).Namespace, nameof(ConfigurationBase));
protected async Task InformAboutChange() => await this.MessageBus.SendMessage(this, Event.CONFIGURATION_CHANGED);
#region Overrides of MSGComponentBase
protected override Task ProcessIncomingMessage(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
{
switch (triggeredEvent)
{
case Event.CONFIGURATION_CHANGED:
this.StateHasChanged();
break;
}
return Task.CompletedTask;
}
#endregion
}