1 | <script type="text/javascript"> |
---|
2 | |
---|
3 | function moveItems(from, to) |
---|
4 | { |
---|
5 | // shortcuts for selection fields |
---|
6 | var src = document.getElementById(from); |
---|
7 | var tgt = document.getElementById(to); |
---|
8 | |
---|
9 | if (src.selectedIndex == -1) selectionError(); |
---|
10 | else |
---|
11 | { |
---|
12 | // iterate over all selected items |
---|
13 | // --> attribute "selectedIndex" doesn't support multiple selection. |
---|
14 | // Anyway, it works here, as a moved item isn't selected anymore, |
---|
15 | // thus "selectedIndex" indicating the "next" selected item :) |
---|
16 | while (src.selectedIndex > -1) |
---|
17 | if (src.options[src.selectedIndex].selected) |
---|
18 | { |
---|
19 | // create a new virtal object with values of item to copy |
---|
20 | temp = new Option(src.options[src.selectedIndex].text, |
---|
21 | src.options[src.selectedIndex].value); |
---|
22 | // append virtual object to targe |
---|
23 | tgt.options[tgt.length] = temp; |
---|
24 | // want to select newly created item |
---|
25 | temp.selected = true; |
---|
26 | // delete moved item in source |
---|
27 | src.options[src.selectedIndex] = null; |
---|
28 | } |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | // move item from "from" selection to "to" selection |
---|
33 | function from2to(name) |
---|
34 | { |
---|
35 | moveItems(name+".from", name+".to"); |
---|
36 | copyDataForSubmit(name); |
---|
37 | } |
---|
38 | |
---|
39 | // move item from "to" selection back to "from" selection |
---|
40 | function to2from(name) |
---|
41 | { |
---|
42 | moveItems(name+".to", name+".from"); |
---|
43 | copyDataForSubmit(name); |
---|
44 | } |
---|
45 | |
---|
46 | function swapFields(a, b) |
---|
47 | { |
---|
48 | // swap text |
---|
49 | var temp = a.text; |
---|
50 | a.text = b.text; |
---|
51 | b.text = temp; |
---|
52 | // swap value |
---|
53 | temp = a.value; |
---|
54 | a.value = b.value; |
---|
55 | b.value = temp; |
---|
56 | // swap selection |
---|
57 | temp = a.selected; |
---|
58 | a.selected = b.selected; |
---|
59 | b.selected = temp; |
---|
60 | } |
---|
61 | |
---|
62 | // move selected item in "to" selection one up |
---|
63 | function moveUp(name) |
---|
64 | { |
---|
65 | // shortcuts for selection field |
---|
66 | var toSel = document.getElementById(name+".to"); |
---|
67 | |
---|
68 | if (toSel.selectedIndex == -1) |
---|
69 | selectionError(); |
---|
70 | else if (toSel.options[0].selected) |
---|
71 | alert("Cannot move further up!"); |
---|
72 | else for (var i = 0; toSel.length > i; i++) |
---|
73 | if (toSel.options[i].selected) |
---|
74 | { |
---|
75 | swapFields(toSel.options[i-1], toSel.options[i]); |
---|
76 | copyDataForSubmit(name); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | // move selected item in "to" selection one down |
---|
81 | function moveDown(name) |
---|
82 | { |
---|
83 | // shortcuts for selection field |
---|
84 | var toSel = document.getElementById(name+".to"); |
---|
85 | |
---|
86 | if (toSel.selectedIndex == -1) { |
---|
87 | selectionError(); |
---|
88 | } else if (toSel.options[toSel.length-1].selected) { |
---|
89 | alert("Cannot move further down!"); |
---|
90 | } else { |
---|
91 | for (var i = toSel.length-1; i >= 0; i--) { |
---|
92 | if (toSel.options[i].selected) { |
---|
93 | swapFields(toSel.options[i+1], toSel.options[i]); |
---|
94 | } |
---|
95 | } |
---|
96 | copyDataForSubmit(name); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | // copy each item of "toSel" into one hidden input field |
---|
101 | function copyDataForSubmit(name) |
---|
102 | { |
---|
103 | // shortcuts for selection field and hidden data field |
---|
104 | var toSel = document.getElementById(name+".to"); |
---|
105 | var toDataContainer = document.getElementById(name+".toDataContainer"); |
---|
106 | |
---|
107 | // delete all child nodes (--> complete content) of "toDataContainer" span |
---|
108 | while (toDataContainer.hasChildNodes()) |
---|
109 | toDataContainer.removeChild(toDataContainer.firstChild); |
---|
110 | |
---|
111 | // create new hidden input fields - one for each selection item of |
---|
112 | // "to" selection |
---|
113 | for (var i = 0; toSel.options.length > i; i++) |
---|
114 | { |
---|
115 | // create virtual node with suitable attributes |
---|
116 | var newNode = document.createElement("input"); |
---|
117 | var newAttr = document.createAttribute("name"); |
---|
118 | newAttr.nodeValue = name; |
---|
119 | newNode.setAttributeNode(newAttr); |
---|
120 | |
---|
121 | newAttr = document.createAttribute("value"); |
---|
122 | newAttr.nodeValue = toSel.options[i].value; |
---|
123 | newNode.setAttributeNode(newAttr); |
---|
124 | |
---|
125 | // actually append virtual node to DOM tree |
---|
126 | toDataContainer.appendChild(newNode); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | // error message for missing selection |
---|
131 | function selectionError() |
---|
132 | {alert("Must select something!")} |
---|
133 | |
---|
134 | </script> |
---|
135 | |
---|
136 | <table border="0" class="ordered-selection-field"> |
---|
137 | <tr> |
---|
138 | <td> |
---|
139 | <select id="from" name="from" size="5" multiple="multiple" |
---|
140 | tal:attributes="name string:${view/name}.from; |
---|
141 | id string:${view/name}.from; |
---|
142 | size view/size" |
---|
143 | > |
---|
144 | <option tal:repeat="entry view/choices" |
---|
145 | tal:attributes="value entry/value" |
---|
146 | tal:content="entry/text"/> |
---|
147 | </select> |
---|
148 | </td> |
---|
149 | <td> |
---|
150 | <button class="btn btn-default" name="from2toButton" type="button" value=" ->" |
---|
151 | onclick="javascript:from2to()" |
---|
152 | tal:attributes="onClick string:javascript:from2to('${view/name}')" |
---|
153 | ><img src="/static/img/arrow-right.png" alt="Add" /></button> |
---|
154 | <br /> |
---|
155 | <button class="btn btn-default" name="to2fromButton" type="button" |
---|
156 | onclick="javascript:to2from()" |
---|
157 | tal:attributes="onClick string:javascript:to2from('${view/name}')" |
---|
158 | ><img src="/static/img/arrow-left.png" alt="Remove" /></button> |
---|
159 | </td> |
---|
160 | <td> |
---|
161 | <select id="to" name="to" size="5" multiple="multiple" |
---|
162 | tal:attributes="name string:${view/name}.to; |
---|
163 | id string:${view/name}.to; |
---|
164 | size view/size"> |
---|
165 | <option tal:repeat="entry view/selected" |
---|
166 | tal:attributes="value entry/value" |
---|
167 | tal:content="entry/text"/> |
---|
168 | </select> |
---|
169 | <input name="foo-empty-marker" type="hidden" |
---|
170 | tal:attributes="name string:${view/name}-empty-marker"/> |
---|
171 | <span id="toDataContainer" style="display: none" |
---|
172 | tal:attributes="id string:${view/name}.toDataContainer"> |
---|
173 | <script type="text/javascript" tal:content="string: |
---|
174 | copyDataForSubmit('${view/name}');"> |
---|
175 | // initial copying of field "field.to" --> "field" |
---|
176 | copyDataForSubmit("<i tal:replace="${view/name}"/>"); |
---|
177 | </script> |
---|
178 | </span> |
---|
179 | </td> |
---|
180 | <td> |
---|
181 | <button class="btn btn-default" |
---|
182 | name="upButton" type="button" value="^" |
---|
183 | onclick="javascript:moveUp()" |
---|
184 | tal:attributes="onClick string:javascript:moveUp('${view/name}')" |
---|
185 | ><img src="/static/img/arrow-up.png" alt="Up" /></button> |
---|
186 | <br /> |
---|
187 | <button class="btn btn-default" |
---|
188 | name="downButton" type="button" value="v" |
---|
189 | onclick="javascript:moveDown()" |
---|
190 | tal:attributes="onClick string:javascript:moveDown('${view/name}')" |
---|
191 | ><img src="/static/img/arrow-down.png" alt="Down" /></button> |
---|
192 | </td> |
---|
193 | </tr> |
---|
194 | </table> |
---|