diff --git a/avisstage/static/js/editstage.js b/avisstage/static/js/editstage.js index db8bd70..195f195 100644 --- a/avisstage/static/js/editstage.js +++ b/avisstage/static/js/editstage.js @@ -121,7 +121,7 @@ function initEditStage(STATIC_URL, API_URL) { // SAUVEGARDE LOCALE EN CAS DE PÉPIN - function localSave () { + function localSave (original) { cleanForm(); var formvals = {}; var els = $("#stageform").get(0).elements; @@ -137,9 +137,16 @@ function initEditStage(STATIC_URL, API_URL) { extvals["lieux-n"+i] = $("#change-lieux-"+i).text(); } extvals.date = new Date().getTime(); - var locsave = {"form": formvals, "misc": extvals}; - window.localStorage.setItem("stage_save_"+stage_object_id, JSON.stringify(locsave)); - console.log(formvals); + var locfvals = JSON.stringify(formvals); + var locsave = JSON.stringify({"form": formvals, "misc": extvals}); + var origfvals = window.localStorage.getItem("stage_origsave_"+stage_object_id); + if (origfvals === null) // sauvegarde de l'original + window.localStorage.setItem("stage_origsave_"+stage_object_id, + locfvals) + else if(origfvals != locfvals) // modifications depuis l'original + window.localStorage.setItem("stage_save_"+stage_object_id, + locsave); + console.log(origfvals == locfvals, formvals); } function restoreSave () { @@ -180,8 +187,8 @@ function initEditStage(STATIC_URL, API_URL) { var locsave = window.localStorage.getItem("stage_save_"+stage_object_id); if (locsave === null) return; locsave = JSON.parse(locsave); - var last_maj = $("#date_maj").val(); - console.log(last_maj, locsave.misc.date/1000); + var last_maj = Number($("#date_maj").val()); + console.log( locsave.misc.date/1000-last_maj); if (locsave.misc.date/1000 > last_maj+60) { var texte = ""; if(stage_object_id == "new") @@ -190,8 +197,13 @@ function initEditStage(STATIC_URL, API_URL) { texte = "Une sauvegarde automatique plus récente que la dernière modification de cette fiche de stage existe, voulez-vous la récupérer ?"; if(confirm(texte)) { restoreSave(); + return; } + } else { + console.log("clearing save"); + window.localStorage.removeItem("stage_origsave_"+stage_object_id); } + setTimeout(localSave, 1000); } function cleanForm() { diff --git a/avisstage/templates/avisstage/formulaires/stage.html b/avisstage/templates/avisstage/formulaires/stage.html index 909cfff..e4c04b7 100644 --- a/avisstage/templates/avisstage/formulaires/stage.html +++ b/avisstage/templates/avisstage/formulaires/stage.html @@ -29,7 +29,7 @@ {% block content %}