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 | | } |
| 4 | var cache = []; |
| 5 | var nogroup = []; |
| 6 | django.jQuery("#" + id + " > option").each(function() { |
| 7 | nogroup.push({value: this.value, text: this.text, displayed: 1}); |
| 8 | }); |
| 9 | cache.push({'group': null, 'items': nogroup}); |
| 10 | |
| 11 | django.jQuery("#" + id + " optgroup").each(function() { |
| 12 | var group = []; |
| 13 | django.jQuery("option", this).each(function() { |
| 14 | group.push({value: this.value, text: this.text, displayed: 1}); |
| 15 | }) |
| 16 | cache.push({'group': django.jQuery(this).attr('label'), 'items': group}); |
| 17 | }); |
| 18 | SelectBox.cache[id] = cache; |
| 19 | SelectBox.sort(id, false); |
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); |
| 23 | var ctr = django.jQuery("#" + id).empty(), ct; |
| 24 | var cache = SelectBox.cache[id]; |
| 25 | for(var i = 0; i < cache.length; i ++) { |
| 26 | var gr = cache[i]; |
| 27 | if(gr.items.length == 0) continue; //skip empty groups |
| 28 | if(gr.group == null) ct = ctr; |
| 29 | else ct = django.jQuery("<optgroup>").attr('label', gr.group).appendTo(ctr); |
| 30 | for(var j = 0; j < gr.items.length; j++) { |
| 31 | var itm = gr.items[j]; |
| 32 | if(itm.displayed) django.jQuery("<option>").attr('value', itm.value).text(itm.text).appendTo(ct); |
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; |
| 68 | add_to_cache: function(id, group, option) { |
| 69 | var cache = SelectBox.cache[id]; |
| 70 | for(var i = 0; i < cache.length; i++) { |
| 71 | if(cache[i].group == group) { |
| 72 | cache[i].items.push({value: option.value, text: option.text, displayed: 1}); |
| 73 | SelectBox.sort(id, group); |
| 74 | return; |
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 | | } |
| 81 | django.jQuery("#" + from + " option:selected").each(function() { |
| 82 | var group = this.parentNode.tagName.toLowerCase() == 'optgroup'? this.parentNode.getAttribute('label'): null; |
| 83 | SelectBox.add_to_cache(to, group, {value: this.value, text: this.text, displayed: 1}); |
| 84 | SelectBox.delete_from_cache(from, this.value); |
| 85 | }); |
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 | | } |
| 90 | django.jQuery("#" + from + " option").each(function() { |
| 91 | var group = this.parentNode.tagName.toLowerCase() == 'optgroup'? this.parentNode.getAttribute('label'): null; |
| 92 | SelectBox.add_to_cache(to, group, {value: this.value, text: this.text, displayed: 1}); |
| 93 | SelectBox.delete_from_cache(from, this.value); |
| 94 | }); |
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 | sort: function(id, group) { |
| 99 | var cache = SelectBox.cache[id]; |
| 100 | for(var i = 0; i < cache.length; i++) { |
| 101 | if(group === false || cache[i].group == group) { |
| 102 | cache[i].items.sort(function(a, b) { |
| 103 | a = a.text.toLowerCase(); |
| 104 | b = b.text.toLowerCase(); |
| 105 | try { |
| 106 | if (a > b) return 1; |
| 107 | if (a < b) return -1; |
| 108 | } catch (e) { |
| 109 | // silently fail on IE 'unknown' exception |
| 110 | } |
| 111 | return 0; |
| 112 | }); |