@@ -5,7 +5,7 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2>
55{{ inline_admin_formset.formset.non_form_errors }}
66
77{% for inline_admin_form in inline_admin_formset %}
8- < div class ="inline-related{% if forloop.last %} last-related {% endif %} " id ="{{ inline_admin_formset.opts.verbose_name}}{{ forloop.counter }} ">
8+ < div class ="inline-related{% if forloop.last %} empty_form {% endif %} " id ="{{ inline_admin_formset.opts.verbose_name}}{% if not forloop.last %}{{ forloop.counter }}{% else %}-empty{% endif % } ">
99 < h3 > < b > {{ inline_admin_formset.opts.verbose_name|title }}:</ b > < span class ="inline_label "> {% if inline_admin_form.original %}{{ inline_admin_form.original }}{% else %} #{{ forloop.counter }}{% endif %}</ span >
1010 {% if inline_admin_formset.formset.can_delete and inline_admin_form.original %}< span class ="delete "> {{ inline_admin_form.deletion_field.field }} {{ inline_admin_form.deletion_field.label_tag }}</ span > {% endif %}
1111 </ h3 >
@@ -22,62 +22,48 @@ <h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b> <span class=
2222</ div >
2323{% endfor %}
2424
25- < ul class ="tools add_inline ">
25+ < ul class ="tools add_inline " id =" {{ inline_admin_formset.opts.verbose_name}}-addinline " >
2626 < li > < a id ="{{ inline_admin_formset.opts.verbose_name }}-add " class ="add " href ="# "> Add a {{ inline_admin_formset.opts.verbose_name }}</ a > </ li >
2727</ ul >
2828
2929</ div >
3030
3131< script type ="text/javascript ">
32- function increment_fields ( el ) {
33- el . attr ( 'id' , el . attr ( 'id' ) . replace ( / - ( \d + ) - / , function ( num ) {
34- var newnum = parseInt ( num . replace ( / - / g, '' ) ) + 1 ;
35- return '-' + newnum + '-' ;
36- } ) ) ;
37-
38- el . attr ( 'name' , el . attr ( 'name' ) . replace ( / - ( \d + ) - / , function ( num ) {
39- var newnum = parseInt ( num . replace ( / - / g, '' ) ) + 1 ;
40- return '-' + newnum + '-' ;
41- } ) ) ;
42- }
43-
4432$ ( function ( ) {
45- var id_prefix = "{{ inline_admin_formset.opts.verbose_name }}" ;
46- var total_forms = $ ( '#' + id_prefix + '-group input[id$="TOTAL_FORMS"]' ) ;
47- var initial_forms = $ ( '#' + id_prefix + '-group' ) . find ( 'input[id$="INITIAL_FORMS"]' ) ;
33+ var prefix = "{{ inline_admin_formset.opts.verbose_name }}" ;
34+ var id_prefix = "#" + prefix ;
35+ var total_forms = $ ( id_prefix + '-group input[id$="TOTAL_FORMS"]' ) ;
36+ var initial_forms = $ ( id_prefix + '-group' ) . find ( 'input[id$="INITIAL_FORMS"]' ) ;
4837
49- // since javascript is turned on, unhide the "add new <inline>" link and hide the extras
38+ // since javascript is turned on, unhide the "add new <inline>" link
5039 $ ( '.add_inline' ) . show ( ) ;
5140
52- if ( parseInt ( initial_forms . val ( ) ) > 0 ) {
53- $ ( '#' + id_prefix + '-group .inline-related:gt(' + ( initial_forms . val ( ) - 1 ) + ')' ) . remove ( ) ;
54- }
55- else {
56- $ ( '#' + id_prefix + '-group .inline-related:gt(0)' ) . remove ( ) ;
57- $ ( '#' + id_prefix + '-group .inline-related:first' ) . hide ( ) ;
58- }
59-
60- total_forms . val ( parseInt ( initial_forms . val ( ) ) ) ;
61-
62- // clicking on the "add" link will add a blank form to add a new inline object
63- $ ( '#' + id_prefix + "-add" ) . click ( function ( ) {
64- if ( parseInt ( total_forms . val ( ) ) == 0 ) {
65- $ ( '#' + id_prefix + '-group .inline-related:first' ) . fadeIn ( 'normal' ) ;
66- total_forms . val ( parseInt ( total_forms . val ( ) ) + 1 ) ;
67- return false ;
41+ // hide the extras, but only if there were no form errors
42+ if ( ! $ ( '.errornote' ) . html ( ) ) {
43+ if ( parseInt ( initial_forms . val ( ) ) > 0 ) {
44+ $ ( id_prefix + '-group .inline-related:gt(' + ( initial_forms . val ( ) - 1 ) + ')' )
45+ . not ( '.empty_form' ) . remove ( ) ;
46+ }
47+ else {
48+ $ ( id_prefix + '-group .inline-related' ) . not ( '.empty_form' ) . remove ( ) ;
6849 }
6950
70- var last_inline = $ ( '#' + id_prefix + '-group .inline-related:last' ) ;
71- var new_inline = last_inline . clone ( true ) . hide ( ) . insertAfter ( last_inline ) . fadeIn ( 'normal' ) ;
51+ total_forms . val ( parseInt ( initial_forms . val ( ) ) ) ;
52+ }
53+
54+ $ ( id_prefix + "-add" ) . click ( function ( ) {
55+ var new_inline = $ ( id_prefix + '-empty' ) . clone ( true )
56+ . insertBefore ( id_prefix + '-addinline' ) . fadeIn ( 'normal' ) ;
7257
73- new_inline . find ( 'input, select' ) . each ( function ( i ) {
74- increment_fields ( $ ( this ) ) ;
75- $ ( this ) . val ( "" ) ;
76- } ) ;
58+ var inline_template = $ ( new_inline ) . html ( ) ;
59+ var new_inline_html = inline_template . replace ( / _ _ p r e f i x _ _ / g, total_forms . val ( ) . toString ( ) ) ;
7760
7861 total_forms . val ( parseInt ( total_forms . val ( ) ) + 1 ) ;
7962
80- new_inline . find ( ".inline_label" ) . text ( '#' + total_forms . val ( ) ) ;
63+ $ ( new_inline ) . html ( new_inline_html ) ;
64+ $ ( new_inline ) . attr ( 'id' , prefix + total_forms . val ( ) . toString ( ) ) ;
65+ $ ( new_inline ) . find ( '.inline_label' ) . html ( '#' + total_forms . val ( ) . toString ( ) ) ;
66+ $ ( new_inline ) . removeClass ( 'empty_form' ) ;
8167
8268 return false ;
8369 } ) ;
0 commit comments