From c2ae6b2af7dcedaf4f6639da184912b6e4b3c1f1 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Thu, 17 Mar 2011 00:44:12 +0000 Subject: [PATCH] Fixed #15569 -- Corrected the numbering updates to inlines when rows are added and deleted. Thanks to sbaechler for the report, to Arthur de Jong and mk for the work on the patch, and to Karen Tracey for the last minute testing help. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15862 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/media/js/inlines.js | 34 ++++---------------- django/contrib/admin/media/js/inlines.min.js | 11 +++---- 2 files changed, 11 insertions(+), 34 deletions(-) diff --git a/django/contrib/admin/media/js/inlines.js b/django/contrib/admin/media/js/inlines.js index 0153a415a3..bddd6f75ba 100644 --- a/django/contrib/admin/media/js/inlines.js +++ b/django/contrib/admin/media/js/inlines.js @@ -18,7 +18,7 @@ $.fn.formset = function(opts) { var options = $.extend({}, $.fn.formset.defaults, opts); var updateElementIndex = function(el, prefix, ndx) { - var id_regex = new RegExp("(" + prefix + "-\\d+)"); + var id_regex = new RegExp("(" + prefix + "-(\\d+|__prefix__))"); var replacement = prefix + "-" + ndx; if ($(el).attr("for")) { $(el).attr("for", $(el).attr("for").replace(id_regex, replacement)); @@ -59,31 +59,6 @@ row.removeClass(options.emptyCssClass) .addClass(options.formCssClass) .attr("id", options.prefix + "-" + nextIndex); - nextIndex += 1; - row.find("*") - .filter(function() { - var el = $(this); - return el.attr("id") && el.attr("id").search(/__prefix__/) >= 0; - }).each(function() { - var el = $(this); - el.attr("id", el.attr("id").replace(/__prefix__/g, nextIndex)); - }) - .end() - .filter(function() { - var el = $(this); - return el.attr("name") && el.attr("name").search(/__prefix__/) >= 0; - }).each(function() { - var el = $(this); - el.attr("name", el.attr("name").replace(/__prefix__/g, nextIndex)); - }) - .end() - .filter(function() { - var el = $(this); - return el.attr("for") && el.attr("for").search(/__prefix__/) >= 0; - }).each(function() { - var el = $(this); - el.attr("for", el.attr("for").replace(/__prefix__/g, nextIndex)); - }); if (row.is("tr")) { // If the forms are laid out in table rows, insert // the remove button into the last table cell: @@ -97,13 +72,14 @@ // last child element of the form's container: row.children(":first").append('' + options.deleteText + ""); } - row.find("input,select,textarea,label,a").each(function() { + row.find("*").each(function() { updateElementIndex(this, options.prefix, totalForms.val()); }); // Insert the new form when it has been fully edited row.insertBefore($(template)); // Update number of total forms $(totalForms).val(parseInt(totalForms.val()) + 1); + nextIndex += 1; // Hide add button in case we've hit the max, except we want to add infinitely if ((maxForms.val() != '') && (maxForms.val()-totalForms.val()) <= 0) { addButton.parent().hide(); @@ -113,6 +89,7 @@ // Remove the parent form containing this button: var row = $(this).parents("." + options.formCssClass); row.remove(); + nextIndex -= 1; // If a post-delete callback was provided, call it with the deleted form: if (options.removed) { options.removed(row); @@ -128,7 +105,8 @@ // so they remain in sequence: for (var i=0, formCount=forms.length; i0;a(this).each(function(){a(this).not("."+b.emptyCssClass).addClass(b.formCssClass)}); -if(a(this).length&&g){var j;if(a(this).attr("tagName")=="TR"){g=this.eq(0).children().length;a(this).parent().append(''+b.addText+"");j=a(this).parent().find("tr:last a")}else{a(this).filter(":last").after('");j=a(this).filter(":last").next().find("a")}j.click(function(){var d=a("#id_"+b.prefix+"-TOTAL_FORMS"),f=a("#"+b.prefix+"-empty"), -e=f.clone(true);e.removeClass(b.emptyCssClass).addClass(b.formCssClass).attr("id",b.prefix+"-"+i);i+=1;e.find("*").filter(function(){var c=a(this);return c.attr("id")&&c.attr("id").search(/__prefix__/)>=0}).each(function(){var c=a(this);c.attr("id",c.attr("id").replace(/__prefix__/g,i))}).end().filter(function(){var c=a(this);return c.attr("name")&&c.attr("name").search(/__prefix__/)>=0}).each(function(){var c=a(this);c.attr("name",c.attr("name").replace(/__prefix__/g,i))}).end().filter(function(){var c= -a(this);return c.attr("for")&&c.attr("for").search(/__prefix__/)>=0}).each(function(){var c=a(this);c.attr("for",c.attr("for").replace(/__prefix__/g,i))});if(e.is("tr"))e.children(":last").append('
'+b.deleteText+"
");else e.is("ul")||e.is("ol")?e.append('
  • '+b.deleteText+"
  • "):e.children(":first").append(''+ -b.deleteText+"");e.find("input,select,textarea,label,a").each(function(){l(this,b.prefix,d.val())});e.insertBefore(a(f));a(d).val(parseInt(d.val())+1);h.val()!=""&&h.val()-d.val()<=0&&j.parent().hide();e.find("a."+b.deleteCssClass).click(function(){var c=a(this).parents("."+b.formCssClass);c.remove();b.removed&&b.removed(c);c=a("."+b.formCssClass);a("#id_"+b.prefix+"-TOTAL_FORMS").val(c.length);if(h.val()==""||h.val()-c.length>0)j.parent().show();for(var k=0,m=c.length;k0;b(this).each(function(){b(this).not("."+ +a.emptyCssClass).addClass(a.formCssClass)});if(b(this).length&&g){var j;if(b(this).attr("tagName")=="TR"){g=this.eq(0).children().length;b(this).parent().append(''+a.addText+"");j=b(this).parent().find("tr:last a")}else{b(this).filter(":last").after('");j=b(this).filter(":last").next().find("a")}j.click(function(){var c=b("#id_"+ +a.prefix+"-TOTAL_FORMS"),f=b("#"+a.prefix+"-empty"),d=f.clone(true);d.removeClass(a.emptyCssClass).addClass(a.formCssClass).attr("id",a.prefix+"-"+l);if(d.is("tr"))d.children(":last").append('
    '+a.deleteText+"
    ");else d.is("ul")||d.is("ol")?d.append('
  • '+a.deleteText+"
  • "):d.children(":first").append(''+ +a.deleteText+"");d.find("*").each(function(){k(this,a.prefix,c.val())});d.insertBefore(b(f));b(c).val(parseInt(c.val())+1);l+=1;h.val()!=""&&h.val()-c.val()<=0&&j.parent().hide();d.find("a."+a.deleteCssClass).click(function(){var e=b(this).parents("."+a.formCssClass);e.remove();l-=1;a.removed&&a.removed(e);e=b("."+a.formCssClass);b("#id_"+a.prefix+"-TOTAL_FORMS").val(e.length);if(h.val()==""||h.val()-e.length>0)j.parent().show();for(var i=0,m=e.length;i