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 OnOpenEvent; internal event Func OnCloseEvent; internal event Func OnUpdateEvent; internal event Func OnDestroyEvent; internal event Func OnDestroyAllEvent; #region SimpleConfirm public ConfirmRef Confirm(ConfirmOptions props) { ConfirmRef confirmRef = new ConfirmRef(props, this); confirmRef.TaskCompletionSource = new TaskCompletionSource(); OnOpenEvent?.Invoke(confirmRef); return confirmRef; } public ConfirmRef Info(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Info; options.OkCancel = false; return Confirm(options); } public ConfirmRef Success(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Success; options.OkCancel = false; return Confirm(options); } public ConfirmRef Error(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Error; options.OkCancel = false; return Confirm(options); } public ConfirmRef Warning(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Warning; options.OkCancel = false; return Confirm(options); } public async Task ConfirmAsync(ConfirmOptions props) { ConfirmRef confirmRef = new ConfirmRef(props, this); confirmRef.TaskCompletionSource = new TaskCompletionSource(); if (OnOpenEvent != null) { await OnOpenEvent.Invoke(confirmRef); } return await confirmRef.TaskCompletionSource.Task .ContinueWith(t => { return t.Result == ConfirmResult.OK; }, TaskScheduler.Default); } public Task InfoAsync(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Info; options.OkCancel = false; return ConfirmAsync(options); } public Task SuccessAsync(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Success; options.OkCancel = false; return ConfirmAsync(options); } public Task ErrorAsync(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.ConfirmIcon = ConfirmIcon.Error; options.OkCancel = false; return ConfirmAsync(options); } 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 public Task Update(ConfirmRef confirmRef) { return OnUpdateEvent?.Invoke(confirmRef); } public Task Destroy(ConfirmRef confirmRef) { return OnDestroyEvent?.Invoke(confirmRef); } public Task DestroyAll() { return OnDestroyAllEvent?.Invoke(); } /// /// Create and open a Moal /// /// Options /// public Task CreateAsync(ConfirmOptions config) { CheckIsNull(config); ConfirmRef confirmRef = new ConfirmRef(config, this); OnOpenEvent?.Invoke(confirmRef); return Task.FromResult(confirmRef); } /// /// Create and open template modal /// /// /// /// /// /// /// public Task> CreateAsync(ConfirmOptions config, TComponentOptions componentOptions) where TComponent : ConfirmTemplate { CheckIsNull(config); ConfirmRef confirmRef = new ConfirmRef(config, this); OnOpenEvent?.Invoke(confirmRef); RenderFragment child = (builder) => { builder.OpenComponent(0); builder.AddAttribute(1, "ConfirmRef", confirmRef); builder.AddAttribute(2, "Options", componentOptions); builder.CloseComponent(); }; config.Content = child; return Task.FromResult(confirmRef); } internal Task OpenAsync(ConfirmRef confirmRef) { if (OnOpenEvent != null) { OnOpenEvent.Invoke(confirmRef); } return Task.CompletedTask; } internal Task CloseAsync(ConfirmRef confirmRef) { if (OnCloseEvent != null) { return OnCloseEvent.Invoke(confirmRef); } return Task.CompletedTask; } private static void CheckIsNull(ConfirmOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } } } }