Ticket #13068: prepopulate.diff

File prepopulate.diff, 6.0 KB (added by Sean Brant, 14 years ago)
  • django/contrib/admin/media/js/prepopulate.js

     
     1(function($) {
     2    $.fn.prepopulate = function(dependencies, maxLength) {
     3        /*
     4            Depends on urlify.js
     5            Populates a selected field with the values of the dependent fields,
     6            URLifies and shortens the string.
     7            dependencies - selected jQuery object of dependent fields
     8            maxLength - maximum length of the URLify'd string
     9        */
     10        return this.each(function() {
     11            var field = $(this);
     12
     13            // Bail if not an input
     14            if (!field.is('INPUT')) return;
     15
     16            field.data('_changed', false);
     17            field.change(function() {
     18                field.data('_changed', true);
     19            });
     20
     21            dependencies.keyup(function () {
     22                // Bail if the fields value has changed
     23                if (field.data('_changed') == true) return;
     24 
     25                var values = [];
     26                dependencies.each(function() {
     27                    if ($(this).val().length > 0) {
     28                        values.push($(this).val());
     29                    }
     30                });
     31                field.val(URLify(values.join(' '), maxLength));
     32            });
     33        });
     34    };
     35})(jQuery.noConflict());
  • django/contrib/admin/options.py

     
    273273            js.extend(['js/jquery.min.js', 'js/actions.min.js'])
    274274        if self.prepopulated_fields:
    275275            js.append('js/urlify.js')
     276            js.append('js/prepopulate.js')
    276277        if self.opts.get_ordered_objects():
    277278            js.extend(['js/getElementsBySelector.js', 'js/dom-drag.js' , 'js/admin/ordering.js'])
    278279
     
    12041205        js = ['js/jquery.min.js', 'js/inlines.min.js']
    12051206        if self.prepopulated_fields:
    12061207            js.append('js/urlify.js')
     1208            js.append('js/prepopulate.js')
    12071209        if self.filter_vertical or self.filter_horizontal:
    12081210            js.extend(['js/SelectBox.js' , 'js/SelectFilter2.js'])
    12091211        return forms.Media(js=['%s%s' % (settings.ADMIN_MEDIA_PREFIX, url) for url in js])
  • django/contrib/admin/templates/admin/edit_inline/tabular.html

     
    9494                })
    9595            }
    9696        }
     97        var initPrepopulatedFields = function(row) {
     98            $(row).find('td.prepopulated_field').each(function() {
     99                var field = $(this);
     100                var input = null;
     101                var dependencies = null;
     102                var selector = null;
     103               
     104                $.each(field.context.className.split(' '), function(key, value) {
     105                    selector = [];
     106                    if (value.search('dependencies_') >= 0) {
     107                        $.each(value.split(/_|,/).slice(1), function(key, value) {
     108                            selector.push('.' + value + ' input');
     109                        });
     110                        dependencies = row.find(selector.join(','));
     111                    }
     112                });
     113               
     114                if (dependencies.length) {
     115                    input = field.find('input');
     116                    input.prepopulate(dependencies, input.attr('maxlength'));
     117                }
     118            });
     119        }
    97120        $(rows).formset({
    98121            prefix: "{{ inline_admin_formset.formset.prefix }}",
    99122            addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
     
    103126            emptyCssClass: "empty-form",
    104127            removed: alternatingRows,
    105128            added: (function(row) {
     129                initPrepopulatedFields(row);
    106130                reinitDateTimeShortCuts();
    107131                updateSelectFilter();
    108132                alternatingRows(row);
  • django/contrib/admin/templates/admin/prepopulated_fields_js.html

     
    11<script type="text/javascript">
     2(function($) {
     3    var field = null;
     4
    25{% for field in prepopulated_fields %}
    3     document.getElementById("{{ field.field.auto_id }}").onchange = function() { this._changed = true; };
     6    $('.empty-form td.{{ field.field.name }}').addClass('prepopulated_field');
     7
     8    field = {
     9        id: '#{{ field.field.auto_id }}',
     10        dependency_ids: [],
     11        dependency_names: [],
     12        maxLength: {{ field.field.field.max_length|default_if_none:"50" }}
     13    };
     14
    415    {% for dependency in field.dependencies %}
    5     document.getElementById("{{ dependency.auto_id }}").onkeyup = function() {
    6         var e = document.getElementById("{{ field.field.auto_id }}");
    7         if (!e._changed) { e.value = URLify({% for innerdep in field.dependencies %}document.getElementById("{{ innerdep.auto_id }}").value{% if not forloop.last %} + ' ' + {% endif %}{% endfor %}, {{ field.field.field.max_length|default_if_none:"50" }}); }
    8     }
     16    field['dependency_ids'].push('#{{ dependency.auto_id }}');
     17    field['dependency_names'].push('{{ dependency.name }}');
    918    {% endfor %}
     19
     20    $('.empty-form td.{{ field.field.name }}').addClass('dependencies_' + field['dependency_names'].join(','));
     21    $(field.id).prepopulate($(field['dependency_ids'].join(',')), field.maxLength);
    1022{% endfor %}
     23})(jQuery.noConflict());
    1124</script>
Back to Top