liquid_feedback_frontend

annotate 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
rev   line source
bsw@519 1 if (!jsFail) {
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 }
bsw@519 101 function voting_move(element, up, dropX, dropY) {
bsw@519 102 if (typeof(element) == "string") element = document.getElementById(element);
bsw@519 103 var mouse = (up == null);
bsw@519 104 var oldParent = element.parentNode;
bsw@519 105 if (mouse) var centerY = dropY + element.clientHeight / 2;
bsw@519 106 var approvalCount = 0;
bsw@519 107 var disapprovalCount = 0;
bsw@519 108 var mainDiv = document.getElementById("voting");
bsw@519 109 var sections = mainDiv.childNodes;
bsw@519 110 for (var i=0; i<sections.length; i++) {
bsw@519 111 var section = sections[i];
bsw@519 112 if (section.className == "approval") approvalCount++;
bsw@519 113 if (section.className == "disapproval") disapprovalCount++;
bsw@519 114 }
bsw@519 115 if (mouse) {
bsw/jbe@19 116 for (var i=0; i<sections.length; i++) {
bsw/jbe@19 117 var section = sections[i];
bsw/jbe@19 118 if (
bsw/jbe@19 119 section.className == "approval" ||
bsw/jbe@19 120 section.className == "abstention" ||
bsw/jbe@19 121 section.className == "disapproval"
bsw/jbe@19 122 ) {
bsw@519 123 if (
bsw@519 124 centerY >= section.offsetTop &&
bsw@519 125 centerY < section.offsetTop + section.clientHeight
bsw@519 126 ) {
bsw@519 127 var entries = section.childNodes;
bsw@519 128 for (var j=0; j<entries.length; j++) {
bsw@519 129 var entry = entries[j];
bsw@519 130 if (entry.className == "movable") {
bsw@519 131 if (centerY < entry.offsetTop + entry.clientHeight / 2) {
bsw@519 132 if (element != entry) {
bsw@519 133 oldParent.removeChild(element);
bsw@519 134 section.insertBefore(element, entry);
bsw@519 135 }
bsw@519 136 break;
bsw@519 137 }
bsw@519 138 }
bsw/jbe@19 139 }
bsw@519 140 if (j == entries.length) {
bsw@519 141 oldParent.removeChild(element);
bsw@519 142 section.appendChild(element);
bsw@519 143 }
bsw/jbe@19 144 break;
bsw/jbe@19 145 }
bsw/jbe@19 146 }
bsw/jbe@19 147 }
bsw/jbe@19 148 if (i == sections.length) {
bsw@519 149 var newSection = document.createElement("div");
bsw@519 150 var cathead = document.createElement("div");
bsw@519 151 cathead.setAttribute("class", "cathead");
bsw@519 152 newSection.appendChild(cathead);
bsw@519 153 for (var i=0; i<sections.length; i++) {
bsw@519 154 var section = sections[i];
bsw@519 155 if (
bsw@519 156 section.className == "approval" ||
bsw@519 157 section.className == "abstention" ||
bsw@519 158 section.className == "disapproval"
bsw@519 159 ) {
bsw@519 160 if (centerY < section.offsetTop + section.clientHeight / 2) {
bsw@519 161 if (section.className == "disapproval") {
bsw@519 162 newSection.setAttribute("class", "disapproval");
bsw@519 163 disapprovalCount++;
bsw@519 164 } else {
bsw@519 165 newSection.setAttribute("class", "approval");
bsw@519 166 approvalCount++;
bsw@519 167 }
bsw@519 168 mainDiv.insertBefore(newSection, section);
bsw@519 169 break;
bsw@519 170 }
bsw@519 171 }
bsw@519 172 }
bsw@519 173 if (i == sections.length) {
bsw@519 174 newSection.setAttribute("class", "disapproval");
bsw@519 175 disapprovalCount++;
bsw@519 176 mainDiv.appendChild(newSection);
bsw@519 177 }
bsw@519 178 oldParent.removeChild(element);
bsw@519 179 newSection.appendChild(element);
bsw/jbe@19 180 }
bsw@519 181 } else {
bsw@519 182 var oldFound = false;
bsw@519 183 var prevSection = null;
bsw@519 184 var nextSection = null;
bsw@519 185 for (var i=0; i<sections.length; i++) {
bsw@519 186 var section = sections[i];
bsw@519 187 if (
bsw@519 188 section.className == "approval" ||
bsw@519 189 section.className == "abstention" ||
bsw@519 190 section.className == "disapproval"
bsw@519 191 ) {
bsw@519 192 if (oldFound) {
bsw@519 193 nextSection = section;
bsw@519 194 break;
bsw@519 195 } else if (section == oldParent) {
bsw@519 196 oldFound = true;
bsw@519 197 } else {
bsw@519 198 prevSection = section;
bsw@519 199 }
bsw/jbe@19 200 }
bsw/jbe@19 201 }
bsw@519 202 var create;
bsw@519 203 if (oldParent.className == "abstention") {
bsw@519 204 create = true;
bsw@519 205 } else {
bsw@519 206 create = false;
bsw@519 207 for (var i=0; i<oldParent.childNodes.length; i++) {
bsw@519 208 var entry = oldParent.childNodes[i];
bsw@519 209 if (entry.className == "movable") {
bsw@519 210 if (entry != element) {
bsw@519 211 create = true;
bsw@519 212 break;
bsw@519 213 }
bsw/jbe@19 214 }
bsw/jbe@19 215 }
bsw/jbe@19 216 }
bsw@519 217 var newSection;
bsw@519 218 if (create) {
bsw@519 219 newSection = document.createElement("div");
bsw@519 220 var cathead = document.createElement("div");
bsw@519 221 cathead.setAttribute("class", "cathead");
bsw@519 222 newSection.appendChild(cathead);
bsw@519 223 if (
bsw@519 224 oldParent.className == "approval" ||
bsw@519 225 (oldParent.className == "abstention" && up)
bsw@519 226 ) {
bsw@519 227 newSection.setAttribute("class", "approval");
bsw@519 228 approvalCount++;
bsw@519 229 } else {
bsw@519 230 newSection.setAttribute("class", "disapproval");
bsw@519 231 disapprovalCount++;
bsw@519 232 }
bsw@519 233 if (up) {
bsw@519 234 mainDiv.insertBefore(newSection, oldParent);
bsw@519 235 } else {
bsw@519 236 if (nextSection) mainDiv.insertBefore(newSection, nextSection);
bsw@519 237 else mainDiv.appendChild(newSection);
bsw@519 238 }
bsw@519 239 } else {
bsw@519 240 if (up) newSection = prevSection;
bsw@519 241 else newSection = nextSection;
bsw@519 242 }
bsw@519 243 if (newSection) {
bsw@519 244 oldParent.removeChild(element);
bsw@519 245 if (create || up) {
bsw@519 246 newSection.appendChild(element);
bsw@519 247 } else {
bsw@519 248 var inserted = false;
bsw@519 249 for (var i=0; i<newSection.childNodes.length; i++) {
bsw@519 250 var entry = newSection.childNodes[i];
bsw@519 251 if (entry.className == "movable") {
bsw@519 252 newSection.insertBefore(element, entry);
bsw@519 253 inserted = true;
bsw@519 254 break;
bsw@519 255 }
bsw@519 256 }
bsw@519 257 if (!inserted) newSection.appendChild(element);
bsw@519 258 }
bsw@519 259 }
bsw/jbe@19 260 }
bsw@519 261 // sections = mainDiv.childNodes;
bsw@519 262 for (i=0; i<sections.length; i++) {
bsw@519 263 var section = sections[i];
bsw/jbe@19 264 if (
bsw@519 265 (section.className == "approval" && approvalCount > 1) ||
bsw@519 266 (section.className == "disapproval" && disapprovalCount > 1)
bsw/jbe@19 267 ) {
bsw@519 268 var entries = section.childNodes;
bsw@519 269 for (var j=0; j<entries.length; j++) {
bsw@519 270 var entry = entries[j];
bsw@519 271 if (entry.className == "movable") break;
bsw/jbe@19 272 }
bsw@519 273 if (j == entries.length) {
bsw@519 274 section.parentNode.removeChild(section);
bsw@519 275 }
bsw/jbe@19 276 }
bsw/jbe@19 277 }
bsw@519 278 voting_setCategoryHeadings();
bsw/jbe@5 279 }
bsw@519 280 function elementDropped(element, dropX, dropY) {
bsw@519 281 voting_move(element, null, dropX, dropY);
bsw/jbe@5 282 }
bsw@519 283 window.addEventListener("load", function(event) {
bsw@519 284 voting_setCategoryHeadings();
bsw@519 285 var mainDiv = document.getElementById("voting");
bsw@519 286 var form = document.getElementById("voting_form");
bsw@519 287 var elements = document.getElementsByTagName("input");
bsw@519 288 for (var i=0; i<elements.length; i++) {
bsw@519 289 var element = elements[i];
bsw@879 290 if (element.className == "voting_done1" ||
bsw@879 291 element.className == "voting_done2" ||
bsw@879 292 element.name == "preview") {
bsw@519 293 element.addEventListener("click", function(event) {
bsw@928 294 if (event.target.name == "preview") {
bsw@879 295 document.getElementById("preview2").value = "1";
bsw@879 296 }
bsw@519 297 var scoringString = "";
bsw@519 298 var approvalCount = 0;
bsw@519 299 var disapprovalCount = 0;
bsw@519 300 var sections = mainDiv.childNodes;
bsw@519 301 for (var j=0; j<sections.length; j++) {
bsw@519 302 var section = sections[j];
bsw@519 303 if (section.className == "approval") approvalCount++;
bsw@519 304 if (section.className == "disapproval") disapprovalCount++;
bsw@519 305 }
bsw@519 306 var approvalIndex = 0;
bsw@519 307 var disapprovalIndex = 0;
bsw@519 308 for (var j=0; j<sections.length; j++) {
bsw@519 309 var section = sections[j];
bsw@519 310 if (
bsw@519 311 section.className == "approval" ||
bsw@519 312 section.className == "abstention" ||
bsw@519 313 section.className == "disapproval"
bsw@519 314 ) {
bsw@519 315 var score;
bsw@519 316 if (section.className == "approval") {
bsw@519 317 score = approvalCount - approvalIndex;
bsw@519 318 approvalIndex++;
bsw@519 319 } else if (section.className == "abstention") {
bsw@519 320 score = 0;
bsw@519 321 } else if (section.className == "disapproval") {
bsw@519 322 score = -1 - disapprovalIndex;
bsw@519 323 disapprovalIndex++;
bsw@519 324 }
bsw@519 325 var entries = section.childNodes;
bsw@519 326 for (var k=0; k<entries.length; k++) {
bsw@519 327 var entry = entries[k];
bsw@519 328 if (entry.className == "movable") {
bsw@519 329 var id = entry.id.match(/[0-9]+/);
bsw@519 330 var field = document.createElement("input");
bsw@519 331 scoringString += id + ":" + score + ";";
bsw@519 332 }
bsw/jbe@5 333 }
bsw/jbe@5 334 }
bsw/jbe@5 335 }
bsw@519 336 var fields = form.childNodes;
bsw@519 337 for (var j=0; j<fields.length; j++) {
bsw@519 338 var field = fields[j];
bsw@519 339 if (field.name == "scoring") {
bsw@519 340 field.setAttribute("value", scoringString);
bsw@519 341 form.submit();
bsw@519 342 return;
bsw@519 343 }
bsw/jbe@5 344 }
bsw@519 345 alert('Hidden input field named "scoring" not found.');
bsw@519 346 }, false);
bsw@519 347 }
bsw/jbe@5 348 }
bsw@519 349 }, false);
bsw@519 350 function voting_moveUp(element) {
bsw@519 351 return voting_move(element, true);
bsw/jbe@5 352 }
bsw@519 353 function voting_moveDown(element) {
bsw@519 354 return voting_move(element, false);
bsw@519 355 }
bsw@519 356
bsw@519 357
bsw@928 358 }

Impressum / About Us