2020-04-24 18:32:50 +08:00
|
|
|
|
using System;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.JSInterop;
|
2021-04-15 14:19:26 +08:00
|
|
|
|
using AntDesign.Core.Extensions;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
public abstract class AntComponentBase : ComponentBase, IDisposable
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
|
|
|
|
[Parameter]
|
2021-01-21 17:20:10 +08:00
|
|
|
|
public ForwardRef RefBack { get; set; } = new ForwardRef();
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
private readonly Queue<Func<Task>> _afterRenderCallQuene = new Queue<Func<Task>>();
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
|
|
|
|
protected void CallAfterRender(Func<Task> action)
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
_afterRenderCallQuene.Enqueue(action);
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 00:25:22 +08:00
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
|
|
|
|
await base.OnAfterRenderAsync(firstRender);
|
|
|
|
|
if (firstRender)
|
|
|
|
|
{
|
|
|
|
|
await OnFirstAfterRenderAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (_afterRenderCallQuene.Count > 0)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
var actions = _afterRenderCallQuene.ToArray();
|
|
|
|
|
_afterRenderCallQuene.Clear();
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
|
|
|
|
foreach (var action in actions)
|
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (IsDisposed)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await action();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual Task OnFirstAfterRenderAsync()
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected AntComponentBase()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void InvokeStateHasChanged()
|
|
|
|
|
{
|
|
|
|
|
InvokeAsync(() =>
|
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (!IsDisposed)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 01:46:35 +08:00
|
|
|
|
protected async Task InvokeStateHasChangedAsync()
|
|
|
|
|
{
|
2020-09-02 15:15:59 +08:00
|
|
|
|
await InvokeAsync(() =>
|
2020-07-11 01:46:35 +08:00
|
|
|
|
{
|
|
|
|
|
if (!IsDisposed)
|
|
|
|
|
{
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 00:51:07 +08:00
|
|
|
|
[Inject]
|
|
|
|
|
protected IJSRuntime Js { get; set; }
|
|
|
|
|
|
|
|
|
|
protected async Task<T> JsInvokeAsync<T>(string code, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await Js.InvokeAsync<T>(code, args);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 00:32:26 +08:00
|
|
|
|
protected async Task JsInvokeAsync(string code, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Js.InvokeVoidAsync(code, args);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 18:13:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Standard Focus. From Net5 uses Blazor extension method on ElementReference.
|
|
|
|
|
/// Before, uses JS implemented exactly the same as Net5 JS.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="target">Element that will receive focus.</param>
|
|
|
|
|
/// <param name="preventScroll">Whether to scroll to focused element</param>
|
|
|
|
|
protected async Task FocusAsync(ElementReference target, bool preventScroll = false)
|
|
|
|
|
=> await Js.FocusAsync(target, preventScroll);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Focus with behaviors. Behavior will work only for elements that are
|
|
|
|
|
/// HTMLInputElement or HTMLTextAreaElement. Otherwise will only focus.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="target">Element that will receive focus.</param>
|
|
|
|
|
/// <param name="behavior">Behavior of focused element</param>
|
|
|
|
|
/// <param name="preventScroll">Whether to scroll to focused element</param>
|
|
|
|
|
protected async Task FocusAsync(ElementReference target, FocusBehavior behavior, bool preventScroll = false)
|
|
|
|
|
=> await Js.FocusAsync(target, behavior, preventScroll);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Standard Blur. Uses JS interop.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="target">Element that will receive focus.</param>
|
|
|
|
|
protected async Task BlurAsync(ElementReference target)
|
|
|
|
|
=> await Js.BlurAsyc(target);
|
2021-04-15 14:19:26 +08:00
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
protected bool IsDisposed { get; private set; }
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
protected virtual void Dispose(bool disposing)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
if (IsDisposed) return;
|
|
|
|
|
|
|
|
|
|
IsDisposed = true;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
public void Dispose()
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2020-04-24 18:32:50 +08:00
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
~AntComponentBase()
|
|
|
|
|
{
|
|
|
|
|
// Finalizer calls Dispose(false)
|
|
|
|
|
Dispose(false);
|
|
|
|
|
}
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|