Ticket #15910: 15910-delete-links-on-all-inline-formset-items.patch

File 15910-delete-links-on-all-inline-formset-items.patch, 5.4 KB (added by Arthur de Jong <arthur@…>, 13 years ago)
  • django/contrib/admin/media/js/inlines.js

     
    3333                var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS").attr("autocomplete", "off");
    3434                var nextIndex = parseInt(totalForms.val());
    3535                var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").attr("autocomplete", "off");
     36                // add delete buttons to all rows that don't have a delete checkbox
     37                $(this).each(function() {
     38                        // skip rows that have an original
     39                        if (!$(this).find("[name=" + $(this).attr("id") + "-id]").val()) {
     40                                if ($(this).is("tr")) {
     41                                        // If the forms are laid out in table rows, insert
     42                                        // the remove button into the last table cell:
     43                                        $(this).children(":last").append('<div><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></div>");
     44                                } else if ($(this).is("ul") || $(this).is("ol")) {
     45                                        // If they're laid out as an ordered/unordered list,
     46                                        // insert an <li> after the last list item:
     47                                        $(this).append('<li><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></li>");
     48                                } else {
     49                                        // Otherwise, just insert the remove button as the
     50                                        // last child element of the form's container:
     51                                        $(this).children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>");
     52                                }
     53                                // The delete button of each row triggers a bunch of other things
     54                                $(this).find("a." + options.deleteCssClass).click(function() {
     55                                        // Remove the parent form containing this button:
     56                                        var row = $(this).parents("." + options.formCssClass);
     57                                        row.remove();
     58                                        nextIndex -= 1;
     59                                        // If a post-delete callback was provided, call it with the deleted form:
     60                                        if (options.removed) {
     61                                                options.removed(row);
     62                                        }
     63                                        // Update the TOTAL_FORMS form count.
     64                                        var forms = $("." + options.formCssClass);
     65                                        $("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length);
     66                                        // Show add button again once we drop below max
     67                                        if ((maxForms.val() == '') || (maxForms.val()-forms.length) > 0) {
     68                                                addButton.parent().show();
     69                                        }
     70                                        // Also, update names and ids for all remaining form controls
     71                                        // so they remain in sequence:
     72                                        for (var i=0, formCount=forms.length; i<formCount; i++)
     73                                        {
     74                                                updateElementIndex($(forms).get(i), options.prefix, i);
     75                                                $(forms.get(i)).find("*").each(function() {
     76                                                        updateElementIndex(this, options.prefix, i);
     77                                                });
     78                                        }
     79                                        return false;
     80                                });
     81                        }
     82                });
    3683                // only show the add button if we are allowed to add more items,
    3784        // note that max_num = None translates to a blank string.
    3885                var showAddButton = maxForms.val() == '' || (maxForms.val()-totalForms.val()) > 0;
     
    59106                                row.removeClass(options.emptyCssClass)
    60107                                    .addClass(options.formCssClass)
    61108                                    .attr("id", options.prefix + "-" + nextIndex);
    62                                 if (row.is("tr")) {
    63                                         // If the forms are laid out in table rows, insert
    64                                         // the remove button into the last table cell:
    65                                         row.children(":last").append('<div><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></div>");
    66                                 } else if (row.is("ul") || row.is("ol")) {
    67                                         // If they're laid out as an ordered/unordered list,
    68                                         // insert an <li> after the last list item:
    69                                         row.append('<li><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></li>");
    70                                 } else {
    71                                         // Otherwise, just insert the remove button as the
    72                                         // last child element of the form's container:
    73                                         row.children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>");
    74                                 }
    75109                                row.find("*").each(function() {
    76110                                        updateElementIndex(this, options.prefix, totalForms.val());
    77111                                });
     
    84118                                if ((maxForms.val() != '') && (maxForms.val()-totalForms.val()) <= 0) {
    85119                                        addButton.parent().hide();
    86120                                }
    87                                 // The delete button of each row triggers a bunch of other things
    88                                 row.find("a." + options.deleteCssClass).click(function() {
    89                                         // Remove the parent form containing this button:
    90                                         var row = $(this).parents("." + options.formCssClass);
    91                                         row.remove();
    92                                         nextIndex -= 1;
    93                                         // If a post-delete callback was provided, call it with the deleted form:
    94                                         if (options.removed) {
    95                                                 options.removed(row);
    96                                         }
    97                                         // Update the TOTAL_FORMS form count.
    98                                         var forms = $("." + options.formCssClass);
    99                                         $("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length);
    100                                         // Show add button again once we drop below max
    101                                         if ((maxForms.val() == '') || (maxForms.val()-forms.length) > 0) {
    102                                                 addButton.parent().show();
    103                                         }
    104                                         // Also, update names and ids for all remaining form controls
    105                                         // so they remain in sequence:
    106                                         for (var i=0, formCount=forms.length; i<formCount; i++)
    107                                         {
    108                                                 updateElementIndex($(forms).get(i), options.prefix, i);
    109                                                 $(forms.get(i)).find("*").each(function() {
    110                                                         updateElementIndex(this, options.prefix, i);
    111                                                 });
    112                                         }
    113                                         return false;
    114                                 });
    115121                                // If a post-add callback was supplied, call it with the added form:
    116122                                if (options.added) {
    117123                                        options.added(row);
Back to Top