Ticket #2545: collapse_empty.patch

File collapse_empty.patch, 3.3 KB (added by Chris Beaven, 18 years ago)
  • django/contrib/admin/media/js/admin/CollapsedFieldsets.js

     
    1313var CollapsedFieldsets = {
    1414    collapse_re: /\bcollapse\b/,   // Class of fieldsets that should be dealt with.
    1515    collapsed_re: /\bcollapsed\b/, // Class that fieldsets get when they're hidden.
     16    collapse_empty_re: /\bcollapse_empty\b/,   // Extra class which only hides fieldsets with no values (still requires "collapse" class)
    1617    collapsed_class: 'collapsed',
    1718    init: function() {
    1819        var fieldsets = document.getElementsByTagName('fieldset');
     
    2122            // Collapse this fieldset if it has the correct class, and if it
    2223            // doesn't have any errors. (Collapsing shouldn't apply in the case
    2324            // of error messages.)
    24             if (fs.className.match(CollapsedFieldsets.collapse_re) && !CollapsedFieldsets.fieldset_has_errors(fs)) {
     25            if (fs.className.match(CollapsedFieldsets.collapse_re)
     26              && !CollapsedFieldsets.fieldset_has_errors(fs)
     27              && (!fs.className.match(CollapsedFieldsets.collapse_empty_re) || !CollapsedFieldsets.fieldset_not_empty(fs))) {
    2528                collapsed_seen = true;
    2629                // Give it an additional class, used by CSS to hide it.
    2730                fs.className += ' ' + CollapsedFieldsets.collapsed_class;
     
    5356        }
    5457        return false;
    5558    },
     59    fieldset_not_empty: function(fs) {
     60        // Returns true if any fields in the fieldset have values.
     61        var tags = new Array('select', 'textarea', 'input');
     62        var tag;
     63        for (var i=0; i<tags.length; i++) {
     64            tag = tags[i];
     65                var objs = fs.getElementsByTagName(tag);
     66                for (var j=0; j<objs.length; j++) {
     67                    o = objs[j];
     68                        if (tag=='input' && o.type=='checkbox') {
     69                            if (o.checked) {
     70                                return true;
     71                            }
     72                        }
     73                        else if (tag!='input' || o.type!='radio') {
     74                            if (o.value) {
     75                                return true;
     76                            }
     77                        }
     78                }
     79            }
     80        return false;
     81    },
    5682    show: function(fieldset_index) {
    5783        var fs = document.getElementsByTagName('fieldset')[fieldset_index];
    5884        // Remove the class name that causes the "display: none".
  • docs/model-api.txt

     
    11711171    'classes': 'wide extrapretty',
    11721172    }
    11731173
    1174 Two useful classes defined by the default admin-site stylesheet are
     1174Some useful classes defined by the default admin-site stylesheet are
    11751175``collapse`` and ``wide``. Fieldsets with the ``collapse`` style will be
    11761176initially collapsed in the admin and replaced with a small "click to expand"
    1177 link. Fieldsets with the ``wide`` style will be given extra horizontal space.
     1177link. Use both ``collapse`` and ``collapse_empty`` to only initially collapse
     1178the fieldset if all its fields are blank. Fieldsets with the ``wide`` style
     1179will be given extra horizontal space.
    11781180
    11791181``description``
    11801182~~~~~~~~~~~~~~~
Back to Top