using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using OneOf; namespace AntDesign { public class DrawerService { internal event Func OnOpenEvent; internal event Func OnCloseEvent; internal event Func OnUpdateEvent; /// /// Create and open a simple drawer without result /// /// drawer options /// The reference of drawer public async Task CreateAsync(DrawerOptions options) { CheckIsNull(options); var drawerRef = new DrawerRef(options, this); await (OnOpenEvent?.Invoke(drawerRef) ?? Task.CompletedTask); return drawerRef; } /// /// Create and open a drawer with the template /// /// The type of DrawerTemplate implement /// The /// The type of return value /// /// /// The reference of drawer public async Task> CreateAsync(DrawerOptions config, TComponentOptions options) where TComponent : FeedbackComponent { CheckIsNull(config); DrawerRef drawerRef = new DrawerRef(config, this); await (OnOpenEvent?.Invoke(drawerRef) ?? Task.CompletedTask); RenderFragment child = (builder) => { builder.OpenComponent(0); builder.AddAttribute(1, "FeedbackRef", drawerRef); builder.AddAttribute(2, "Options", options); builder.CloseComponent(); }; config.ChildContent = child; return drawerRef; } /// /// Update a drawer /// /// /// public async Task UpdateAsync(DrawerRef drawerRef) { await (OnUpdateEvent?.Invoke(drawerRef) ?? Task.CompletedTask); } /// /// Create and open a drawer /// /// /// /// /// /// /// public async Task CreateDialogAsync(DrawerOptions config, TComponentOptions options) where TComponent : FeedbackComponent { CheckIsNull(config); DrawerRef drawerRef = new DrawerRef(config, this); drawerRef.TaskCompletionSource = new TaskCompletionSource(); ; await (OnOpenEvent?.Invoke(drawerRef) ?? Task.CompletedTask); RenderFragment child = (builder) => { builder.OpenComponent(0); builder.AddAttribute(1, "FeedbackRef", drawerRef); builder.AddAttribute(2, "Options", options); builder.CloseComponent(); }; config.ChildContent = child; return await drawerRef.TaskCompletionSource.Task; } public async Task CreateDialogAsync(TComponentOptions options, bool closable = true, bool maskClosable = true, string title = null, int width = 256, bool mask = true, bool noAnimation = false, string placement = "right") where TComponent : FeedbackComponent { var config = new DrawerOptions() { Closable = closable, MaskClosable = maskClosable, Title = title, Width = width, Mask = mask, NoAnimation = noAnimation, Placement = placement, }; return await CreateDialogAsync(config, options); } internal Task OpenAsync(DrawerRef drawerRef) { if (OnOpenEvent != null) { return OnOpenEvent.Invoke(drawerRef); } return Task.CompletedTask; } internal Task CloseAsync(DrawerRef drawerRef) { if (OnCloseEvent != null) { return OnCloseEvent.Invoke(drawerRef); } return Task.CompletedTask; } private static void CheckIsNull(DrawerOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } } } }