Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…lds and javascript inlines, and related bugs. Thanks to hejsan for the report, and to Sean Brant and Carl Meyer for the patch. #13068 is a regression caused by the new javascript inline forms in the admin. The others were existing javascript bugs with prepopulated fields. Since the solution depends on jQuery and would likely be very hard to backport without it, it will not be backported to 1.1.X. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12937 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
1 parent
97b9c75
commit 736b751
Showing
8 changed files
with
123 additions
and
23 deletions.
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
(function($) { | ||
$.fn.prepopulate = function(dependencies, maxLength) { | ||
/* | ||
Depends on urlify.js | ||
Populates a selected field with the values of the dependent fields, | ||
URLifies and shortens the string. | ||
dependencies - selected jQuery object of dependent fields | ||
maxLength - maximum length of the URLify'd string | ||
*/ | ||
return this.each(function() { | ||
var field = $(this); | ||
|
||
field.data('_changed', false); | ||
field.change(function() { | ||
field.data('_changed', true); | ||
}); | ||
|
||
var populate = function () { | ||
// Bail if the fields value has changed | ||
if (field.data('_changed') == true) return; | ||
|
||
var values = []; | ||
dependencies.each(function() { | ||
if ($(this).val().length > 0) { | ||
values.push($(this).val()); | ||
} | ||
}); | ||
field.val(URLify(values.join(' '), maxLength)); | ||
}; | ||
|
||
dependencies.keyup(populate).change(populate).focus(populate); | ||
}); | ||
}; | ||
})(jQuery.noConflict()); |
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
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
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
22 changes: 17 additions & 5 deletions
22
django/contrib/admin/templates/admin/prepopulated_fields_js.html
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
<script type="text/javascript"> | ||
(function($) { | ||
var field = null; | ||
|
||
{% for field in prepopulated_fields %} | ||
document.getElementById("{{ field.field.auto_id }}").onchange = function() { this._changed = true; }; | ||
field = { | ||
id: '#{{ field.field.auto_id }}', | ||
dependency_ids: [], | ||
dependency_list: [], | ||
maxLength: {{ field.field.field.max_length|default_if_none:"50" }} | ||
}; | ||
|
||
{% for dependency in field.dependencies %} | ||
document.getElementById("{{ dependency.auto_id }}").onkeyup = function() { | ||
var e = document.getElementById("{{ field.field.auto_id }}"); | ||
if (!e._changed) { e.value = URLify({% for innerdep in field.dependencies %}document.getElementById("{{ innerdep.auto_id }}").value{% if not forloop.last %} + ' ' + {% endif %}{% endfor %}, {{ field.field.field.max_length|default_if_none:"50" }}); } | ||
} | ||
field['dependency_ids'].push('#{{ dependency.auto_id }}'); | ||
field['dependency_list'].push('.{{ dependency.name }}'); | ||
{% endfor %} | ||
|
||
$('.empty-form .{{ field.field.name }}').addClass('prepopulated_field'); | ||
$(field.id).data('dependency_list', field['dependency_list']) | ||
.prepopulate($(field['dependency_ids'].join(',')), field.maxLength); | ||
{% endfor %} | ||
})(jQuery.noConflict()); | ||
</script> |