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 | | } |
| 10 | var cache = []; |
| 11 | var nogroup = []; |
| 12 | django.jQuery("#" + id + " > option").each(function() { |
| 13 | nogroup.push({value: this.value, text: this.text, displayed: 1}); |
| 14 | }); |
| 15 | cache.push({'group': null, 'items': nogroup}); |
| 16 | |
| 17 | django.jQuery("#" + id + " optgroup").each(function() { |
| 18 | var group = []; |
| 19 | django.jQuery("option", this).each(function() { |
| 20 | group.push({value: this.value, text: this.text, displayed: 1}); |
| 21 | }) |
| 22 | cache.push({'group': django.jQuery(this).attr('label'), 'items': group}); |
| 23 | }); |
| 24 | SelectBox.cache[id] = cache; |
| 25 | 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); |
| 29 | var ctr = django.jQuery("#" + id).empty(), ct; |
| 30 | var cache = SelectBox.cache[id]; |
| 31 | for(var i = 0; i < cache.length; i ++) { |
| 32 | var gr = cache[i]; |
| 33 | if(gr.items.length == 0) continue; //skip empty groups |
| 34 | if(gr.group == null) ct = ctr; |
| 35 | else ct = django.jQuery("<optgroup>").attr('label', gr.group).appendTo(ctr); |
| 36 | for(var j = 0; j < gr.items.length; j++) { |
| 37 | var itm = gr.items[j]; |
| 38 | 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}); |
| 74 | add_to_cache: function(id, option, group) { |
| 75 | var cache = SelectBox.cache[id]; |
| 76 | if(!group) group = null; |
| 77 | for(var i = 0; i < cache.length; i++) { |
| 78 | if(cache[i].group == group) { |
| 79 | cache[i].items.push({value: option.value, text: option.text, displayed: 1}); |
| 80 | SelectBox.sort(id, group); |
| 81 | return; |
| 82 | } |
| 83 | } |
| 84 | //new group |
| 85 | cache.push({'group': group, 'items': [{value: option.value, text: option.text, displayed: 1}]}); |
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 | | } |
| 103 | django.jQuery("#" + from + " option:selected").each(function() { |
| 104 | var group = this.parentNode.tagName.toLowerCase() == 'optgroup'? this.parentNode.getAttribute('label'): null; |
| 105 | SelectBox.add_to_cache(to, {value: this.value, text: this.text, displayed: 1}, group); |
| 106 | SelectBox.delete_from_cache(from, this.value); |
| 107 | }); |
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 | | } |
| 112 | django.jQuery("#" + from + " option").each(function() { |
| 113 | var group = this.parentNode.tagName.toLowerCase() == 'optgroup'? this.parentNode.getAttribute('label'): null; |
| 114 | SelectBox.add_to_cache(to, {value: this.value, text: this.text, displayed: 1}, group); |
| 115 | SelectBox.delete_from_cache(from, this.value); |
| 116 | }); |
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 |
| 120 | sort: function(id, group) { |
| 121 | var cache = SelectBox.cache[id]; |
| 122 | if(!group && group !== false) group = null; |
| 123 | for(var i = 0; i < cache.length; i++) { |
| 124 | if(group === false || cache[i].group == group) { |
| 125 | cache[i].items.sort(function(a, b) { |
| 126 | a = a.text.toLowerCase(); |
| 127 | b = b.text.toLowerCase(); |
| 128 | try { |
| 129 | if (a > b) return 1; |
| 130 | if (a < b) return -1; |
| 131 | } catch (e) { |
| 132 | // silently fail on IE 'unknown' exception |
| 133 | } |
| 134 | return 0; |
| 135 | }); |