|
4 | 4 | <div
|
5 | 5 | @click="menuClicked(parent, x)"
|
6 | 6 | class="menu_item"
|
| 7 | + draggable="true" |
| 8 | + @dragstart="onDragStart(DragType.Parent, x)" |
| 9 | + @dragenter.prevent="onDragEnter(DragType.Parent, x)" |
7 | 10 | :class="{ active: props.activeIndex === `${x}` }"
|
8 | 11 | >
|
9 | 12 | <Icon icon="ep:fold" color="black" />{{ parent.name }}
|
|
13 | 16 | <div class="subtitle menu_bottom" v-for="(child, y) in parent.children" :key="y">
|
14 | 17 | <div
|
15 | 18 | class="menu_subItem"
|
| 19 | + draggable="true" |
| 20 | + @dragstart="onDragStart(DragType.Child, y)" |
| 21 | + @dragenter.prevent="onDragEnter(DragType.Child, x, y)" |
16 | 22 | v-if="parent.children"
|
17 | 23 | :class="{ active: props.activeIndex === `${x}-${y}` }"
|
18 | 24 | @click="subMenuClicked(child, x, y)"
|
@@ -43,7 +49,7 @@ const props = defineProps<{
|
43 | 49 | modelValue: Menu[]
|
44 | 50 | activeIndex: string
|
45 | 51 | parentIndex: number
|
46 |
| - accountId?: number |
| 52 | + accountId: number |
47 | 53 | }>()
|
48 | 54 |
|
49 | 55 | const emit = defineEmits<{
|
@@ -94,6 +100,44 @@ const menuClicked = (parent: Menu, x: number) => {
|
94 | 100 | const subMenuClicked = (child: Menu, x: number, y: number) => {
|
95 | 101 | emit('submenu-clicked', child, x, y)
|
96 | 102 | }
|
| 103 | +
|
| 104 | +// ======================== 菜单排序 ======================== |
| 105 | +const dragIndex = ref<number>(0) |
| 106 | +enum DragType { |
| 107 | + Parent = 'parent', |
| 108 | + Child = 'child' |
| 109 | +} |
| 110 | +const dragType = ref<DragType>() |
| 111 | +
|
| 112 | +/** |
| 113 | + * 菜单开始拖动回调,记录被拖动菜单的信息(类型,下标) |
| 114 | + * |
| 115 | + * @param type DragType, 拖动类型,父节点、子节点 |
| 116 | + * @param index number, 被拖动的菜单下标 |
| 117 | + */ |
| 118 | +const onDragStart = (type: DragType, index: number) => { |
| 119 | + dragIndex.value = index |
| 120 | + dragType.value = type |
| 121 | +} |
| 122 | +
|
| 123 | +/** |
| 124 | + * 拖动其他菜单位置回调, 判断【被拖动】及【被替换位置】的两个菜单是否同个类型,同类型才会进行插入 |
| 125 | + * |
| 126 | + * @param type: DragType, 拖动类型,父节点、子节点 |
| 127 | + * @param x number, 准备替换父节点位置的下标 |
| 128 | + * @param y number, 准备替换子节点位置的下标, 父节点拖动时可选 |
| 129 | + */ |
| 130 | +const onDragEnter = (type: DragType, x: number, y = -1) => { |
| 131 | + if (dragIndex.value !== x && dragType.value === type) { |
| 132 | + if (type === DragType.Parent) { |
| 133 | + const source = menuList.value.splice(dragIndex.value, 1) |
| 134 | + menuList.value.splice(x, 0, ...source) |
| 135 | + } else { |
| 136 | + const source = menuList.value[x].children?.splice(dragIndex.value, 1) |
| 137 | + menuList.value[x].children?.splice(y, 0, ...(source as any)) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
97 | 141 | </script>
|
98 | 142 |
|
99 | 143 | <style lang="scss" scoped>
|
|
0 commit comments