Ticket #9102: efficient-filters.diff
File efficient-filters.diff, 6.3 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/media/js/SelectFilter2.js
59 59 var selector_filter = quickElement('p', selector_chosen, gettext('Select your choice(s) and click ')); 60 60 selector_filter.className = 'selector-filter'; 61 61 quickElement('img', selector_filter, '', 'src', admin_media_prefix + (is_stacked ? 'img/admin/selector_stacked-add.gif':'img/admin/selector-add.gif'), 'alt', 'Add'); 62 // clone the box to avoid redisplay issues 62 63 var to_box = quickElement('select', selector_chosen, '', 'id', field_id + '_to', 'multiple', 'multiple', 'size', from_box.size, 'name', from_box.getAttribute('name')); 63 64 to_box.className = 'filtered'; 65 var node; 66 for (var i = 0; (node = from_box.options[i]); i++) { 67 var option = new Option(node.text, node.value, false, false); 68 option.style.display = 'none'; 69 to_box.options[to_box.options.length] = option; 70 } 64 71 var clear_all = quickElement('a', selector_chosen, gettext('Clear all'), 'href', 'javascript: (function() { SelectBox.move_all("' + field_id + '_to", "' + field_id + '_from");})()'); 65 72 clear_all.className = 'selector-clearall'; 66 73 -
django/contrib/admin/media/js/SelectBox.js
3 3 init: function(id) { 4 4 var box = document.getElementById(id); 5 5 var node; 6 SelectBox.cache[id] = new Array();6 SelectBox.cache[id] = new Object(); 7 7 var cache = SelectBox.cache[id]; 8 8 for (var i = 0; (node = box.options[i]); i++) { 9 cache .push({value: node.value, text: node.text, displayed: 1});9 cache[node.value] = node; 10 10 } 11 11 }, 12 redisplay: function(id) {13 // Repopulate HTML select box from cache14 var box = document.getElementById(id);15 box.options.length = 0; // clear all options16 for (var i = 0, j = SelectBox.cache[id].length; i < j; i++) {17 var node = SelectBox.cache[id][i];18 if (node.displayed) {19 box.options[box.options.length] = new Option(node.text, node.value, false, false);20 }21 }22 },23 12 filter: function(id, text) { 24 13 // Redisplay the HTML select box, displaying only the choices containing ALL 25 14 // the words in text. (It's an AND search.) 26 15 var tokens = text.toLowerCase().split(/\s+/); 16 var box = document.getElementById(id); 27 17 var node, token; 28 for (var i = 0; (node = SelectBox.cache[id][i]); i++) {18 for (var i = 0; (node = box[i]); i++) { 29 19 node.displayed = 1; 30 20 for (var j = 0; (token = tokens[j]); j++) { 31 21 if (node.text.toLowerCase().indexOf(token) == -1) { 32 node.displayed = 0; 22 node.style.display = 'none'; 23 } else { 24 node.style.display = 'block'; 33 25 } 34 26 } 35 27 } 36 SelectBox.redisplay(id);37 28 }, 38 delete_from_cache: function(id, value) { 39 var node, delete_index = null; 40 for (var i = 0; (node = SelectBox.cache[id][i]); i++) { 41 if (node.value == value) { 42 delete_index = i; 43 break; 44 } 45 } 46 var j = SelectBox.cache[id].length - 1; 47 for (var i = delete_index; i < j; i++) { 48 SelectBox.cache[id][i] = SelectBox.cache[id][i+1]; 49 } 50 SelectBox.cache[id].length--; 29 hide_option: function(id, value) { 30 SelectBox.cache[id][value].style.display = 'none'; 51 31 }, 52 add_to_cache: function(id, option) {53 SelectBox.cache[id] .push({value: option.value, text: option.text, displayed: 1});32 show_option: function(id, value) { 33 SelectBox.cache[id][value].style.display = 'block'; 54 34 }, 55 cache_contains: function(id, value) {56 // Check if an item is contained in the cache57 var node;58 for (var i = 0; (node = SelectBox.cache[id][i]); i++) {59 if (node.value == value) {60 return true;61 }62 }63 return false;64 },65 35 move: function(from, to) { 66 36 var from_box = document.getElementById(from); 67 37 var to_box = document.getElementById(to); 68 38 var option; 69 39 for (var i = 0; (option = from_box.options[i]); i++) { 70 if (option.selected && SelectBox.cache_contains(from, option.value)) {71 SelectBox. add_to_cache(to, {value: option.value, text: option.text, displayed: 1});72 SelectBox. delete_from_cache(from, option.value);40 if (option.selected) { 41 SelectBox.show_option(to, option.value); 42 SelectBox.hide_option(from, option.value); 73 43 } 74 44 } 75 SelectBox.redisplay(from);76 SelectBox.redisplay(to);77 45 }, 78 46 move_all: function(from, to) { 79 47 var from_box = document.getElementById(from); 80 48 var to_box = document.getElementById(to); 81 49 var option; 82 50 for (var i = 0; (option = from_box.options[i]); i++) { 83 if (SelectBox.cache_contains(from, option.value)) { 84 SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1}); 85 SelectBox.delete_from_cache(from, option.value); 86 } 51 SelectBox.show_option(to, option.value); 52 SelectBox.hide_option(from, option.value); 87 53 } 88 SelectBox.redisplay(from);89 SelectBox.redisplay(to);90 54 }, 91 sort: function(id) {92 SelectBox.cache[id].sort( function(a, b) {93 a = a.text.toLowerCase();94 b = b.text.toLowerCase();95 try {96 if (a > b) return 1;97 if (a < b) return -1;98 }99 catch (e) {100 // silently fail on IE 'unknown' exception101 }102 return 0;103 } );104 },105 55 select_all: function(id) { 106 56 var box = document.getElementById(id); 107 57 for (var i = 0; i < box.options.length; i++) {