Permalink
Browse files

Fixed #12692 - Properly handle the extra clause of admin inline forms…

…ets. Also fixes #12703, second error.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12369 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent f70a088 commit 9555f2be9dc7c5cc1aeb3f4a047550ccd9b0cb2c @jezdez jezdez committed Feb 1, 2010
@@ -20,29 +20,24 @@
var updateElementIndex = function(el, prefix, ndx) {
var id_regex = new RegExp("(" + prefix + "-\\d+)");
var replacement = prefix + "-" + ndx;
- if ($(el).attr("for")) $(el).attr("for", $(el).attr("for").replace(id_regex, replacement));
- if (el.id) el.id = el.id.replace(id_regex, replacement);
- if (el.name) el.name = el.name.replace(id_regex, replacement);
+ if ($(el).attr("for")) {
+ $(el).attr("for", $(el).attr("for").replace(id_regex, replacement));
+ }
+ if (el.id) {
+ el.id = el.id.replace(id_regex, replacement);
+ }
+ if (el.name) {
+ el.name = el.name.replace(id_regex, replacement);
+ }
};
var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS");
var initialForms = $("#id_" + options.prefix + "-INITIAL_FORMS");
- var maxForms = parseInt(totalForms.val());
+ var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS");
// only show the add button if we are allowed to add more items
- var showAddButton = (maxForms - parseInt(initialForms.val())) > 0;
+ var showAddButton = ((maxForms.val() == 0) || ((maxForms.val()-totalForms.val()) > 0));
var selectedItems = this;
$(this).each(function(i) {
$(this).not("." + options.emptyCssClass).addClass(options.formCssClass);
- // hide the extras, but only if there were no form errors
- if (!$(".errornote").html()) {
- var relatedItems = $(selectedItems).not("." + options.emptyCssClass);
- extraRows = relatedItems.length;
- if (parseInt(initialForms.val()) >= 0) {
- $(relatedItems).slice(initialForms.val()).remove();
- } else {
- $(relatedItems).remove();
- }
- totalForms.val(parseInt(initialForms.val()));
- }
});
if ($(this).length && showAddButton) {
var addButton;
@@ -58,9 +53,8 @@
addButton = $(this).filter(":last").next().find("a");
}
addButton.click(function() {
- var totalForms = parseInt($("#id_" + options.prefix + "-TOTAL_FORMS").val());
- var initialForms = parseInt($("#id_" + options.prefix + "-INITIAL_FORMS").val());
- var nextIndex = totalForms + 1;
+ var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS");
+ var nextIndex = parseInt(totalForms.val()) + 1;
var template = $("#" + options.prefix + "-empty");
var row = template.clone(true).get(0);
$(row).removeClass(options.emptyCssClass).removeAttr("id").insertBefore($(template));
@@ -79,10 +73,13 @@
// last child element of the form's container:
$(row).children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>");
}
+ $(row).find("input,select,textarea,label").each(function() {
+ updateElementIndex(this, options.prefix, totalForms.val());
+ });
// Update number of total forms
- $("#id_" + options.prefix + "-TOTAL_FORMS").val(nextIndex);
- // Hide add button in case we've hit the max
- if (maxForms <= nextIndex) {
+ $(totalForms).val(nextIndex);
+ // Hide add button in case we've hit the max, except we want to add infinitely
+ if ((maxForms.val() != 0) && (maxForms.val() <= totalForms.val())) {
addButton.parent().hide();
}
// The delete button of each row triggers a bunch of other things
@@ -91,44 +88,45 @@
var row = $(this).parents("." + options.formCssClass);
row.remove();
// If a post-delete callback was provided, call it with the deleted form:
- if (options.removed) options.removed(row);
+ if (options.removed) {
+ options.removed(row);
+ }
// Update the TOTAL_FORMS form count.
var forms = $("." + options.formCssClass);
$("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length);
// Show add button again once we drop below max
- if (maxForms >= forms.length) {
+ if ((maxForms.val() == 0) || (maxForms.val() >= forms.length)) {
addButton.parent().show();
}
// Also, update names and ids for all remaining form controls
// so they remain in sequence:
- for (var i=0, formCount=forms.length; i<formCount; i++) {
+ for (var i=0, formCount=forms.length; i<formCount; i++)
+ {
$(forms.get(i)).find("input,select,textarea,label").each(function() {
updateElementIndex(this, options.prefix, i);
});
}
return false;
});
- $(row).find("input,select,textarea,label").each(function() {
- updateElementIndex(this, options.prefix, totalForms);
- });
// If a post-add callback was supplied, call it with the added form:
- if (options.added) options.added($(row));
+ if (options.added) {
+ options.added($(row));
+ }
return false;
});
}
- return $(this);
+ return this;
}
-
/* Setup plugin defaults */
$.fn.formset.defaults = {
- prefix: "form", // The form prefix for your django formset
- addText: "add another", // Text for the add link
- deleteText: "remove", // Text for the delete link
- addCssClass: "add-row", // CSS class applied to the add link
- deleteCssClass: "delete-row", // CSS class applied to the delete link
- emptyCssClass: "empty-row", // CSS class applied to the empty row
- formCssClass: "dynamic-form", // CSS class applied to each form in a formset
- added: null, // Function called each time a new form is added
- removed: null // Function called each time a form is deleted
+ prefix: "form", // The form prefix for your django formset
+ addText: "add another", // Text for the add link
+ deleteText: "remove", // Text for the delete link
+ addCssClass: "add-row", // CSS class applied to the add link
+ deleteCssClass: "delete-row", // CSS class applied to the delete link
+ emptyCssClass: "empty-row", // CSS class applied to the empty row
+ formCssClass: "dynamic-form", // CSS class applied to each form in a formset
+ added: null, // Function called each time a new form is added
+ removed: null // Function called each time a form is deleted
}
-})(jQuery)
+})(jQuery);
@@ -1 +1,5 @@
-(function(a){a.fn.formset=function(b){var k=a.extend({},a.fn.formset.defaults,b);var j=function(o,p,m){var l=new RegExp("("+p+"-\\d+)");var n=p+"-"+m;if(a(o).attr("for")){a(o).attr("for",a(o).attr("for").replace(l,n))}if(o.id){o.id=o.id.replace(l,n)}if(o.name){o.name=o.name.replace(l,n)}};var f=a("#id_"+k.prefix+"-TOTAL_FORMS");var c=a("#id_"+k.prefix+"-INITIAL_FORMS");var h=parseInt(f.val());var i=(h-parseInt(c.val()))>0;var e=this;a(this).each(function(m){a(this).not("."+k.emptyCssClass).addClass(k.formCssClass);if(!a(".errornote").html()){var l=a(e).not("."+k.emptyCssClass);extraRows=l.length;if(parseInt(c.val())>=0){a(l).slice(c.val()).remove()}else{a(l).remove()}f.val(parseInt(c.val()))}});if(a(this).length&&i){var g;if(a(this).attr("tagName")=="TR"){var d=this.eq(0).children().length;a(this).parent().append('<tr class="'+k.addCssClass+'"><td colspan="'+d+'"><a href="javascript:void(0)">'+k.addText+"</a></tr>");g=a(this).parent().find("tr:last a")}else{a(this).filter(":last").after('<div class="'+k.addCssClass+'"><a href="javascript:void(0)">'+k.addText+"</a></div>");g=a(this).filter(":last").next().find("a")}g.click(function(){var o=parseInt(a("#id_"+k.prefix+"-TOTAL_FORMS").val());var n=parseInt(a("#id_"+k.prefix+"-INITIAL_FORMS").val());var l=o+1;var m=a("#"+k.prefix+"-empty");var p=m.clone(true).get(0);a(p).removeClass(k.emptyCssClass).removeAttr("id").insertBefore(a(m));a(p).html(a(p).html().replace(/__prefix__/g,l));a(p).addClass(k.formCssClass).attr("id",k.prefix+l);if(a(p).is("TR")){a(p).children(":last").append('<div><a class="'+k.deleteCssClass+'" href="javascript:void(0)">'+k.deleteText+"</a></div>")}else{if(a(p).is("UL")||a(p).is("OL")){a(p).append('<li><a class="'+k.deleteCssClass+'" href="javascript:void(0)">'+k.deleteText+"</a></li>")}else{a(p).children(":first").append('<span><a class="'+k.deleteCssClass+'" href="javascript:void(0)">'+k.deleteText+"</a></span>")}}a("#id_"+k.prefix+"-TOTAL_FORMS").val(l);if(h<=l){g.parent().hide()}a(p).find("a."+k.deleteCssClass).click(function(){var t=a(this).parents("."+k.formCssClass);t.remove();if(k.removed){k.removed(t)}var q=a("."+k.formCssClass);a("#id_"+k.prefix+"-TOTAL_FORMS").val(q.length);if(h>=q.length){g.parent().show()}for(var r=0,s=q.length;r<s;r++){a(q.get(r)).find("input,select,textarea,label").each(function(){j(this,k.prefix,r)})}return false});a(p).find("input,select,textarea,label").each(function(){j(this,k.prefix,o)});if(k.added){k.added(a(p))}return false})}return a(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}})(jQuery);
+(function(a){a.fn.formset=function(f){var b=a.extend({},a.fn.formset.defaults,f),l=function(d,e,j){var c=new RegExp("("+e+"-\\d+)");e=e+"-"+j;a(d).attr("for")&&a(d).attr("for",a(d).attr("for").replace(c,e));if(d.id)d.id=d.id.replace(c,e);if(d.name)d.name=d.name.replace(c,e)};f=a("#id_"+b.prefix+"-TOTAL_FORMS");a("#id_"+b.prefix+"-INITIAL_FORMS");var h=a("#id_"+b.prefix+"-MAX_NUM_FORMS");f=h.val()==0||h.val()-f.val()>0;a(this).each(function(){a(this).not("."+b.emptyCssClass).addClass(b.formCssClass)});
+if(a(this).length&&f){var i;if(a(this).attr("tagName")=="TR"){f=this.eq(0).children().length;a(this).parent().append('<tr class="'+b.addCssClass+'"><td colspan="'+f+'"><a href="javascript:void(0)">'+b.addText+"</a></tr>");i=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>");i=a(this).filter(":last").next().find("a")}i.click(function(){var d=a("#id_"+b.prefix+"-TOTAL_FORMS"),e=parseInt(d.val())+
+1,j=a("#"+b.prefix+"-empty"),c=j.clone(true).get(0);a(c).removeClass(b.emptyCssClass).removeAttr("id").insertBefore(a(j));a(c).html(a(c).html().replace(/__prefix__/g,e));a(c).addClass(b.formCssClass).attr("id",b.prefix+e);if(a(c).is("TR"))a(c).children(":last").append('<div><a class="'+b.deleteCssClass+'" href="javascript:void(0)">'+b.deleteText+"</a></div>");else a(c).is("UL")||a(c).is("OL")?a(c).append('<li><a class="'+b.deleteCssClass+'" href="javascript:void(0)">'+b.deleteText+"</a></li>"):a(c).children(":first").append('<span><a class="'+
+b.deleteCssClass+'" href="javascript:void(0)">'+b.deleteText+"</a></span>");a(c).find("input,select,textarea,label").each(function(){l(this,b.prefix,d.val())});a(d).val(e);h.val()!=0&&h.val()<=d.val()&&i.parent().hide();a(c).find("a."+b.deleteCssClass).click(function(){var g=a(this).parents("."+b.formCssClass);g.remove();b.removed&&b.removed(g);g=a("."+b.formCssClass);a("#id_"+b.prefix+"-TOTAL_FORMS").val(g.length);if(h.val()==0||h.val()>=g.length)i.parent().show();for(var k=0,m=g.length;k<m;k++)a(g.get(k)).find("input,select,textarea,label").each(function(){l(this,
+b.prefix,k)});return false});b.added&&b.added(a(c));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}})(jQuery);
@@ -19,24 +19,25 @@
</div>
<script type="text/javascript">
-jQuery.noConflict();
-jQuery(document).ready(function($) {
- var rows = "#{{ inline_admin_formset.formset.prefix }}-group .inline-related";
- updateInlineLabel = function(row) {
- $(rows).find(".inline_label").each(function(i) {
- var count = i + 1;
- $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
+(function($) {
+ $(document).ready(function() {
+ var rows = "#{{ inline_admin_formset.formset.prefix }}-group .inline-related";
+ updateInlineLabel = function(row) {
+ $(rows).find(".inline_label").each(function(i) {
+ var count = i + 1;
+ $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
+ });
+ }
+ $(rows).formset({
+ prefix: "{{ inline_admin_formset.formset.prefix }}",
+ addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
+ formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
+ deleteCssClass: "inline-deletelink",
+ deleteText: "{% trans "Remove" %}",
+ emptyCssClass: "empty-form",
+ removed: updateInlineLabel,
+ added: updateInlineLabel
});
- }
- $(rows).formset({
- prefix: "{{ inline_admin_formset.formset.prefix }}",
- addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
- formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
- deleteCssClass: "inline-deletelink",
- deleteText: "{% trans "Remove" %}",
- emptyCssClass: "empty-form",
- removed: updateInlineLabel,
- added: updateInlineLabel
});
-});
+})(jQuery.noConflict());
</script>
@@ -65,23 +65,24 @@
</div>
<script type="text/javascript">
-jQuery.noConflict();
-jQuery(document).ready(function($) {
- var rows = "#{{ inline_admin_formset.formset.prefix }}-group .tabular.inline-related tbody tr";
- alternatingRows = function(row) {
- $(rows).not(".add-row").removeClass("row1 row2")
- .filter(":even").addClass("row1").end()
- .filter(rows + ":odd").addClass("row2");
- }
- $(rows).formset({
- prefix: "{{ inline_admin_formset.formset.prefix }}",
- addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
- formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
- deleteCssClass: "inline-deletelink",
- deleteText: "{% trans "Remove" %}",
- emptyCssClass: "empty-form",
- removed: alternatingRows,
- added: alternatingRows
+(function($) {
+ $(document).ready(function($) {
+ var rows = "#{{ inline_admin_formset.formset.prefix }}-group .tabular.inline-related tbody tr";
+ alternatingRows = function(row) {
+ $(rows).not(".add-row").removeClass("row1 row2")
+ .filter(":even").addClass("row1").end()
+ .filter(rows + ":odd").addClass("row2");
+ }
+ $(rows).formset({
+ prefix: "{{ inline_admin_formset.formset.prefix }}",
+ addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
+ formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
+ deleteCssClass: "inline-deletelink",
+ deleteText: "{% trans "Remove" %}",
+ emptyCssClass: "empty-form",
+ removed: alternatingRows,
+ added: alternatingRows
+ });
});
-});
+})(jQuery.noConflict());
</script>
@@ -12,6 +12,7 @@
# special field names
TOTAL_FORM_COUNT = 'TOTAL_FORMS'
INITIAL_FORM_COUNT = 'INITIAL_FORMS'
+MAX_NUM_FORM_COUNT = 'MAX_NUM_FORMS'
ORDERING_FIELD_NAME = 'ORDER'
DELETION_FIELD_NAME = 'DELETE'
@@ -24,6 +25,7 @@ class ManagementForm(Form):
def __init__(self, *args, **kwargs):
self.base_fields[TOTAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
self.base_fields[INITIAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
+ self.base_fields[MAX_NUM_FORM_COUNT] = IntegerField(widget=HiddenInput)
super(ManagementForm, self).__init__(*args, **kwargs)
class BaseFormSet(StrAndUnicode):
@@ -56,7 +58,8 @@ def _management_form(self):
else:
form = ManagementForm(auto_id=self.auto_id, prefix=self.prefix, initial={
TOTAL_FORM_COUNT: self.total_form_count(),
- INITIAL_FORM_COUNT: self.initial_form_count()
+ INITIAL_FORM_COUNT: self.initial_form_count(),
+ MAX_NUM_FORM_COUNT: self.max_num
})
return form
management_form = property(_management_form)
Oops, something went wrong. Retry.

0 comments on commit 9555f2b

Please sign in to comment.