2020-10-14 17:33:36 +08:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-09-09 00:52:14 +08:00
|
|
|
|
using AntDesign.JsInterop;
|
2020-06-07 19:41:00 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2020-03-27 17:24:16 +08:00
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-03-27 17:24:16 +08:00
|
|
|
|
{
|
2020-06-07 19:41:00 +08:00
|
|
|
|
public partial class BackTop : AntDomComponentBase
|
2020-03-27 17:24:16 +08:00
|
|
|
|
{
|
2020-10-14 17:33:36 +08:00
|
|
|
|
private bool _visible = false;
|
|
|
|
|
|
2020-10-09 10:59:52 +08:00
|
|
|
|
[Inject]
|
2021-09-09 12:56:11 +08:00
|
|
|
|
private IDomEventListener DomEventListener { get; set; }
|
2020-10-09 10:59:52 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Icon { get; set; } = "vertical-align-top";
|
|
|
|
|
|
2020-03-27 17:24:16 +08:00
|
|
|
|
[Parameter]
|
2020-10-09 10:59:52 +08:00
|
|
|
|
public RenderFragment ChildContent { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-10-09 10:59:52 +08:00
|
|
|
|
[Parameter]
|
2020-10-14 17:33:36 +08:00
|
|
|
|
public double VisibilityHeight { get; set; } = 400;
|
2020-10-09 10:59:52 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 回到顶部的目标控件
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string TargetSelector { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-03-27 17:24:16 +08:00
|
|
|
|
protected ClassMapper BackTopContentClassMapper { get; set; } = new ClassMapper();
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-03-27 17:24:16 +08:00
|
|
|
|
protected ClassMapper BackTopIconClassMapper { get; set; } = new ClassMapper();
|
|
|
|
|
|
2020-10-09 10:59:52 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback OnClick { get; set; }
|
|
|
|
|
|
|
|
|
|
protected async Task OnClickHandle()
|
2020-03-27 17:24:16 +08:00
|
|
|
|
{
|
2020-10-09 10:59:52 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(TargetSelector))
|
|
|
|
|
await JsInvokeAsync<DomRect>(JSInteropConstants.BackTop);
|
|
|
|
|
else
|
|
|
|
|
await JsInvokeAsync<DomRect>(JSInteropConstants.BackTop, TargetSelector);
|
|
|
|
|
|
|
|
|
|
if (OnClick.HasDelegate)
|
|
|
|
|
await OnClick.InvokeAsync(null);
|
2020-03-27 17:24:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
SetClass();
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
2020-10-14 17:33:36 +08:00
|
|
|
|
protected async override Task OnFirstAfterRenderAsync()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(TargetSelector))
|
|
|
|
|
{
|
2021-09-09 12:56:11 +08:00
|
|
|
|
DomEventListener.AddExclusive<JsonElement>("window", "scroll", OnScroll);
|
2020-10-14 17:33:36 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-09-09 12:56:11 +08:00
|
|
|
|
DomEventListener.AddExclusive<JsonElement>(TargetSelector, "scroll", OnScroll);
|
2020-10-14 17:33:36 +08:00
|
|
|
|
}
|
|
|
|
|
await base.OnFirstAfterRenderAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 17:24:16 +08:00
|
|
|
|
protected void SetClass()
|
|
|
|
|
{
|
|
|
|
|
string prefixCls = "ant-back-top";
|
2020-10-09 10:59:52 +08:00
|
|
|
|
ClassMapper.Add(prefixCls);
|
2021-01-20 15:36:46 +08:00
|
|
|
|
|
|
|
|
|
BackTopContentClassMapper
|
|
|
|
|
.If($"{prefixCls}-content", () => ChildContent == null)
|
2021-03-12 15:46:48 +08:00
|
|
|
|
.If("ant-fade ant-fade-leave ant-fade-leave-active", () => !_visible)
|
|
|
|
|
.If("ant-fade ant-fade-enter ant-fade-enter-active", () => _visible)
|
2021-01-20 15:36:46 +08:00
|
|
|
|
;
|
|
|
|
|
|
2020-03-27 17:24:16 +08:00
|
|
|
|
BackTopIconClassMapper.Add($"{prefixCls}-icon");
|
|
|
|
|
}
|
2020-10-14 17:33:36 +08:00
|
|
|
|
|
|
|
|
|
private async void OnScroll(JsonElement obj)
|
|
|
|
|
{
|
|
|
|
|
JsonElement scrollInfo = await JsInvokeAsync<JsonElement>(JSInteropConstants.GetScroll);
|
|
|
|
|
double offset = scrollInfo.GetProperty("y").GetDouble();
|
|
|
|
|
_visible = offset > VisibilityHeight;
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
2021-09-09 12:56:11 +08:00
|
|
|
|
DomEventListener.DisposeExclusive();
|
2020-10-14 17:33:36 +08:00
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
2020-03-27 17:24:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|