Permalink
Browse files

Fixed #12508 - Added ability to dynamically add inlines in the admin …

…app.

Refs #13. Also introduces an ``empty_form`` attribute on formsets to make it easier to implement dynamic forms.

Many thanks to Zain Memon for the initial patch from his Summer of Code 2009 project, Stanislaus Madueke for his django-dynamic-formset app and all the other people helping out.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12297 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent df82175 commit c14937cf7a1e8c25702e89485cc2dd33aa0d3a16 @jezdez jezdez committed Jan 26, 2010
View
@@ -295,6 +295,7 @@ answer newbie questions, and generally made Django that much better:
msaelices <msaelices@gmail.com>
Martin Mahner <http://www.mahner.org/>
Matt McClanahan <http://mmcc.cx/>
+ Stanislaus Madueke
Frantisek Malina <vizualbod@vizualbod.com>
Mike Malone <mjmalone@gmail.com>
Martin Maney <http://www.chipy.org/Martin_Maney>
@@ -314,6 +315,7 @@ answer newbie questions, and generally made Django that much better:
mccutchen@gmail.com
Paul McLanahan <paul@mclanahan.net>
Tobias McNulty <http://www.caktusgroup.com/blog>
+ Zain Memon
Christian Metts
michael.mcewan@gmail.com
michal@plovarna.cz
@@ -195,6 +195,9 @@ def __iter__(self):
self.opts.prepopulated_fields, None, self.readonly_fields,
model_admin=self.model_admin)
+ yield InlineAdminForm(self.formset, self.formset.empty_form,
+ self.fieldsets, self.opts.prepopulated_fields, None)
+
def fields(self):
fk = getattr(self.formset, "fk", None)
for i, field in enumerate(flatten_fieldsets(self.fieldsets)):
@@ -365,7 +365,7 @@ input[type=text], input[type=password], textarea, select, .vTextField {
.button, input[type=submit], input[type=button], .submit-row input {
background: white url(../img/admin/nav-bg.gif) bottom repeat-x;
- padding: 3px;
+ padding: 3px 5px;
color: black;
border: 1px solid #bbb;
border-color: #ddd #aaa #aaa #ddd;
@@ -247,10 +247,7 @@ fieldset.monospace textarea {
}
.inline-related h3 span.delete {
- padding-left: 20px;
- position: absolute;
- top: 2px;
- right: 10px;
+ float: right;
}
.inline-related h3 span.delete label {
@@ -274,6 +271,11 @@ fieldset.monospace textarea {
color: #fff;
}
+.inline-group .tabular fieldset.module {
+ border: none;
+ border-bottom: 1px solid #ddd;
+}
+
.inline-related.tabular fieldset.module table {
width: 100%;
}
@@ -320,8 +322,28 @@ fieldset.monospace textarea {
padding: 0 5px;
}
-.inline-group ul.tools a.add {
+.inline-group div.add-row,
+.inline-group .tabular tr.add-row td {
+ color: #666;
+ padding: 3px 5px;
+ border-bottom: 1px solid #ddd;
+ background: #e1e1e1 url(../img/admin/nav-bg.gif) top left repeat-x;
+}
+
+.inline-group .tabular tr.add-row td {
+ padding: 4px 5px 3px;
+ border-bottom: none;
+}
+
+.inline-group ul.tools a.add,
+.inline-group div.add-row a,
+.inline-group .tabular tr.add-row td a {
background: url(../img/admin/icon_addlink.gif) 0 50% no-repeat;
padding-left: 14px;
+ font-size: 11px;
+ outline: 0; /* Remove dotted border around link */
}
+.empty-form {
+ display: none;
+}
@@ -48,4 +48,10 @@
.change-list .filtered {
_height: 400px;
+}
+
+/* IE doesn't know alpha transparency in PNGs */
+
+.inline-deletelink {
+ background: transparent url(../img/admin/inline-delete-8bit.png) no-repeat;
}
@@ -187,6 +187,10 @@ fieldset .field-box {
text-align: right;
}
+.inline-deletelink {
+ float: left;
+}
+
/* MISC */
.inline-related h2 {
@@ -416,13 +416,13 @@ ul.orderer li.deleted:hover, ul.orderer li.deleted a.selector:hover {
/* EDIT INLINE */
.inline-deletelink {
- display: block;
+ float: right;
text-indent: -9999px;
background: transparent url(../img/admin/inline-delete.png) no-repeat;
width: 15px;
height: 15px;
- margin: 0.4em 0;
border: 0px none;
+ outline: 0; /* Remove dotted border around link */
}
.inline-deletelink:hover {
@@ -0,0 +1,134 @@
+/**
+ * Django admin inlines
+ *
+ * Based on jQuery Formset 1.1
+ * @author Stanislaus Madueke (stan DOT madueke AT gmail DOT com)
+ * @requires jQuery 1.2.6 or later
+ *
+ * Copyright (c) 2009, Stanislaus Madueke
+ * All rights reserved.
+ *
+ * Spiced up with Code from Zain Memon's GSoC project 2009
+ * and modified for Django by Jannis Leidel
+ *
+ * Licensed under the New BSD License
+ * See: http://www.opensource.org/licenses/bsd-license.php
+ */
+(function($) {
+ $.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 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);
+ };
+ var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS");
+ var initialForms = $("#id_" + options.prefix + "-INITIAL_FORMS");
+ var maxForms = parseInt(totalForms.val());
+ // only show the add button if we are allowed to add more items
+ var showAddButton = (maxForms - parseInt(initialForms.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;
+ if ($(this).attr("tagName") == "TR") {
+ // If forms are laid out as table rows, insert the
+ // "add" button in a new table row:
+ var numCols = this.eq(0).children().length;
+ $(this).parent().append('<tr class="' + options.addCssClass + '"><td colspan="' + numCols + '"><a href="javascript:void(0)">' + options.addText + "</a></tr>");
+ addButton = $(this).parent().find("tr:last a");
+ } else {
+ // Otherwise, insert it immediately after the last form:
+ $(this).filter(":last").after('<div class="' + options.addCssClass + '"><a href="javascript:void(0)">' + options.addText + "</a></div>");
+ 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 template = $("#" + options.prefix + "-empty");
+ var row = template.clone(true).get(0);
+ $(row).removeClass(options.emptyCssClass).removeAttr("id").insertBefore($(template));
+ $(row).html($(row).html().replace(/__prefix__/g, nextIndex));
+ $(row).addClass(options.formCssClass).attr("id", options.prefix + nextIndex);
+ if ($(row).is("TR")) {
+ // If the forms are laid out in table rows, insert
+ // the remove button into the last table cell:
+ $(row).children(":last").append('<div><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></div>");
+ } else if ($(row).is("UL") || $(row).is("OL")) {
+ // If they're laid out as an ordered/unordered list,
+ // insert an <li> after the last list item:
+ $(row).append('<li><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></li>");
+ } else {
+ // Otherwise, just insert the remove button as the
+ // 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>");
+ }
+ // 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) {
+ addButton.parent().hide();
+ }
+ // The delete button of each row triggers a bunch of other things
+ $(row).find("a." + options.deleteCssClass).click(function() {
+ // Remove the parent form containing this button:
+ 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);
+ // 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) {
+ 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++) {
+ $(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));
+ return false;
+ });
+ }
+ 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
+ }
+})(jQuery)
@@ -0,0 +1 @@
+(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);
Oops, something went wrong.

0 comments on commit c14937c

Please sign in to comment.