Ticket #3099: SelectBox.js

File SelectBox.js, 3.6 KB (added by Graham King <graham@…>, 17 years ago)

Replacement for django/contrib/admin/media/js/SelectBox

Line 
1var SelectBox = {
2 cache: new Object(),
3 init: function(id) {
4 var box = document.getElementById(id);
5 var node;
6 SelectBox.cache[id] = new Object();
7 var boxCache = SelectBox.cache[id];
8 for (var i = 0; (node = box.options[i]); i++) {
9 boxCache[ node.value ] = { value: node.value, text: node.text, displayed: 1 };
10 }
11 },
12 redisplay: function(id) {
13 // Repopulate HTML select box from cache
14 var box = document.getElementById(id);
15 box.options.length = 0; // clear all options
16 var elementsArray = SelectBox.sort(id);
17 for( var i=0; i<elementsArray.length; i++ ) {
18 var node = elementsArray[i]; //SelectBox.cache[id][nodeValue];
19
20 if (node.displayed) {
21 box.options[box.options.length] = new Option(node.text, node.value, false, false);
22 }
23 }
24 },
25 filter: function(id, text) {
26 // Redisplay the HTML select box, displaying only the choices containing ALL
27 // the words in text. (It's an AND search.)
28 var tokens = text.toLowerCase().split(/\s+/);
29 var node, token;
30 for (var nodeValue in SelectBox.cache[id]) {
31 node = SelectBox.cache[id][nodeValue];
32 node.displayed = 1;
33 for (var j = 0; (token = tokens[j]); j++) {
34 if (node.text.toLowerCase().indexOf(token) == -1) {
35 node.displayed = 0;
36 }
37 }
38 }
39 SelectBox.redisplay(id);
40 },
41 delete_from_cache: function(id, value) {
42 delete SelectBox.cache[id][value];
43 },
44 add_to_cache: function(id, option) {
45 SelectBox.cache[id][ option.value ] = { value: option.value, text: option.text, displayed: 1 };
46 },
47 cache_contains: function(id, value) {
48 return SelectBox.cache[id].hasOwnProperty(value);
49 },
50 move: function(from, to) {
51 var from_box = document.getElementById(from);
52 var to_box = document.getElementById(to);
53 var option;
54 for (var i = 0; (option = from_box.options[i]); i++) {
55 if (option.selected && SelectBox.cache_contains(from, option.value)) {
56 SelectBox.add_to_cache(to, { value: option.value, text: option.text, displayed: 1 });
57 SelectBox.delete_from_cache(from, option.value);
58 }
59 }
60 SelectBox.redisplay(from);
61 SelectBox.redisplay(to);
62 },
63 move_all: function(from, to) {
64 var from_box = document.getElementById(from);
65 var to_box = document.getElementById(to);
66 var option;
67 for (var i = 0; (option = from_box.options[i]); i++) {
68 SelectBox.add_to_cache(to, { value: option.value, text: option.text, displayed: 1 });
69 SelectBox.delete_from_cache(from, option.value);
70 }
71 SelectBox.redisplay(from);
72 SelectBox.redisplay(to);
73 },
74 sort: function(id) {
75
76 var cache = SelectBox.cache[id];
77 var array = new Array();
78 for( optionValue in cache ) {
79 var option = cache[optionValue];
80 array.push(option);
81 }
82 array.sort( function(a, b) {
83 a = a.text.toLowerCase();
84 b = b.text.toLowerCase();
85 try {
86 if (a > b) return 1;
87 if (a < b) return -1;
88 }
89 catch (e) {
90 // silently fail on IE 'unknown' exception
91 }
92 return 0;
93 } );
94 return array;
95 },
96 select_all: function(id) {
97 var box = document.getElementById(id);
98 for (var i = 0; i < box.options.length; i++) {
99 box.options[i].selected = 'selected';
100 }
101 }
102}
Back to Top