Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay dirty form check’s snapshot to avoid race conditions. Fix #4978 #5469

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me null communicates "not yet defined" better than an empty string (which implies "this is defined and has empty content"), so I'm inclined to keep this as is.

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