Skip to content

Commit

Permalink
Fixed #12705 -- Date/time and select filter widgets now work again wi…
Browse files Browse the repository at this point in the history
…th newly added inline forms in the admin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12454 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jbronn committed Feb 17, 2010
1 parent 3425170 commit eaa17e1
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 10 deletions.
4 changes: 4 additions & 0 deletions django/contrib/admin/media/js/SelectFilter2.js
Expand Up @@ -16,6 +16,10 @@ function findForm(node) {


var SelectFilter = { var SelectFilter = {
init: function(field_id, field_name, is_stacked, admin_media_prefix) { init: function(field_id, field_name, is_stacked, admin_media_prefix) {
if (field_id.match(/__prefix__/)){
// Don't intialize on empty forms.
return;
}
var from_box = document.getElementById(field_id); var from_box = document.getElementById(field_id);
from_box.id += '_from'; // change its ID from_box.id += '_from'; // change its ID
from_box.className = 'filtered'; from_box.className = 'filtered';
Expand Down
3 changes: 3 additions & 0 deletions django/contrib/admin/media/js/admin/DateTimeShortcuts.js
Expand Up @@ -11,6 +11,7 @@ var DateTimeShortcuts = {
calendarLinkName: 'calendarlink',// name of the link that is used to toggle calendarLinkName: 'calendarlink',// name of the link that is used to toggle
clockDivName: 'clockbox', // name of clock <div> that gets toggled clockDivName: 'clockbox', // name of clock <div> that gets toggled
clockLinkName: 'clocklink', // name of the link that is used to toggle clockLinkName: 'clocklink', // name of the link that is used to toggle
shortCutsClass: 'datetimeshortcuts', // class of the clock and cal shortcuts
admin_media_prefix: '', admin_media_prefix: '',
init: function() { init: function() {
// Deduce admin_media_prefix by looking at the <script>s in the // Deduce admin_media_prefix by looking at the <script>s in the
Expand Down Expand Up @@ -42,6 +43,7 @@ var DateTimeShortcuts = {


// Shortcut links (clock icon and "Now" link) // Shortcut links (clock icon and "Now" link)
var shortcuts_span = document.createElement('span'); var shortcuts_span = document.createElement('span');
shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling); inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
var now_link = document.createElement('a'); var now_link = document.createElement('a');
now_link.setAttribute('href', "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + get_format('TIME_INPUT_FORMATS')[0] + "'));"); now_link.setAttribute('href', "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + get_format('TIME_INPUT_FORMATS')[0] + "'));");
Expand Down Expand Up @@ -128,6 +130,7 @@ var DateTimeShortcuts = {


// Shortcut links (calendar icon and "Today" link) // Shortcut links (calendar icon and "Today" link)
var shortcuts_span = document.createElement('span'); var shortcuts_span = document.createElement('span');
shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling); inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
var today_link = document.createElement('a'); var today_link = document.createElement('a');
today_link.setAttribute('href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);'); today_link.setAttribute('href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);');
Expand Down
1 change: 0 additions & 1 deletion django/contrib/admin/media/js/inlines.js
Expand Up @@ -34,7 +34,6 @@
var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").attr("autocomplete", "off"); var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").attr("autocomplete", "off");
// only show the add button if we are allowed to add more items // only show the add button if we are allowed to add more items
var showAddButton = ((maxForms.val() == 0) || ((maxForms.val()-totalForms.val()) > 0)); var showAddButton = ((maxForms.val() == 0) || ((maxForms.val()-totalForms.val()) > 0));
var selectedItems = this;
$(this).each(function(i) { $(this).each(function(i) {
$(this).not("." + options.emptyCssClass).addClass(options.formCssClass); $(this).not("." + options.emptyCssClass).addClass(options.formCssClass);
}); });
Expand Down
32 changes: 28 additions & 4 deletions django/contrib/admin/templates/admin/edit_inline/stacked.html
@@ -1,4 +1,4 @@
{% load i18n %} {% load i18n adminmedia %}
<div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group"> <div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
<h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2> <h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2>
{{ inline_admin_formset.formset.management_form }} {{ inline_admin_formset.formset.management_form }}
Expand All @@ -22,12 +22,32 @@ <h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b>&nbsp;<span class=
(function($) { (function($) {
$(document).ready(function() { $(document).ready(function() {
var rows = "#{{ inline_admin_formset.formset.prefix }}-group .inline-related"; var rows = "#{{ inline_admin_formset.formset.prefix }}-group .inline-related";
updateInlineLabel = function(row) { var updateInlineLabel = function(row) {
$(rows).find(".inline_label").each(function(i) { $(rows).find(".inline_label").each(function(i) {
var count = i + 1; var count = i + 1;
$(this).html($(this).html().replace(/(#\d+)/g, "#" + count)); $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
}); });
} }
var reinitDateTimeShortCuts = function() {
// Reinitialize the calendar and clock widgets by force, yuck.
if (typeof DateTimeShortcuts != "undefined") {
$(".datetimeshortcuts").remove();
DateTimeShortcuts.init();
}
}
var updateSelectFilter = function() {
// If any SelectFilter widgets were added, instantiate a new instance.
if (typeof SelectFilter != "undefined"){
$(".selectfilter").each(function(index, value){
var namearr = value.name.split('-');
SelectFilter.init(value.id, namearr[namearr.length-1], false, "{% admin_media_prefix %}");
})
$(".selectfilterstacked").each(function(index, value){
var namearr = value.name.split('-');
SelectFilter.init(value.id, namearr[namearr.length-1], true, "{% admin_media_prefix %}");
})
}
}
$(rows).formset({ $(rows).formset({
prefix: "{{ inline_admin_formset.formset.prefix }}", prefix: "{{ inline_admin_formset.formset.prefix }}",
addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}", addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
Expand All @@ -36,8 +56,12 @@ <h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b>&nbsp;<span class=
deleteText: "{% trans "Remove" %}", deleteText: "{% trans "Remove" %}",
emptyCssClass: "empty-form", emptyCssClass: "empty-form",
removed: updateInlineLabel, removed: updateInlineLabel,
added: updateInlineLabel added: (function(row) {
reinitDateTimeShortCuts();
updateSelectFilter();
updateInlineLabel(row);
})
}); });
}); });
})(jQuery.noConflict()); })(jQuery.noConflict());
</script> </script>
33 changes: 29 additions & 4 deletions django/contrib/admin/templates/admin/edit_inline/tabular.html
@@ -1,4 +1,4 @@
{% load i18n %} {% load i18n adminmedia %}
<div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group"> <div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
<div class="tabular inline-related {% if forloop.last %}last-related{% endif %}"> <div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
{{ inline_admin_formset.formset.management_form }} {{ inline_admin_formset.formset.management_form }}
Expand Down Expand Up @@ -68,11 +68,32 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
(function($) { (function($) {
$(document).ready(function($) { $(document).ready(function($) {
var rows = "#{{ inline_admin_formset.formset.prefix }}-group .tabular.inline-related tbody tr"; var rows = "#{{ inline_admin_formset.formset.prefix }}-group .tabular.inline-related tbody tr";
alternatingRows = function(row) { var alternatingRows = function(row) {
$(rows).not(".add-row").removeClass("row1 row2") $(rows).not(".add-row").removeClass("row1 row2")
.filter(":even").addClass("row1").end() .filter(":even").addClass("row1").end()
.filter(rows + ":odd").addClass("row2"); .filter(rows + ":odd").addClass("row2");
} }
var reinitDateTimeShortCuts = function() {
// Reinitialize the calendar and clock widgets by force
if (typeof DateTimeShortcuts != "undefined") {
$(".datetimeshortcuts").remove();
DateTimeShortcuts.init();
}
}
var updateSelectFilter = function() {
// If any SelectFilter widgets are a part of the new form,
// instantiate a new SelectFilter instance for it.
if (typeof SelectFilter != "undefined"){
$(".selectfilter").each(function(index, value){
var namearr = value.name.split('-');
SelectFilter.init(value.id, namearr[namearr.length-1], false, "{% admin_media_prefix %}");
})
$(".selectfilterstacked").each(function(index, value){
var namearr = value.name.split('-');
SelectFilter.init(value.id, namearr[namearr.length-1], true, "{% admin_media_prefix %}");
})
}
}
$(rows).formset({ $(rows).formset({
prefix: "{{ inline_admin_formset.formset.prefix }}", prefix: "{{ inline_admin_formset.formset.prefix }}",
addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}", addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
Expand All @@ -81,8 +102,12 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
deleteText: "{% trans "Remove" %}", deleteText: "{% trans "Remove" %}",
emptyCssClass: "empty-form", emptyCssClass: "empty-form",
removed: alternatingRows, removed: alternatingRows,
added: alternatingRows added: (function(row) {
reinitDateTimeShortCuts();
updateSelectFilter();
alternatingRows(row);
})
}); });
}); });
})(jQuery.noConflict()); })(jQuery.noConflict());
</script> </script>
3 changes: 3 additions & 0 deletions django/contrib/admin/widgets.py
Expand Up @@ -33,6 +33,9 @@ def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
super(FilteredSelectMultiple, self).__init__(attrs, choices) super(FilteredSelectMultiple, self).__init__(attrs, choices)


def render(self, name, value, attrs=None, choices=()): def render(self, name, value, attrs=None, choices=()):
if attrs is None: attrs = {}
attrs['class'] = 'selectfilter'
if self.is_stacked: attrs['class'] += 'stacked'
output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)] output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)]
output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {') output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
# TODO: "id_" is hard-coded here. This should instead use the correct # TODO: "id_" is hard-coded here. This should instead use the correct
Expand Down
8 changes: 7 additions & 1 deletion tests/regressiontests/admin_widgets/models.py
Expand Up @@ -90,10 +90,16 @@ class CarTire(models.Model):
>>> w = FilteredSelectMultiple('test', False) >>> w = FilteredSelectMultiple('test', False)
>>> print conditional_escape(w.render('test', 'test')) >>> print conditional_escape(w.render('test', 'test'))
<select multiple="multiple" name="test"> <select multiple="multiple" name="test" class="selectfilter">
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script> </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
<BLANKLINE> <BLANKLINE>
>>> w = FilteredSelectMultiple('test', True)
>>> print conditional_escape(w.render('test', 'test'))
<select multiple="multiple" name="test" class="selectfilterstacked">
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
<BLANKLINE>
>>> w = AdminSplitDateTime() >>> w = AdminSplitDateTime()
>>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))) >>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
<p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p> <p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
Expand Down

0 comments on commit eaa17e1

Please sign in to comment.