rev |
line source |
jbe@945
|
1 jsProtect(function() {
|
bsw@519
|
2 voting_text_approval_single = "Approval"
|
bsw@519
|
3 voting_text_approval_multi = "Approval"
|
bsw@519
|
4 voting_text_first_preference_single = "Approval (first preference)"
|
bsw@519
|
5 voting_text_first_preference_multi = "Approval (first preference)"
|
bsw@519
|
6 voting_text_second_preference_single = "Approval (second preference)"
|
bsw@519
|
7 voting_text_second_preference_multi = "Approval (second preference)"
|
bsw@519
|
8 voting_text_third_preference_single = "Approval (third preference)"
|
bsw@519
|
9 voting_text_third_preference_multi = "Approval (third preference)"
|
bsw@519
|
10 voting_text_numeric_preference_single = "Approval (#th preference)"
|
bsw@519
|
11 voting_text_numeric_preference_multi = "Approval (#th preference)"
|
bsw@519
|
12 voting_text_abstention_single = "Abstention"
|
bsw@519
|
13 voting_text_abstention_multi = "Abstention"
|
bsw@519
|
14 voting_text_disapproval_above_one_single = "Disapproval (prefer to lower block)"
|
bsw@519
|
15 voting_text_disapproval_above_one_multi = "Disapproval (prefer to lower block)"
|
bsw@519
|
16 voting_text_disapproval_above_many_single = "Disapproval (prefer to lower blocks)"
|
bsw@519
|
17 voting_text_disapproval_above_many_multi = "Disapproval (prefer to lower blocks)"
|
bsw@519
|
18 voting_text_disapproval_above_last_single = "Disapproval (prefer to last block)"
|
bsw@519
|
19 voting_text_disapproval_above_last_multi = "Disapproval (prefer to last block)"
|
bsw@519
|
20 voting_text_disapproval_single = "Disapproval"
|
bsw@519
|
21 voting_text_disapproval_multi = "Disapproval"
|
bsw/jbe@19
|
22
|
bsw@519
|
23 function voting_setCategoryHeadings() {
|
bsw@519
|
24 var approvalCount = 0;
|
bsw@519
|
25 var disapprovalCount = 0;
|
bsw@519
|
26 var sections = document.getElementById("voting").childNodes;
|
bsw@519
|
27 for (var i=0; i<sections.length; i++) {
|
bsw@519
|
28 var section = sections[i];
|
bsw@519
|
29 if (section.className == "approval") approvalCount++;
|
bsw@519
|
30 if (section.className == "disapproval") disapprovalCount++;
|
bsw/jbe@5
|
31 }
|
bsw@519
|
32 var approvalIndex = 0;
|
bsw@519
|
33 var disapprovalIndex = 0;
|
bsw/jbe@5
|
34 for (var i=0; i<sections.length; i++) {
|
bsw/jbe@5
|
35 var section = sections[i];
|
bsw/jbe@5
|
36 if (
|
bsw/jbe@5
|
37 section.className == "approval" ||
|
bsw/jbe@5
|
38 section.className == "abstention" ||
|
bsw/jbe@5
|
39 section.className == "disapproval"
|
bsw/jbe@5
|
40 ) {
|
bsw@519
|
41 var setHeading = function(heading) {
|
bsw@519
|
42 var headingNodes = section.childNodes;
|
bsw@519
|
43 for (var j=0; j<headingNodes.length; j++) {
|
bsw@519
|
44 var headingNode = headingNodes[j];
|
bsw@519
|
45 if (headingNode.className == "cathead") {
|
bsw@519
|
46 headingNode.textContent = heading;
|
bsw/jbe@19
|
47 }
|
bsw/jbe@5
|
48 }
|
bsw@519
|
49 }
|
bsw@519
|
50 var count = 0;
|
bsw@519
|
51 var entries = section.childNodes;
|
bsw@519
|
52 for (var j=0; j<entries.length; j++) {
|
bsw@519
|
53 var entry = entries[j];
|
bsw@519
|
54 if (entry.className == "movable") count++;
|
bsw@519
|
55 }
|
bsw@519
|
56 if (section.className == "approval") {
|
bsw@519
|
57 if (approvalCount > 1) {
|
bsw@519
|
58 if (approvalIndex == 0) {
|
bsw@519
|
59 if (count == 1) setHeading(voting_text_first_preference_single);
|
bsw@519
|
60 else setHeading(voting_text_first_preference_multi);
|
bsw@519
|
61 } else if (approvalIndex == 1) {
|
bsw@519
|
62 if (count == 1) setHeading(voting_text_second_preference_single);
|
bsw@519
|
63 else setHeading(voting_text_second_preference_multi);
|
bsw@519
|
64 } else if (approvalIndex == 2) {
|
bsw@519
|
65 if (count == 1) setHeading(voting_text_third_preference_single);
|
bsw@519
|
66 else setHeading(voting_text_third_preference_multi);
|
bsw@519
|
67 } else {
|
bsw@519
|
68 var text;
|
bsw@519
|
69 if (count == 1) text = voting_text_numeric_preference_single;
|
bsw@519
|
70 else text = voting_text_numeric_preference_multi;
|
bsw@519
|
71 text = text.replace(/#/, "" + (approvalIndex + 1))
|
bsw@519
|
72 setHeading(text);
|
bsw@519
|
73 }
|
bsw@519
|
74 } else {
|
bsw@519
|
75 if (count == 1) setHeading(voting_text_approval_single);
|
bsw@519
|
76 else setHeading(voting_text_approval_multi);
|
bsw/jbe@19
|
77 }
|
bsw@519
|
78 approvalIndex++;
|
bsw@519
|
79 } else if (section.className == "abstention") {
|
bsw@519
|
80 if (count == 1) setHeading(voting_text_abstention_single);
|
bsw@519
|
81 else setHeading(voting_text_abstention_multi);
|
bsw@519
|
82 } else if (section.className == "disapproval") {
|
bsw@519
|
83 if (disapprovalCount > disapprovalIndex + 2) {
|
bsw@519
|
84 if (count == 1) setHeading(voting_text_disapproval_above_many_single);
|
bsw@519
|
85 else setHeading(voting_text_disapproval_above_many_multi);
|
bsw@519
|
86 } else if (disapprovalCount == 2 && disapprovalIndex == 0) {
|
bsw@519
|
87 if (count == 1) setHeading(voting_text_disapproval_above_one_single);
|
bsw@519
|
88 else setHeading(voting_text_disapproval_above_one_multi);
|
bsw@519
|
89 } else if (disapprovalIndex == disapprovalCount - 2) {
|
bsw@519
|
90 if (count == 1) setHeading(voting_text_disapproval_above_last_single);
|
bsw@519
|
91 else setHeading(voting_text_disapproval_above_last_multi);
|
bsw@519
|
92 } else {
|
bsw@519
|
93 if (count == 1) setHeading(voting_text_disapproval_single);
|
bsw@519
|
94 else setHeading(voting_text_disapproval_multi);
|
bsw@519
|
95 }
|
bsw@519
|
96 disapprovalIndex++;
|
bsw/jbe@5
|
97 }
|
bsw/jbe@5
|
98 }
|
bsw/jbe@5
|
99 }
|
bsw@519
|
100 }
|
jbe@946
|
101 function voting_calculateScoring() {
|
jbe@946
|
102 var mainDiv = document.getElementById("voting");
|
jbe@946
|
103 var form = document.getElementById("voting_form");
|
jbe@946
|
104 var scoringString = "";
|
jbe@946
|
105 var approvalCount = 0;
|
jbe@946
|
106 var disapprovalCount = 0;
|
jbe@946
|
107 var sections = mainDiv.childNodes;
|
jbe@946
|
108 for (var j=0; j<sections.length; j++) {
|
jbe@946
|
109 var section = sections[j];
|
jbe@946
|
110 if (section.className == "approval") approvalCount++;
|
jbe@946
|
111 if (section.className == "disapproval") disapprovalCount++;
|
jbe@946
|
112 }
|
jbe@946
|
113 var approvalIndex = 0;
|
jbe@946
|
114 var disapprovalIndex = 0;
|
jbe@946
|
115 for (var j=0; j<sections.length; j++) {
|
jbe@946
|
116 var section = sections[j];
|
jbe@946
|
117 if (
|
jbe@946
|
118 section.className == "approval" ||
|
jbe@946
|
119 section.className == "abstention" ||
|
jbe@946
|
120 section.className == "disapproval"
|
jbe@946
|
121 ) {
|
jbe@946
|
122 var score;
|
jbe@946
|
123 if (section.className == "approval") {
|
jbe@946
|
124 score = approvalCount - approvalIndex;
|
jbe@946
|
125 approvalIndex++;
|
jbe@946
|
126 } else if (section.className == "abstention") {
|
jbe@946
|
127 score = 0;
|
jbe@946
|
128 } else if (section.className == "disapproval") {
|
jbe@946
|
129 score = -1 - disapprovalIndex;
|
jbe@946
|
130 disapprovalIndex++;
|
jbe@946
|
131 }
|
jbe@946
|
132 var entries = section.childNodes;
|
jbe@946
|
133 for (var k=0; k<entries.length; k++) {
|
jbe@946
|
134 var entry = entries[k];
|
jbe@946
|
135 if (entry.className == "movable") {
|
jbe@946
|
136 var id = entry.id.match(/[0-9]+/);
|
jbe@946
|
137 var field = document.createElement("input");
|
jbe@946
|
138 scoringString += id + ":" + score + ";";
|
jbe@946
|
139 }
|
jbe@946
|
140 }
|
jbe@946
|
141 }
|
jbe@946
|
142 }
|
jbe@946
|
143 var fields = form.childNodes;
|
jbe@946
|
144 for (var j=0; j<fields.length; j++) {
|
jbe@946
|
145 var field = fields[j];
|
jbe@946
|
146 if (field.name == "scoring") {
|
jbe@946
|
147 field.setAttribute("value", scoringString);
|
jbe@946
|
148 return;
|
jbe@946
|
149 }
|
jbe@946
|
150 }
|
jbe@946
|
151 throw('Hidden input field named "scoring" not found.');
|
jbe@946
|
152 }
|
bsw@519
|
153 function voting_move(element, up, dropX, dropY) {
|
jbe@945
|
154 jsProtect(function() {
|
jbe@945
|
155 if (typeof(element) == "string") element = document.getElementById(element);
|
jbe@945
|
156 var mouse = (up == null);
|
jbe@945
|
157 var oldParent = element.parentNode;
|
jbe@945
|
158 if (mouse) var centerY = dropY + element.clientHeight / 2;
|
jbe@945
|
159 var approvalCount = 0;
|
jbe@945
|
160 var disapprovalCount = 0;
|
jbe@945
|
161 var mainDiv = document.getElementById("voting");
|
jbe@945
|
162 var sections = mainDiv.childNodes;
|
bsw/jbe@19
|
163 for (var i=0; i<sections.length; i++) {
|
bsw/jbe@19
|
164 var section = sections[i];
|
jbe@945
|
165 if (section.className == "approval") approvalCount++;
|
jbe@945
|
166 if (section.className == "disapproval") disapprovalCount++;
|
jbe@945
|
167 }
|
jbe@945
|
168 if (mouse) {
|
jbe@945
|
169 for (var i=0; i<sections.length; i++) {
|
jbe@945
|
170 var section = sections[i];
|
bsw@519
|
171 if (
|
jbe@945
|
172 section.className == "approval" ||
|
jbe@945
|
173 section.className == "abstention" ||
|
jbe@945
|
174 section.className == "disapproval"
|
bsw@519
|
175 ) {
|
jbe@945
|
176 if (
|
jbe@945
|
177 centerY >= section.offsetTop &&
|
jbe@945
|
178 centerY < section.offsetTop + section.clientHeight
|
jbe@945
|
179 ) {
|
jbe@945
|
180 var entries = section.childNodes;
|
jbe@945
|
181 for (var j=0; j<entries.length; j++) {
|
jbe@945
|
182 var entry = entries[j];
|
jbe@945
|
183 if (entry.className == "movable") {
|
jbe@945
|
184 if (centerY < entry.offsetTop + entry.clientHeight / 2) {
|
jbe@945
|
185 if (element != entry) {
|
jbe@945
|
186 oldParent.removeChild(element);
|
jbe@945
|
187 section.insertBefore(element, entry);
|
jbe@945
|
188 }
|
jbe@945
|
189 break;
|
bsw@519
|
190 }
|
bsw@519
|
191 }
|
bsw@519
|
192 }
|
jbe@945
|
193 if (j == entries.length) {
|
jbe@945
|
194 oldParent.removeChild(element);
|
jbe@945
|
195 section.appendChild(element);
|
jbe@945
|
196 }
|
jbe@945
|
197 break;
|
bsw/jbe@19
|
198 }
|
bsw/jbe@19
|
199 }
|
bsw/jbe@19
|
200 }
|
jbe@945
|
201 if (i == sections.length) {
|
jbe@945
|
202 var newSection = document.createElement("div");
|
jbe@945
|
203 var cathead = document.createElement("div");
|
jbe@945
|
204 cathead.setAttribute("class", "cathead");
|
jbe@945
|
205 newSection.appendChild(cathead);
|
jbe@945
|
206 for (var i=0; i<sections.length; i++) {
|
jbe@945
|
207 var section = sections[i];
|
jbe@945
|
208 if (
|
jbe@945
|
209 section.className == "approval" ||
|
jbe@945
|
210 section.className == "abstention" ||
|
jbe@945
|
211 section.className == "disapproval"
|
jbe@945
|
212 ) {
|
jbe@945
|
213 if (centerY < section.offsetTop + section.clientHeight / 2) {
|
jbe@945
|
214 if (section.className == "disapproval") {
|
jbe@945
|
215 newSection.setAttribute("class", "disapproval");
|
jbe@945
|
216 disapprovalCount++;
|
jbe@945
|
217 } else {
|
jbe@945
|
218 newSection.setAttribute("class", "approval");
|
jbe@945
|
219 approvalCount++;
|
jbe@945
|
220 }
|
jbe@945
|
221 mainDiv.insertBefore(newSection, section);
|
jbe@945
|
222 break;
|
jbe@945
|
223 }
|
jbe@945
|
224 }
|
jbe@945
|
225 }
|
jbe@945
|
226 if (i == sections.length) {
|
jbe@945
|
227 newSection.setAttribute("class", "disapproval");
|
jbe@945
|
228 disapprovalCount++;
|
jbe@945
|
229 mainDiv.appendChild(newSection);
|
jbe@945
|
230 }
|
jbe@945
|
231 oldParent.removeChild(element);
|
jbe@945
|
232 newSection.appendChild(element);
|
jbe@945
|
233 }
|
jbe@945
|
234 } else {
|
jbe@945
|
235 var oldFound = false;
|
jbe@945
|
236 var prevSection = null;
|
jbe@945
|
237 var nextSection = null;
|
bsw@519
|
238 for (var i=0; i<sections.length; i++) {
|
bsw@519
|
239 var section = sections[i];
|
bsw@519
|
240 if (
|
bsw@519
|
241 section.className == "approval" ||
|
bsw@519
|
242 section.className == "abstention" ||
|
bsw@519
|
243 section.className == "disapproval"
|
bsw@519
|
244 ) {
|
jbe@945
|
245 if (oldFound) {
|
jbe@945
|
246 nextSection = section;
|
jbe@945
|
247 break;
|
jbe@945
|
248 } else if (section == oldParent) {
|
jbe@945
|
249 oldFound = true;
|
jbe@945
|
250 } else {
|
jbe@945
|
251 prevSection = section;
|
jbe@945
|
252 }
|
jbe@945
|
253 }
|
jbe@945
|
254 }
|
jbe@945
|
255 var create;
|
jbe@945
|
256 if (oldParent.className == "abstention") {
|
jbe@945
|
257 create = true;
|
jbe@945
|
258 } else {
|
jbe@945
|
259 create = false;
|
jbe@945
|
260 for (var i=0; i<oldParent.childNodes.length; i++) {
|
jbe@945
|
261 var entry = oldParent.childNodes[i];
|
jbe@945
|
262 if (entry.className == "movable") {
|
jbe@945
|
263 if (entry != element) {
|
jbe@945
|
264 create = true;
|
jbe@945
|
265 break;
|
bsw@519
|
266 }
|
bsw@519
|
267 }
|
bsw@519
|
268 }
|
bsw@519
|
269 }
|
jbe@945
|
270 var newSection;
|
jbe@945
|
271 if (create) {
|
jbe@945
|
272 newSection = document.createElement("div");
|
jbe@945
|
273 var cathead = document.createElement("div");
|
jbe@945
|
274 cathead.setAttribute("class", "cathead");
|
jbe@945
|
275 newSection.appendChild(cathead);
|
jbe@945
|
276 if (
|
jbe@945
|
277 oldParent.className == "approval" ||
|
jbe@945
|
278 (oldParent.className == "abstention" && up)
|
jbe@945
|
279 ) {
|
jbe@945
|
280 newSection.setAttribute("class", "approval");
|
jbe@945
|
281 approvalCount++;
|
jbe@945
|
282 } else {
|
jbe@945
|
283 newSection.setAttribute("class", "disapproval");
|
jbe@945
|
284 disapprovalCount++;
|
jbe@945
|
285 }
|
jbe@945
|
286 if (up) {
|
jbe@945
|
287 mainDiv.insertBefore(newSection, oldParent);
|
jbe@945
|
288 } else {
|
jbe@945
|
289 if (nextSection) mainDiv.insertBefore(newSection, nextSection);
|
jbe@945
|
290 else mainDiv.appendChild(newSection);
|
jbe@945
|
291 }
|
jbe@945
|
292 } else {
|
jbe@945
|
293 if (up) newSection = prevSection;
|
jbe@945
|
294 else newSection = nextSection;
|
bsw@519
|
295 }
|
jbe@945
|
296 if (newSection) {
|
jbe@945
|
297 oldParent.removeChild(element);
|
jbe@945
|
298 if (create || up) {
|
jbe@945
|
299 newSection.appendChild(element);
|
bsw@519
|
300 } else {
|
jbe@945
|
301 var inserted = false;
|
jbe@945
|
302 for (var i=0; i<newSection.childNodes.length; i++) {
|
jbe@945
|
303 var entry = newSection.childNodes[i];
|
jbe@945
|
304 if (entry.className == "movable") {
|
jbe@945
|
305 newSection.insertBefore(element, entry);
|
jbe@945
|
306 inserted = true;
|
jbe@945
|
307 break;
|
jbe@945
|
308 }
|
bsw@519
|
309 }
|
jbe@945
|
310 if (!inserted) newSection.appendChild(element);
|
bsw/jbe@19
|
311 }
|
bsw/jbe@19
|
312 }
|
bsw/jbe@19
|
313 }
|
jbe@945
|
314 // sections = mainDiv.childNodes;
|
jbe@945
|
315 for (i=0; i<sections.length; i++) {
|
jbe@945
|
316 var section = sections[i];
|
bsw@519
|
317 if (
|
jbe@945
|
318 (section.className == "approval" && approvalCount > 1) ||
|
jbe@945
|
319 (section.className == "disapproval" && disapprovalCount > 1)
|
bsw@519
|
320 ) {
|
jbe@945
|
321 var entries = section.childNodes;
|
jbe@945
|
322 for (var j=0; j<entries.length; j++) {
|
jbe@945
|
323 var entry = entries[j];
|
jbe@945
|
324 if (entry.className == "movable") break;
|
bsw@519
|
325 }
|
jbe@945
|
326 if (j == entries.length) {
|
jbe@945
|
327 section.parentNode.removeChild(section);
|
jbe@945
|
328 }
|
bsw@519
|
329 }
|
bsw@519
|
330 }
|
jbe@945
|
331 voting_setCategoryHeadings();
|
jbe@946
|
332 voting_calculateScoring();
|
jbe@945
|
333 });
|
bsw/jbe@5
|
334 }
|
jbe@948
|
335 window.elementDropped = function(element, dropX, dropY) {
|
bsw@519
|
336 voting_move(element, null, dropX, dropY);
|
bsw/jbe@5
|
337 }
|
bsw@519
|
338 window.addEventListener("load", function(event) {
|
jbe@945
|
339 jsProtect(function() {
|
jbe@945
|
340 voting_setCategoryHeadings();
|
jbe@949
|
341 voting_calculateScoring(); // checks if script works fine
|
jbe@945
|
342 });
|
bsw@519
|
343 }, false);
|
jbe@947
|
344 window.voting_moveUp = function(element) {
|
bsw@519
|
345 return voting_move(element, true);
|
bsw/jbe@5
|
346 }
|
jbe@947
|
347 window.voting_moveDown = function(element) {
|
bsw@519
|
348 return voting_move(element, false);
|
bsw@519
|
349 }
|
jbe@945
|
350 });
|