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]
|
|
|
|
|
public DomEventService DomEventService { get; set; }
|
|
|
|
|
|
|
|
|
|
[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))
|
|
|
|
|
{
|
|
|
|
|
DomEventService.AddEventListener("window", "scroll", OnScroll);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DomEventService.AddEventListener(TargetSelector, "scroll", OnScroll);
|
|
|
|
|
}
|
|
|
|
|
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);
|
2020-03-27 17:24:16 +08:00
|
|
|
|
BackTopContentClassMapper.Add($"{prefixCls}-content");
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
if (string.IsNullOrWhiteSpace(TargetSelector))
|
|
|
|
|
{
|
|
|
|
|
DomEventService.RemoveEventListerner<JsonElement>("window", "scroll", OnScroll);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DomEventService.RemoveEventListerner<JsonElement>(TargetSelector, "scroll", OnScroll);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-27 17:24:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|