2022-07-20 04:32:51 -07:00
|
|
|
<template>
|
|
|
|
<div ref="target">
|
|
|
|
<slot :droppable="droppable" :activeDrop="activeDrop"></slot>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2022-11-04 06:04:31 -07:00
|
|
|
import { useNDVStore } from '@/stores/ndv';
|
|
|
|
import { mapStores } from 'pinia';
|
2022-07-20 04:32:51 -07:00
|
|
|
import Vue from 'vue';
|
|
|
|
|
|
|
|
export default Vue.extend({
|
|
|
|
props: {
|
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
},
|
|
|
|
sticky: {
|
|
|
|
type: Boolean,
|
|
|
|
},
|
|
|
|
stickyOffset: {
|
|
|
|
type: Number,
|
|
|
|
default: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
hovering: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
window.addEventListener('mousemove', this.onMouseMove);
|
|
|
|
window.addEventListener('mouseup', this.onMouseUp);
|
|
|
|
},
|
|
|
|
destroyed() {
|
|
|
|
window.removeEventListener('mousemove', this.onMouseMove);
|
|
|
|
window.removeEventListener('mouseup', this.onMouseUp);
|
|
|
|
},
|
|
|
|
computed: {
|
2022-12-14 01:04:10 -08:00
|
|
|
...mapStores(useNDVStore),
|
2022-07-20 04:32:51 -07:00
|
|
|
isDragging(): boolean {
|
2022-11-04 06:04:31 -07:00
|
|
|
return this.ndvStore.isDraggableDragging;
|
2022-07-20 04:32:51 -07:00
|
|
|
},
|
|
|
|
draggableType(): string {
|
2022-11-04 06:04:31 -07:00
|
|
|
return this.ndvStore.draggableType;
|
2022-07-20 04:32:51 -07:00
|
|
|
},
|
|
|
|
droppable(): boolean {
|
|
|
|
return !this.disabled && this.isDragging && this.draggableType === this.type;
|
|
|
|
},
|
|
|
|
activeDrop(): boolean {
|
|
|
|
return this.droppable && this.hovering;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onMouseMove(e: MouseEvent) {
|
|
|
|
const target = this.$refs.target as HTMLElement;
|
|
|
|
|
2022-08-24 05:47:42 -07:00
|
|
|
if (target && this.isDragging) {
|
2022-07-20 04:32:51 -07:00
|
|
|
const dim = target.getBoundingClientRect();
|
|
|
|
|
2022-12-14 01:04:10 -08:00
|
|
|
this.hovering =
|
|
|
|
e.clientX >= dim.left &&
|
|
|
|
e.clientX <= dim.right &&
|
|
|
|
e.clientY >= dim.top &&
|
|
|
|
e.clientY <= dim.bottom;
|
2022-07-20 04:32:51 -07:00
|
|
|
|
2022-09-21 06:44:45 -07:00
|
|
|
if (!this.disabled && this.sticky && this.hovering) {
|
2022-12-14 01:04:10 -08:00
|
|
|
this.ndvStore.setDraggableStickyPos([
|
|
|
|
dim.left + this.stickyOffset,
|
|
|
|
dim.top + this.stickyOffset,
|
|
|
|
]);
|
2022-07-20 04:32:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onMouseUp(e: MouseEvent) {
|
|
|
|
if (this.activeDrop) {
|
2022-11-04 06:04:31 -07:00
|
|
|
const data = this.ndvStore.draggableData;
|
2022-07-20 04:32:51 -07:00
|
|
|
this.$emit('drop', data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
activeDrop(active) {
|
2022-11-04 06:04:31 -07:00
|
|
|
this.ndvStore.setDraggableCanDrop(active);
|
2022-07-20 04:32:51 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|