Created
June 28, 2016 22:36
-
-
Save JoshBarr/3ebec1c50c1262b7155f17a38eae2fb3 to your computer and use it in GitHub Desktop.
Fix enableDirtyFormCheck for safari.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% overextends "wagtailadmin/pages/edit.html" %} | |
{% load i18n %} | |
{% load wagtailadmin_tags %} | |
{# Put this in core/templateswagtailadmin/pages/edit.html. Assumes overextends. #} | |
{% block extra_js %} | |
{% include "wagtailadmin/pages/_editor_js.html" %} | |
<script> | |
(function($, window, document) { | |
window.lastClickedIgnoredTrigger = false; | |
function enableDirtyFormCheck(formSelector, options) { | |
var defaultSelector = 'input[type="submit"], button[type="submit"]'; | |
var ignoredButtonsSelector = options.ignoredButtonsSelector ? options.ignoredButtonsSelector : defaultSelector; | |
var $form = $(formSelector); | |
var $ignoredButtons = $form.find(ignoredButtonsSelector); | |
var confirmationMessage = options.confirmationMessage || ' '; | |
var alwaysDirty = options.alwaysDirty || false; | |
var initialData = $form.serialize(); | |
$ignoredButtons.each(function(index) { | |
var $el = $(this); | |
$el.on('click', function(e) { | |
window.lastClickedIgnoredTrigger = this; | |
}) | |
}); | |
window.addEventListener('beforeunload', function(event) { | |
// Ignore if the user clicked on an ignored element | |
var triggeredByIgnoredButton = false; | |
var $trigger = $(event.explicitOriginalTarget || document.activeElement); | |
$ignoredButtons.each(function() { | |
if ($(this).is($trigger) || this === window.lastClickedIgnoredTrigger) { | |
triggeredByIgnoredButton = true; | |
} | |
}); | |
if (dirtyFormCheckIsActive && !triggeredByIgnoredButton && (alwaysDirty || $form.serialize() != initialData)) { | |
event.returnValue = confirmationMessage; | |
return confirmationMessage; | |
} | |
}); | |
} | |
$(function() { | |
/* Make user confirm before leaving the editor if there are unsaved changes */ | |
{% trans "This page has unsaved changes." as confirmation_message %} | |
enableDirtyFormCheck( | |
'#page-edit-form', | |
{ | |
confirmationMessage: '{{ confirmation_message|escapejs }}', | |
{% if form.errors %} | |
alwaysDirty: true, | |
{% endif %} | |
} | |
); | |
}); | |
})($, window, document); | |
</script> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment