From bec2b4d52e110a146cc66a69b313387b0c4eba84 Mon Sep 17 00:00:00 2001 From: drusepth Date: Mon, 30 Jan 2023 18:36:58 -0800 Subject: [PATCH] work around browser triggering onblur with remote form submissions (oof) --- app/assets/javascripts/autosave.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/autosave.js b/app/assets/javascripts/autosave.js index b974383f..9c969f05 100644 --- a/app/assets/javascripts/autosave.js +++ b/app/assets/javascripts/autosave.js @@ -1,4 +1,6 @@ $(document).ready(function() { + var recent_autosave = false; + $('.autosave-closest-form-on-change').change(function () { var content_form = $(this).closest('form'); @@ -6,7 +8,10 @@ $(document).ready(function() { if (content_form) { // M.toast({ html: 'Saving changes...' }); console.log('saving changes'); - content_form.submit(); + recent_autosave = true; + content_form.trigger('submit'); + + setTimeout(() => recent_autosave = true, 1000); } else { console.log('error saving changes'); // M.toast({ html: "There was an error saving your changes. Please back up any changes and refresh the page." }); @@ -23,6 +28,8 @@ $(document).ready(function() { // To ensure all fields get unblurred (and therefore autosaved) upon navigation, // we use this little ditty: window.onbeforeunload = function(e){ - $(document.activeElement).blur(); + if (!recent_autosave) { + $(document.activeElement).blur(); + } } });