Commits

za...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed 85259e2

[soc2009/admin-ui] Javascript inline addition for tabular inlines

  • Participants
  • Parent commits 173c411
  • Branches soc2009/admin-ui

Comments (0)

Files changed (3)

File django/contrib/admin/media/css/forms.css

     color: #fff;
 }
 
-.inline-related.tabular fieldset.module table {
+.tabular.inline-group fieldset.module table {
     width: 100%;
 }
 
     border: none;
 }
 
-.inline-group .tabular tr.has_original td {
+.tabular.inline-group tr.has_original td {
     padding-top: 2em;
 }
 
-.inline-group .tabular tr td.original {
+.tabular.inline-group tr td.original {
     padding: 2px 0 0 0;
     width: 0;
     _position: relative;
 }
 
-.inline-group .tabular th.original {
+.tabular.inline-group th.original {
     width: 0px;
     padding: 0;
 }
 
-.inline-group .tabular td.original p {
+.tabular.inline-group td.original p {
     position: absolute;
     left: 0;
     height: 1.1em;
-    padding: 2px 7px;
+    padding: 2px 25px;
     overflow: hidden;
     font-size: 9px;
     font-weight: bold;

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

     
     $(id_prefix + "-add").click(function() {
         var new_inline = $(id_prefix + '-empty').clone(true)
-                            .insertBefore(id_prefix + '-addinline').fadeIn('normal');
+                            .insertBefore(id_prefix + '-empty').fadeIn('normal');
         
         var inline_template = $(new_inline).html();
         var new_inline_html = inline_template.replace(/__prefix__/g, total_forms.val().toString());

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

 {% load i18n %}
-<div class="inline-group">
-  <div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
+<div class="tabular inline-group" id="{{ inline_admin_formset.opts.verbose_name}}-group">
 {{ inline_admin_formset.formset.management_form }}
 <fieldset class="module">
    <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
         {% if inline_admin_form.form.non_field_errors %}
         <tr><td colspan="{{ inline_admin_form.field_count }}">{{ inline_admin_form.form.non_field_errors }}</td></tr>
         {% endif %}
-        <tr class="{% if not inline_admin_formset.opts.order_field %}{% cycle 'row1' 'row2' %} {% endif %}{% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}">
+        <tr class="inline-related {% if forloop.last %}empty_form{% endif %} {% if not inline_admin_formset.opts.order_field %}{% cycle 'row1' 'row2' %} {% endif %}{% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}"
+             id="{{ inline_admin_formset.opts.verbose_name}}{% if not forloop.last %}{{ forloop.counter }}{% else %}-empty{% endif %}">
 
         <td class="original">
           {% if inline_admin_form.original or inline_admin_form.show_url %}<p>
    </table>
 
 </fieldset>
-  </div>
 
-  {# <ul class="tools"> #}
-  {#   <li><a class="add" href="">Add another {{ inline_admin_formset.opts.verbose_name|title }}</a></li> #}
-  {# </ul> #}
+<ul class="tools add_inline" id="{{ inline_admin_formset.opts.verbose_name}}-addinline">
+    <li><a id="{{ inline_admin_formset.opts.verbose_name }}-add" class="add" href="#">Add a {{ inline_admin_formset.opts.verbose_name }}</a></li>
+</ul>
 
 </div>
 
 <script type="text/javascript">
 $(function() {
+    /* Add inline */
+    // TODO: this block of code is the same for all inlines, so move it out into a separate file
+    var prefix = "{{ inline_admin_formset.opts.verbose_name }}";
+    var id_prefix = "#" + prefix;
+    var total_forms = $(id_prefix + '-group input[id$="TOTAL_FORMS"]');
+    var initial_forms = $(id_prefix + '-group').find('input[id$="INITIAL_FORMS"]');
+    
+    // since javascript is turned on, unhide the "add new <inline>" link
+    $('.add_inline').show();
+    
+    // hide the extras, but only if there were no form errors
+    if (!$('.errornote').html()) {
+        if (parseInt(initial_forms.val()) > 0) {
+            $(id_prefix + '-group .inline-related:gt(' + (initial_forms.val() - 1) + ')')
+                .not('.empty_form').remove();
+        }
+        else {
+            $(id_prefix + '-group .inline-related').not('.empty_form').remove();
+        }
+        
+        total_forms.val(parseInt(initial_forms.val()));
+    }
+    
+    $(id_prefix + "-add").click(function() {
+        var new_inline = $(id_prefix + '-empty').clone(true)
+                            .insertBefore(id_prefix + '-empty').fadeIn('normal');
+        
+        var inline_template = $(new_inline).html();
+        var new_inline_html = inline_template.replace(/__prefix__/g, total_forms.val().toString());
+        
+        total_forms.val(parseInt(total_forms.val()) + 1);
+        
+        $(new_inline).html(new_inline_html);
+        $(new_inline).attr('id', prefix + total_forms.val().toString());
+        $(new_inline).find('.inline_label').html('#' + total_forms.val().toString());
+        $(new_inline).removeClass('empty_form');
+        
+        return false;
+    });
+    
     {% if inline_admin_formset.opts.order_field %}
-        // highlight each row on hover
-        $("table.orderable tbody tr").hover(
-            function() {
-                $(this).addClass('hover');
-            }, 
-            function() {
-                $(this).removeClass('hover');
-            }
-        );
+    /* Reordering Inlines */
     
-        $('table.orderable').tableDnD({
-            onDragStart: function(table, row_obj) {
-                // once you start dragging a row, stop the hover() highlighting. 
-                // this is needed because the highlighting from onDragStyle isn't always in 
-                // sync with the hover() highlighting, which results in times where you have 
-                // two rows highlighted. 
-                $("table.orderable tbody tr").unbind('mouseenter').unbind('mouseleave');
-            },
-            onDrop: function(table, row_obj) {
-                var counter = 0;
-            
-                $(row_obj).parent().find('input[id$="{{ inline_admin_formset.opts.order_field }}"]')
-                    .each(function (i) {
-                        $(this).val(counter++);
-                    });
-            
-                // rebind hover
-                $("table.orderable tbody tr").hover(
-                    function() {
-                        $(this).addClass('hover');
-                    }, 
-                    function() {
-                        $(this).removeClass('hover');
-                    }
-                );
-            }
-        });
+    // highlight each row on hover
+    $("table.orderable tbody tr").hover(
+        function() {
+            $(this).addClass('hover');
+        }, 
+        function() {
+            $(this).removeClass('hover');
+        }
+    );
+
+    $('table.orderable').tableDnD({
+        onDragStart: function(table, row_obj) {
+            // once you start dragging a row, stop the hover() highlighting. 
+            // this is needed because the highlighting from onDragStyle isn't always in 
+            // sync with the hover() highlighting, which results in times where you have 
+            // two rows highlighted. 
+            $("table.orderable tbody tr").unbind('mouseenter').unbind('mouseleave');
+        },
+        onDrop: function(table, row_obj) {
+            var counter = 0;
+        
+            $(row_obj).parent().find('input[id$="{{ inline_admin_formset.opts.order_field }}"]')
+                .each(function (i) {
+                    $(this).val(counter++);
+                });
+        
+            // rebind hover
+            $("table.orderable tbody tr").hover(
+                function() {
+                    $(this).addClass('hover');
+                }, 
+                function() {
+                    $(this).removeClass('hover');
+                }
+            );
+        }
+    });
     {% endif %}
 });
 </script>