liquid_feedback_frontend

view static/js/voting.js @ 928:ebcc40a3f8b3

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

Impressum / About Us