From db7f2b50b109b7aa4b8cff178dd25c6b96e2f99b Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 28 Oct 2021 23:43:38 +0200 Subject: [PATCH 4/4] js: delay submit until autosave ajax request is finished (#58276) --- wcs/qommon/static/js/qommon.forms.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/wcs/qommon/static/js/qommon.forms.js b/wcs/qommon/static/js/qommon.forms.js index 573f203f..eec2c4d1 100644 --- a/wcs/qommon/static/js/qommon.forms.js +++ b/wcs/qommon/static/js/qommon.forms.js @@ -125,6 +125,8 @@ $(function() { var autosave_timeout_id = null; var autosave_is_running = false; var autosave_xhr = null; + var autosave_button_to_click_on_complete = null; + if ($('form[data-has-draft]').length == 1) { var last_auto_save = $('form[data-has-draft]').serialize(); function autosave() { @@ -151,6 +153,9 @@ $(function() { if (autosave_timeout_id !== null) { install_autosave(); } + if (autosave_button_to_click_on_complete !== null) { + autosave_button_to_click_on_complete.click(); + } } }); } @@ -158,7 +163,7 @@ $(function() { function install_autosave() { // debounce window.clearTimeout(autosave_timeout_id); - autosave_timeout_id = window.setTimeout(autosave, 8000); + autosave_timeout_id = window.setTimeout(autosave, 500); } $(document).on('mouseover scroll keydown', function() { @@ -393,6 +398,14 @@ $(function() { } return true; }); + $('form .buttons.submit button').on('click', function (event) { + if (autosave_is_running) { + autosave_button_to_click_on_complete = event.target; + /* prevent more autosave */ + autosave_timeout_id = null; + event.preventDefault(); + } + }); $('form').on('submit', function(event) { var $form = $(this); /* abort current autosave */ -- 2.33.0