|
| 1 | +<template> |
| 2 | + <v-treeview |
| 3 | + v-model:activated="activated" |
| 4 | + :items="items" |
| 5 | + item-key="id" |
| 6 | + item-value="id" |
| 7 | + activatable |
| 8 | + open-all |
| 9 | + > |
| 10 | + <template v-slot:append="{ item, depth, isFirst, isLast }"> |
| 11 | + <v-icon-btn :disabled="!depth" icon="mdi-arrow-left" @click.stop="move(item, 'left')"></v-icon-btn> |
| 12 | + <v-icon-btn :disabled="isFirst" icon="mdi-arrow-up" @click.stop="move(item, 'up')"></v-icon-btn> |
| 13 | + <v-icon-btn :disabled="isLast" icon="mdi-arrow-down" @click.stop="move(item, 'down')"></v-icon-btn> |
| 14 | + <v-icon-btn :disabled="isFirst" icon="mdi-arrow-right" @click.stop="move(item, 'right')"></v-icon-btn> |
| 15 | + </template> |
| 16 | + </v-treeview> |
| 17 | +</template> |
| 18 | + |
| 19 | +<script setup> |
| 20 | + import { ref, shallowRef } from 'vue' |
| 21 | +
|
| 22 | + const activated = ref([]) |
| 23 | + const root = { |
| 24 | + id: 0, |
| 25 | + children: [ |
| 26 | + { |
| 27 | + id: 1, |
| 28 | + title: 'Office Tools', |
| 29 | + children: [ |
| 30 | + { id: 2, title: 'Calendar' }, |
| 31 | + { id: 3, title: 'Notepad' }, |
| 32 | + ], |
| 33 | + }, |
| 34 | + { |
| 35 | + id: 4, |
| 36 | + title: 'Dev Tools', |
| 37 | + children: [ |
| 38 | + { id: 5, title: 'VS Code' }, |
| 39 | + { id: 6, title: 'Figma' }, |
| 40 | + { id: 7, title: 'Webstorm' }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + ], |
| 44 | + } |
| 45 | + const items = shallowRef([...root.children]) |
| 46 | +
|
| 47 | + function findParent (id, items = [root]) { |
| 48 | + if (items.length === 0) return null |
| 49 | + return items.find(item => item.children?.some(c => c.id === id)) ?? |
| 50 | + findParent(id, items.flatMap(item => item.children ?? [])) |
| 51 | + } |
| 52 | +
|
| 53 | + function findItemBefore (item) { |
| 54 | + return findParent(item.id).children |
| 55 | + .find((_, i, all) => all[i + 1]?.id === item.id) |
| 56 | + } |
| 57 | +
|
| 58 | + function findItemAfter (item) { |
| 59 | + return findParent(item.id).children |
| 60 | + .find((_, i, all) => all[i - 1]?.id === item.id) |
| 61 | + } |
| 62 | +
|
| 63 | + function detach (item) { |
| 64 | + const parent = findParent(item.id) |
| 65 | + parent.children.splice(parent.children.indexOf(item), 1) |
| 66 | + if (parent.children.length === 0) parent.children = undefined |
| 67 | + } |
| 68 | +
|
| 69 | + function injectNextTo (item, target, after = true) { |
| 70 | + if (!target || target === root) return |
| 71 | + detach(item) |
| 72 | + const targetParent = findParent(target.id) |
| 73 | + targetParent.children.splice(targetParent.children.indexOf(target) + (after ? 1 : 0), 0, item) |
| 74 | + activated.value = [item.id] |
| 75 | + } |
| 76 | +
|
| 77 | + function appendTo (item, target) { |
| 78 | + if (!target) return |
| 79 | + detach(item) |
| 80 | + target.children ??= [] |
| 81 | + target.children.push(item) |
| 82 | + activated.value = [item.id] |
| 83 | + } |
| 84 | +
|
| 85 | + function move (item, direction) { |
| 86 | + switch (direction) { |
| 87 | + case 'left': |
| 88 | + injectNextTo(item, findParent(item.id)) |
| 89 | + break |
| 90 | + case 'up': |
| 91 | + injectNextTo(item, findItemBefore(item), false) |
| 92 | + break |
| 93 | + case 'right': |
| 94 | + appendTo(item, findItemBefore(item)) |
| 95 | + break |
| 96 | + case 'down': |
| 97 | + injectNextTo(item, findItemAfter(item)) |
| 98 | + break |
| 99 | + } |
| 100 | + items.value = [...root.children] |
| 101 | + } |
| 102 | +</script> |
| 103 | +
|
| 104 | +<script> |
| 105 | + export default { |
| 106 | + data: () => ({ |
| 107 | + activated: [], |
| 108 | + root: { |
| 109 | + id: 0, |
| 110 | + children: [ |
| 111 | + { |
| 112 | + id: 1, |
| 113 | + title: 'Office Tools', |
| 114 | + children: [ |
| 115 | + { id: 2, title: 'Calendar' }, |
| 116 | + { id: 3, title: 'Notepad' }, |
| 117 | + ], |
| 118 | + }, |
| 119 | + { |
| 120 | + id: 4, |
| 121 | + title: 'Dev Tools', |
| 122 | + children: [ |
| 123 | + { id: 5, title: 'VS Code' }, |
| 124 | + { id: 6, title: 'Figma' }, |
| 125 | + { id: 7, title: 'Webstorm' }, |
| 126 | + ], |
| 127 | + }, |
| 128 | + ], |
| 129 | + }, |
| 130 | + items: [], |
| 131 | + }), |
| 132 | + mounted () { |
| 133 | + this.items = [...this.root.children] |
| 134 | + }, |
| 135 | + methods: { |
| 136 | + findParent (id, items) { |
| 137 | + items ??= [this.root] |
| 138 | + if (items.length === 0) return null |
| 139 | + return items.find(item => item.children?.some(c => c.id === id)) ?? |
| 140 | + this.findParent(id, items.flatMap(item => item.children ?? [])) |
| 141 | + }, |
| 142 | + findItemBefore (item) { |
| 143 | + return this.findParent(item.id).children |
| 144 | + .find((_, i, all) => all[i + 1]?.id === item.id) |
| 145 | + }, |
| 146 | + findItemAfter (item) { |
| 147 | + return this.findParent(item.id).children |
| 148 | + .find((_, i, all) => all[i - 1]?.id === item.id) |
| 149 | + }, |
| 150 | + detach (item) { |
| 151 | + const parent = this.findParent(item.id) |
| 152 | + parent.children.splice(parent.children.indexOf(item), 1) |
| 153 | + if (parent.children.length === 0) parent.children = undefined |
| 154 | + }, |
| 155 | + injectNextTo (item, target, after = true) { |
| 156 | + if (!target || target === this.root) return |
| 157 | + this.detach(item) |
| 158 | + const targetParent = this.findParent(target.id) |
| 159 | + targetParent.children.splice(targetParent.children.indexOf(target) + (after ? 1 : 0), 0, item) |
| 160 | + this.activated = [item.id] |
| 161 | + }, |
| 162 | + appendTo (item, target) { |
| 163 | + if (!target) return |
| 164 | + this.detach(item) |
| 165 | + target.children ??= [] |
| 166 | + target.children.push(item) |
| 167 | + this.activated = [item.id] |
| 168 | + }, |
| 169 | + move (item, direction) { |
| 170 | + switch (direction) { |
| 171 | + case 'left': |
| 172 | + this.injectNextTo(item, this.findParent(item.id)) |
| 173 | + break |
| 174 | + case 'up': |
| 175 | + this.injectNextTo(item, this.findItemBefore(item), false) |
| 176 | + break |
| 177 | + case 'right': |
| 178 | + this.appendTo(item, this.findItemBefore(item)) |
| 179 | + break |
| 180 | + case 'down': |
| 181 | + this.injectNextTo(item, this.findItemAfter(item)) |
| 182 | + break |
| 183 | + } |
| 184 | + this.items = [...this.root.children] |
| 185 | + }, |
| 186 | + }, |
| 187 | + } |
| 188 | +</script> |
0 commit comments