2020-08-22 09:24:55 +08:00
using System ;
using System.Collections.Generic ;
using System.Text ;
using System.Threading.Tasks ;
using Microsoft.AspNetCore.Components ;
using OneOf ;
namespace AntDesign
{
2020-12-26 21:39:04 +08:00
/// <summary>
/// show a simple Confirm dialog like MessageBox of Windows, it's different from ModalService. ModalService can only create OK-Cancel Confirm dialog and return ConfirmRef, but ConfirmService return ConfirmResult
/// </summary>
2020-08-22 09:24:55 +08:00
public class ConfirmService
{
2020-10-14 15:09:11 +08:00
internal event Func < ConfirmRef , Task > OnOpenEvent ;
2020-08-22 09:24:55 +08:00
/// <summary>
2020-10-09 11:32:32 +08:00
/// show a confirm dialog like MessageBox of Windows
2020-08-22 09:24:55 +08:00
/// </summary>
2020-10-09 11:32:32 +08:00
/// <param name="content">the content of dialog</param>
/// <param name="title">the title of dialog</param>
/// <param name="confirmButtons">the buttons of dialog</param>
/// <param name="confirmIcon">the icon of dialog</param>
/// <param name="options">the configuration options for dialog</param>
2020-08-22 09:24:55 +08:00
/// <returns></returns>
public async Task < ConfirmResult > Show (
OneOf < string , RenderFragment > content ,
OneOf < string , RenderFragment > title ,
ConfirmButtons confirmButtons ,
ConfirmIcon confirmIcon ,
ConfirmButtonOptions options )
{
if ( options = = null ) throw new ArgumentNullException ( nameof ( options ) ) ;
ConfirmOptions confirmOptions = new ConfirmOptions ( )
{
Content = content ,
ConfirmButtons = confirmButtons ,
ConfirmIcon = confirmIcon ,
} ;
2020-12-26 21:39:04 +08:00
if ( title . IsT0 )
{
confirmOptions . Title = title . AsT0 ;
}
else
{
confirmOptions . TitleTemplate = title . AsT1 ;
}
2020-08-22 09:24:55 +08:00
#region config button default properties
if ( options . Button1Props ! = null )
{
confirmOptions . Button1Props = options . Button1Props ;
}
if ( options . Button2Props ! = null )
{
confirmOptions . Button2Props = options . Button2Props ;
}
if ( options . Button3Props ! = null )
{
confirmOptions . Button3Props = options . Button3Props ;
}
#endregion
2020-12-26 21:39:04 +08:00
var confirmRef = new ConfirmRef ( confirmOptions )
2020-10-09 11:32:32 +08:00
{
TaskCompletionSource = new TaskCompletionSource < ConfirmResult > ( )
} ;
if ( OnOpenEvent ! = null )
{
2020-12-26 21:39:04 +08:00
await OnOpenEvent . Invoke ( confirmRef ) ;
2020-10-09 11:32:32 +08:00
}
2020-12-26 21:39:04 +08:00
return await confirmRef . TaskCompletionSource . Task ;
2020-08-22 09:24:55 +08:00
}
2020-10-09 11:32:32 +08:00
/// <summary>
/// show a confirm dialog like MessageBox of Windows
/// </summary>
/// <param name="content">the content of dialog</param>
/// <param name="title">the title of dialog</param>
/// <param name="confirmButtons">the buttons of dialog</param>
/// <param name="confirmIcon">the icon of dialog</param>
/// <returns></returns>
2020-08-22 09:24:55 +08:00
public Task < ConfirmResult > Show
( OneOf < string , RenderFragment > content ,
OneOf < string , RenderFragment > title ,
ConfirmButtons confirmButtons = ConfirmButtons . OKCancel ,
ConfirmIcon confirmIcon = ConfirmIcon . Info )
{
return Show ( content , title , confirmButtons , confirmIcon , new ConfirmButtonOptions ( ) ) ;
}
}
}