Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12705 -- Date/time and select filter widgets now work again wi…

…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...
commit eaa17e13e849a18b0b0fb19468bd65a31b86b3b7 1 parent 3425170
Justin Bronn authored February 17, 2010
4  django/contrib/admin/media/js/SelectFilter2.js
@@ -16,6 +16,10 @@ function findForm(node) {
16 16
 
17 17
 var SelectFilter = {
18 18
     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
+        }
19 23
         var from_box = document.getElementById(field_id);
20 24
         from_box.id += '_from'; // change its ID
21 25
         from_box.className = 'filtered';
3  django/contrib/admin/media/js/admin/DateTimeShortcuts.js
@@ -11,6 +11,7 @@ var DateTimeShortcuts = {
11 11
     calendarLinkName: 'calendarlink',// name of the link that is used to toggle
12 12
     clockDivName: 'clockbox',        // name of clock <div> that gets toggled
13 13
     clockLinkName: 'clocklink',      // name of the link that is used to toggle
  14
+    shortCutsClass: 'datetimeshortcuts', // class of the clock and cal shortcuts
14 15
     admin_media_prefix: '',
15 16
     init: function() {
16 17
         // Deduce admin_media_prefix by looking at the <script>s in the
@@ -42,6 +43,7 @@ var DateTimeShortcuts = {
42 43
 
43 44
         // Shortcut links (clock icon and "Now" link)
44 45
         var shortcuts_span = document.createElement('span');
  46
+        shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
45 47
         inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
46 48
         var now_link = document.createElement('a');
47 49
         now_link.setAttribute('href', "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + get_format('TIME_INPUT_FORMATS')[0] + "'));");
@@ -128,6 +130,7 @@ var DateTimeShortcuts = {
128 130
 
129 131
         // Shortcut links (calendar icon and "Today" link)
130 132
         var shortcuts_span = document.createElement('span');
  133
+        shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
131 134
         inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
132 135
         var today_link = document.createElement('a');
133 136
         today_link.setAttribute('href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);');
1  django/contrib/admin/media/js/inlines.js
@@ -34,7 +34,6 @@
34 34
 		var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").attr("autocomplete", "off");
35 35
 		// only show the add button if we are allowed to add more items
36 36
 		var showAddButton = ((maxForms.val() == 0) || ((maxForms.val()-totalForms.val()) > 0));
37  
-		var selectedItems = this;
38 37
 		$(this).each(function(i) {
39 38
 			$(this).not("." + options.emptyCssClass).addClass(options.formCssClass);
40 39
 		});
32  django/contrib/admin/templates/admin/edit_inline/stacked.html
... ...
@@ -1,4 +1,4 @@
1  
-{% load i18n %}
  1
+{% load i18n adminmedia %}
2 2
 <div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
3 3
   <h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2>
4 4
 {{ inline_admin_formset.formset.management_form }}
@@ -22,12 +22,32 @@
22 22
 (function($) {
23 23
     $(document).ready(function() {
24 24
         var rows = "#{{ inline_admin_formset.formset.prefix }}-group .inline-related";
25  
-        updateInlineLabel = function(row) {
  25
+        var updateInlineLabel = function(row) {
26 26
             $(rows).find(".inline_label").each(function(i) {
27 27
                 var count = i + 1;
28 28
                 $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
29 29
             });
30 30
         }
  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
+        }
31 51
         $(rows).formset({
32 52
             prefix: "{{ inline_admin_formset.formset.prefix }}",
33 53
             addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
@@ -36,8 +56,12 @@
36 56
             deleteText: "{% trans "Remove" %}",
37 57
             emptyCssClass: "empty-form",
38 58
             removed: updateInlineLabel,
39  
-            added: updateInlineLabel
  59
+            added: (function(row) {
  60
+                reinitDateTimeShortCuts();
  61
+                updateSelectFilter();
  62
+                updateInlineLabel(row);
  63
+            })
40 64
         });
41 65
     });
42 66
 })(jQuery.noConflict());
43  
-</script>
  67
+</script>
33  django/contrib/admin/templates/admin/edit_inline/tabular.html
... ...
@@ -1,4 +1,4 @@
1  
-{% load i18n %}
  1
+{% load i18n adminmedia %}
2 2
 <div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
3 3
   <div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
4 4
 {{ inline_admin_formset.formset.management_form }}
@@ -68,11 +68,32 @@
68 68
 (function($) {
69 69
     $(document).ready(function($) {
70 70
         var rows = "#{{ inline_admin_formset.formset.prefix }}-group .tabular.inline-related tbody tr";
71  
-        alternatingRows = function(row) {
  71
+        var alternatingRows = function(row) {
72 72
             $(rows).not(".add-row").removeClass("row1 row2")
73 73
                 .filter(":even").addClass("row1").end()
74 74
                 .filter(rows + ":odd").addClass("row2");
75 75
         }
  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
+        }
76 97
         $(rows).formset({
77 98
             prefix: "{{ inline_admin_formset.formset.prefix }}",
78 99
             addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
@@ -81,8 +102,12 @@
81 102
             deleteText: "{% trans "Remove" %}",
82 103
             emptyCssClass: "empty-form",
83 104
             removed: alternatingRows,
84  
-            added: alternatingRows
  105
+            added: (function(row) {
  106
+                reinitDateTimeShortCuts();
  107
+                updateSelectFilter();
  108
+                alternatingRows(row);
  109
+            })
85 110
         });
86 111
     });
87 112
 })(jQuery.noConflict());
88  
-</script>
  113
+</script>
3  django/contrib/admin/widgets.py
@@ -33,6 +33,9 @@ def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
33 33
         super(FilteredSelectMultiple, self).__init__(attrs, choices)
34 34
 
35 35
     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'
36 39
         output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)]
37 40
         output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
38 41
         # TODO: "id_" is hard-coded here. This should instead use the correct
8  tests/regressiontests/admin_widgets/models.py
@@ -90,10 +90,16 @@ class CarTire(models.Model):
90 90
 
91 91
 >>> w = FilteredSelectMultiple('test', False)
92 92
 >>> print conditional_escape(w.render('test', 'test'))
93  
-<select multiple="multiple" name="test">
  93
+<select multiple="multiple" name="test" class="selectfilter">
94 94
 </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
95 95
 <BLANKLINE>
96 96
 
  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
+
97 103
 >>> w = AdminSplitDateTime()
98 104
 >>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
99 105
 <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 notes on commit eaa17e1

Please sign in to comment.
Something went wrong with that request. Please try again.