using Connected.Annotations;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace Connected.Components;
public partial class MessageBox : UIComponent
{
[Inject] private IDialogService DialogService { get; set; }
[CascadingParameter] private DialogInstance DialogInstance { get; set; }
///
/// The message box title. If null or empty, title will be hidden
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public string Title { get; set; }
///
/// Define the message box title as a renderfragment (overrides Title)
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public RenderFragment TitleContent { get; set; }
///
/// The message box message as string.
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public string Message { get; set; }
///
/// The message box message as markup string.
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public MarkupString MarkupMessage { get; set; }
///
/// Define the message box body as a renderfragment (overrides Message)
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public RenderFragment MessageContent { get; set; }
///
/// Text of the cancel button. Leave null to hide the button.
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public string CancelText { get; set; }
///
/// Define the cancel button as a render fragment (overrides CancelText).
/// Must be a MudButton
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public RenderFragment CancelButton { get; set; }
///
/// Text of the no button. Leave null to hide the button.
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public string NoText { get; set; }
///
/// Define the no button as a render fragment (overrides CancelText).
/// Must be a MudButton
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public RenderFragment NoButton { get; set; }
///
/// Text of the yes/OK button. Leave null to hide the button.
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public string YesText { get; set; } = "OK";
///
/// Define the cancel button as a render fragment (overrides CancelText).
/// Must be a MudButton
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public RenderFragment YesButton { get; set; }
///
/// Fired when the yes button is clicked
///
[Parameter]
public EventCallback OnYes { get; set; }
///
/// Fired when the no button is clicked
///
[Parameter]
public EventCallback OnNo { get; set; }
///
/// Fired when the cancel button is clicked or the msg box was closed via the X
///
[Parameter]
public EventCallback OnCancel { get; set; }
///
/// Bind this two-way to show and close an inlined message box. Has no effect on opened msg boxes
///
[Parameter]
[Category(CategoryTypes.MessageBox.Behavior)]
public bool IsVisible
{
get => _isVisible;
set
{
if (_isVisible == value)
return;
_isVisible = value;
if (IsInline)
{
if (_isVisible)
_ = Show();
else
Close();
}
IsVisibleChanged.InvokeAsync(value);
}
}
private bool _isVisible;
private bool IsInline => DialogInstance == null;
private IDialogReference _reference;
///
/// Raised when the inline dialog's display status changes.
///
[Parameter]
public EventCallback IsVisibleChanged { get; set; }
public async Task Show(DialogOptions options = null)
{
if (DialogService == null)
return null;
var parameters = new DialogParameters()
{
[nameof(Title)] = Title,
[nameof(TitleContent)] = TitleContent,
[nameof(Message)] = Message,
[nameof(MarkupMessage)] = MarkupMessage,
[nameof(MessageContent)] = MessageContent,
[nameof(CancelText)] = CancelText,
[nameof(CancelButton)] = CancelButton,
[nameof(NoText)] = NoText,
[nameof(NoButton)] = NoButton,
[nameof(YesText)] = YesText,
[nameof(YesButton)] = YesButton,
};
_reference = await DialogService.ShowAsync(parameters: parameters, options: options, title: Title);
var result = await _reference.Result;
if (result.Cancelled || result.Data is not bool data)
return null;
return data;
}
public void Close()
{
_reference?.Close();
}
private ActivatableCallback _yesCallback, _cancelCallback, _noCallback;
protected override void OnInitialized()
{
base.OnInitialized();
if (YesButton != null)
_yesCallback = new ActivatableCallback() { ActivateCallback = OnYesActivated };
if (NoButton != null)
_noCallback = new ActivatableCallback() { ActivateCallback = OnNoActivated };
if (CancelButton != null)
_cancelCallback = new ActivatableCallback() { ActivateCallback = OnCancelActivated };
}
private void OnYesActivated(object arg1, MouseEventArgs arg2) => OnYesClicked();
private void OnNoActivated(object arg1, MouseEventArgs arg2) => OnNoClicked();
private void OnCancelActivated(object arg1, MouseEventArgs arg2) => OnCancelClicked();
private void OnYesClicked() => DialogInstance.Close(DialogResult.Ok(true));
private void OnNoClicked() => DialogInstance.Close(DialogResult.Ok(false));
private void OnCancelClicked() => DialogInstance.Close(DialogResult.Cancel());
private void HandleKeyDown(KeyboardEventArgs args)
{
if (args.Key == "Escape")
{
OnCancelClicked();
}
}
}