build
[semainetech_django.git] / project / media / django / js / SelectBox.js
1 var SelectBox = {
2 cache: new Object(),
3 init: function(id) {
4 var box = document.getElementById(id);
5 var node;
6 SelectBox.cache[id] = new Array();
7 var cache = SelectBox.cache[id];
8 for (var i = 0; (node = box.options[i]); i++) {
9 cache.push({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 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 filter: function(id, text) {
24 // Redisplay the HTML select box, displaying only the choices containing ALL
25 // the words in text. (It's an AND search.)
26 var tokens = text.toLowerCase().split(/\s+/);
27 var node, token;
28 for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
29 node.displayed = 1;
30 for (var j = 0; (token = tokens[j]); j++) {
31 if (node.text.toLowerCase().indexOf(token) == -1) {
32 node.displayed = 0;
33 }
34 }
35 }
36 SelectBox.redisplay(id);
37 },
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--;
51 },
52 add_to_cache: function(id, option) {
53 SelectBox.cache[id].push({value: option.value, text: option.text, displayed: 1});
54 },
55 cache_contains: function(id, value) {
56 // Check if an item is contained in the cache
57 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 move: function(from, to) {
66 var from_box = document.getElementById(from);
67 var to_box = document.getElementById(to);
68 var option;
69 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);
73 }
74 }
75 SelectBox.redisplay(from);
76 SelectBox.redisplay(to);
77 },
78 move_all: function(from, to) {
79 var from_box = document.getElementById(from);
80 var to_box = document.getElementById(to);
81 var option;
82 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 }
87 }
88 SelectBox.redisplay(from);
89 SelectBox.redisplay(to);
90 },
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' exception
101 }
102 return 0;
103 } );
104 },
105 select_all: function(id) {
106 var box = document.getElementById(id);
107 for (var i = 0; i < box.options.length; i++) {
108 box.options[i].selected = 'selected';
109 }
110 }
111 }