11{% load i18n %}
2- < div class ="inline-group ">
3- < div class ="tabular inline-related {% if forloop.last %}last-related{% endif %} ">
2+ < div class ="tabular inline-group " id ="{{ inline_admin_formset.opts.verbose_name}}-group ">
43{{ inline_admin_formset.formset.management_form }}
54< fieldset class ="module ">
65 < h2 > {{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</ h2 >
@@ -19,7 +18,8 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
1918 {% if inline_admin_form.form.non_field_errors %}
2019 < tr > < td colspan ="{{ inline_admin_form.field_count }} "> {{ inline_admin_form.form.non_field_errors }}</ td > </ tr >
2120 {% endif %}
22- < 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 %} ">
21+ < 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 %} "
22+ id ="{{ inline_admin_formset.opts.verbose_name}}{% if not forloop.last %}{{ forloop.counter }}{% else %}-empty{% endif %} ">
2323
2424 < td class ="original ">
2525 {% if inline_admin_form.original or inline_admin_form.show_url %}< p >
@@ -61,54 +61,95 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
6161 </ table >
6262
6363</ fieldset >
64- </ div >
6564
66- {# < ul class ="tools " > #}
67- {# < li > < a class ="add " href =""> Add another {{ inline_admin_formset.opts.verbose_name|title }}</ a > </ li > #}
68- {# </ ul > #}
65+ < ul class ="tools add_inline " id =" {{ inline_admin_formset.opts.verbose_name}}-addinline " >
66+ < li > < a id =" {{ inline_admin_formset.opts.verbose_name }}-add " class ="add " href ="# "> Add a {{ inline_admin_formset.opts.verbose_name }}</ a > </ li >
67+ </ ul >
6968
7069</ div >
7170
7271< script type ="text/javascript ">
7372$ ( function ( ) {
73+ /* Add inline */
74+ // TODO: this block of code is the same for all inlines, so move it out into a separate file
75+ var prefix = "{{ inline_admin_formset.opts.verbose_name }}" ;
76+ var id_prefix = "#" + prefix ;
77+ var total_forms = $ ( id_prefix + '-group input[id$="TOTAL_FORMS"]' ) ;
78+ var initial_forms = $ ( id_prefix + '-group' ) . find ( 'input[id$="INITIAL_FORMS"]' ) ;
79+
80+ // since javascript is turned on, unhide the "add new <inline>" link
81+ $ ( '.add_inline' ) . show ( ) ;
82+
83+ // hide the extras, but only if there were no form errors
84+ if ( ! $ ( '.errornote' ) . html ( ) ) {
85+ if ( parseInt ( initial_forms . val ( ) ) > 0 ) {
86+ $ ( id_prefix + '-group .inline-related:gt(' + ( initial_forms . val ( ) - 1 ) + ')' )
87+ . not ( '.empty_form' ) . remove ( ) ;
88+ }
89+ else {
90+ $ ( id_prefix + '-group .inline-related' ) . not ( '.empty_form' ) . remove ( ) ;
91+ }
92+
93+ total_forms . val ( parseInt ( initial_forms . val ( ) ) ) ;
94+ }
95+
96+ $ ( id_prefix + "-add" ) . click ( function ( ) {
97+ var new_inline = $ ( id_prefix + '-empty' ) . clone ( true )
98+ . insertBefore ( id_prefix + '-empty' ) . fadeIn ( 'normal' ) ;
99+
100+ var inline_template = $ ( new_inline ) . html ( ) ;
101+ var new_inline_html = inline_template . replace ( / _ _ p r e f i x _ _ / g, total_forms . val ( ) . toString ( ) ) ;
102+
103+ total_forms . val ( parseInt ( total_forms . val ( ) ) + 1 ) ;
104+
105+ $ ( new_inline ) . html ( new_inline_html ) ;
106+ $ ( new_inline ) . attr ( 'id' , prefix + total_forms . val ( ) . toString ( ) ) ;
107+ $ ( new_inline ) . find ( '.inline_label' ) . html ( '#' + total_forms . val ( ) . toString ( ) ) ;
108+ $ ( new_inline ) . removeClass ( 'empty_form' ) ;
109+
110+ return false ;
111+ } ) ;
112+
74113 { % if inline_admin_formset . opts . order_field % }
75- // highlight each row on hover
76- $ ( "table.orderable tbody tr" ) . hover (
77- function ( ) {
78- $ ( this ) . addClass ( 'hover' ) ;
79- } ,
80- function ( ) {
81- $ ( this ) . removeClass ( 'hover' ) ;
82- }
83- ) ;
114+ /* Reordering Inlines */
84115
85- $ ( 'table.orderable' ) . tableDnD ( {
86- onDragStart : function ( table , row_obj ) {
87- // once you start dragging a row, stop the hover() highlighting.
88- // this is needed because the highlighting from onDragStyle isn't always in
89- // sync with the hover() highlighting, which results in times where you have
90- // two rows highlighted.
91- $ ( "table.orderable tbody tr" ) . unbind ( 'mouseenter' ) . unbind ( 'mouseleave' ) ;
92- } ,
93- onDrop : function ( table , row_obj ) {
94- var counter = 0 ;
95-
96- $ ( row_obj ) . parent ( ) . find ( 'input[id$="{{ inline_admin_formset.opts.order_field }}"]' )
97- . each ( function ( i ) {
98- $ ( this ) . val ( counter ++ ) ;
99- } ) ;
100-
101- // rebind hover
102- $ ( "table.orderable tbody tr" ) . hover (
103- function ( ) {
104- $ ( this ) . addClass ( 'hover' ) ;
105- } ,
106- function ( ) {
107- $ ( this ) . removeClass ( 'hover' ) ;
108- }
109- ) ;
110- }
111- } ) ;
116+ // highlight each row on hover
117+ $ ( "table.orderable tbody tr" ) . hover (
118+ function ( ) {
119+ $ ( this ) . addClass ( 'hover' ) ;
120+ } ,
121+ function ( ) {
122+ $ ( this ) . removeClass ( 'hover' ) ;
123+ }
124+ ) ;
125+
126+ $ ( 'table.orderable' ) . tableDnD ( {
127+ onDragStart : function ( table , row_obj ) {
128+ // once you start dragging a row, stop the hover() highlighting.
129+ // this is needed because the highlighting from onDragStyle isn't always in
130+ // sync with the hover() highlighting, which results in times where you have
131+ // two rows highlighted.
132+ $ ( "table.orderable tbody tr" ) . unbind ( 'mouseenter' ) . unbind ( 'mouseleave' ) ;
133+ } ,
134+ onDrop : function ( table , row_obj ) {
135+ var counter = 0 ;
136+
137+ $ ( row_obj ) . parent ( ) . find ( 'input[id$="{{ inline_admin_formset.opts.order_field }}"]' )
138+ . each ( function ( i ) {
139+ $ ( this ) . val ( counter ++ ) ;
140+ } ) ;
141+
142+ // rebind hover
143+ $ ( "table.orderable tbody tr" ) . hover (
144+ function ( ) {
145+ $ ( this ) . addClass ( 'hover' ) ;
146+ } ,
147+ function ( ) {
148+ $ ( this ) . removeClass ( 'hover' ) ;
149+ }
150+ ) ;
151+ }
152+ } ) ;
112153 { % endif % }
113154} ) ;
114155</ script >
0 commit comments