1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #31483 -- Rewrote change_form.js without jQuery.

The use of $(document).ready() was removed. The script is loaded at the
end of the document. Therefore, the referenced DOM elements are already
available and the script does not need to wait for the full DOM to be
ready before continuing.

Now that the script has no external dependencies, it can be loaded
asynchronously. As such, the async attribute was added to the script
element.
This commit is contained in:
Jon Dufresne 2020-04-19 15:10:56 -07:00 committed by Carlton Gibson
parent a8bb53dbd2
commit f27482f147
2 changed files with 17 additions and 8 deletions

View File

@ -1,9 +1,17 @@
(function($) {
(function() {
'use strict';
$(document).ready(function() {
var modelName = $('#django-admin-form-add-constants').data('modelName');
if (modelName) {
$('form#' + modelName + '_form :input:visible:enabled:first').focus();
var inputTags = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA'];
var modelName = document.getElementById('django-admin-form-add-constants').dataset.modelName;
if (modelName) {
var form = document.getElementById(modelName + '_form');
for (var i = 0; i < form.elements.length; i++) {
var element = form.elements[i];
// HTMLElement.offsetParent returns null when the element is not
// rendered.
if (inputTags.includes(element.tagName) && !element.disabled && element.offsetParent) {
element.focus();
break;
}
}
});
})(django.jQuery);
}
})();

View File

@ -68,7 +68,8 @@
src="{% static 'admin/js/change_form.js' %}"
{% if adminform and add %}
data-model-name="{{ opts.model_name }}"
{% endif %}>
{% endif %}
async>
</script>
{% endblock %}