using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; namespace AntDesign { public partial class ComfirmContainer { [Inject] private ModalService ModalService { get; set; } [Inject] private ConfirmService ConfirmService { get; set; } private readonly List _confirmRefs = new List(); #region override /// /// Registration events /// protected override void OnInitialized() { ModalService.OnConfirmOpenEvent += OnConfirmOpen; ModalService.OnConfirmCloseEvent += OnConfirmClose; ModalService.OnConfirmCloseAllEvent += OnConfirmCloseAll; ModalService.OnConfirmUpdateEvent += OnConfirmUpdate; ConfirmService.OnOpenEvent += OnConfirmOpen; } #endregion /// /// create and open a Confirm dialog /// private async Task OnConfirmOpen(ConfirmRef confirmRef) { confirmRef.Config.Visible = true; if (!_confirmRefs.Contains(confirmRef)) { confirmRef.Config.BuildButtonsDefaultOptions(); _confirmRefs.Add(confirmRef); } await InvokeAsync(StateHasChanged); } /// /// update Confirm dialog /// /// /// private async Task OnConfirmUpdate(ConfirmRef confirmRef) { if (confirmRef.Config.Visible) { await InvokeAsync(StateHasChanged); } } /// /// destroy Confirm dialog /// /// /// private async Task OnConfirmClose(ConfirmRef confirmRef) { confirmRef.Config.Visible = false; await InvokeAsync(StateHasChanged); if (confirmRef.OnClose != null) { await confirmRef.OnClose.Invoke(); } } /// /// after Confirm dialog remove from DOM, to remove it from _confirmRefs /// /// /// private Task OnConfirmRemove(ConfirmRef confirmRef) { if (_confirmRefs.Contains(confirmRef)) { _confirmRefs.Remove(confirmRef); } return Task.CompletedTask; } /// /// destroy all Confirm dialog /// /// private async Task OnConfirmCloseAll() { // avoid iterations the change of _confirmRefs affects the iterative process var confirmRefsTemp = new List(_confirmRefs); foreach (var confirmRef in confirmRefsTemp) { await OnConfirmClose(confirmRef); } } /// /// Unregister events /// /// protected override void Dispose(bool disposing) { ModalService.OnConfirmOpenEvent -= OnConfirmOpen; ModalService.OnConfirmCloseEvent -= OnConfirmClose; ModalService.OnConfirmCloseAllEvent -= OnConfirmCloseAll; ModalService.OnConfirmUpdateEvent -= OnConfirmUpdate; ConfirmService.OnOpenEvent -= OnConfirmOpen; base.Dispose(disposing); } } }