|
| 1 | +<!-- Tree with all leaf nodes draggable --> |
| 2 | +<script> |
| 3 | +import Tree from 'primevue/tree' |
| 4 | +import { draggable } from '@atlaskit/pragmatic-drag-and-drop/element/adapter' |
| 5 | +import { h, onMounted, onBeforeUnmount, computed } from 'vue' |
| 6 | +
|
| 7 | +export default { |
| 8 | + name: 'TreePlus', |
| 9 | + extends: Tree, |
| 10 | + props: { |
| 11 | + dragSelector: { |
| 12 | + type: String, |
| 13 | + default: '.p-tree-node' |
| 14 | + }, |
| 15 | + // Explicitly declare all v-model props |
| 16 | + expandedKeys: { |
| 17 | + type: Object, |
| 18 | + default: () => ({}) |
| 19 | + }, |
| 20 | + selectionKeys: { |
| 21 | + type: Object, |
| 22 | + default: () => ({}) |
| 23 | + } |
| 24 | + }, |
| 25 | + emits: ['update:expandedKeys', 'update:selectionKeys'], |
| 26 | + setup(props, context) { |
| 27 | + // Create computed properties for each v-model prop |
| 28 | + const computedExpandedKeys = computed({ |
| 29 | + get: () => props.expandedKeys, |
| 30 | + set: (value) => context.emit('update:expandedKeys', value) |
| 31 | + }) |
| 32 | +
|
| 33 | + const computedSelectionKeys = computed({ |
| 34 | + get: () => props.selectionKeys, |
| 35 | + set: (value) => context.emit('update:selectionKeys', value) |
| 36 | + }) |
| 37 | +
|
| 38 | + let observer = null |
| 39 | +
|
| 40 | + const makeDraggable = (element) => { |
| 41 | + if (!element._draggableCleanup) { |
| 42 | + element._draggableCleanup = draggable({ |
| 43 | + element |
| 44 | + }) |
| 45 | + } |
| 46 | + } |
| 47 | +
|
| 48 | + const observeTreeChanges = (treeElement) => { |
| 49 | + observer = new MutationObserver((mutations) => { |
| 50 | + mutations.forEach((mutation) => { |
| 51 | + if (mutation.type === 'childList') { |
| 52 | + mutation.addedNodes.forEach((node) => { |
| 53 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 54 | + node.querySelectorAll(props.dragSelector).forEach(makeDraggable) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | + }) |
| 59 | + }) |
| 60 | +
|
| 61 | + observer.observe(treeElement, { childList: true, subtree: true }) |
| 62 | +
|
| 63 | + // Make existing nodes draggable |
| 64 | + treeElement.querySelectorAll(props.dragSelector).forEach(makeDraggable) |
| 65 | + } |
| 66 | +
|
| 67 | + onMounted(() => { |
| 68 | + const treeElement = document.querySelector('.p-tree') |
| 69 | + if (treeElement) { |
| 70 | + observeTreeChanges(treeElement) |
| 71 | + } |
| 72 | + }) |
| 73 | +
|
| 74 | + onBeforeUnmount(() => { |
| 75 | + if (observer) { |
| 76 | + observer.disconnect() |
| 77 | + } |
| 78 | + // Clean up draggable instances if necessary |
| 79 | + const treeElement = document.querySelector('.p-tree') |
| 80 | + if (treeElement) { |
| 81 | + treeElement.querySelectorAll(props.dragSelector).forEach((node) => { |
| 82 | + if (node._draggableCleanup) { |
| 83 | + node._draggableCleanup() |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | + }) |
| 88 | +
|
| 89 | + return () => |
| 90 | + h( |
| 91 | + Tree, |
| 92 | + { |
| 93 | + ...context.attrs, |
| 94 | + ...props, |
| 95 | + expandedKeys: computedExpandedKeys.value, |
| 96 | + selectionKeys: computedSelectionKeys.value, |
| 97 | + 'onUpdate:expandedKeys': (value) => |
| 98 | + (computedExpandedKeys.value = value), |
| 99 | + 'onUpdate:selectionKeys': (value) => |
| 100 | + (computedSelectionKeys.value = value) |
| 101 | + }, |
| 102 | + context.slots |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
| 106 | +</script> |
0 commit comments