2020-06-02 14:25:44 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dialog only control component show or hide,
|
|
|
|
|
/// Elements are not removed from the DOM tree
|
|
|
|
|
/// </summary>
|
2020-06-02 14:25:44 +08:00
|
|
|
|
public partial class Dialog
|
|
|
|
|
{
|
|
|
|
|
private const string IdPrefix = "Ant-Design-";
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// default Dialog close icon
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal static readonly RenderFragment DefaultCloseIcon = (builder) =>
|
|
|
|
|
{
|
|
|
|
|
builder.OpenComponent<Icon>(0);
|
|
|
|
|
builder.AddAttribute(1, "Type", "close");
|
|
|
|
|
builder.AddAttribute(2, "Theme", "outline");
|
|
|
|
|
builder.CloseComponent();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// default modal footer
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal static readonly RenderFragment DefaultFooter = (builder) =>
|
|
|
|
|
{
|
|
|
|
|
builder.OpenComponent<ModalFooter>(0);
|
|
|
|
|
builder.CloseComponent();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Parameters
|
|
|
|
|
|
|
|
|
|
#pragma warning disable 1591
|
|
|
|
|
|
2020-06-02 14:25:44 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public DialogOptions Config { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Visible { get; set; }
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
#pragma warning restore 1591
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2021-01-21 19:07:21 +08:00
|
|
|
|
[CascadingParameter(Name = "DialogWrapperId")]
|
|
|
|
|
public string DialogWrapperId { get; set; } = "";
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
private string _maskAnimationClsName = "";
|
|
|
|
|
private string _modalAnimationClsName = "";
|
2020-06-02 14:25:44 +08:00
|
|
|
|
private string _maskHideClsName = "";
|
2020-12-26 21:39:04 +08:00
|
|
|
|
private string _wrapStyle = "";
|
2020-06-02 14:25:44 +08:00
|
|
|
|
|
|
|
|
|
private bool _hasShow;
|
|
|
|
|
private bool _hasDestroy = true;
|
2020-06-04 17:03:13 +08:00
|
|
|
|
private bool _disableBodyScroll;
|
2020-12-26 21:39:04 +08:00
|
|
|
|
private bool _doDraggable;
|
2020-06-02 14:25:44 +08:00
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
#pragma warning disable 649
|
2020-06-02 14:25:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// dialog root container
|
|
|
|
|
/// </summary>
|
|
|
|
|
private ElementReference _element;
|
|
|
|
|
|
2020-08-26 07:28:44 +08:00
|
|
|
|
private ElementReference _dialogHeader;
|
|
|
|
|
|
|
|
|
|
private ElementReference _modal;
|
2020-12-26 21:39:04 +08:00
|
|
|
|
#pragma warning restore 649
|
2020-08-26 07:28:44 +08:00
|
|
|
|
|
|
|
|
|
#region ant-modal style
|
|
|
|
|
|
|
|
|
|
private string _modalStyle = null;
|
2020-06-02 14:25:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// ant-modal style
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private string GetStyle()
|
|
|
|
|
{
|
2020-08-26 07:28:44 +08:00
|
|
|
|
if (_modalStyle == null)
|
|
|
|
|
{
|
|
|
|
|
var style = $"{Config.GetWidth()};";
|
|
|
|
|
|
|
|
|
|
if (Config.Draggable)
|
|
|
|
|
{
|
|
|
|
|
string left = $"margin: 0; padding-bottom:0;";
|
|
|
|
|
style += left;
|
|
|
|
|
}
|
|
|
|
|
_modalStyle = style;
|
|
|
|
|
}
|
2020-06-02 14:25:44 +08:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(Style))
|
|
|
|
|
{
|
2020-08-26 07:28:44 +08:00
|
|
|
|
return _modalStyle + Style + ";";
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
2020-08-26 07:28:44 +08:00
|
|
|
|
return _modalStyle;
|
|
|
|
|
}
|
2020-06-02 14:25:44 +08:00
|
|
|
|
|
2020-08-26 07:28:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// if Modal is draggable, reset the position style similar with the first show
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal async Task TryResetModalStyle()
|
|
|
|
|
{
|
|
|
|
|
if (Config.Draggable)
|
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.ResetModalPosition, _dialogHeader);
|
2020-08-26 07:28:44 +08:00
|
|
|
|
}
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 07:28:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2020-06-02 14:25:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// append To body
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private async Task AppendToContainer()
|
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.AddElementTo, _element, Config.GetContainer);
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region mask and dialog click event
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// check is dialog click
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool _dialogMouseDown = false;
|
|
|
|
|
|
|
|
|
|
private void OnDialogMouseDown()
|
|
|
|
|
{
|
|
|
|
|
_dialogMouseDown = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
private async Task OnMaskMouseUp()
|
|
|
|
|
{
|
|
|
|
|
if (Config.MaskClosable && _dialogMouseDown)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(50);
|
|
|
|
|
_dialogMouseDown = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task OnMaskClick()
|
2020-06-02 14:25:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (Config.MaskClosable
|
|
|
|
|
&& !_dialogMouseDown)
|
|
|
|
|
{
|
|
|
|
|
await CloseAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region keyboard control
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// TAB keyboard control
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _sentinelStart = IdPrefix + Guid.NewGuid().ToString();
|
|
|
|
|
private readonly string _sentinelEnd = IdPrefix + Guid.NewGuid().ToString();
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tab start control id
|
|
|
|
|
/// </summary>
|
2020-08-26 07:28:44 +08:00
|
|
|
|
public string SentinelStart => _sentinelStart;
|
2020-06-02 14:25:44 +08:00
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Listening for Tab and ESC key events
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-06-02 14:25:44 +08:00
|
|
|
|
private async Task OnKeyDown(KeyboardEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (Config.Keyboard && e.Key == "Escape")
|
|
|
|
|
{
|
|
|
|
|
await CloseAsync();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (Visible)
|
|
|
|
|
{
|
|
|
|
|
if (e.Key == "Tab")
|
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
var activeElement = await JsInvokeAsync<string>(JSInteropConstants.GetActiveElement, _sentinelEnd);
|
2020-06-02 14:25:44 +08:00
|
|
|
|
if (e.ShiftKey)
|
|
|
|
|
{
|
|
|
|
|
if (activeElement == _sentinelStart)
|
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.Focus, "#" + _sentinelEnd);
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (activeElement == _sentinelEnd)
|
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.Focus, "#" + _sentinelStart);
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// closer(X) click event
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private async Task OnCloserClick()
|
2020-06-02 14:25:44 +08:00
|
|
|
|
{
|
|
|
|
|
await CloseAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CloseAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_hasDestroy)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-06-23 15:19:44 +08:00
|
|
|
|
if (Config.OnCancel != null)
|
2020-06-02 14:25:44 +08:00
|
|
|
|
{
|
2020-06-23 15:19:44 +08:00
|
|
|
|
await Config.OnCancel.Invoke(null);
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region control show and hide class name and style
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// show dialog through animation
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private void Show()
|
2020-06-02 14:25:44 +08:00
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
if (!_hasShow && Visible)
|
2020-06-02 14:25:44 +08:00
|
|
|
|
{
|
2020-08-26 07:28:44 +08:00
|
|
|
|
if (Config.Draggable)
|
|
|
|
|
{
|
|
|
|
|
_wrapStyle = "display:flex;justify-content: center;";
|
|
|
|
|
if (Config.Centered)
|
|
|
|
|
{
|
|
|
|
|
_wrapStyle += "align-items: center;";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_wrapStyle += "align-items: flex-start;";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_wrapStyle = "";
|
|
|
|
|
}
|
2020-06-02 14:25:44 +08:00
|
|
|
|
_maskHideClsName = "";
|
2020-12-26 21:39:04 +08:00
|
|
|
|
_maskAnimationClsName = ModalAnimation.MaskEnter;
|
|
|
|
|
_modalAnimationClsName = ModalAnimation.ModalEnter;
|
2020-06-02 14:25:44 +08:00
|
|
|
|
_hasShow = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Hide Dialog through animation
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
internal async Task Hide()
|
2020-06-02 14:25:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (!_hasShow)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Visible)
|
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
_maskAnimationClsName = ModalAnimation.MaskLeave;
|
|
|
|
|
_modalAnimationClsName = ModalAnimation.ModalLeave;
|
2020-06-02 14:25:44 +08:00
|
|
|
|
await Task.Delay(200);
|
|
|
|
|
_wrapStyle = "display: none;";
|
|
|
|
|
_maskHideClsName = "ant-modal-mask-hidden";
|
|
|
|
|
_hasShow = false;
|
2020-12-26 21:39:04 +08:00
|
|
|
|
await InvokeStateHasChangedAsync();
|
2020-06-23 15:19:44 +08:00
|
|
|
|
if (Config.OnClosed != null)
|
2020-06-02 14:25:44 +08:00
|
|
|
|
{
|
2020-06-23 15:19:44 +08:00
|
|
|
|
await Config.OnClosed.Invoke();
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determine whether Dialog is displayed
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2020-09-19 09:15:16 +08:00
|
|
|
|
public bool IsShow()
|
|
|
|
|
{
|
|
|
|
|
return _hasShow;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 14:25:44 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
#region build element's class name
|
|
|
|
|
|
2020-06-02 14:25:44 +08:00
|
|
|
|
private string GetMaskClsName()
|
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
var clsName = _maskHideClsName;
|
|
|
|
|
clsName += _maskAnimationClsName;
|
2020-06-02 14:25:44 +08:00
|
|
|
|
return clsName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetModalClsName()
|
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
var clsName = Config.ClassName;
|
|
|
|
|
return clsName + _modalAnimationClsName;
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2020-06-02 14:25:44 +08:00
|
|
|
|
#region override
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2020-06-02 14:25:44 +08:00
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
|
|
|
{
|
|
|
|
|
//Reduce one rendering when showing and not destroyed
|
|
|
|
|
if (Visible)
|
|
|
|
|
{
|
|
|
|
|
if (!_hasDestroy)
|
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
Show();
|
|
|
|
|
//await InvokeStateHasChangedAsync();
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_wrapStyle = "display: none;";
|
|
|
|
|
_maskHideClsName = "ant-modal-mask-hidden";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await Hide();
|
|
|
|
|
}
|
|
|
|
|
await base.OnParametersSetAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="isFirst"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-06-02 14:25:44 +08:00
|
|
|
|
protected override async Task OnAfterRenderAsync(bool isFirst)
|
|
|
|
|
{
|
|
|
|
|
if (Visible)
|
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
// disable body scroll
|
2020-06-02 14:25:44 +08:00
|
|
|
|
if (_hasDestroy)
|
|
|
|
|
{
|
|
|
|
|
_hasDestroy = false;
|
2020-12-26 21:39:04 +08:00
|
|
|
|
await AppendToContainer();
|
2020-06-02 14:25:44 +08:00
|
|
|
|
Show();
|
2020-12-26 21:39:04 +08:00
|
|
|
|
await InvokeStateHasChangedAsync();
|
2020-06-02 14:25:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 17:03:13 +08:00
|
|
|
|
if (!_disableBodyScroll)
|
|
|
|
|
{
|
|
|
|
|
_disableBodyScroll = true;
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.DisableBodyScroll);
|
2020-06-04 17:03:13 +08:00
|
|
|
|
}
|
2020-08-26 07:28:44 +08:00
|
|
|
|
|
2020-12-26 21:39:04 +08:00
|
|
|
|
// enable drag and drop
|
|
|
|
|
if (Config.Draggable && !_doDraggable)
|
2020-08-26 07:28:44 +08:00
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
_doDraggable = true;
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.EnableDraggable, _dialogHeader, _modal, Config.DragInViewport);
|
2020-08-26 07:28:44 +08:00
|
|
|
|
}
|
2020-06-04 17:03:13 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
// enable body scroll
|
2020-06-04 17:03:13 +08:00
|
|
|
|
if (_disableBodyScroll)
|
|
|
|
|
{
|
|
|
|
|
_disableBodyScroll = false;
|
|
|
|
|
await Task.Delay(250);
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.EnableBodyScroll);
|
2020-06-04 17:03:13 +08:00
|
|
|
|
}
|
2020-12-26 21:39:04 +08:00
|
|
|
|
// disable drag and drop
|
|
|
|
|
if (Config.Draggable && _doDraggable)
|
2020-08-26 07:28:44 +08:00
|
|
|
|
{
|
2020-12-26 21:39:04 +08:00
|
|
|
|
_doDraggable = false;
|
2020-09-09 00:52:14 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.DisableDraggable, _dialogHeader);
|
2020-08-26 07:28:44 +08:00
|
|
|
|
}
|
2020-06-04 17:03:13 +08:00
|
|
|
|
}
|
2020-12-26 21:39:04 +08:00
|
|
|
|
|
2020-06-02 14:25:44 +08:00
|
|
|
|
await base.OnAfterRenderAsync(isFirst);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|