liquid_feedback_frontend

annotate static/js/voting.js @ 330:b91269b2a43f

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

Impressum / About Us