diff --git a/src/modules/04_registryPerson/views/edit/components/DialogSort.vue b/src/modules/04_registryPerson/views/edit/components/DialogSort.vue index 8ae625f38..6cae824e4 100644 --- a/src/modules/04_registryPerson/views/edit/components/DialogSort.vue +++ b/src/modules/04_registryPerson/views/edit/components/DialogSort.vue @@ -40,14 +40,14 @@ const profileId = ref(route.params.id.toString()); //id profile /** ข้อมูล Table*/ const rowsData = ref([]); -/** - * fiunction จัดลำดับ - * @param from ตำแหน่งปัจุบัน - * @param to ตำแหน่งที่จะย้ายไป - */ -function onDrop(from: number, to: number) { - rowsData.value.splice(to - 1, 0, rowsData.value.splice(from - 1, 1)[0]); -} +// /** +// * fiunction จัดลำดับ +// * @param from ตำแหน่งปัจุบัน +// * @param to ตำแหน่งที่จะย้ายไป +// */ +// function onDrop(from: number, to: number) { +// rowsData.value.splice(to - 1, 0, rowsData.value.splice(from - 1, 1)[0]); +// } /** function บันทึกการจัดลำดับ*/ function onSubmit() { @@ -72,6 +72,62 @@ function onSubmit() { }); } +let draggedIndex = -1; // index ของ item ที่ถูก drag +const cardSectionRef = ref(null); // ref สำหรับ container ที่จะ scroll + +/** + * ฟังก์ชันนี้ใช้สำหรับการ drag item + * @param index index ของ item ที่ถูก drag + */ +function onDragStart(index: number) { + //จะทำการ set draggedIndex เป็น index ที่ถูก drag + draggedIndex = index; +} + +/** + * ฟังก์ชันนี้ใช้สำหรับการ drop item + * @param targetIndex index ที่จะ drop + */ +function onDrop(targetIndex: number) { + // ถ้า draggedIndex เป็น -1 หรือ draggedIndex เท่ากับ targetIndex จะไม่ทำอะไร + if (draggedIndex === -1 || draggedIndex === targetIndex) return; + // ถ้า draggedIndex ไม่เท่ากับ targetIndex จะทำการย้าย item + const item = rowsData.value.splice(draggedIndex, 1)[0]; + rowsData.value.splice(targetIndex, 0, item); + draggedIndex = -1; +} + +/** + * ฟังก์ชันนี้ใช้สำหรับการเลื่อน scroll ของ container + * @param e DragEvent + * @description ทำให้ scroll container ได้เวลา drag อยู่ใกล้ขอบ + */ +function onDragOver(e: DragEvent) { + let container = cardSectionRef.value; + // ถ้าเป็น vue proxy ให้ใช้ container.$el + if (container && (container as any).$el) { + container = (container as any).$el; + } + // ถ้า container ไม่ใช่ HTMLElement ให้แสดง warning + if ( + !container || + typeof (container as HTMLElement).getBoundingClientRect !== "function" + ) { + console.warn("container is not HTMLElement:", container); + return; + } + const rect = (container as HTMLElement).getBoundingClientRect(); + const offset = 40; // ระยะห่างจากขอบที่ต้องการให้ scroll + const scrollSpeed = 30; // ความเร็วในการ scroll + + // ถ้า mouse อยู่ใกล้ขอบบนหรือขอบล่างของ container ให้ scroll + if (e.clientY < rect.top + offset) { + (container as HTMLElement).scrollTop -= scrollSpeed; + } else if (e.clientY > rect.bottom - offset) { + (container as HTMLElement).scrollTop += scrollSpeed; + } +} + watch(modal, async () => { if (modal.value && props.dataSort) { const dataList = props.dataSort; @@ -82,82 +138,77 @@ watch(modal, async () => { });