Skip to content

Commit eaa17e1

Browse files
committed
Fixed #12705 -- Date/time and select filter widgets now work again with newly added inline forms in the admin.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12454 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 3425170 commit eaa17e1

File tree

7 files changed

+74
-10
lines changed

7 files changed

+74
-10
lines changed

django/contrib/admin/media/js/SelectFilter2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ function findForm(node) {
1616

1717
var SelectFilter = {
1818
init: function(field_id, field_name, is_stacked, admin_media_prefix) {
19+
if (field_id.match(/__prefix__/)){
20+
// Don't intialize on empty forms.
21+
return;
22+
}
1923
var from_box = document.getElementById(field_id);
2024
from_box.id += '_from'; // change its ID
2125
from_box.className = 'filtered';

django/contrib/admin/media/js/admin/DateTimeShortcuts.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var DateTimeShortcuts = {
1111
calendarLinkName: 'calendarlink',// name of the link that is used to toggle
1212
clockDivName: 'clockbox', // name of clock <div> that gets toggled
1313
clockLinkName: 'clocklink', // name of the link that is used to toggle
14+
shortCutsClass: 'datetimeshortcuts', // class of the clock and cal shortcuts
1415
admin_media_prefix: '',
1516
init: function() {
1617
// Deduce admin_media_prefix by looking at the <script>s in the
@@ -42,6 +43,7 @@ var DateTimeShortcuts = {
4243

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

129131
// Shortcut links (calendar icon and "Today" link)
130132
var shortcuts_span = document.createElement('span');
133+
shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
131134
inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
132135
var today_link = document.createElement('a');
133136
today_link.setAttribute('href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);');

django/contrib/admin/media/js/inlines.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").attr("autocomplete", "off");
3535
// only show the add button if we are allowed to add more items
3636
var showAddButton = ((maxForms.val() == 0) || ((maxForms.val()-totalForms.val()) > 0));
37-
var selectedItems = this;
3837
$(this).each(function(i) {
3938
$(this).not("." + options.emptyCssClass).addClass(options.formCssClass);
4039
});

django/contrib/admin/templates/admin/edit_inline/stacked.html

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% load i18n %}
1+
{% load i18n adminmedia %}
22
<div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
33
<h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2>
44
{{ inline_admin_formset.formset.management_form }}
@@ -22,12 +22,32 @@ <h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b>&nbsp;<span class=
2222
(function($) {
2323
$(document).ready(function() {
2424
var rows = "#{{ inline_admin_formset.formset.prefix }}-group .inline-related";
25-
updateInlineLabel = function(row) {
25+
var updateInlineLabel = function(row) {
2626
$(rows).find(".inline_label").each(function(i) {
2727
var count = i + 1;
2828
$(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
2929
});
3030
}
31+
var reinitDateTimeShortCuts = function() {
32+
// Reinitialize the calendar and clock widgets by force, yuck.
33+
if (typeof DateTimeShortcuts != "undefined") {
34+
$(".datetimeshortcuts").remove();
35+
DateTimeShortcuts.init();
36+
}
37+
}
38+
var updateSelectFilter = function() {
39+
// If any SelectFilter widgets were added, instantiate a new instance.
40+
if (typeof SelectFilter != "undefined"){
41+
$(".selectfilter").each(function(index, value){
42+
var namearr = value.name.split('-');
43+
SelectFilter.init(value.id, namearr[namearr.length-1], false, "{% admin_media_prefix %}");
44+
})
45+
$(".selectfilterstacked").each(function(index, value){
46+
var namearr = value.name.split('-');
47+
SelectFilter.init(value.id, namearr[namearr.length-1], true, "{% admin_media_prefix %}");
48+
})
49+
}
50+
}
3151
$(rows).formset({
3252
prefix: "{{ inline_admin_formset.formset.prefix }}",
3353
addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
@@ -36,8 +56,12 @@ <h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b>&nbsp;<span class=
3656
deleteText: "{% trans "Remove" %}",
3757
emptyCssClass: "empty-form",
3858
removed: updateInlineLabel,
39-
added: updateInlineLabel
59+
added: (function(row) {
60+
reinitDateTimeShortCuts();
61+
updateSelectFilter();
62+
updateInlineLabel(row);
63+
})
4064
});
4165
});
4266
})(jQuery.noConflict());
43-
</script>
67+
</script>

django/contrib/admin/templates/admin/edit_inline/tabular.html

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% load i18n %}
1+
{% load i18n adminmedia %}
22
<div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
33
<div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
44
{{ inline_admin_formset.formset.management_form }}
@@ -68,11 +68,32 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
6868
(function($) {
6969
$(document).ready(function($) {
7070
var rows = "#{{ inline_admin_formset.formset.prefix }}-group .tabular.inline-related tbody tr";
71-
alternatingRows = function(row) {
71+
var alternatingRows = function(row) {
7272
$(rows).not(".add-row").removeClass("row1 row2")
7373
.filter(":even").addClass("row1").end()
7474
.filter(rows + ":odd").addClass("row2");
7575
}
76+
var reinitDateTimeShortCuts = function() {
77+
// Reinitialize the calendar and clock widgets by force
78+
if (typeof DateTimeShortcuts != "undefined") {
79+
$(".datetimeshortcuts").remove();
80+
DateTimeShortcuts.init();
81+
}
82+
}
83+
var updateSelectFilter = function() {
84+
// If any SelectFilter widgets are a part of the new form,
85+
// instantiate a new SelectFilter instance for it.
86+
if (typeof SelectFilter != "undefined"){
87+
$(".selectfilter").each(function(index, value){
88+
var namearr = value.name.split('-');
89+
SelectFilter.init(value.id, namearr[namearr.length-1], false, "{% admin_media_prefix %}");
90+
})
91+
$(".selectfilterstacked").each(function(index, value){
92+
var namearr = value.name.split('-');
93+
SelectFilter.init(value.id, namearr[namearr.length-1], true, "{% admin_media_prefix %}");
94+
})
95+
}
96+
}
7697
$(rows).formset({
7798
prefix: "{{ inline_admin_formset.formset.prefix }}",
7899
addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
@@ -81,8 +102,12 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
81102
deleteText: "{% trans "Remove" %}",
82103
emptyCssClass: "empty-form",
83104
removed: alternatingRows,
84-
added: alternatingRows
105+
added: (function(row) {
106+
reinitDateTimeShortCuts();
107+
updateSelectFilter();
108+
alternatingRows(row);
109+
})
85110
});
86111
});
87112
})(jQuery.noConflict());
88-
</script>
113+
</script>

django/contrib/admin/widgets.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
3333
super(FilteredSelectMultiple, self).__init__(attrs, choices)
3434

3535
def render(self, name, value, attrs=None, choices=()):
36+
if attrs is None: attrs = {}
37+
attrs['class'] = 'selectfilter'
38+
if self.is_stacked: attrs['class'] += 'stacked'
3639
output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)]
3740
output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
3841
# TODO: "id_" is hard-coded here. This should instead use the correct

tests/regressiontests/admin_widgets/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,16 @@ class CarTire(models.Model):
9090
9191
>>> w = FilteredSelectMultiple('test', False)
9292
>>> print conditional_escape(w.render('test', 'test'))
93-
<select multiple="multiple" name="test">
93+
<select multiple="multiple" name="test" class="selectfilter">
9494
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
9595
<BLANKLINE>
9696
97+
>>> w = FilteredSelectMultiple('test', True)
98+
>>> print conditional_escape(w.render('test', 'test'))
99+
<select multiple="multiple" name="test" class="selectfilterstacked">
100+
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
101+
<BLANKLINE>
102+
97103
>>> w = AdminSplitDateTime()
98104
>>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
99105
<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>

0 commit comments

Comments
 (0)