Code

Ticket #12705: 12705.2.diff

File 12705.2.diff, 9.0 KB (added by jbronn, 4 years ago)

Now works with SelectFIlter

Line 
1diff -r 3e100985dbfa django/contrib/admin/media/js/SelectFilter2.js
2--- a/django/contrib/admin/media/js/SelectFilter2.js    Sun Feb 14 20:17:07 2010 -0600
3+++ b/django/contrib/admin/media/js/SelectFilter2.js    Tue Feb 16 16:17:36 2010 -0600
4@@ -16,6 +16,10 @@
5 
6 var SelectFilter = {
7     init: function(field_id, field_name, is_stacked, admin_media_prefix) {
8+        if (field_id.match(/__prefix__/)){
9+            // Don't intialize on empty forms.
10+            return;
11+        }
12         var from_box = document.getElementById(field_id);
13         from_box.id += '_from'; // change its ID
14         from_box.className = 'filtered';
15diff -r 3e100985dbfa django/contrib/admin/media/js/admin/DateTimeShortcuts.js
16--- a/django/contrib/admin/media/js/admin/DateTimeShortcuts.js  Sun Feb 14 20:17:07 2010 -0600
17+++ b/django/contrib/admin/media/js/admin/DateTimeShortcuts.js  Tue Feb 16 16:17:36 2010 -0600
18@@ -11,6 +11,7 @@
19     calendarLinkName: 'calendarlink',// name of the link that is used to toggle
20     clockDivName: 'clockbox',        // name of clock <div> that gets toggled
21     clockLinkName: 'clocklink',      // name of the link that is used to toggle
22+    shortCutsClass: 'datetimeshortcuts', // class of the clock and cal shortcuts
23     admin_media_prefix: '',
24     init: function() {
25         // Deduce admin_media_prefix by looking at the <script>s in the
26@@ -42,6 +43,7 @@
27 
28         // Shortcut links (clock icon and "Now" link)
29         var shortcuts_span = document.createElement('span');
30+        shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
31         inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
32         var now_link = document.createElement('a');
33         now_link.setAttribute('href', "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().strftime('" + get_format('TIME_INPUT_FORMATS')[0] + "'));");
34@@ -128,6 +130,7 @@
35 
36         // Shortcut links (calendar icon and "Today" link)
37         var shortcuts_span = document.createElement('span');
38+        shortcuts_span.className = DateTimeShortcuts.shortCutsClass;
39         inp.parentNode.insertBefore(shortcuts_span, inp.nextSibling);
40         var today_link = document.createElement('a');
41         today_link.setAttribute('href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);');
42diff -r 3e100985dbfa django/contrib/admin/media/js/inlines.js
43--- a/django/contrib/admin/media/js/inlines.js  Sun Feb 14 20:17:07 2010 -0600
44+++ b/django/contrib/admin/media/js/inlines.js  Tue Feb 16 16:17:36 2010 -0600
45@@ -34,7 +34,6 @@
46                var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").attr("autocomplete", "off");
47                // only show the add button if we are allowed to add more items
48                var showAddButton = ((maxForms.val() == 0) || ((maxForms.val()-totalForms.val()) > 0));
49-               var selectedItems = this;
50                $(this).each(function(i) {
51                        $(this).not("." + options.emptyCssClass).addClass(options.formCssClass);
52                });
53diff -r 3e100985dbfa django/contrib/admin/templates/admin/edit_inline/stacked.html
54--- a/django/contrib/admin/templates/admin/edit_inline/stacked.html     Sun Feb 14 20:17:07 2010 -0600
55+++ b/django/contrib/admin/templates/admin/edit_inline/stacked.html     Tue Feb 16 16:17:36 2010 -0600
56@@ -1,4 +1,4 @@
57-{% load i18n %}
58+{% load i18n adminmedia %}
59 <div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
60   <h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2>
61 {{ inline_admin_formset.formset.management_form }}
62@@ -22,12 +22,32 @@
63 (function($) {
64     $(document).ready(function() {
65         var rows = "#{{ inline_admin_formset.formset.prefix }}-group .inline-related";
66-        updateInlineLabel = function(row) {
67+        var updateInlineLabel = function(row) {
68             $(rows).find(".inline_label").each(function(i) {
69                 var count = i + 1;
70                 $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
71             });
72         }
73+        var reinitDateTimeShortCuts = function() {
74+            // Reinitialize the calendar and clock widgets by force, yuck.
75+            if (typeof DateTimeShortcuts != "undefined") {
76+                $(".datetimeshortcuts").remove();
77+                DateTimeShortcuts.init();
78+            }
79+        }
80+        var updateSelectFilter = function() {
81+            // If any SelectFilter widgets were added, instantiate a new instance.
82+            if (typeof SelectFilter != "undefined"){
83+                $(".selectfilter").each(function(index, value){
84+                  var namearr = value.name.split('-');
85+                  SelectFilter.init(value.id, namearr[namearr.length-1], false, "{% admin_media_prefix %}");
86+                })
87+                $(".selectfilterstacked").each(function(index, value){
88+                  var namearr = value.name.split('-');
89+                  SelectFilter.init(value.id, namearr[namearr.length-1], true, "{% admin_media_prefix %}");
90+                })
91+            }
92+        }
93         $(rows).formset({
94             prefix: "{{ inline_admin_formset.formset.prefix }}",
95             addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
96@@ -36,8 +56,12 @@
97             deleteText: "{% trans "Remove" %}",
98             emptyCssClass: "empty-form",
99             removed: updateInlineLabel,
100-            added: updateInlineLabel
101+            added: (function(row) {
102+                reinitDateTimeShortCuts();
103+                updateSelectFilter();
104+                updateInlineLabel(row);
105+            })
106         });
107     });
108 })(jQuery.noConflict());
109-</script>
110\ No newline at end of file
111+</script>
112diff -r 3e100985dbfa django/contrib/admin/templates/admin/edit_inline/tabular.html
113--- a/django/contrib/admin/templates/admin/edit_inline/tabular.html     Sun Feb 14 20:17:07 2010 -0600
114+++ b/django/contrib/admin/templates/admin/edit_inline/tabular.html     Tue Feb 16 16:17:36 2010 -0600
115@@ -1,4 +1,4 @@
116-{% load i18n %}
117+{% load i18n adminmedia %}
118 <div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
119   <div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
120 {{ inline_admin_formset.formset.management_form }}
121@@ -68,11 +68,32 @@
122 (function($) {
123     $(document).ready(function($) {
124         var rows = "#{{ inline_admin_formset.formset.prefix }}-group .tabular.inline-related tbody tr";
125-        alternatingRows = function(row) {
126+        var alternatingRows = function(row) {
127             $(rows).not(".add-row").removeClass("row1 row2")
128                 .filter(":even").addClass("row1").end()
129                 .filter(rows + ":odd").addClass("row2");
130         }
131+        var reinitDateTimeShortCuts = function() {
132+            // Reinitialize the calendar and clock widgets by force
133+            if (typeof DateTimeShortcuts != "undefined") {
134+                $(".datetimeshortcuts").remove();
135+                DateTimeShortcuts.init();
136+            }
137+        }
138+        var updateSelectFilter = function() {
139+            // If any SelectFilter widgets are a part of the new form,
140+            // instantiate a new SelectFilter instance for it.
141+            if (typeof SelectFilter != "undefined"){
142+                $(".selectfilter").each(function(index, value){
143+                  var namearr = value.name.split('-');
144+                  SelectFilter.init(value.id, namearr[namearr.length-1], false, "{% admin_media_prefix %}");
145+                })
146+                $(".selectfilterstacked").each(function(index, value){
147+                  var namearr = value.name.split('-');
148+                  SelectFilter.init(value.id, namearr[namearr.length-1], true, "{% admin_media_prefix %}");
149+                })
150+            }
151+        }
152         $(rows).formset({
153             prefix: "{{ inline_admin_formset.formset.prefix }}",
154             addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
155@@ -81,8 +102,12 @@
156             deleteText: "{% trans "Remove" %}",
157             emptyCssClass: "empty-form",
158             removed: alternatingRows,
159-            added: alternatingRows
160+            added: (function(row) {
161+                reinitDateTimeShortCuts();
162+                updateSelectFilter();
163+                alternatingRows(row);
164+            })
165         });
166     });
167 })(jQuery.noConflict());
168-</script>
169\ No newline at end of file
170+</script>
171diff -r 3e100985dbfa django/contrib/admin/widgets.py
172--- a/django/contrib/admin/widgets.py   Sun Feb 14 20:17:07 2010 -0600
173+++ b/django/contrib/admin/widgets.py   Tue Feb 16 16:17:36 2010 -0600
174@@ -33,6 +33,9 @@
175         super(FilteredSelectMultiple, self).__init__(attrs, choices)
176 
177     def render(self, name, value, attrs=None, choices=()):
178+        if attrs is None: attrs = {}
179+        attrs['class'] = 'selectfilter'
180+        if self.is_stacked: attrs['class'] += 'stacked'
181         output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choices)]
182         output.append(u'<script type="text/javascript">addEvent(window, "load", function(e) {')
183         # TODO: "id_" is hard-coded here. This should instead use the correct