Rev 12784 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
12784 | obado | 1 | |
2 | /** |
||
3 | * chooselist, inspired by qfamsHAndler HTML_QuickForm_advmultiselect |
||
4 | |||
5 | * JavaScript functions to handle a multiselect element (Move elements between 2 select boxes) |
||
6 | * @author Laurent Laville <pear@laurent-laville.org> |
||
7 | * @copyright 2007-2009 Laurent Laville |
||
8 | * @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
||
9 | * @website https://github.com/pear/HTML_QuickForm_advmultiselect |
||
10 | */ |
||
11 | |||
12 | |||
13 | /** |
||
14 | * QFAMS.moveSelection |
||
15 | * in double select box mode, to move current selection and update live counter |
||
16 | * |
||
17 | * @param dom element selectLeft Data source list |
||
18 | * @param dom element selectRight Target data list |
||
19 | * @param dom element selectHidden Full data source (selected, unselected) |
||
20 | * private usage |
||
21 | * @param string action Action name (add, remove, all, none, toggle) |
||
22 | */ |
||
2788 | guerimand | 23 | function moveSelections(selectLeft, selectRight, selectHidden, action) { |
13495 | obado | 24 | var isIE = /*@cc_on!@*/false; //IE detector |
25 | var source = null; |
||
26 | var target = null; |
||
27 | var option; |
||
28 | var c = null; |
||
29 | var s = null; |
||
30 | var i; |
||
31 | var maxFrom, maxTo; |
||
12784 | obado | 32 | |
13495 | obado | 33 | if (action === 'add' || action === 'all' || action === 'toggle') { |
34 | source = selectLeft; |
||
35 | target = selectRight; |
||
36 | } else { |
||
37 | source = selectRight; |
||
38 | target = selectLeft; |
||
39 | } |
||
40 | // Don't do anything if nothing selected. Otherwise we throw javascript errors. |
||
41 | if (source.selectedIndex === -1 && (action === 'add' || action === 'remove')) { |
||
42 | return; |
||
43 | } |
||
44 | maxFrom = source.options.length; |
||
45 | maxTo = target.options.length; |
||
2788 | guerimand | 46 | |
13495 | obado | 47 | // check if target list is empty and remove fake empty option (tip to be XHTML compliant) |
48 | if (maxTo > 0 && target.options[0].value === "") { |
||
49 | target.removeAttribute("disabled"); |
||
50 | target.options[0] = null; |
||
51 | } |
||
52 | |||
53 | // Add items to the 'TO' list. |
||
54 | for (i = (maxFrom - 1); i >= 0; i--) { |
||
55 | if (action === 'all' || action === 'none' || action === 'toggle' || source.options[i].selected === true) { |
||
56 | if (source.options[i].disabled === false) { |
||
57 | if (isIE) { |
||
58 | option = source.options[i].removeNode(true); |
||
59 | //option.selected = env.persistantSelection; |
||
60 | target.appendChild(option); |
||
61 | } else { |
||
62 | option = source.options[i].cloneNode(true); |
||
63 | //option.selected = env.persistantSelection; |
||
64 | target.options[target.options.length] = option; |
||
65 | } |
||
66 | } |
||
12784 | obado | 67 | } |
13495 | obado | 68 | } |
12784 | obado | 69 | |
13495 | obado | 70 | // Remove items from the 'FROM' list. |
71 | if (!isIE) { |
||
12784 | obado | 72 | for (i = (maxFrom - 1); i >= 0; i--) { |
13495 | obado | 73 | if (action === 'all' || action === 'none' || action === 'toggle' || source.options[i].selected === true) { |
74 | if (source.options[i].disabled === false) { |
||
75 | source.options[i] = null; |
||
2788 | guerimand | 76 | } |
13495 | obado | 77 | } |
2788 | guerimand | 78 | } |
13495 | obado | 79 | } |
2788 | guerimand | 80 | |
13495 | obado | 81 | // Add items to the 'FROM' list for toggle function |
82 | if (action === 'toggle') { |
||
83 | for (i = (maxTo - 1); i >= 0; i--) { |
||
84 | if (target.options[i].disabled === false) { |
||
85 | if (isIE) { |
||
86 | option = target.options[i].removeNode(true); |
||
87 | //option.selected = env.persistantSelection; |
||
88 | source.appendChild(option); |
||
89 | } else { |
||
90 | option = target.options[i].cloneNode(true); |
||
91 | //option.selected = env.persistantSelection; |
||
92 | source.options[source.options.length] = option; |
||
2788 | guerimand | 93 | } |
13495 | obado | 94 | } |
2788 | guerimand | 95 | } |
13495 | obado | 96 | if (!isIE) { |
97 | for (i = (maxTo - 1); i >= 0; i--) { |
||
98 | if (target.options[i].disabled === false) { |
||
99 | target.options[i] = null; |
||
12784 | obado | 100 | } |
13495 | obado | 101 | } |
12784 | obado | 102 | } |
13495 | obado | 103 | } |
12784 | obado | 104 | |
13495 | obado | 105 | // Set the appropriate items as 'selected in the hidden select. |
106 | // These are the values that will actually be posted with the form. |
||
107 | updateHidden(selectHidden, selectRight); |
||
2788 | guerimand | 108 | } |
109 | |||
12784 | obado | 110 | /* |
111 | * QFAMS.updateHidden |
||
112 | * updates the private list that handle selection of all elements (selected and unselected) |
||
113 | * @param dom element h hidden list (contains all elements) |
||
114 | * @param dom element r selection list (contains only elements selected) |
||
115 | */ |
||
116 | function updateHidden(h, r) { |
||
13495 | obado | 117 | var i; |
12784 | obado | 118 | |
13495 | obado | 119 | for(i = h.options.length - 1 ; i >= 0 ; i--) |
120 | { |
||
121 | //h.options[i].selected = false; |
||
122 | h.remove(i); |
||
123 | } |
||
2788 | guerimand | 124 | |
13495 | obado | 125 | for (i = 0; i < r.length; i++) { |
126 | h.options[h.length] = new Option(r.options[i].text, r.options[i].value); |
||
127 | h.options[h.length - 1].selected = true; |
||
128 | } |
||
2788 | guerimand | 129 | } |
130 | |||
12784 | obado | 131 | /** |
132 | * QFAMS.moveUp |
||
133 | * end-user may arrange and element up to the selection list |
||
134 | * |
||
135 | * @param dom element l selection list (contains only elements selected) |
||
136 | * @param dom element h hidden list (contains all elements) |
||
137 | * |
||
138 | */ |
||
139 | function moveUp(l, h) { |
||
13495 | obado | 140 | var indice = l.selectedIndex; |
141 | if (indice < 0) { |
||
142 | return; |
||
143 | } |
||
144 | if (indice > 0) { |
||
145 | moveSwap(l, indice, indice - 1); |
||
146 | updateHidden(h, l); |
||
147 | } |
||
2788 | guerimand | 148 | } |
149 | |||
12784 | obado | 150 | /** |
151 | * QFAMS.moveDown |
||
152 | * end-user may arrange and element down to the selection list |
||
153 | * |
||
154 | * @param dom element l selection list (contains only elements selected) |
||
155 | * @param dom element h hidden list (contains all elements) |
||
156 | * |
||
157 | */ |
||
158 | function moveDown(l, h) { |
||
13495 | obado | 159 | var indice = l.selectedIndex; |
160 | if (indice < 0) { |
||
161 | return; |
||
162 | } |
||
163 | if (indice < l.options.length - 1) { |
||
164 | moveSwap(l, indice, indice + 1); |
||
165 | updateHidden(h, l); |
||
166 | } |
||
2788 | guerimand | 167 | } |
168 | |||
12784 | obado | 169 | /** |
170 | * QFAMS.moveSwap |
||
171 | * end-user may invert two elements position in the selection list |
||
172 | * |
||
173 | * @param dom element l selection list (contains only elements selected) |
||
174 | * @param integer i element source indice |
||
175 | * @param integer j element target indice |
||
176 | * |
||
177 | */ |
||
2788 | guerimand | 178 | function moveSwap(l,i,j) { |
13495 | obado | 179 | var valeur = l.options[i].value; |
180 | var texte = l.options[i].text; |
||
181 | l.options[i].value = l.options[j].value; |
||
182 | l.options[i].text = l.options[j].text; |
||
183 | l.options[j].value = valeur; |
||
184 | l.options[j].text = texte; |
||
185 | l.selectedIndex = j |
||
2788 | guerimand | 186 | } |
187 | |||
12784 | obado | 188 | |
189 | /** |
||
190 | * filterSelectExoSheet |
||
191 | * Display only exo corresponding to specific sheet Id in a select box |
||
192 | * The Sheet Id is the value selected by selectFilter (it can be a list like 1,2,3) |
||
193 | * |
||
194 | * @param dom element selectbox selection list |
||
195 | * @param dom element selectFilter WIMS Sheet Id |
||
196 | * |
||
197 | */ |
||
198 | function filterSelectExoSheet(selectTarget, selectFilter) { |
||
13495 | obado | 199 | var i,current; |
200 | // Converts selectFilter to an array |
||
201 | var sheetIds = selectFilter.value.split(','); |
||
12784 | obado | 202 | |
13495 | obado | 203 | for(i = selectTarget.options.length - 1 ; i >= 0 ; i--){ |
204 | current = selectTarget.options[i] |
||
205 | if (sheetIds.indexOf(current.dataset.sheetid) >= 0){ |
||
206 | selectTarget.options[i].style.display = "block"; |
||
12784 | obado | 207 | } |
13495 | obado | 208 | else{ |
209 | selectTarget.options[i].style.display = "none"; |
||
210 | selectTarget.options[i].selected = false; |
||
211 | } |
||
212 | } |
||
12784 | obado | 213 | } |
214 | |||
13495 | obado | 215 |