6 var box = document.getElementById(id);
8 SelectBox.cache[id] = [];
9 var cache = SelectBox.cache[id];
10 var boxOptions = box.options;
11 var boxOptionsLength = boxOptions.length;
12 for (var i = 0, j = boxOptionsLength; i < j; i++) {
14 cache.push({value: node.value, text: node.text, displayed: 1});
17 redisplay: function(id) {
18 // Repopulate HTML select box from cache
19 var box = document.getElementById(id);
21 $(box).empty(); // clear all options
22 var new_options = box.outerHTML.slice(0, -9); // grab just the opening tag
23 var cache = SelectBox.cache[id];
24 for (var i = 0, j = cache.length; i < j; i++) {
27 var new_option = new Option(node.text, node.value, false, false);
28 // Shows a tooltip when hovering over the option
29 new_option.setAttribute("title", node.text);
30 new_options += new_option.outerHTML;
33 new_options += '</select>';
34 box.outerHTML = new_options;
36 filter: function(id, text) {
37 // Redisplay the HTML select box, displaying only the choices containing ALL
38 // the words in text. (It's an AND search.)
39 var tokens = text.toLowerCase().split(/\s+/);
41 var cache = SelectBox.cache[id];
42 for (var i = 0, j = cache.length; i < j; i++) {
45 var node_text = node.text.toLowerCase();
46 var numTokens = tokens.length;
47 for (var k = 0; k < numTokens; k++) {
49 if (node_text.indexOf(token) === -1) {
51 break; // Once the first token isn't found we're done
55 SelectBox.redisplay(id);
57 delete_from_cache: function(id, value) {
58 var node, delete_index = null;
59 var cache = SelectBox.cache[id];
60 for (var i = 0, j = cache.length; i < j; i++) {
62 if (node.value === value) {
67 cache.splice(delete_index, 1);
69 add_to_cache: function(id, option) {
70 SelectBox.cache[id].push({value: option.value, text: option.text, displayed: 1});
72 cache_contains: function(id, value) {
73 // Check if an item is contained in the cache
75 var cache = SelectBox.cache[id];
76 for (var i = 0, j = cache.length; i < j; i++) {
78 if (node.value === value) {
84 move: function(from, to) {
85 var from_box = document.getElementById(from);
87 var boxOptions = from_box.options;
88 var boxOptionsLength = boxOptions.length;
89 for (var i = 0, j = boxOptionsLength; i < j; i++) {
90 option = boxOptions[i];
91 var option_value = option.value;
92 if (option.selected && SelectBox.cache_contains(from, option_value)) {
93 SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
94 SelectBox.delete_from_cache(from, option_value);
97 SelectBox.redisplay(from);
98 SelectBox.redisplay(to);
100 move_all: function(from, to) {
101 var from_box = document.getElementById(from);
103 var boxOptions = from_box.options;
104 var boxOptionsLength = boxOptions.length;
105 for (var i = 0, j = boxOptionsLength; i < j; i++) {
106 option = boxOptions[i];
107 var option_value = option.value;
108 if (SelectBox.cache_contains(from, option_value)) {
109 SelectBox.add_to_cache(to, {value: option_value, text: option.text, displayed: 1});
110 SelectBox.delete_from_cache(from, option_value);
113 SelectBox.redisplay(from);
114 SelectBox.redisplay(to);
117 SelectBox.cache[id].sort(function(a, b) {
118 a = a.text.toLowerCase();
119 b = b.text.toLowerCase();
129 // silently fail on IE 'unknown' exception
134 select_all: function(id) {
135 var box = document.getElementById(id);
136 var boxOptions = box.options;
137 var boxOptionsLength = boxOptions.length;
138 for (var i = 0; i < boxOptionsLength; i++) {
139 boxOptions[i].selected = 'selected';
143 window.SelectBox = SelectBox;