From 3ad9ab15d54d5279a5e0a65ff2595bfff7b0ab09 Mon Sep 17 00:00:00 2001 From: Iziram Date: Tue, 24 Oct 2023 10:49:29 +0200 Subject: [PATCH] Assiduite : saisie sans date Fix #763 Close #760 --- app/static/js/assiduites.js | 37 +++++++++++++++---- .../pages/signal_assiduites_etud.j2 | 1 - 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/static/js/assiduites.js b/app/static/js/assiduites.js index d077ee0c2..ecc2ce516 100644 --- a/app/static/js/assiduites.js +++ b/app/static/js/assiduites.js @@ -573,20 +573,43 @@ function formatDate(date, styles = { dateStyle: "full" }) { function updateDate() { const dateInput = document.querySelector("#tl_date"); - const date = dateInput.valueAsDate; + const date = dateInput.valueAsDate ?? new Date(); + let dateStr = ""; if (!verifyNonWorkDays(date.getDay(), nonWorkDays)) { - $("#datestr").text(formatDate(date).capitalize()); - dateInput.setAttribute("value", date.toISOString().split("T")[0]); - return true; + dateStr = formatDate(date).capitalize(); } else { + // On se rend au dernier jour travaillé disponible + const lastWorkDay = getNearestWorkDay(date); const att = document.createTextNode( `Le jour sélectionné (${formatDate(date)}) n'est pas un jour travaillé.` ); - openAlertModal("Erreur", att, "", "crimson"); - dateInput.value = dateInput.getAttribute("value"); - return false; + const div = document.createElement("div"); + div.appendChild(att); + div.appendChild(document.createElement("br")); + div.appendChild( + document.createTextNode( + `Le dernier jour travaillé disponible a été sélectionné : ${formatDate( + lastWorkDay + )}.` + ) + ); + openAlertModal("Attention", div, "", "#eec660"); + dateInput.value = lastWorkDay.toISOString().split("T")[0]; + dateStr = formatDate(lastWorkDay).capitalize(); } + document.querySelector("#datestr").textContent = dateStr; + return true; +} + +function getNearestWorkDay(date) { + const aDay = 86400000; // 24 * 3600 * 1000 | H * s * ms + let day = date; + let count = 0; + while (verifyNonWorkDays(day.getDay(), nonWorkDays) && count++ < 7) { + day = new Date(day - aDay); + } + return day; } function verifyDateInSemester() { diff --git a/app/templates/assiduites/pages/signal_assiduites_etud.j2 b/app/templates/assiduites/pages/signal_assiduites_etud.j2 index 7dc6cc3bb..7a55c7a9e 100644 --- a/app/templates/assiduites/pages/signal_assiduites_etud.j2 +++ b/app/templates/assiduites/pages/signal_assiduites_etud.j2 @@ -126,7 +126,6 @@ function setTimeLineTimes(a, b) { setPeriodValues(a, b); updateJustifyBtn(); - } window.forceModule = "{{ forcer_module }}"