PageRenderTime 20ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/admin/media/js/SelectFilter2.js

https://code.google.com/p/mango-py/
JavaScript | 130 lines | 94 code | 13 blank | 23 comment | 36 complexity | 01340a8affd2285048eb186a5f64c925 MD5 | raw file
  1/*
  2SelectFilter2 - Turns a multiple-select box into a filter interface.
  3
  4Different than SelectFilter because this is coupled to the admin framework.
  5
  6Requires core.js, SelectBox.js and addevent.js.
  7*/
  8
  9function findForm(node) {
 10    // returns the node of the form containing the given node
 11    if (node.tagName.toLowerCase() != 'form') {
 12        return findForm(node.parentNode);
 13    }
 14    return node;
 15}
 16
 17var SelectFilter = {
 18    init: function(field_id, field_name, is_stacked, admin_media_prefix) {
 19        if (field_id.match(/__prefix__/)){
 20            // Don't intialize on empty forms.
 21            return;
 22        }
 23        var from_box = document.getElementById(field_id);
 24        from_box.id += '_from'; // change its ID
 25        from_box.className = 'filtered';
 26
 27        var ps = from_box.parentNode.getElementsByTagName('p');
 28        for (var i=0; i<ps.length; i++) {
 29            if (ps[i].className.indexOf("info") != -1) {
 30                // Remove <p class="info">, because it just gets in the way.
 31                from_box.parentNode.removeChild(ps[i]);
 32            } else if (ps[i].className.indexOf("help") != -1) {
 33                // Move help text up to the top so it isn't below the select
 34                // boxes or wrapped off on the side to the right of the add
 35                // button:
 36                from_box.parentNode.insertBefore(ps[i], from_box.parentNode.firstChild);
 37            }
 38        }
 39
 40        // <div class="selector"> or <div class="selector stacked">
 41        var selector_div = quickElement('div', from_box.parentNode);
 42        selector_div.className = is_stacked ? 'selector stacked' : 'selector';
 43
 44        // <div class="selector-available">
 45        var selector_available = quickElement('div', selector_div, '');
 46        selector_available.className = 'selector-available';
 47        quickElement('h2', selector_available, interpolate(gettext('Available %s'), [field_name]));
 48        var filter_p = quickElement('p', selector_available, '');
 49        filter_p.className = 'selector-filter';
 50
 51        var search_filter_label = quickElement('label', filter_p, '', 'for', field_id + "_input", 'style', 'width:16px;padding:2px');
 52
 53        var search_selector_img = quickElement('img', search_filter_label, '', 'src', admin_media_prefix + 'img/admin/selector-search.gif');
 54        search_selector_img.alt = gettext("Filter");
 55
 56        filter_p.appendChild(document.createTextNode(' '));
 57
 58        var filter_input = quickElement('input', filter_p, '', 'type', 'text');
 59        filter_input.id = field_id + '_input';
 60        selector_available.appendChild(from_box);
 61        var choose_all = quickElement('a', selector_available, gettext('Choose all'), 'href', 'javascript: (function(){ SelectBox.move_all("' + field_id + '_from", "' + field_id + '_to"); })()');
 62        choose_all.className = 'selector-chooseall';
 63
 64        // <ul class="selector-chooser">
 65        var selector_chooser = quickElement('ul', selector_div, '');
 66        selector_chooser.className = 'selector-chooser';
 67        var add_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Add'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_from","' + field_id + '_to");})()');
 68        add_link.className = 'selector-add';
 69        var remove_link = quickElement('a', quickElement('li', selector_chooser, ''), gettext('Remove'), 'href', 'javascript: (function(){ SelectBox.move("' + field_id + '_to","' + field_id + '_from");})()');
 70        remove_link.className = 'selector-remove';
 71
 72        // <div class="selector-chosen">
 73        var selector_chosen = quickElement('div', selector_div, '');
 74        selector_chosen.className = 'selector-chosen';
 75        quickElement('h2', selector_chosen, interpolate(gettext('Chosen %s'), [field_name]));
 76        var selector_filter = quickElement('p', selector_chosen, gettext('Select your choice(s) and click '));
 77        selector_filter.className = 'selector-filter';
 78        quickElement('img', selector_filter, '', 'src', admin_media_prefix + (is_stacked ? 'img/admin/selector_stacked-add.gif':'img/admin/selector-add.gif'), 'alt', 'Add');
 79        var to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', 'multiple', 'size', from_box.size, 'name', from_box.getAttribute('name'));
 80        to_box.className = 'filtered';
 81        var clear_all = quickElement('a', selector_chosen, gettext('Clear all'), 'href', 'javascript: (function() { SelectBox.move_all("' + field_id + '_to", "' + field_id + '_from");})()');
 82        clear_all.className = 'selector-clearall';
 83
 84        from_box.setAttribute('name', from_box.getAttribute('name') + '_old');
 85
 86        // Set up the JavaScript event handlers for the select box filter interface
 87        addEvent(filter_input, 'keyup', function(e) { SelectFilter.filter_key_up(e, field_id); });
 88        addEvent(filter_input, 'keydown', function(e) { SelectFilter.filter_key_down(e, field_id); });
 89        addEvent(from_box, 'dblclick', function() { SelectBox.move(field_id + '_from', field_id + '_to'); });
 90        addEvent(to_box, 'dblclick', function() { SelectBox.move(field_id + '_to', field_id + '_from'); });
 91        addEvent(findForm(from_box), 'submit', function() { SelectBox.select_all(field_id + '_to'); });
 92        SelectBox.init(field_id + '_from');
 93        SelectBox.init(field_id + '_to');
 94        // Move selected from_box options to to_box
 95        SelectBox.move(field_id + '_from', field_id + '_to');
 96    },
 97    filter_key_up: function(event, field_id) {
 98        from = document.getElementById(field_id + '_from');
 99        // don't submit form if user pressed Enter
100        if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
101            from.selectedIndex = 0;
102            SelectBox.move(field_id + '_from', field_id + '_to');
103            from.selectedIndex = 0;
104            return false;
105        }
106        var temp = from.selectedIndex;
107        SelectBox.filter(field_id + '_from', document.getElementById(field_id + '_input').value);
108        from.selectedIndex = temp;
109        return true;
110    },
111    filter_key_down: function(event, field_id) {
112        from = document.getElementById(field_id + '_from');
113        // right arrow -- move across
114        if ((event.which && event.which == 39) || (event.keyCode && event.keyCode == 39)) {
115            var old_index = from.selectedIndex;
116            SelectBox.move(field_id + '_from', field_id + '_to');
117            from.selectedIndex = (old_index == from.length) ? from.length - 1 : old_index;
118            return false;
119        }
120        // down arrow -- wrap around
121        if ((event.which && event.which == 40) || (event.keyCode && event.keyCode == 40)) {
122            from.selectedIndex = (from.length == from.selectedIndex + 1) ? 0 : from.selectedIndex + 1;
123        }
124        // up arrow -- wrap around
125        if ((event.which && event.which == 38) || (event.keyCode && event.keyCode == 38)) {
126            from.selectedIndex = (from.selectedIndex == 0) ? from.length - 1 : from.selectedIndex - 1;
127        }
128        return true;
129    }
130}