annotate media/django/js/SelectBox.js @ 1:0dcfcdf50c62

Initial import of Madeira project from the private repository.
author Brian Neal <bgneal@gmail.com>
date Mon, 06 Apr 2009 03:10:59 +0000
parents
children
rev   line source
bgneal@1 1 var SelectBox = {
bgneal@1 2 cache: new Object(),
bgneal@1 3 init: function(id) {
bgneal@1 4 var box = document.getElementById(id);
bgneal@1 5 var node;
bgneal@1 6 SelectBox.cache[id] = new Array();
bgneal@1 7 var cache = SelectBox.cache[id];
bgneal@1 8 for (var i = 0; (node = box.options[i]); i++) {
bgneal@1 9 cache.push({value: node.value, text: node.text, displayed: 1});
bgneal@1 10 }
bgneal@1 11 },
bgneal@1 12 redisplay: function(id) {
bgneal@1 13 // Repopulate HTML select box from cache
bgneal@1 14 var box = document.getElementById(id);
bgneal@1 15 box.options.length = 0; // clear all options
bgneal@1 16 for (var i = 0, j = SelectBox.cache[id].length; i < j; i++) {
bgneal@1 17 var node = SelectBox.cache[id][i];
bgneal@1 18 if (node.displayed) {
bgneal@1 19 box.options[box.options.length] = new Option(node.text, node.value, false, false);
bgneal@1 20 }
bgneal@1 21 }
bgneal@1 22 },
bgneal@1 23 filter: function(id, text) {
bgneal@1 24 // Redisplay the HTML select box, displaying only the choices containing ALL
bgneal@1 25 // the words in text. (It's an AND search.)
bgneal@1 26 var tokens = text.toLowerCase().split(/\s+/);
bgneal@1 27 var node, token;
bgneal@1 28 for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
bgneal@1 29 node.displayed = 1;
bgneal@1 30 for (var j = 0; (token = tokens[j]); j++) {
bgneal@1 31 if (node.text.toLowerCase().indexOf(token) == -1) {
bgneal@1 32 node.displayed = 0;
bgneal@1 33 }
bgneal@1 34 }
bgneal@1 35 }
bgneal@1 36 SelectBox.redisplay(id);
bgneal@1 37 },
bgneal@1 38 delete_from_cache: function(id, value) {
bgneal@1 39 var node, delete_index = null;
bgneal@1 40 for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
bgneal@1 41 if (node.value == value) {
bgneal@1 42 delete_index = i;
bgneal@1 43 break;
bgneal@1 44 }
bgneal@1 45 }
bgneal@1 46 var j = SelectBox.cache[id].length - 1;
bgneal@1 47 for (var i = delete_index; i < j; i++) {
bgneal@1 48 SelectBox.cache[id][i] = SelectBox.cache[id][i+1];
bgneal@1 49 }
bgneal@1 50 SelectBox.cache[id].length--;
bgneal@1 51 },
bgneal@1 52 add_to_cache: function(id, option) {
bgneal@1 53 SelectBox.cache[id].push({value: option.value, text: option.text, displayed: 1});
bgneal@1 54 },
bgneal@1 55 cache_contains: function(id, value) {
bgneal@1 56 // Check if an item is contained in the cache
bgneal@1 57 var node;
bgneal@1 58 for (var i = 0; (node = SelectBox.cache[id][i]); i++) {
bgneal@1 59 if (node.value == value) {
bgneal@1 60 return true;
bgneal@1 61 }
bgneal@1 62 }
bgneal@1 63 return false;
bgneal@1 64 },
bgneal@1 65 move: function(from, to) {
bgneal@1 66 var from_box = document.getElementById(from);
bgneal@1 67 var to_box = document.getElementById(to);
bgneal@1 68 var option;
bgneal@1 69 for (var i = 0; (option = from_box.options[i]); i++) {
bgneal@1 70 if (option.selected && SelectBox.cache_contains(from, option.value)) {
bgneal@1 71 SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1});
bgneal@1 72 SelectBox.delete_from_cache(from, option.value);
bgneal@1 73 }
bgneal@1 74 }
bgneal@1 75 SelectBox.redisplay(from);
bgneal@1 76 SelectBox.redisplay(to);
bgneal@1 77 },
bgneal@1 78 move_all: function(from, to) {
bgneal@1 79 var from_box = document.getElementById(from);
bgneal@1 80 var to_box = document.getElementById(to);
bgneal@1 81 var option;
bgneal@1 82 for (var i = 0; (option = from_box.options[i]); i++) {
bgneal@1 83 if (SelectBox.cache_contains(from, option.value)) {
bgneal@1 84 SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1});
bgneal@1 85 SelectBox.delete_from_cache(from, option.value);
bgneal@1 86 }
bgneal@1 87 }
bgneal@1 88 SelectBox.redisplay(from);
bgneal@1 89 SelectBox.redisplay(to);
bgneal@1 90 },
bgneal@1 91 sort: function(id) {
bgneal@1 92 SelectBox.cache[id].sort( function(a, b) {
bgneal@1 93 a = a.text.toLowerCase();
bgneal@1 94 b = b.text.toLowerCase();
bgneal@1 95 try {
bgneal@1 96 if (a > b) return 1;
bgneal@1 97 if (a < b) return -1;
bgneal@1 98 }
bgneal@1 99 catch (e) {
bgneal@1 100 // silently fail on IE 'unknown' exception
bgneal@1 101 }
bgneal@1 102 return 0;
bgneal@1 103 } );
bgneal@1 104 },
bgneal@1 105 select_all: function(id) {
bgneal@1 106 var box = document.getElementById(id);
bgneal@1 107 for (var i = 0; i < box.options.length; i++) {
bgneal@1 108 box.options[i].selected = 'selected';
bgneal@1 109 }
bgneal@1 110 }
bgneal@1 111 }