@@ -9,13 +9,13 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
99
1010 < ul class ="inline-selector ">
1111 {% for inline_admin_form in inline_admin_formset %}
12- < li class ="inline-selector-item ">
12+ < li class ="inline-selector-item{% if forloop.last %} empty_form{% endif %} " id =" {{ inline_admin_formset.opts.verbose_name}}{% if not forloop.last %}{{ forloop.counter }}{% else %}-empty{% endif %}-selector ">
1313 < a href ="# " class ="inline-select " title ="{{ inline_admin_formset.opts.verbose_name}}{{ forloop.counter }} ">
1414 {% if inline_admin_form.original or inline_admin_form.show_url %}
1515 {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
1616 {% if inline_admin_form.show_url %}< a href ="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/ "> {% trans "View on site" %}</ a > {% endif %}
1717 {% else %}
18- {{ inline_admin_formset.opts.verbose_name|title}}: #{{ forloop.counter }}
18+ {{ inline_admin_formset.opts.verbose_name|title}}: < span class =" inline_label " > #{{ forloop.counter }}</ span >
1919 {% endif %}
2020 </ a >
2121 </ li >
@@ -24,13 +24,13 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
2424
2525 < div class ="inline-detail ">
2626 {% for inline_admin_form in inline_admin_formset %}
27- < div class ="selector-item-detail " id ="{{ inline_admin_formset.opts.verbose_name}}{{ forloop.counter }} ">
27+ < div class ="selector-item-detail{% if forloop.last %} empty_form{% endif %} " id ="{{ inline_admin_formset.opts.verbose_name}}{% if not forloop.last %}{{ forloop.counter }}{% else %}-empty{% endif % } ">
2828 < h3 >
2929 {% if inline_admin_form.original or inline_admin_form.show_url %}
3030 {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
3131 {% if inline_admin_form.show_url %}< a href ="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/ "> {% trans "View on site" %}</ a > {% endif %}
3232 {% else %}
33- {{ inline_admin_formset.opts.verbose_name|title}}: #{{ forloop.counter }}
33+ {{ inline_admin_formset.opts.verbose_name|title}}: < span class =" inline_label " > #{{ forloop.counter }}</ span >
3434 {% endif %}
3535
3636 {% if inline_admin_formset.formset.can_delete %}
8585 {% endfor %}
8686 </ div >
8787 < div class ="clear "> </ div >
88+
89+ < ul class ="tools add_inline " id ="{{ inline_admin_formset.opts.verbose_name}}-addinline ">
90+ < li > < a id ="{{ inline_admin_formset.opts.verbose_name }}-add " class ="add " href ="# "> Add a {{ inline_admin_formset.opts.verbose_name }}</ a > </ li >
91+ </ ul >
92+
8893 </ div >
8994</ div >
9095
9196< script type ="text/javascript ">
9297$ ( function ( ) {
98+ /* Selector */
9399 $ ( '.inline-detail .selector-item-detail' ) . hide ( ) ;
94100 $ ( '.inline-detail .selector-item-detail:first' ) . show ( ) ;
95101 $ ( '.inline-selector .inline-selector-item:first' ) . addClass ( 'inline-selected' ) ;
@@ -105,11 +111,75 @@ <h3>
105111
106112 return false ;
107113 } ) ;
108-
114+
115+ /* Add inline */
116+ var prefix = "{{ inline_admin_formset.opts.verbose_name }}" ;
117+ var id_prefix = "#" + prefix ;
118+ var total_forms = $ ( id_prefix + '-group input[id$="TOTAL_FORMS"]' ) ;
119+ var initial_forms = $ ( id_prefix + '-group' ) . find ( 'input[id$="INITIAL_FORMS"]' ) ;
120+
121+ // since javascript is turned on, unhide the "add new <inline>" link
122+ $ ( '.add_inline' ) . show ( ) ;
123+
124+ // hide the extras, but only if there were no form errors
125+ if ( ! $ ( '.errornote' ) . html ( ) ) {
126+ if ( parseInt ( initial_forms . val ( ) ) > 0 ) {
127+ $ ( id_prefix + '-group .inline-selector-item:gt(' + ( initial_forms . val ( ) - 1 ) + ')' )
128+ . not ( '.empty_form' ) . remove ( ) ;
129+ $ ( id_prefix + '-group .selector-item-detail:gt(' + ( initial_forms . val ( ) - 1 ) + ')' )
130+ . not ( '.empty_form' ) . remove ( ) ;
131+ }
132+ else {
133+ $ ( id_prefix + '-group .inline-selector-item' ) . not ( '.empty_form' ) . remove ( ) ;
134+ $ ( id_prefix + '-group .selector-item-detail' ) . not ( '.empty_form' ) . remove ( ) ;
135+ }
136+
137+ total_forms . val ( parseInt ( initial_forms . val ( ) ) ) ;
138+ }
139+
140+ $ ( id_prefix + "-add" ) . click ( function ( ) {
141+ // selector
142+ var new_selector = $ ( '.inline-selector ' + id_prefix + '-empty-selector' ) . clone ( true )
143+ . insertBefore ( '.inline-selector ' + id_prefix + '-empty-selector' ) . fadeIn ( 'normal' ) ;
144+
145+ var selector_template = $ ( new_selector ) . html ( ) ;
146+ var new_selector_html = selector_template . replace ( / _ _ p r e f i x _ _ / g, total_forms . val ( ) . toString ( ) ) ;
147+
148+ // form
149+ var new_form = $ ( '.inline-detail ' + id_prefix + '-empty' ) . clone ( true )
150+ . insertBefore ( '.inline-detail ' + id_prefix + '-empty' ) ;
151+
152+ var form_template = $ ( new_form ) . html ( ) ;
153+ var new_form_html = form_template . replace ( / _ _ p r e f i x _ _ / g, total_forms . val ( ) . toString ( ) ) ;
154+
155+
156+ total_forms . val ( parseInt ( total_forms . val ( ) ) + 1 ) ;
157+
158+ // selector
159+ $ ( new_selector ) . html ( new_selector_html ) ;
160+ $ ( new_selector ) . attr ( 'id' , prefix + total_forms . val ( ) . toString ( ) + '-selector' ) ;
161+ $ ( new_selector ) . find ( 'a' ) . attr ( 'title' , prefix + total_forms . val ( ) . toString ( ) ) ;
162+ $ ( new_selector ) . find ( '.inline_label' ) . html ( '#' + total_forms . val ( ) . toString ( ) ) ;
163+ $ ( new_selector ) . removeClass ( 'empty_form' ) ;
164+
165+ // form
166+ $ ( new_form ) . html ( new_form_html ) ;
167+ $ ( new_form ) . attr ( 'id' , prefix + total_forms . val ( ) . toString ( ) ) ;
168+ $ ( new_form ) . find ( '.inline_label' ) . html ( '#' + total_forms . val ( ) . toString ( ) ) ;
169+ $ ( new_form ) . removeClass ( 'empty_form' ) ;
170+
171+ { % if inline_admin_formset . opts . order_field % }
172+ // make the new inline re-orderable
173+ $ ( 'ul.inline-selector' ) . ListReorder ( { useDefaultDragHandle : true } ) ;
174+ $ ( new_form ) . find ( 'input[id$="{{ inline_admin_formset.opts.order_field }}"]' ) . val ( total_forms . val ( ) ) ;
175+ { % endif % }
176+
177+ return false ;
178+ } ) ;
179+
109180 { % if inline_admin_formset . opts . order_field % }
110181 /* Reordering Inlines */
111182 var list = $ ( 'ul.inline-selector' ) . ListReorder ( { useDefaultDragHandle : true } ) ;
112- var id_prefix = '#{{ inline_admin_formset.opts.verbose_name}}' ;
113183
114184 list . bind ( 'listorderchanged' , function ( evt , jq_list , list_order ) {
115185 counter = 1 ;
0 commit comments