1 | /**
|
---|
2 | * Overrides admin's SelectBox.js wich can handle optgroups.
|
---|
3 | */
|
---|
4 |
|
---|
5 | var SelectBox = {
|
---|
6 | cache: new Object(),
|
---|
7 | init: function(id) {
|
---|
8 | var cache = [];
|
---|
9 | var nogroup = [];
|
---|
10 | django.jQuery("#" + id + " > option").each(function() {
|
---|
11 | nogroup.push({value: this.value, text: this.text, displayed: 1});
|
---|
12 | });
|
---|
13 | cache.push({'group': null, 'items': nogroup});
|
---|
14 |
|
---|
15 | django.jQuery("#" + id + " optgroup").each(function() {
|
---|
16 | var group = [];
|
---|
17 | django.jQuery("option", this).each(function() {
|
---|
18 | group.push({value: this.value, text: this.text, displayed: 1});
|
---|
19 | })
|
---|
20 | cache.push({'group': django.jQuery(this).attr('label'), 'items': group});
|
---|
21 | });
|
---|
22 | SelectBox.cache[id] = cache;
|
---|
23 | SelectBox.sort(id, false);
|
---|
24 | },
|
---|
25 | redisplay: function(id) {
|
---|
26 | // Repopulate HTML select box from cache
|
---|
27 | var ctr = django.jQuery("#" + id).empty(), ct;
|
---|
28 | var cache = SelectBox.cache[id];
|
---|
29 | for(var i = 0; i < cache.length; i ++) {
|
---|
30 | var gr = cache[i];
|
---|
31 | if(gr.items.length == 0) continue; //skip empty groups
|
---|
32 | if(gr.group == null) ct = ctr;
|
---|
33 | else ct = django.jQuery("<optgroup>").attr('label', gr.group).appendTo(ctr);
|
---|
34 | for(var j = 0; j < gr.items.length; j++) {
|
---|
35 | var itm = gr.items[j];
|
---|
36 | if(itm.displayed) django.jQuery("<option>").attr('value', itm.value).text(itm.text).appendTo(ct);
|
---|
37 | }
|
---|
38 | }
|
---|
39 | },
|
---|
40 | filter: function(id, text) {
|
---|
41 | // Redisplay the HTML select box, displaying only the choices containing ALL
|
---|
42 | // the words in text. (It's an AND search.)
|
---|
43 | var tokens = text.toLowerCase().split(/\s+/);
|
---|
44 | var cache = SelectBox.cache[id];
|
---|
45 | for(var i = 0; i < cache.length; i ++) {
|
---|
46 | var gr = cache[i].items;
|
---|
47 | for(var k = 0; k < gr.length; k++) {
|
---|
48 | gr[k].displayed = 1;
|
---|
49 | for(var j = 0; j < tokens.length; j++) {
|
---|
50 | if(gr[k].text.toLowerCase.indexOf(tokens[j]) < 0) {
|
---|
51 | gr[k].displayed = 0;
|
---|
52 | break;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | SelectBox.redisplay(id);
|
---|
58 | },
|
---|
59 | delete_from_cache: function(id, value) {
|
---|
60 | var cache = SelectBox.cache[id];
|
---|
61 | outer:
|
---|
62 | for(var i = 0; i < cache.length; i ++) {
|
---|
63 | var gr = cache[i].items;
|
---|
64 | for(var j = 0; j < gr.length; j++) {
|
---|
65 | if(gr[j].value == value) {
|
---|
66 | gr.splice(j, 1);
|
---|
67 | break outer;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | },
|
---|
72 | add_to_cache: function(id, group, option) {
|
---|
73 | var cache = SelectBox.cache[id];
|
---|
74 | for(var i = 0; i < cache.length; i++) {
|
---|
75 | if(cache[i].group == group) {
|
---|
76 | cache[i].items.push({value: option.value, text: option.text, displayed: 1});
|
---|
77 | SelectBox.sort(id, group);
|
---|
78 | return;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | //new group
|
---|
82 | cache.push({'group': group, 'items': [{value: option.value, text: option.text, displayed: 1}]});
|
---|
83 | },
|
---|
84 | move: function(from, to) {
|
---|
85 | django.jQuery("#" + from + " option:selected").each(function() {
|
---|
86 | var group = this.parentNode.tagName.toLowerCase() == 'optgroup'? this.parentNode.getAttribute('label'): null;
|
---|
87 | SelectBox.add_to_cache(to, group, {value: this.value, text: this.text, displayed: 1});
|
---|
88 | SelectBox.delete_from_cache(from, this.value);
|
---|
89 | });
|
---|
90 | SelectBox.redisplay(from);
|
---|
91 | SelectBox.redisplay(to);
|
---|
92 | },
|
---|
93 | move_all: function(from, to) {
|
---|
94 | django.jQuery("#" + from + " option").each(function() {
|
---|
95 | var group = this.parentNode.tagName.toLowerCase() == 'optgroup'? this.parentNode.getAttribute('label'): null;
|
---|
96 | SelectBox.add_to_cache(to, group, {value: this.value, text: this.text, displayed: 1});
|
---|
97 | SelectBox.delete_from_cache(from, this.value);
|
---|
98 | });
|
---|
99 | SelectBox.redisplay(from);
|
---|
100 | SelectBox.redisplay(to);
|
---|
101 | },
|
---|
102 | sort: function(id, group) {
|
---|
103 | var cache = SelectBox.cache[id];
|
---|
104 | for(var i = 0; i < cache.length; i++) {
|
---|
105 | if(group === false || cache[i].group == group) {
|
---|
106 | cache[i].items.sort(function(a, b) {
|
---|
107 | a = a.text.toLowerCase();
|
---|
108 | b = b.text.toLowerCase();
|
---|
109 | try {
|
---|
110 | if (a > b) return 1;
|
---|
111 | if (a < b) return -1;
|
---|
112 | } catch (e) {
|
---|
113 | // silently fail on IE 'unknown' exception
|
---|
114 | }
|
---|
115 | return 0;
|
---|
116 | });
|
---|
117 | }
|
---|
118 | }
|
---|
119 | },
|
---|
120 | select_all: function(id) {
|
---|
121 | django.jQuery("#" + id + " option").attr('selected', 'selected');
|
---|
122 | }
|
---|
123 | }
|
---|