|
4 | 4 | item-key="id"
|
5 | 5 | ghost-class="draggable-ghost"
|
6 | 6 | :animation="400"
|
7 |
| - @end="onDragEnd" |
| 7 | + @end="onParentDragEnd" |
8 | 8 | >
|
9 | 9 | <template #item="{ element: parent, index: x }">
|
10 | 10 | <div class="menu_bottom">
|
|
23 | 23 | item-key="id"
|
24 | 24 | ghost-class="draggable-ghost"
|
25 | 25 | :animation="400"
|
| 26 | + @end="onChildDragEnd" |
26 | 27 | >
|
27 | 28 | <template #item="{ element: child, index: y }">
|
28 | 29 | <div class="subtitle menu_bottom">
|
@@ -118,42 +119,49 @@ const subMenuClicked = (child: Menu, x: number, y: number) => {
|
118 | 119 | }
|
119 | 120 |
|
120 | 121 | /**
|
121 |
| - * 处理一级菜单展开后被拖动 |
| 122 | + * 处理一级菜单展开后被拖动,激活(展开)原来活动的一级菜单 |
122 | 123 | *
|
123 | 124 | * @param oldIndex: 一级菜单拖动前的位置
|
124 | 125 | * @param newIndex: 一级菜单拖动后的位置
|
125 | 126 | */
|
126 |
| -const onDragEnd = ({ oldIndex, newIndex }) => { |
| 127 | +const onParentDragEnd = ({ oldIndex, newIndex }) => { |
127 | 128 | // 二级菜单没有展开,直接返回
|
128 | 129 | if (props.activeIndex === '__MENU_NOT_SELECTED__') {
|
129 | 130 | return
|
130 | 131 | }
|
131 | 132 |
|
132 |
| - let newParent = props.parentIndex |
133 |
| - if (props.parentIndex === oldIndex) { |
134 |
| - newParent = newIndex |
135 |
| - } else if (props.parentIndex === newIndex) { |
136 |
| - newParent = oldIndex |
137 |
| - } else { |
138 |
| - // 如果展开的二级菜单下标`props.parentIndex`不是被移动的菜单的前后下标。 |
139 |
| - // 那么使用一个辅助素组来模拟菜单移动,然后找到展开的二级菜单的新下标`newParent` |
140 |
| - let positions = new Array<boolean>(menuList.value.length).fill(false) |
141 |
| - positions[props.parentIndex] = true |
142 |
| - positions.splice(oldIndex, 1) |
143 |
| - positions.splice(newIndex, 0, true) |
144 |
| - newParent = positions.indexOf(true) |
145 |
| - } |
| 133 | + // 使用一个辅助数组来模拟菜单移动,然后找到展开的二级菜单的新下标`newParent` |
| 134 | + let positions = new Array<boolean>(menuList.value.length).fill(false) |
| 135 | + positions[props.parentIndex] = true |
| 136 | + const [out] = positions.splice(oldIndex, 1) // 移出菜单,保存到变量out |
| 137 | + positions.splice(newIndex, 0, out) // 把out变量插入被移出的菜单 |
| 138 | + const newParentIndex = positions.indexOf(true) |
146 | 139 |
|
147 | 140 | // 找到菜单元素,触发一级菜单点击
|
148 |
| - const parent = menuList.value[newParent] |
149 |
| - emit('menu-clicked', parent, newParent) |
| 141 | + const parent = menuList.value[newParentIndex] |
| 142 | + emit('menu-clicked', parent, newParentIndex) |
| 143 | +} |
| 144 | +
|
| 145 | +/** |
| 146 | + * 处理二级菜单展开后被拖动,激活被拖动的菜单 |
| 147 | + * |
| 148 | + * @param newIndex 二级菜单拖动后的位置 |
| 149 | + */ |
| 150 | +const onChildDragEnd = ({ newIndex }) => { |
| 151 | + const x = props.parentIndex |
| 152 | + const y = newIndex |
| 153 | + const children = menuList.value[x]?.children |
| 154 | + if (children && children?.length > 0) { |
| 155 | + const child = children[y] |
| 156 | + emit('submenu-clicked', child, x, y) |
| 157 | + } |
150 | 158 | }
|
151 | 159 | </script>
|
152 | 160 |
|
153 | 161 | <style lang="scss" scoped>
|
154 | 162 | .menu_bottom {
|
155 | 163 | position: relative;
|
156 |
| - display: inline-block; |
| 164 | + display: block; |
157 | 165 | float: left;
|
158 | 166 | width: 85.5px;
|
159 | 167 | text-align: center;
|
|
0 commit comments