Ticket #13614: 13614.filterselect-webkit-bug.2.diff

File 13614.filterselect-webkit-bug.2.diff, 7.8 KB (added by Julien Phalip, 8 years ago)
  • django/contrib/admin/static/admin/js/SelectFilter2.js

    diff --git a/django/contrib/admin/static/admin/js/SelectFilter2.js b/django/contrib/admin/static/admin/js/SelectFilter2.js
    index 2eb67e8..02c871b 100644
    a b window.SelectFilter = { 
    2121            return;
    2222        }
    2323        var from_box = document.getElementById(field_id);
    24         from_box.id += '_from'; // change its ID
     24
     25        // Create a clone to keep the actual values to be sent with the form
     26        var actual_box = $(from_box).clone().insertBefore(from_box).hide();
     27
     28        from_box.id += '_from';
     29        from_box.name += '_from';
    2530        from_box.className = 'filtered';
    2631
    2732        var ps = from_box.parentNode.getElementsByTagName('p');
    window.SelectFilter = { 
    6065        filter_input.id = field_id + '_input';
    6166
    6267        selector_available.appendChild(from_box);
    63         var choose_all = quickElement('a', selector_available, gettext('Add all'), 'href', 'javascript: (function(){ SelectBox.move_all("' + field_id + '_from", "' + field_id + '_to"); SelectFilter.refresh_icons("' + field_id + '");})()', 'id', field_id + '_add_all_link');
     68        var choose_all = quickElement('a', selector_available, gettext('Add all'), 'href', 'javascript: (function(){ SelectBox.move_all("' + field_id + '_from", "' + field_id + '_to"); SelectFilter.refresh_state("' + field_id + '");})()', 'id', field_id + '_add_all_link');
    6469        choose_all.className = 'selector-chooseall';
    6570
    6671        // <ul class="selector-chooser">
    6772        var selector_chooser = quickElement('ul', selector_div, '');
    6873        selector_chooser.className = 'selector-chooser';
    69         var add_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Add'), 'title', gettext('Add'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_from","' + field_id + '_to"); SelectFilter.refresh_icons("' + field_id + '");})()', 'id', field_id + '_add_link');
     74        var add_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Add'), 'title', gettext('Add'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_from","' + field_id + '_to"); SelectFilter.refresh_state("' + field_id + '");})()', 'id', field_id + '_add_link');
    7075        add_link.className = 'selector-add';
    71         var remove_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Remove'), 'title', gettext('Remove'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_to","' + field_id + '_from"); SelectFilter.refresh_icons("' + field_id + '");})()', 'id', field_id + '_remove_link');
     76        var remove_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Remove'), 'title', gettext('Remove'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_to","' + field_id + '_from"); SelectFilter.refresh_state("' + field_id + '");})()', 'id', field_id + '_remove_link');
    7277        remove_link.className = 'selector-remove';
    7378
    7479        // <div class="selector-chosen">
    window.SelectFilter = { 
    7782        var title_chosen = quickElement('h2', selector_chosen, interpolate(gettext('Added %s') + ' ', [field_name]));
    7883        quickElement('img', title_chosen, '', 'src', admin_media_prefix + 'img/icon-unknown.gif', 'width', '10', 'height', '10', 'class', 'help help-tooltip', 'title', interpolate(gettext('This is the list of added %s. You may remove some by selecting them below and then clicking the "Remove" button.'), [field_name]));
    7984
    80         var to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', 'multiple', 'size', from_box.size, 'name', from_box.getAttribute('name'));
     85        var to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', 'multiple', 'size', from_box.size, 'name', actual_box.attr('name') + '_to');
    8186        to_box.className = 'filtered';
    82         var clear_all = quickElement('a', selector_chosen, gettext('Remove all'), 'href', 'javascript: (function() { SelectBox.move_all("' + field_id + '_to", "' + field_id + '_from"); SelectFilter.refresh_icons("' + field_id + '");})()', 'id', field_id + '_remove_all_link');
     87        var clear_all = quickElement('a', selector_chosen, gettext('Remove all'), 'href', 'javascript: (function() { SelectBox.move_all("' + field_id + '_to", "' + field_id + '_from"); SelectFilter.refresh_state("' + field_id + '");})()', 'id', field_id + '_remove_all_link');
    8388        clear_all.className = 'selector-clearall';
    8489
    85         from_box.setAttribute('name', from_box.getAttribute('name') + '_old');
    86 
    8790        // Set up the JavaScript event handlers for the select box filter interface
    8891        addEvent(filter_input, 'keyup', function(e) { SelectFilter.filter_key_up(e, field_id); });
    8992        addEvent(filter_input, 'keydown', function(e) { SelectFilter.filter_key_down(e, field_id); });
    90         addEvent(from_box, 'change', function(e) { SelectFilter.refresh_icons(field_id) });
    91         addEvent(to_box, 'change', function(e) { SelectFilter.refresh_icons(field_id) });
    92         addEvent(from_box, 'dblclick', function() { SelectBox.move(field_id + '_from', field_id + '_to'); SelectFilter.refresh_icons(field_id); });
    93         addEvent(to_box, 'dblclick', function() { SelectBox.move(field_id + '_to', field_id + '_from'); SelectFilter.refresh_icons(field_id); });
    94         addEvent(findForm(from_box), 'submit', function() { SelectBox.select_all(field_id + '_to'); });
     93        addEvent(from_box, 'change', function(e) { SelectFilter.refresh_icons(field_id); });
     94        addEvent(to_box, 'change', function(e) { SelectFilter.refresh_icons(field_id); });
     95        addEvent(from_box, 'dblclick', function() { SelectBox.move(field_id + '_from', field_id + '_to'); SelectFilter.refresh_state(field_id); });
     96        addEvent(to_box, 'dblclick', function() { SelectBox.move(field_id + '_to', field_id + '_from'); SelectFilter.refresh_state(field_id); });
    9597        SelectBox.init(field_id + '_from');
    9698        SelectBox.init(field_id + '_to');
    9799        // Move selected from_box options to to_box
    window.SelectFilter = { 
    102104            $(to_box).height($(filter_p).outerHeight() + $(from_box).outerHeight());
    103105        }
    104106
    105         // Initial icon refresh
    106         SelectFilter.refresh_icons(field_id);
     107        // Initial state refresh
     108        SelectFilter.refresh_state(field_id);
    107109    },
    108110    refresh_icons: function(field_id) {
    109111        var from = $('#' + field_id + '_from');
    window.SelectFilter = { 
    117119        $('#' + field_id + '_add_all_link').toggleClass('active', from.find('option').length > 0);
    118120        $('#' + field_id + '_remove_all_link').toggleClass('active', to.find('option').length > 0);
    119121    },
     122    refresh_state: function(field_id) {
     123        SelectFilter.refresh_icons(field_id);
     124
     125        // Re-generate the content of the actual, hidden, box.
     126        var actual_box = document.getElementById(field_id);
     127
     128        // Clear all options.
     129        actual_box.options.length = 0;
     130
     131        var node, i;
     132        var cache_to = SelectBox.cache[field_id + '_to'];
     133        var cache_from = SelectBox.cache[field_id + '_from'];
     134
     135        // Add all options from the 'to' box and select them.
     136        for (i = 0; i < cache_to.length; i++) {
     137            node = cache_to[i];
     138            actual_box.options[actual_box.options.length] = new Option(node.text, node.value, false, true);
     139        }
     140        // Add all options from the 'from' box and keep them unselected.
     141        for (i = 0; i < cache_from.length; i++) {
     142            node = cache_from[i];
     143            actual_box.options[actual_box.options.length] = new Option(node.text, node.value, false, false);
     144        }
     145    },
    120146    filter_key_up: function(event, field_id) {
    121147        var from = document.getElementById(field_id + '_from');
    122148        // don't submit form if user pressed Enter
Back to Top