using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; namespace AntDesign { public partial class TreeNodeTitle : ComponentBase { #region fields private const double OffSETX = 25; /// /// /// private double _dragTargetClientX = 0; #endregion /// /// 树控件本身 /// [CascadingParameter(Name = "Tree")] public Tree TreeComponent { get; set; } /// /// 当前节点 /// [CascadingParameter(Name = "SelfNode")] public TreeNode SelfNode { get; set; } private bool CanDraggable => TreeComponent.Draggable && !SelfNode.Disabled; private bool IsSwitcherOpen => SelfNode.Expanded && !SelfNode.IsLeaf; private bool IsSwitcherClose => !SelfNode.Expanded && !SelfNode.IsLeaf; protected ClassMapper TitleClassMapper { get; } = new ClassMapper(); private void SetTitleClassMapper() { TitleClassMapper .Add("ant-tree-node-content-wrapper") .If("draggable", () => CanDraggable) .If("ant-tree-node-content-wrapper-open", () => IsSwitcherOpen) .If("ant-tree-node-content-wrapper-close", () => IsSwitcherClose) .If("ant-tree-node-selected", () => SelfNode.Selected); } protected override void OnInitialized() { SetTitleClassMapper(); base.OnInitialized(); } /// /// /// /// /// private async Task OnClick(MouseEventArgs args) { SelfNode.SetSelected(!SelfNode.Selected); if (TreeComponent.OnClick.HasDelegate && args.Button == 0) await TreeComponent.OnClick.InvokeAsync(new TreeEventArgs(TreeComponent, SelfNode, args)); else if (TreeComponent.OnContextMenu.HasDelegate && args.Button == 2) await TreeComponent.OnContextMenu.InvokeAsync(new TreeEventArgs(TreeComponent, SelfNode, args)); TreeComponent.UpdateBindData(); } /// /// /// /// /// private async Task OnDblClick(MouseEventArgs args) { if (TreeComponent.OnDblClick.HasDelegate && args.Button == 0) await TreeComponent.OnDblClick.InvokeAsync(new TreeEventArgs(TreeComponent, SelfNode, args)); } /// /// /// /// private void OnDragStart(DragEventArgs e) { TreeComponent.DragItem = SelfNode; SelfNode.Expand(false); if (TreeComponent.OnDragStart.HasDelegate) TreeComponent.OnDragStart.InvokeAsync(new TreeEventArgs(TreeComponent, SelfNode)); } /// /// Leaving releases the target /// /// private void OnDragLeave(DragEventArgs e) { SelfNode.DragTarget = false; SelfNode.SetParentTargetContainer(); if (TreeComponent.OnDragLeave.HasDelegate) TreeComponent.OnDragLeave.InvokeAsync(new TreeEventArgs(TreeComponent, SelfNode)); } /// /// /// /// 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(TreeComponent, SelfNode)); } /// /// Can be treated as a child if the target is moved to the right beyond the OffsetX distance /// /// 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); } } /// /// /// /// 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) TreeComponent.OnDrop.InvokeAsync(new TreeEventArgs(TreeComponent, TreeComponent.DragItem) { TargetNode = SelfNode }); } /// /// Drag the end /// /// private void OnDragEnd(DragEventArgs e) { if (TreeComponent.OnDragEnd.HasDelegate) TreeComponent.OnDragEnd.InvokeAsync(new TreeEventArgs(TreeComponent, TreeComponent.DragItem) { TargetNode = SelfNode }); } } }