2020-10-09 15:13:32 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
2021-01-10 18:49:52 +08:00
|
|
|
|
public partial class TreeNodeTitle<TItem> : ComponentBase
|
2020-10-09 15:13:32 +08:00
|
|
|
|
{
|
2021-07-11 18:43:25 +08:00
|
|
|
|
#region fields
|
|
|
|
|
|
|
|
|
|
private const double OffSETX = 25;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
private double _dragTargetClientX = 0;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-10-09 15:13:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 树控件本身
|
|
|
|
|
/// </summary>
|
|
|
|
|
[CascadingParameter(Name = "Tree")]
|
2020-12-30 18:38:35 +08:00
|
|
|
|
public Tree<TItem> TreeComponent { get; set; }
|
2020-10-09 15:13:32 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前节点
|
|
|
|
|
/// </summary>
|
|
|
|
|
[CascadingParameter(Name = "SelfNode")]
|
2020-12-30 18:38:35 +08:00
|
|
|
|
public TreeNode<TItem> SelfNode { get; set; }
|
2020-10-09 15:13:32 +08:00
|
|
|
|
|
2021-01-07 00:41:26 +08:00
|
|
|
|
private bool CanDraggable => TreeComponent.Draggable && !SelfNode.Disabled;
|
2020-10-09 15:13:32 +08:00
|
|
|
|
|
2021-01-07 00:41:26 +08:00
|
|
|
|
private bool IsSwitcherOpen => SelfNode.Expanded && !SelfNode.IsLeaf;
|
2020-10-09 15:13:32 +08:00
|
|
|
|
|
2021-01-07 00:41:26 +08:00
|
|
|
|
private bool IsSwitcherClose => !SelfNode.Expanded && !SelfNode.IsLeaf;
|
2020-10-09 15:13:32 +08:00
|
|
|
|
|
|
|
|
|
protected ClassMapper TitleClassMapper { get; } = new ClassMapper();
|
|
|
|
|
|
|
|
|
|
private void SetTitleClassMapper()
|
|
|
|
|
{
|
2021-07-11 18:43:25 +08:00
|
|
|
|
TitleClassMapper
|
|
|
|
|
.Add("ant-tree-node-content-wrapper")
|
2020-10-09 15:13:32 +08:00
|
|
|
|
.If("draggable", () => CanDraggable)
|
|
|
|
|
.If("ant-tree-node-content-wrapper-open", () => IsSwitcherOpen)
|
|
|
|
|
.If("ant-tree-node-content-wrapper-close", () => IsSwitcherClose)
|
2021-01-07 00:41:26 +08:00
|
|
|
|
.If("ant-tree-node-selected", () => SelfNode.Selected);
|
2020-10-09 15:13:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
SetTitleClassMapper();
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:43:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-10-09 15:13:32 +08:00
|
|
|
|
private async Task OnClick(MouseEventArgs args)
|
|
|
|
|
{
|
2021-01-07 00:41:26 +08:00
|
|
|
|
SelfNode.SetSelected(!SelfNode.Selected);
|
2020-10-09 15:13:32 +08:00
|
|
|
|
if (TreeComponent.OnClick.HasDelegate && args.Button == 0)
|
2020-12-30 18:38:35 +08:00
|
|
|
|
await TreeComponent.OnClick.InvokeAsync(new TreeEventArgs<TItem>(TreeComponent, SelfNode, args));
|
2020-10-09 15:13:32 +08:00
|
|
|
|
else if (TreeComponent.OnContextMenu.HasDelegate && args.Button == 2)
|
2020-12-30 18:38:35 +08:00
|
|
|
|
await TreeComponent.OnContextMenu.InvokeAsync(new TreeEventArgs<TItem>(TreeComponent, SelfNode, args));
|
2021-08-17 23:36:10 +08:00
|
|
|
|
|
|
|
|
|
TreeComponent.UpdateBindData();
|
2020-10-09 15:13:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 18:43:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-10-09 15:13:32 +08:00
|
|
|
|
private async Task OnDblClick(MouseEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (TreeComponent.OnDblClick.HasDelegate && args.Button == 0)
|
2020-12-30 18:38:35 +08:00
|
|
|
|
await TreeComponent.OnDblClick.InvokeAsync(new TreeEventArgs<TItem>(TreeComponent, SelfNode, args));
|
2020-10-09 15:13:32 +08:00
|
|
|
|
}
|
2021-07-11 18:43:25 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void OnDragStart(DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
TreeComponent.DragItem = SelfNode;
|
|
|
|
|
SelfNode.Expand(false);
|
|
|
|
|
if (TreeComponent.OnDragStart.HasDelegate)
|
|
|
|
|
TreeComponent.OnDragStart.InvokeAsync(new TreeEventArgs<TItem>(TreeComponent, SelfNode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Leaving releases the target
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void OnDragLeave(DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SelfNode.DragTarget = false;
|
|
|
|
|
SelfNode.SetParentTargetContainer();
|
|
|
|
|
if (TreeComponent.OnDragLeave.HasDelegate)
|
|
|
|
|
TreeComponent.OnDragLeave.InvokeAsync(new TreeEventArgs<TItem>(TreeComponent, SelfNode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void OnDragEnter(DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (TreeComponent.DragItem == SelfNode) return;
|
|
|
|
|
SelfNode.DragTarget = true;
|
|
|
|
|
_dragTargetClientX = e.ClientX;
|
|
|
|
|
|
|
|
|
|
System.Diagnostics.Debug.WriteLine($"OnDragEnter {SelfNode.Title} {System.Text.Json.JsonSerializer.Serialize(e)}");
|
|
|
|
|
|
|
|
|
|
if (TreeComponent.OnDragEnter.HasDelegate)
|
|
|
|
|
TreeComponent.OnDragEnter.InvokeAsync(new TreeEventArgs<TItem>(TreeComponent, SelfNode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Can be treated as a child if the target is moved to the right beyond the OffsetX distance
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void OnDragOver(DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (TreeComponent.DragItem == SelfNode) return;
|
|
|
|
|
if (e.ClientX - _dragTargetClientX > OffSETX)
|
|
|
|
|
{
|
|
|
|
|
SelfNode.SetTargetBottom();
|
|
|
|
|
SelfNode.SetParentTargetContainer();
|
|
|
|
|
SelfNode.Expand(true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SelfNode.SetTargetBottom(true);
|
|
|
|
|
SelfNode.SetParentTargetContainer(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void OnDrop(DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SelfNode.DragTarget = false;
|
|
|
|
|
SelfNode.SetParentTargetContainer();
|
|
|
|
|
if (SelfNode.DragTargetBottom)
|
|
|
|
|
TreeComponent.DragItem.DragMoveDown(SelfNode);
|
|
|
|
|
else
|
|
|
|
|
TreeComponent.DragItem.DragMoveInto(SelfNode);
|
|
|
|
|
|
|
|
|
|
if (TreeComponent.OnDrop.HasDelegate)
|
2022-11-08 12:50:17 +08:00
|
|
|
|
TreeComponent.OnDrop.InvokeAsync(new TreeEventArgs<TItem>(TreeComponent, TreeComponent.DragItem, e, SelfNode.DragTargetBottom) { TargetNode = SelfNode });
|
2021-07-11 18:43:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Drag the end
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void OnDragEnd(DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (TreeComponent.OnDragEnd.HasDelegate)
|
|
|
|
|
TreeComponent.OnDragEnd.InvokeAsync(new TreeEventArgs<TItem>(TreeComponent, TreeComponent.DragItem) { TargetNode = SelfNode });
|
|
|
|
|
}
|
2020-10-09 15:13:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|