Ticket #13068: prepopulate-with-9983-and-9784-fix.3.diff

File prepopulate-with-9983-and-9784-fix.3.diff, 7.2 KB (added by Sean Brant, 14 years ago)

Fixed adding the list as part of the css class hack

  • django/contrib/admin/media/js/admin/DateTimeShortcuts.js

     
    120120    },
    121121    handleClockQuicklink: function(num, val) {
    122122       DateTimeShortcuts.clockInputs[num].value = val;
     123       DateTimeShortcuts.clockInputs[num].focus();
    123124       DateTimeShortcuts.dismissClock(num);
    124125    },
    125126    // Add calendar widget to a given field.
     
    247248        format = format.replace('\n', '\\n');
    248249        format = format.replace('\t', '\\t');
    249250        format = format.replace("'", "\\'");
    250         return "function(y, m, d) { DateTimeShortcuts.calendarInputs["+num+"].value = new Date(y, m-1, d).strftime('"+format+"');document.getElementById(DateTimeShortcuts.calendarDivName1+"+num+").style.display='none';}";
     251        return "function(y, m, d) { DateTimeShortcuts.calendarInputs["+num+"].value = new Date(y, m-1, d).strftime('"+format+"');DateTimeShortcuts.calendarInputs["+num+"].focus();document.getElementById(DateTimeShortcuts.calendarDivName1+"+num+").style.display='none';}";
    251252    },
    252253    handleCalendarQuickLink: function(num, offset) {
    253254       var d = new Date();
    254255       d.setDate(d.getDate() + offset)
    255256       DateTimeShortcuts.calendarInputs[num].value = d.strftime(get_format('DATE_INPUT_FORMATS')[0]);
     257       DateTimeShortcuts.calendarInputs[num].focus();
    256258       DateTimeShortcuts.dismissCalendar(num);
    257259    },
    258260    cancelEventPropagation: function(e) {
  • 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            field.data('_changed', false);
     14            field.change(function() {
     15                field.data('_changed', true);
     16            });
     17
     18            var populate = function () {
     19                // Bail if the fields value has changed
     20                if (field.data('_changed') == true) return;
     21 
     22                var values = [];
     23                dependencies.each(function() {
     24                    if ($(this).val().length > 0) {
     25                        values.push($(this).val());
     26                    }
     27                });
     28                field.val(URLify(values.join(' '), maxLength));
     29            };
     30
     31            dependencies.keyup(populate).change(populate).focus(populate);
     32        });
     33    };
     34})(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 input = $(this).find('input');
     100                // This is pretty nasty
     101                var classes = [];
     102                $.each(this.className.split(' '), function(key, value) {
     103                    classes.push('.' + value);
     104                });
     105                var dependency_list = $('.empty-form td.' + classes.join('') + ' input').data('dependency_list');
     106                var dependencies = row.find(dependency_list.join(','));
     107                if (dependencies.length) {
     108                    input.prepopulate(dependencies, input.attr('maxlength'));
     109                }
     110            });
     111        }
    97112        $(rows).formset({
    98113            prefix: "{{ inline_admin_formset.formset.prefix }}",
    99114            addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
     
    103118            emptyCssClass: "empty-form",
    104119            removed: alternatingRows,
    105120            added: (function(row) {
     121                initPrepopulatedFields(row);
    106122                reinitDateTimeShortCuts();
    107123                updateSelectFilter();
    108124                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    field = {
     7        id: '#{{ field.field.auto_id }}',
     8        dependency_ids: [],
     9        dependency_list: [],
     10        maxLength: {{ field.field.field.max_length|default_if_none:"50" }}
     11    };
     12
    413    {% 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     }
     14    field['dependency_ids'].push('#{{ dependency.auto_id }}');
     15    field['dependency_list'].push('.{{ dependency.name }} input');
    916    {% endfor %}
     17   
     18    $('.empty-form td.{{ field.field.name }}').addClass('prepopulated_field');
     19    $(field.id).data('dependency_list', field['dependency_list'])
     20               .prepopulate($(field['dependency_ids'].join(',')), field.maxLength);
    1021{% endfor %}
     22})(jQuery.noConflict());
    1123</script>
Back to Top