Skip to content

Commit

Permalink
Delay dirty form check data snapshot to avoid race conditions. Fix #4978
Browse files Browse the repository at this point in the history
 (#5469)

User interaction with the form within the 10s delay also won’t trigger the confirmation message. There will still be race condition issues if form widgets like rich text take 10+ seconds to initialise – but that doesn’t seem likely.
  • Loading branch information
thibaudcolas authored and gasman committed Jul 30, 2019
1 parent 56aadf8 commit 6deb5a8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions wagtail/admin/static_src/wagtailadmin/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,23 @@ function enableDirtyFormCheck(formSelector, options) {
var $form = $(formSelector);
var confirmationMessage = options.confirmationMessage || ' ';
var alwaysDirty = options.alwaysDirty || false;
var initialData = $form.serialize();
var initialData = null;
var formSubmitted = false;

$form.on('submit', function() {
formSubmitted = true;
});

// Delay snapshotting the form’s data to avoid race conditions with form widgets that might process the values.
// User interaction with the form within that delay also won’t trigger the confirmation message.
setTimeout(function() {
initialData = $form.serialize();
}, 1000 * 10);

window.addEventListener('beforeunload', function(event) {
var isDirty = initialData && $form.serialize() != initialData;
var displayConfirmation = (
!formSubmitted && (alwaysDirty || $form.serialize() != initialData)
!formSubmitted && (alwaysDirty || isDirty)
);

if (displayConfirmation) {
Expand Down

0 comments on commit 6deb5a8

Please sign in to comment.