/** * jQuery Formset 1.2 * @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. * * 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), flatExtraClasses = options.extraClasses.join(' '), $$ = $(this), applyExtraClasses = function(row, ndx) { if (options.extraClasses) { row.removeClass(flatExtraClasses); row.addClass(options.extraClasses[ndx % options.extraClasses.length]); } }, updateElementIndex = function(elem, prefix, ndx) { var idRegex = new RegExp('(' + prefix + '-\\d+-)|(^)'), replacement = prefix + '-' + ndx + '-'; if (elem.attr("for")) elem.attr("for", elem.attr("for").replace(idRegex, replacement)); if (elem.attr('id')) elem.attr('id', elem.attr('id').replace(idRegex, replacement)); if (elem.attr('name')) elem.attr('name', elem.attr('name').replace(idRegex, replacement)); }, hasChildElements = function(row) { return row.find('input,select,textarea,label').length > 0; }, insertDeleteLink = function(row) { 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('' + options.deleteText + ''); } else if (row.is('UL') || row.is('OL')) { // If they're laid out as an ordered/unordered list, // insert an
  • after the last list item: row.append('
  • ' + options.deleteText +'
  • '); } else { // Otherwise, just insert the remove button as the // last child element of the form's container: row.append('' + options.deleteText +''); } row.find('a.' + options.deleteCssClass).click(function() { var row = $(this).parents('.' + options.formCssClass), del = row.find('input:hidden[id $= "-DELETE"]'); if (del.length) { // We're dealing with an inline formset; rather than remove // this form from the DOM, we'll mark it as deleted and hide // it, then let Django handle the deleting: del.val('on'); row.hide(); } else { row.remove(); // Update the TOTAL_FORMS form count. // Also update names and IDs for all remaining form controls so they remain in sequence: var forms = $('.' + options.formCssClass).not('.formset-custom-template'); $('#id_' + options.prefix + '-TOTAL_FORMS').val(forms.length); for (var i=0, formCount=forms.length; i