Skip to content

Commit

Permalink
Fixed #12508 - Added ability to dynamically add inlines in the admin …
Browse files Browse the repository at this point in the history
…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
jezdez committed Jan 26, 2010
1 parent df82175 commit c14937c
Show file tree
Hide file tree
Showing 19 changed files with 6,503 additions and 36 deletions.
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions django/contrib/admin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
2 changes: 1 addition & 1 deletion django/contrib/admin/media/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 27 additions & 5 deletions django/contrib/admin/media/css/forms.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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%;
}
Expand Down Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions django/contrib/admin/media/css/ie.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions django/contrib/admin/media/css/rtl.css
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ fieldset .field-box {
text-align: right;
}

.inline-deletelink {
float: left;
}

/* MISC */

.inline-related h2 {
Expand Down
4 changes: 2 additions & 2 deletions django/contrib/admin/media/css/widgets.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
134 changes: 134 additions & 0 deletions django/contrib/admin/media/js/inlines.js
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions django/contrib/admin/media/js/inlines.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c14937c

Please sign in to comment.