Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit c2ae6b2af7dcedaf4f6639da184912b6e4b3c1f1 1 parent 274849c
Russell Keith-Magee authored March 17, 2011
34  django/contrib/admin/media/js/inlines.js
@@ -18,7 +18,7 @@
18 18
 	$.fn.formset = function(opts) {
19 19
 		var options = $.extend({}, $.fn.formset.defaults, opts);
20 20
 		var updateElementIndex = function(el, prefix, ndx) {
21  
-			var id_regex = new RegExp("(" + prefix + "-\\d+)");
  21
+			var id_regex = new RegExp("(" + prefix + "-(\\d+|__prefix__))");
22 22
 			var replacement = prefix + "-" + ndx;
23 23
 			if ($(el).attr("for")) {
24 24
 				$(el).attr("for", $(el).attr("for").replace(id_regex, replacement));
@@ -59,31 +59,6 @@
59 59
 				row.removeClass(options.emptyCssClass)
60 60
 				    .addClass(options.formCssClass)
61 61
 				    .attr("id", options.prefix + "-" + nextIndex);
62  
-				nextIndex += 1;
63  
-				row.find("*")
64  
-				    .filter(function() {
65  
-				        var el = $(this);
66  
-				        return el.attr("id") && el.attr("id").search(/__prefix__/) >= 0;
67  
-				    }).each(function() {
68  
-				        var el = $(this);
69  
-				        el.attr("id", el.attr("id").replace(/__prefix__/g, nextIndex));
70  
-				    })
71  
-				    .end()
72  
-				    .filter(function() {
73  
-				        var el = $(this);
74  
-				        return el.attr("name") && el.attr("name").search(/__prefix__/) >= 0;
75  
-				    }).each(function() {
76  
-				        var el = $(this);
77  
-				        el.attr("name", el.attr("name").replace(/__prefix__/g, nextIndex));
78  
-				    })
79  
-				    .end()
80  
-				    .filter(function() {
81  
-				        var el = $(this);
82  
-				        return el.attr("for") && el.attr("for").search(/__prefix__/) >= 0;
83  
-				    }).each(function() {
84  
-				        var el = $(this);
85  
-				        el.attr("for", el.attr("for").replace(/__prefix__/g, nextIndex));
86  
-				    });
87 62
 				if (row.is("tr")) {
88 63
 					// If the forms are laid out in table rows, insert
89 64
 					// the remove button into the last table cell:
@@ -97,13 +72,14 @@
97 72
 					// last child element of the form's container:
98 73
 					row.children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>");
99 74
 				}
100  
-				row.find("input,select,textarea,label,a").each(function() {
  75
+				row.find("*").each(function() {
101 76
 					updateElementIndex(this, options.prefix, totalForms.val());
102 77
 				});
103 78
 				// Insert the new form when it has been fully edited
104 79
 				row.insertBefore($(template));
105 80
 				// Update number of total forms
106 81
 				$(totalForms).val(parseInt(totalForms.val()) + 1);
  82
+				nextIndex += 1;
107 83
 				// Hide add button in case we've hit the max, except we want to add infinitely
108 84
 				if ((maxForms.val() != '') && (maxForms.val()-totalForms.val()) <= 0) {
109 85
 					addButton.parent().hide();
@@ -113,6 +89,7 @@
113 89
 					// Remove the parent form containing this button:
114 90
 					var row = $(this).parents("." + options.formCssClass);
115 91
 					row.remove();
  92
+					nextIndex -= 1;
116 93
 					// If a post-delete callback was provided, call it with the deleted form:
117 94
 					if (options.removed) {
118 95
 						options.removed(row);
@@ -128,7 +105,8 @@
128 105
 					// so they remain in sequence:
129 106
 					for (var i=0, formCount=forms.length; i<formCount; i++)
130 107
 					{
131  
-						$(forms.get(i)).find("input,select,textarea,label,a").each(function() {
  108
+						updateElementIndex($(forms).get(i), options.prefix, i);
  109
+						$(forms.get(i)).find("*").each(function() {
132 110
 							updateElementIndex(this, options.prefix, i);
133 111
 						});
134 112
 					}
11  django/contrib/admin/media/js/inlines.min.js
... ...
@@ -1,6 +1,5 @@
1  
-(function(a){a.fn.formset=function(g){var b=a.extend({},a.fn.formset.defaults,g),l=function(d,f,e){var c=RegExp("("+f+"-\\d+)");f=f+"-"+e;a(d).attr("for")&&a(d).attr("for",a(d).attr("for").replace(c,f));if(d.id)d.id=d.id.replace(c,f);if(d.name)d.name=d.name.replace(c,f)};g=a("#id_"+b.prefix+"-TOTAL_FORMS").attr("autocomplete","off");var i=parseInt(g.val()),h=a("#id_"+b.prefix+"-MAX_NUM_FORMS").attr("autocomplete","off");g=h.val()==""||h.val()-g.val()>0;a(this).each(function(){a(this).not("."+b.emptyCssClass).addClass(b.formCssClass)});
2  
-if(a(this).length&&g){var j;if(a(this).attr("tagName")=="TR"){g=this.eq(0).children().length;a(this).parent().append('<tr class="'+b.addCssClass+'"><td colspan="'+g+'"><a href="javascript:void(0)">'+b.addText+"</a></tr>");j=a(this).parent().find("tr:last a")}else{a(this).filter(":last").after('<div class="'+b.addCssClass+'"><a href="javascript:void(0)">'+b.addText+"</a></div>");j=a(this).filter(":last").next().find("a")}j.click(function(){var d=a("#id_"+b.prefix+"-TOTAL_FORMS"),f=a("#"+b.prefix+"-empty"),
3  
-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=
4  
-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('<div><a class="'+b.deleteCssClass+'" href="javascript:void(0)">'+b.deleteText+"</a></div>");else e.is("ul")||e.is("ol")?e.append('<li><a class="'+b.deleteCssClass+'" href="javascript:void(0)">'+b.deleteText+"</a></li>"):e.children(":first").append('<span><a class="'+b.deleteCssClass+'" href="javascript:void(0)">'+
5  
-b.deleteText+"</a></span>");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;k<m;k++)a(c.get(k)).find("input,select,textarea,label,a").each(function(){l(this,
6  
-b.prefix,k)});return false});b.added&&b.added(e);return false})}return this};a.fn.formset.defaults={prefix:"form",addText:"add another",deleteText:"remove",addCssClass:"add-row",deleteCssClass:"delete-row",emptyCssClass:"empty-row",formCssClass:"dynamic-form",added:null,removed:null}})(django.jQuery);
  1
+(function(b){b.fn.formset=function(g){var a=b.extend({},b.fn.formset.defaults,g),k=function(c,f,d){var e=new RegExp("("+f+"-(\\d+|__prefix__))");f=f+"-"+d;b(c).attr("for")&&b(c).attr("for",b(c).attr("for").replace(e,f));if(c.id)c.id=c.id.replace(e,f);if(c.name)c.name=c.name.replace(e,f)};g=b("#id_"+a.prefix+"-TOTAL_FORMS").attr("autocomplete","off");var l=parseInt(g.val()),h=b("#id_"+a.prefix+"-MAX_NUM_FORMS").attr("autocomplete","off");g=h.val()==""||h.val()-g.val()>0;b(this).each(function(){b(this).not("."+
  2
+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('<tr class="'+a.addCssClass+'"><td colspan="'+g+'"><a href="javascript:void(0)">'+a.addText+"</a></tr>");j=b(this).parent().find("tr:last a")}else{b(this).filter(":last").after('<div class="'+a.addCssClass+'"><a href="javascript:void(0)">'+a.addText+"</a></div>");j=b(this).filter(":last").next().find("a")}j.click(function(){var c=b("#id_"+
  3
+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('<div><a class="'+a.deleteCssClass+'" href="javascript:void(0)">'+a.deleteText+"</a></div>");else d.is("ul")||d.is("ol")?d.append('<li><a class="'+a.deleteCssClass+'" href="javascript:void(0)">'+a.deleteText+"</a></li>"):d.children(":first").append('<span><a class="'+a.deleteCssClass+'" href="javascript:void(0)">'+
  4
+a.deleteText+"</a></span>");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<m;i++){k(b(e).get(i),a.prefix,i);
  5
+b(e.get(i)).find("*").each(function(){k(this,a.prefix,i)})}return false});a.added&&a.added(d);return false})}return this};b.fn.formset.defaults={prefix:"form",addText:"add another",deleteText:"remove",addCssClass:"add-row",deleteCssClass:"delete-row",emptyCssClass:"empty-row",formCssClass:"dynamic-form",added:null,removed:null}})(django.jQuery);

0 notes on commit c2ae6b2

Please sign in to comment.
Something went wrong with that request. Please try again.