From 044926fd62819d818cc0de68bdcaa8db172a36f6 Mon Sep 17 00:00:00 2001 From: iziram Date: Mon, 11 Sep 2023 10:45:21 +0200 Subject: [PATCH] Assiduites : saisie sur mobile #723 --- app/templates/assiduites/widgets/timeline.j2 | 138 +++++++++++-------- 1 file changed, 80 insertions(+), 58 deletions(-) diff --git a/app/templates/assiduites/widgets/timeline.j2 b/app/templates/assiduites/widgets/timeline.j2 index 706c5f50..576e3021 100644 --- a/app/templates/assiduites/widgets/timeline.j2 +++ b/app/templates/assiduites/widgets/timeline.j2 @@ -18,6 +18,8 @@ const period_default = {{ periode_defaut }}; + let handleMoving = false; + function createTicks() { let i = t_start; @@ -87,72 +89,92 @@ } - function setupTimeLine(callback) { + function timelineMainEvent(event, callback) { const func_call = callback ? callback : () => { }; - timelineContainer.addEventListener("mousedown", (event) => { - const startX = event.clientX; - if (event.target === periodTimeLine) { - const startLeft = parseFloat(periodTimeLine.style.left); + const startX = (event.clientX || event.changedTouches[0].clientX); - const onMouseMove = (moveEvent) => { - const deltaX = moveEvent.clientX - startX; - const containerWidth = timelineContainer.clientWidth; + if (event.target.classList.contains("period-handle")) { + const startWidth = parseFloat(periodTimeLine.style.width); + const startLeft = parseFloat(periodTimeLine.style.left); + const isLeftHandle = event.target.classList.contains("left"); + handleMoving = true + const onMouseMove = (moveEvent) => { + + if (!handleMoving) return; + + const deltaX = (moveEvent.clientX || moveEvent.changedTouches[0].clientX) - startX; + const containerWidth = timelineContainer.clientWidth; + const newWidth = + startWidth + ((isLeftHandle ? -deltaX : deltaX) / containerWidth) * 100; + + if (isLeftHandle) { const newLeft = startLeft + (deltaX / containerWidth) * 100; + adjustPeriodPosition(newLeft, newWidth); + } else { + adjustPeriodPosition(parseFloat(periodTimeLine.style.left), newWidth); + } - adjustPeriodPosition(newLeft, parseFloat(periodTimeLine.style.width)); + updatePeriodTimeLabel(); + }; + const mouseUp = () => { + snapHandlesToQuarters(); + generateAllEtudRow(); - updatePeriodTimeLabel(); - }; + timelineContainer.removeEventListener("mousemove", onMouseMove); + handleMoving = false; + func_call(); - document.addEventListener("mousemove", onMouseMove); - document.addEventListener( - "mouseup", - () => { - generateAllEtudRow(); - snapHandlesToQuarters(); - document.removeEventListener("mousemove", onMouseMove); - func_call(); - }, - { once: true } - ); - } else if (event.target.classList.contains("period-handle")) { - const startWidth = parseFloat(periodTimeLine.style.width); - const startLeft = parseFloat(periodTimeLine.style.left); - const isLeftHandle = event.target.classList.contains("left"); - - const onMouseMove = (moveEvent) => { - const deltaX = moveEvent.clientX - startX; - const containerWidth = timelineContainer.clientWidth; - const newWidth = - startWidth + ((isLeftHandle ? -deltaX : deltaX) / containerWidth) * 100; - - if (isLeftHandle) { - const newLeft = startLeft + (deltaX / containerWidth) * 100; - adjustPeriodPosition(newLeft, newWidth); - } else { - adjustPeriodPosition(parseFloat(periodTimeLine.style.left), newWidth); - } - - updatePeriodTimeLabel(); - }; - - document.addEventListener("mousemove", onMouseMove); - document.addEventListener( - "mouseup", - () => { - snapHandlesToQuarters(); - generateAllEtudRow(); - - document.removeEventListener("mousemove", onMouseMove); - - func_call(); - - }, - { once: true } - ); } - }); + timelineContainer.addEventListener("mousemove", onMouseMove); + timelineContainer.addEventListener("touchmove", onMouseMove); + document.addEventListener( + "mouseup", + mouseUp, + ); + document.addEventListener( + "touchend", + mouseUp, + ); + } else if (event.target === periodTimeLine) { + + const startLeft = parseFloat(periodTimeLine.style.left); + + const onMouseMove = (moveEvent) => { + console.warn("move Period") + if (handleMoving) return; + const deltaX = (moveEvent.clientX || moveEvent.changedTouches[0].clientX) - startX; + const containerWidth = timelineContainer.clientWidth; + const newLeft = startLeft + (deltaX / containerWidth) * 100; + + adjustPeriodPosition(newLeft, parseFloat(periodTimeLine.style.width)); + + updatePeriodTimeLabel(); + }; + const mouseUp = () => { + generateAllEtudRow(); + snapHandlesToQuarters(); + timelineContainer.removeEventListener("mousemove", onMouseMove); + func_call(); + } + timelineContainer.addEventListener("mousemove", onMouseMove); + timelineContainer.addEventListener("touchmove", onMouseMove); + document.addEventListener( + "mouseup", + mouseUp, + { once: true } + ); + document.addEventListener( + "touchend", + mouseUp, + { once: true } + ); + } + } + + function setupTimeLine(callback) { + timelineContainer.addEventListener("mousedown", (e) => { timelineMainEvent(e, callback) }); + timelineContainer.addEventListener("touchstart", (e) => { timelineMainEvent(e, callback) }); } function adjustPeriodPosition(newLeft, newWidth) {