123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- // WIP
- import { ref, onUnmounted } from "vue";
- export default () => {
- const dragBox = ref({
- top: 0,
- left: 0,
- pos1: 0,
- pos2: 0,
- pos3: 0,
- pos4: 0,
- width: 400,
- height: 50,
- initial: {
- top: 0,
- left: 0
- },
- latest: {
- top: null,
- left: null
- },
- debounceTimeout: null,
- lastTappedDate: 0
- });
- const onDragBoxUpdate = ref();
- const setInitialBox = (initial, reset) => {
- dragBox.value.initial = initial || dragBox.value.initial;
- if (reset)
- dragBox.value = { ...dragBox.value, ...dragBox.value.initial };
- };
- const resetBoxPosition = preventUpdate => {
- setInitialBox(null, true);
- dragBox.value.latest.top = dragBox.value.top;
- dragBox.value.latest.left = dragBox.value.left;
- if (!preventUpdate && typeof onDragBoxUpdate.value === "function")
- onDragBoxUpdate.value();
- };
- const onDragBox = e => {
- const e1 = e || window.event;
- const e1IsTouch = e1.type === "touchstart";
- e1.preventDefault();
- if (e1IsTouch) {
- // Handle double click from touch (if this method is twice in a row within one second)
- if (Date.now() - dragBox.value.lastTappedDate <= 1000) {
- resetBoxPosition();
- dragBox.value.lastTappedDate = 0;
- return;
- }
- dragBox.value.lastTappedDate = Date.now();
- }
- dragBox.value.pos3 = e1IsTouch
- ? e1.changedTouches[0].clientX
- : e1.clientX;
- dragBox.value.pos4 = e1IsTouch
- ? e1.changedTouches[0].clientY
- : e1.clientY;
- document.onmousemove = document.ontouchmove = e => {
- const e2 = e || window.event;
- const e2IsTouch = e2.type === "touchmove";
- if (!e2IsTouch) e2.preventDefault();
- // Get the clientX and clientY
- const e2ClientX = e2IsTouch
- ? e2.changedTouches[0].clientX
- : e2.clientX;
- const e2ClientY = e2IsTouch
- ? e2.changedTouches[0].clientY
- : e2.clientY;
- // calculate the new cursor position:
- dragBox.value.pos1 = dragBox.value.pos3 - e2ClientX;
- dragBox.value.pos2 = dragBox.value.pos4 - e2ClientY;
- dragBox.value.pos3 = e2ClientX;
- dragBox.value.pos4 = e2ClientY;
- // set the element's new position:
- dragBox.value.top -= dragBox.value.pos2;
- dragBox.value.left -= dragBox.value.pos1;
- if (
- dragBox.value.top >
- document.body.clientHeight - dragBox.value.height
- )
- dragBox.value.top =
- document.body.clientHeight - dragBox.value.height;
- if (dragBox.value.top < 0) dragBox.value.top = 0;
- if (
- dragBox.value.left >
- document.body.clientWidth - dragBox.value.width
- )
- dragBox.value.left =
- document.body.clientWidth - dragBox.value.width;
- if (dragBox.value.left < 0) dragBox.value.left = 0;
- };
- document.onmouseup = document.ontouchend = () => {
- document.onmouseup = null;
- document.ontouchend = null;
- document.onmousemove = null;
- document.ontouchmove = null;
- if (typeof onDragBoxUpdate.value === "function")
- onDragBoxUpdate.value();
- };
- };
- const onWindowResizeDragBox = () => {
- if (dragBox.value.debounceTimeout)
- clearTimeout(dragBox.value.debounceTimeout);
- dragBox.value.debounceTimeout = setTimeout(() => {
- if (
- dragBox.value.top === dragBox.value.latest.top &&
- dragBox.value.left === dragBox.value.latest.left
- )
- resetBoxPosition();
- else {
- if (
- dragBox.value.top >
- document.body.clientHeight - dragBox.value.height
- )
- dragBox.value.top =
- document.body.clientHeight - dragBox.value.height;
- if (dragBox.value.top < 0) dragBox.value.top = 0;
- if (
- dragBox.value.left >
- document.body.clientWidth - dragBox.value.width
- )
- dragBox.value.left =
- document.body.clientWidth - dragBox.value.width;
- if (dragBox.value.left < 0) dragBox.value.left = 0;
- if (typeof onDragBoxUpdate.value === "function")
- onDragBoxUpdate.value();
- }
- }, 50);
- };
- resetBoxPosition(true);
- onWindowResizeDragBox();
- window.addEventListener("resize", onWindowResizeDragBox);
- onUnmounted(() => {
- window.removeEventListener("resize", onWindowResizeDragBox);
- if (dragBox.value.debounceTimeout)
- clearTimeout(dragBox.value.debounceTimeout);
- });
- return {
- dragBox,
- setInitialBox,
- onDragBox,
- resetBoxPosition,
- onWindowResizeDragBox,
- onDragBoxUpdate
- };
- };
|