|
@ -1,7 +1,7 @@ |
|
|
<template> |
|
|
<template> |
|
|
<div class="ps-tree component"> |
|
|
<div class="ps-tree component"> |
|
|
<template v-for="(item, index) in list" :key="item.key"> |
|
|
<template v-for="(item, index) in list" :key="item.key"> |
|
|
<node @onDragstart="onDragstart" @onDragEnd="onDragEnd" @onDrop="onDrop" :data-source-key="dataSourceKey" |
|
|
<node @onDragstart="onDragstart" @expand="onExpand" @onDragEnd="onDragEnd" @onDrop="onDrop" :data-source-key="dataSourceKey" |
|
|
:data="item" :list="list" :level="level" @click="(item) => clickNode(item)"> |
|
|
:data="item" :list="list" :level="level" @click="(item) => clickNode(item)"> |
|
|
<template |
|
|
<template |
|
|
#default="{ data, deep, dataSourceKey, status }: { data: INiuTreeData, deep: number, dataSourceKey: INiuTreeKey, status: ENiuTreeStatus }"> |
|
|
#default="{ data, deep, dataSourceKey, status }: { data: INiuTreeData, deep: number, dataSourceKey: INiuTreeKey, status: ENiuTreeStatus }"> |
|
@ -45,6 +45,10 @@ const slot = useSlots() |
|
|
|
|
|
|
|
|
provide("tree:opts", props) |
|
|
provide("tree:opts", props) |
|
|
|
|
|
|
|
|
|
|
|
function onExpand(item: INiuTreeData) { |
|
|
|
|
|
emit("expand", item) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function clickNode(item: INiuTreeData) { |
|
|
function clickNode(item: INiuTreeData) { |
|
|
if (props.justOpenOne) { |
|
|
if (props.justOpenOne) { |
|
|
forEachTree(props.list, (node: INiuTreeData) => { |
|
|
forEachTree(props.list, (node: INiuTreeData) => { |
|
@ -60,6 +64,7 @@ function clickNode(item: INiuTreeData) { |
|
|
|
|
|
|
|
|
const emit = defineEmits<{ |
|
|
const emit = defineEmits<{ |
|
|
(e: 'change'): void |
|
|
(e: 'change'): void |
|
|
|
|
|
(e: 'expand', data: INiuTreeData): void |
|
|
(e: 'itemDragstart'): void |
|
|
(e: 'itemDragstart'): void |
|
|
}>() |
|
|
}>() |
|
|
|
|
|
|
|
@ -78,8 +83,6 @@ async function onDrop(key: INiuTreeKey, status?: ENiuTreeStatus) { |
|
|
let targetData = findByKey(key, props.list) |
|
|
let targetData = findByKey(key, props.list) |
|
|
const sourceKey = dataSourceKey.value; |
|
|
const sourceKey = dataSourceKey.value; |
|
|
dataSourceKey.value = undefined |
|
|
dataSourceKey.value = undefined |
|
|
const isSuccess = (await props.dropFn?.(status, data, targetData)) |
|
|
|
|
|
if (isSuccess == undefined || isSuccess) { |
|
|
|
|
|
switch (status) { |
|
|
switch (status) { |
|
|
case ENiuTreeStatus.DragIn: |
|
|
case ENiuTreeStatus.DragIn: |
|
|
if ( |
|
|
if ( |
|
@ -89,6 +92,8 @@ async function onDrop(key: INiuTreeKey, status?: ENiuTreeStatus) { |
|
|
!isChildOf(key, sourceKey, props.list) && |
|
|
!isChildOf(key, sourceKey, props.list) && |
|
|
targetData.children |
|
|
targetData.children |
|
|
) { |
|
|
) { |
|
|
|
|
|
const isSuccess = (await props.dropFn?.(status, data, targetData)) |
|
|
|
|
|
if (isSuccess == undefined || isSuccess) { |
|
|
removeByKey(sourceKey, props.list) |
|
|
removeByKey(sourceKey, props.list) |
|
|
targetData.children.push(data) |
|
|
targetData.children.push(data) |
|
|
if (props.autoExpand) { |
|
|
if (props.autoExpand) { |
|
@ -96,6 +101,7 @@ async function onDrop(key: INiuTreeKey, status?: ENiuTreeStatus) { |
|
|
} |
|
|
} |
|
|
emit("change") |
|
|
emit("change") |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
break |
|
|
break |
|
|
case ENiuTreeStatus.DragDown: |
|
|
case ENiuTreeStatus.DragDown: |
|
|
// 按索引往列表添加节点 |
|
|
// 按索引往列表添加节点 |
|
@ -105,10 +111,13 @@ async function onDrop(key: INiuTreeKey, status?: ENiuTreeStatus) { |
|
|
sourceKey != key && |
|
|
sourceKey != key && |
|
|
!isChildOf(key, sourceKey, props.list) |
|
|
!isChildOf(key, sourceKey, props.list) |
|
|
) { |
|
|
) { |
|
|
|
|
|
const isSuccess = (await props.dropFn?.(status, data, targetData)) |
|
|
|
|
|
if (isSuccess == undefined || isSuccess) { |
|
|
removeByKey(sourceKey, props.list) |
|
|
removeByKey(sourceKey, props.list) |
|
|
insertAfterByKey(key, data, props.list) |
|
|
insertAfterByKey(key, data, props.list) |
|
|
emit("change") |
|
|
emit("change") |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
break |
|
|
break |
|
|
case ENiuTreeStatus.DragUp: |
|
|
case ENiuTreeStatus.DragUp: |
|
|
// 按索引往列表添加节点 |
|
|
// 按索引往列表添加节点 |
|
@ -118,12 +127,14 @@ async function onDrop(key: INiuTreeKey, status?: ENiuTreeStatus) { |
|
|
sourceKey != key && |
|
|
sourceKey != key && |
|
|
!isChildOf(key, sourceKey, props.list) |
|
|
!isChildOf(key, sourceKey, props.list) |
|
|
) { |
|
|
) { |
|
|
|
|
|
const isSuccess = (await props.dropFn?.(status, data, targetData)) |
|
|
|
|
|
if (isSuccess == undefined || isSuccess) { |
|
|
removeByKey(sourceKey, props.list) |
|
|
removeByKey(sourceKey, props.list) |
|
|
insertBeforeByKey(key, data, props.list) |
|
|
insertBeforeByKey(key, data, props.list) |
|
|
emit("change") |
|
|
emit("change") |
|
|
} |
|
|
} |
|
|
break |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
break |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
</script> |
|
|
</script> |
|
|