using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; namespace AntDesign { public partial class ModalService { internal event Func OnConfirmOpenEvent; internal event Func OnConfirmUpdateEvent; internal event Func OnConfirmCloseEvent; internal event Func OnConfirmCloseAllEvent; #region SimpleConfirm /// /// create and open a OK-Cancel Confirm dialog /// /// /// public ConfirmRef Confirm(ConfirmOptions props) { CheckConfirmOptionsIsNull(props); ConfirmRef confirmRef = new ConfirmRef(props, this); confirmRef.TaskCompletionSource = new TaskCompletionSource(); OnConfirmOpenEvent?.Invoke(confirmRef); return confirmRef; } /// /// create and open a OK-Cancel Confirm dialog with info icon /// /// /// public ConfirmRef Info(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Info; options.OkCancel = false; return Confirm(options); } /// /// create and open a OK-Cancel Confirm dialog with success icon /// /// /// public ConfirmRef Success(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Success; options.OkCancel = false; return Confirm(options); } /// /// create and open a OK-Cancel Confirm dialog with error icon /// /// /// public ConfirmRef Error(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Error; options.OkCancel = false; return Confirm(options); } /// /// create and open a OK-Cancel Confirm dialog with Warning icon /// /// /// public ConfirmRef Warning(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Warning; options.OkCancel = false; return Confirm(options); } #endregion #region Confirm with return the OK button is clicked /// /// create and open a OK-Cancel Confirm dialog, /// and return a bool value which indicates whether the OK button has been clicked /// /// /// public async Task ConfirmAsync(ConfirmOptions props) { ConfirmRef confirmRef = new ConfirmRef(props, this); confirmRef.TaskCompletionSource = new TaskCompletionSource(); if (OnConfirmOpenEvent != null) { await OnConfirmOpenEvent.Invoke(confirmRef); } return await confirmRef.TaskCompletionSource.Task .ContinueWith(t => t.Result == ConfirmResult.OK, TaskScheduler.Default); } /// /// create and open a OK-Cancel Confirm dialog with info icon, /// and return a bool value which indicates whether the OK button has been clicked /// /// /// public Task InfoAsync(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Info; options.OkCancel = false; return ConfirmAsync(options); } /// /// create and open a OK-Cancel Confirm dialog with success icon, /// and return a bool value which indicates whether the OK button has been clicked /// /// /// public Task SuccessAsync(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Success; options.OkCancel = false; return ConfirmAsync(options); } /// /// create and open a OK-Cancel Confirm dialog with error icon, /// and return a bool value which indicates whether the OK button has been clicked /// /// /// public Task ErrorAsync(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Error; options.OkCancel = false; return ConfirmAsync(options); } /// /// create and open a OK-Cancel Confirm dialog with warning icon, /// and return a bool value which indicates whether the OK button has been clicked /// /// /// public Task WarningAsync(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Warning; options.OkCancel = false; return ConfirmAsync(options); } #endregion SimpleConfirm /// /// update Confirm which Visible=true /// /// /// [Obsolete("Use the UpdateConfirmAsync method instead")] public Task Update(ConfirmRef confirmRef) { return UpdateConfirmAsync(confirmRef); } /// /// update Confirm which Visible=true /// /// /// public async Task UpdateConfirmAsync(ConfirmRef confirmRef) { await (OnConfirmUpdateEvent?.Invoke(confirmRef) ?? Task.CompletedTask); } /// /// close a Confirm dialog /// /// /// [Obsolete("Use the DestroyConfirmAsync method instead")] public Task Destroy(ConfirmRef confirmRef) { return DestroyConfirmAsync(confirmRef); } /// /// close the Confirm dialog /// /// /// public async Task DestroyConfirmAsync(ConfirmRef confirmRef) { await (OnConfirmCloseEvent?.Invoke(confirmRef) ?? Task.CompletedTask); } /// /// close all Confirm dialog /// /// [Obsolete("Use the DestroyAllConfirmAsync method instead")] public Task DestroyAll() { return DestroyAllConfirmAsync(); } /// /// close all Confirm dialog /// /// public async Task DestroyAllConfirmAsync() { await (OnConfirmCloseAllEvent?.Invoke() ?? Task.CompletedTask); } /// /// Create and open a OK-Cancel Confirm asynchronous /// /// Options /// [Obsolete("Use the CreateAsync method instead")] public Task CreateAsync(ConfirmOptions config) { return CreateConfirmAsync(config); } /// /// Create and open a OK-Cancel Confirm asynchronous /// /// Options /// public Task CreateConfirmAsync(ConfirmOptions config) { var confirmRef = Confirm(config); return Task.FromResult(confirmRef); } /// /// Create and open template Confirm dialog /// /// /// /// /// /// /// [Obsolete("Use the CreateAsync method instead")] public Task> CreateAsync (ConfirmOptions config, TComponentOptions componentOptions) where TComponent : FeedbackComponent { return CreateConfirmAsync(config, componentOptions); } /// /// Create and open template Confirm dialog /// /// /// /// /// /// /// public Task> CreateConfirmAsync(ConfirmOptions config, TComponentOptions componentOptions) where TComponent : FeedbackComponent { CheckConfirmOptionsIsNull(config); ConfirmRef confirmRef = new ConfirmRef(config, this); OnConfirmOpenEvent?.Invoke(confirmRef); RenderFragment child = (builder) => { builder.OpenComponent(0); builder.AddAttribute(1, "FeedbackRef", confirmRef); builder.AddAttribute(2, "Options", componentOptions); builder.CloseComponent(); }; config.Content = child; return Task.FromResult(confirmRef); } /// /// open the Confirm dialog /// /// /// internal async Task OpenConfirmAsync(ConfirmRef confirmRef) { if (OnConfirmOpenEvent != null) { await OnConfirmOpenEvent.Invoke(confirmRef); } } /// /// check Confirm options is null /// /// private static void CheckConfirmOptionsIsNull(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } } } }