Skip to content

Commit

Permalink
PATCH: feat(bimdata-tree): add dragAndDrop prop to enable/disable dra…
Browse files Browse the repository at this point in the history
…g and drop feature
  • Loading branch information
NicolasRichel committed Apr 11, 2024
1 parent 620fdd0 commit b13e024
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
16 changes: 14 additions & 2 deletions src/BIMDataComponents/BIMDataTree/BIMDataTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
@mouseup.self="onMouseUp"
>
<div class="bimdata-tree__nodes" @mouseleave="onNodesMouseLeave">
<Node v-for="node of data" :key="node.id" :node="node">
<Node
v-for="node of data"
:key="node.id"
:node="node"
:drag-and-drop="dragAndDrop"
>
<slot
name="node"
:node="node"
Expand Down Expand Up @@ -73,6 +78,10 @@ export default {
type: Number,
default: null,
},
dragAndDrop: {
type: Boolean,
default: false,
},
},
emits: ["drop", "click", "hover"],
setup(props, { emit }) {
Expand All @@ -95,7 +104,10 @@ export default {
const dropHelperDisplayed = computed(
() =>
hover.value && state.dragPosition !== null && state.hoveredNode === null
props.dragAndDrop &&
hover.value &&
state.dragPosition !== null &&
state.hoveredNode === null
);
return {
Expand Down
20 changes: 17 additions & 3 deletions src/BIMDataComponents/BIMDataTree/Node.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
v-if="node.children?.length > 0 && expanded"
:node="node"
:depth="depth + 1"
:drag-and-drop="dragAndDrop"
/>
</template>

Expand All @@ -64,20 +65,25 @@ NodeChildren = {
type: Number,
default: 0,
},
dragAndDrop: {
type: Boolean,
default: false,
},
},
render() {
const { node, depth, root } = this;
const { node, depth, dragAndDrop, root } = this;
const state = root.state;
return node.children.map(child =>
h(Node, { node: child, depth, key: child.id }, () =>
h(Node, { node: child, depth, dragAndDrop, key: child.id }, () =>
root.$slots.node?.({
node: child,
depth,
selected: child.id === state.selectedId,
hovered: child.id === state.hoveredNode?.id,
ancestorSelected: state.hasAncestorSelected(child),
dragAndDrop,
})
)
);
Expand All @@ -99,6 +105,10 @@ Node = {
type: Number,
default: 0,
},
dragAndDrop: {
type: Boolean,
default: false,
},
},
setup(props) {
const state = inject("state");
Expand Down Expand Up @@ -154,7 +164,11 @@ Node = {
};
const dropHelperPosition = computed(() => {
if (!state.dragPosition || state.hoveredNode?.id !== props.node.id)
if (
!props.dragAndDrop ||
!state.dragPosition ||
state.hoveredNode?.id !== props.node.id
)
return null;
return state.getNodeDropPosition(props.node, nodeRef.value);
Expand Down
4 changes: 2 additions & 2 deletions src/BIMDataComponents/BIMDataTree/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default function useState(props, emit) {
state.dragPosition = { x: clientX, y: clientY };
};
const onNodeMouseUp = (node, domElement, expanded) => {
if (!state.dragPosition) return;
if (!props.dragAndDrop || !state.dragPosition) return;

if (state.hoveredNode?.id === pressedNode.value?.id) return;

Expand Down Expand Up @@ -131,7 +131,7 @@ export default function useState(props, emit) {
emit("click", null);
};
const onTreeMouseUp = () => {
if (!state.dragPosition) return;
if (!props.dragAndDrop || !state.dragPosition) return;

if (state.trees.indexOf(pressedNode.value) === state.trees.length - 1) {
// to drop the last root node at the end of the list does not fire the drop event
Expand Down

0 comments on commit b13e024

Please sign in to comment.