using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using OneOf;
namespace AntDesign
{
///
/// Confirm dialog options
///
public class ConfirmOptions : DialogOptionsBase
{
public ConfirmOptions()
{
Width = 416;
Mask = true;
MaskClosable = false;
Locale = LocaleProvider.CurrentLocale.Confirm;
}
#region internal
///
/// OK
///
internal string DefaultBtn1Text { get; } = "OK";
///
/// Cancel
///
internal string DefaultBtn2Text { get; } = "Cancel";
///
/// Ignore
///
internal string DefaultBtn3Text { get; } = "Ignore";
#endregion
///
/// Confirm Locale
///
public ConfirmLocale Locale { get; set; } = LocaleProvider.CurrentLocale.Confirm;
///
/// the class name of the element of ".ant-modal"
///
public string ClassName { get; set; }
///
///
///
public bool Visible { get; set; }
///
/// ChildContent
///
public OneOf Content { get; set; }
///
/// Confirm left top icon
///
public RenderFragment? Icon { get; set; } = null;
///
/// .ant-modal element's style
///
public string Style { get; set; }
///
///
///
public ConfirmAutoFocusButton AutoFocusButton { get; set; } = ConfirmAutoFocusButton.Ok;
///
/// set OK button type for the leftmost button: OK or Yes button
///
public new string OkType
{
get
{
Button1Props ??= new ButtonProps();
return Button1Props.Type;
}
set
{
Button1Props ??= new ButtonProps();
Button1Props.Type = value;
}
}
#region button text
///
/// set OK button content for the leftmost button: OK or Yes button, it will override the ConfirmLocale
///
public new OneOf? OkText { get => Button1Text; set => Button1Text = value; }
///
/// set Cancel button content for the second on the left button: Cancel or NO button, it will override the ConfirmLocale
///
public new OneOf? CancelText { get => Button2Text; set => Button2Text = value; }
///
/// the leftmost button in LTR layout, it will override the ConfirmLocale
///
internal OneOf? Button1Text { get => Button1Props.ChildContent; set => Button1Props.ChildContent = value; }
///
/// The second button on the left is in the LTR layout, it will override the ConfirmLocale
///
internal OneOf? Button2Text { get => Button2Props.ChildContent; set => Button2Props.ChildContent = value; }
///
/// the rightmost button in LTR layout, it will override the ConfirmLocale
///
internal OneOf? Button3Text { get => Button3Props.ChildContent; set => Button3Props.ChildContent = value; }
#endregion
#region button OnClick callback
///
/// for OK-Cancel Confirm dialog, cancel button clicked callback.
/// It's only trigger in Confirm created by ModalService mode
///
public Func OnCancel { get; set; }
///
/// for OK-Cancel Confirm dialog, OK button clicked callback.
/// It's only trigger in Confirm created by ModalService mode
///
public Func OnOk { get; set; }
#endregion
#region button props
///
/// OK-Cancel Confirm dialog's OK button props. It is equivalent to Button1Props.
///
public new ButtonProps OkButtonProps { get => Button1Props; set => Button1Props = value; }
///
/// OK-Cancel Confirm dialog's cancel button props. It is equivalent to Button2Props.
///
public new ButtonProps CancelButtonProps { get => Button2Props; set => Button2Props = value; }
///
/// the leftmost button in LTR layout
///
public ButtonProps Button1Props
{
get => _button1Props;
set => _button1Props = SetButtonProps(value, _button1Props);
}
///
/// The second button on the left is in the LTR layout
///
public ButtonProps Button2Props
{
get => _button2Props;
set => _button2Props = SetButtonProps(value, _button2Props);
}
///
/// the rightmost button in LTR layout
///
public ButtonProps Button3Props
{
get => _button3Props;
set => _button3Props = SetButtonProps(value, _button3Props);
}
private ButtonProps _button1Props = new ButtonProps() { Type = ButtonType.Primary };
private ButtonProps _button2Props = new ButtonProps();
private ButtonProps _button3Props = new ButtonProps();
#endregion
#region Confirm buttons config
///
/// show Cancel button for OK-Cancel Confirm dialog
///
public bool OkCancel
{
get => ConfirmButtons != ConfirmButtons.OK;
set
{
ConfirmButtons = !value ? ConfirmButtons.OK : ConfirmButtons.OKCancel;
}
}
internal ConfirmButtons ConfirmButtons { get; set; } = ConfirmButtons.OKCancel;
#endregion
#region config confirm icon style
internal string ConfirmType { get; set; } = "confirm";
private ConfirmIcon _confirmIcon;
internal ConfirmIcon ConfirmIcon
{
get => _confirmIcon;
set
{
_confirmIcon = value;
Icon = ConfirmIconRenderFragments.GetByConfirmIcon(value);
if (value == ConfirmIcon.None)
{
ConfirmType = "confirm";
}
else
{
ConfirmType = value.ToString().ToLower(CultureInfo.CurrentUICulture);
}
}
}
#endregion
private static ButtonProps SetButtonProps(ButtonProps newProps, ButtonProps oldProps)
{
if (newProps == null || newProps.ChildContent.HasValue)
{
return newProps;
}
if (oldProps != null)
{
newProps.ChildContent = oldProps.ChildContent;
}
return newProps;
}
///
/// set default options for buttons
///
internal void BuildButtonsDefaultOptions()
{
// config default button text
switch (ConfirmButtons)
{
case ConfirmButtons.OK:
{
this.Button1Text ??= Locale.OkText;
break;
}
case ConfirmButtons.OKCancel:
{
this.Button1Text ??= Locale.OkText;
this.Button2Text ??= Locale.CancelText;
break;
}
case ConfirmButtons.YesNo:
case ConfirmButtons.YesNoCancel:
{
this.Button1Text ??= Locale.YesText;
this.Button2Text ??= Locale.NoText;
if (ConfirmButtons == ConfirmButtons.YesNoCancel)
{
this.Button3Text ??= Locale.CancelText;
// config button2 default type
this.Button2Props.Danger ??= true;
}
break;
}
case ConfirmButtons.RetryCancel:
{
this.Button1Text ??= Locale.RetryText;
this.Button2Text ??= Locale.CancelText;
break;
}
case ConfirmButtons.AbortRetryIgnore:
{
this.Button1Text ??= Locale.AbortText;
this.Button2Text ??= Locale.RetryText;
this.Button3Text ??= Locale.IgnoreText;
// config button2 default type
this.Button2Props.Danger ??= true;
break;
}
default:
break;
}
}
}
}