source: main/waeup.kofa/trunk/src/waeup/kofa/widgets/orderedSelectionList.pt @ 14181

Last change on this file since 14181 was 13032, checked in by Henrik Bettermann, 10 years ago

Register KofaSourceOrderedMultiSelectWidget? for IterableSource? and
KofaSourceOrderedMultiSelectWidget? for IVocabularyTokenized.

File size: 6.3 KB
Line 
1<script type="text/javascript">
2
3function 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
33function from2to(name)
34  {
35  moveItems(name+".from", name+".to");
36  copyDataForSubmit(name);
37  }
38
39// move item from "to" selection back to "from" selection
40function to2from(name)
41  {
42  moveItems(name+".to", name+".from");
43  copyDataForSubmit(name);
44  }
45
46function 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
63function 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
81function 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
101function 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("type");
122    newAttr.nodeValue = "hidden";
123    newNode.setAttributeNode(newAttr);
124
125    newAttr = document.createAttribute("value");
126    newAttr.nodeValue = toSel.options[i].value;
127    newNode.setAttributeNode(newAttr);
128
129    // actually append virtual node to DOM tree
130    toDataContainer.appendChild(newNode);
131    }
132  }
133
134// error message for missing selection
135function selectionError()
136  {alert("Must select something!")}
137
138</script>
139
140<table border="0" class="ordered-selection-field">
141  <tr>
142    <td>
143      <select id="from" name="from" size="5" multiple=""
144          tal:attributes="name string:${view/name}.from;
145                          id string:${view/name}.from;
146                          size view/size"
147                          >
148        <option tal:repeat="entry view/choices"
149                tal:attributes="value entry/value"
150                tal:content="entry/text"/>
151      </select>
152    </td>
153    <td>
154      <button class="btn btn-default" name="from2toButton" type="button" value=" -&gt;"
155          onclick="javascript:from2to()"
156          tal:attributes="onClick string:javascript:from2to('${view/name}')"
157          ><img src="/static/img/arrow-right.png" alt="Add" /></button>
158      <br />
159      <button class="btn btn-default" name="to2fromButton" type="button"
160          onclick="javascript:to2from()"
161          tal:attributes="onClick string:javascript:to2from('${view/name}')"
162          ><img src="/static/img/arrow-left.png" alt="Remove" /></button>
163    </td>
164    <td>
165      <select id="to" name="to" size="5" multiple=""
166          tal:attributes="name string:${view/name}.to;
167                          id string:${view/name}.to;
168                          size view/size">
169        <option tal:repeat="entry view/selected"
170                tal:attributes="value entry/value"
171                tal:content="entry/text"/>
172      </select>
173      <input name="foo-empty-marker" type="hidden"
174        tal:attributes="name string:${view/name}-empty-marker"/>
175      <span id="toDataContainer"
176            tal:attributes="id string:${view/name}.toDataContainer">
177        <script type="text/javascript" tal:content="string:
178          copyDataForSubmit('${view/name}');">
179          // initial copying of field "field.to" --> "field"
180          copyDataForSubmit("<i tal:replace="${view/name}"/>");
181        </script>
182      </span>
183    </td>
184    <td>
185      <button class="btn btn-default"
186          name="upButton" type="button" value="^"
187          onclick="javascript:moveUp()"
188          tal:attributes="onClick string:javascript:moveUp('${view/name}')"
189          ><img src="/static/img/arrow-up.png" alt="Up" /></button>
190      <br />
191      <button class="btn btn-default"
192          name="downButton" type="button" value="v"
193          onclick="javascript:moveDown()"
194          tal:attributes="onClick string:javascript:moveDown('${view/name}')"
195          ><img src="/static/img/arrow-down.png" alt="Down" /></button>
196    </td>
197  </tr>
198</table>
Note: See TracBrowser for help on using the repository browser.