liquid_feedback_frontend
diff static/js/voting.js @ 5:afd9f769c7ae
Version beta1
Final voting with Schulze-Method is now possible
Many bug fixes and code cleanup
Registration with invite codes
More sort and filter options
Seperated display of "supporters" and "potential supporters"
Optical changes
Flood limit / initiative contigent is now checked by frontend
Neccessary changes to access core beta11
Final voting with Schulze-Method is now possible
Many bug fixes and code cleanup
Registration with invite codes
More sort and filter options
Seperated display of "supporters" and "potential supporters"
Optical changes
Flood limit / initiative contigent is now checked by frontend
Neccessary changes to access core beta11
author | bsw/jbe |
---|---|
date | Fri Dec 25 12:00:00 2009 +0100 (2009-12-25) |
parents | |
children | 00d1004545f1 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/static/js/voting.js Fri Dec 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,227 @@ 1.4 +function setCategoryHeadings() { 1.5 + var approvalCount = 0; 1.6 + var disapprovalCount = 0; 1.7 + var sections = document.getElementById("voting").childNodes; 1.8 + for (var i=0; i<sections.length; i++) { 1.9 + var section = sections[i]; 1.10 + if (section.className == "approval") approvalCount++; 1.11 + if (section.className == "disapproval") disapprovalCount++; 1.12 + } 1.13 + var approvalIndex = 0; 1.14 + var disapprovalIndex = 0; 1.15 + for (var i=0; i<sections.length; i++) { 1.16 + var section = sections[i]; 1.17 + if ( 1.18 + section.className == "approval" || 1.19 + section.className == "abstention" || 1.20 + section.className == "disapproval" 1.21 + ) { 1.22 + var setHeading = function(heading) { 1.23 + var headingNodes = section.childNodes; 1.24 + for (var j=0; j<headingNodes.length; j++) { 1.25 + var headingNode = headingNodes[j]; 1.26 + if (headingNode.className == "cathead") { 1.27 + headingNode.textContent = heading; 1.28 + } 1.29 + } 1.30 + } 1.31 + var count = 0; 1.32 + var entries = section.childNodes; 1.33 + for (var j=0; j<entries.length; j++) { 1.34 + var entry = entries[j]; 1.35 + if (entry.className == "movable") count++; 1.36 + } 1.37 + if (section.className == "approval") { 1.38 + if (approvalCount > 1) { 1.39 + if (approvalIndex == 0) { 1.40 + if (count == 1) setHeading("Zustimmung (Erstwunsch)"); 1.41 + else setHeading("Zustimmung (Erstwünsche)"); 1.42 + } else if (approvalIndex == 1) { 1.43 + if (count == 1) setHeading("Zustimmung (Zweitwunsch)"); 1.44 + else setHeading("Zustimmung (Zweitwünsche)"); 1.45 + } else if (approvalIndex == 2) { 1.46 + if (count == 1) setHeading("Zustimmung (Drittwunsch)"); 1.47 + else setHeading("Zustimmung (Drittwünsche)"); 1.48 + } else { 1.49 + if (count == 1) setHeading("Zustimmung (" + (approvalIndex+1) + ".-Wunsch)"); 1.50 + else setHeading("Zustimmung (" + (approvalIndex+1) + ".-Wünsche)"); 1.51 + } 1.52 + } else { 1.53 + setHeading("Zustimmung"); 1.54 + } 1.55 + approvalIndex++; 1.56 + } else if (section.className == "abstention") { 1.57 + setHeading("Enthaltung"); 1.58 + } else if (section.className == "disapproval") { 1.59 + if (disapprovalCount > disapprovalIndex + 2) { 1.60 + setHeading("Ablehnung (jedoch Bevorzugung gegenüber unteren Ablehnungsblöcken)") 1.61 + } else if (disapprovalCount == 2 && disapprovalIndex == 0) { 1.62 + setHeading("Ablehnung (jedoch Bevorzugung gegenüber unterem Ablehnungsblock)") 1.63 + } else if (disapprovalIndex == disapprovalCount - 2) { 1.64 + setHeading("Ablehnung (jedoch Bevorzugung gegenüber letztem Ablehnungsblock)") 1.65 + } else { 1.66 + setHeading("Ablehnung"); 1.67 + } 1.68 + disapprovalIndex++; 1.69 + } 1.70 + } 1.71 + } 1.72 +} 1.73 +function elementDropped(element, dropX, dropY) { 1.74 + var oldParent = element.parentNode; 1.75 + var centerY = dropY + element.clientHeight / 2 1.76 + var approvalCount = 0; 1.77 + var disapprovalCount = 0; 1.78 + var mainDiv = document.getElementById("voting"); 1.79 + var sections = mainDiv.childNodes; 1.80 + for (var i=0; i<sections.length; i++) { 1.81 + var section = sections[i]; 1.82 + if (section.className == "approval") approvalCount++; 1.83 + if (section.className == "disapproval") disapprovalCount++; 1.84 + } 1.85 + for (var i=0; i<sections.length; i++) { 1.86 + var section = sections[i]; 1.87 + if ( 1.88 + section.className == "approval" || 1.89 + section.className == "abstention" || 1.90 + section.className == "disapproval" 1.91 + ) { 1.92 + if ( 1.93 + centerY >= section.offsetTop && 1.94 + centerY < section.offsetTop + section.clientHeight 1.95 + ) { 1.96 + var entries = section.childNodes; 1.97 + for (var j=0; j<entries.length; j++) { 1.98 + var entry = entries[j]; 1.99 + if (entry.className == "movable") { 1.100 + if (centerY < entry.offsetTop + entry.clientHeight / 2) { 1.101 + if (element != entry) { 1.102 + oldParent.removeChild(element); 1.103 + section.insertBefore(element, entry); 1.104 + } 1.105 + break; 1.106 + } 1.107 + } 1.108 + } 1.109 + if (j == entries.length) { 1.110 + oldParent.removeChild(element); 1.111 + section.appendChild(element); 1.112 + } 1.113 + break; 1.114 + } 1.115 + } 1.116 + } 1.117 + if (i == sections.length) { 1.118 + var newSection = document.createElement("div"); 1.119 + var cathead = document.createElement("div"); 1.120 + cathead.setAttribute("class", "cathead"); 1.121 + newSection.appendChild(cathead); 1.122 + for (var i=0; i<sections.length; i++) { 1.123 + var section = sections[i]; 1.124 + if ( 1.125 + section.className == "approval" || 1.126 + section.className == "abstention" || 1.127 + section.className == "disapproval" 1.128 + ) { 1.129 + if (centerY < section.offsetTop + section.clientHeight / 2) { 1.130 + if (section.className == "disapproval") { 1.131 + newSection.setAttribute("class", "disapproval"); 1.132 + disapprovalCount++; 1.133 + } else { 1.134 + newSection.setAttribute("class", "approval"); 1.135 + approvalCount++; 1.136 + } 1.137 + mainDiv.insertBefore(newSection, section); 1.138 + break; 1.139 + } 1.140 + } 1.141 + } 1.142 + if (i == sections.length) { 1.143 + newSection.setAttribute("class", "disapproval"); 1.144 + disapprovalCount++; 1.145 + mainDiv.appendChild(newSection); 1.146 + } 1.147 + oldParent.removeChild(element); 1.148 + newSection.appendChild(element); 1.149 + } 1.150 + sections = mainDiv.childNodes; 1.151 + for (i=0; i<sections.length; i++) { 1.152 + var section = sections[i]; 1.153 + if ( 1.154 + (section.className == "approval" && approvalCount > 1) || 1.155 + (section.className == "disapproval" && disapprovalCount > 1) 1.156 + ) { 1.157 + var entries = section.childNodes; 1.158 + for (var j=0; j<entries.length; j++) { 1.159 + var entry = entries[j]; 1.160 + if (entry.className == "movable") break; 1.161 + } 1.162 + if (j == entries.length) { 1.163 + section.parentNode.removeChild(section); 1.164 + } 1.165 + } 1.166 + } 1.167 + setCategoryHeadings(); 1.168 +} 1.169 +window.addEventListener("load", function(event) { 1.170 + setCategoryHeadings(); 1.171 + var mainDiv = document.getElementById("voting"); 1.172 + var form = document.getElementById("voting_form"); 1.173 + var elements = document.getElementsByTagName("input"); 1.174 + for (var i=0; i<elements.length; i++) { 1.175 + var element = elements[i]; 1.176 + if (element.className == "voting_done") { 1.177 + element.addEventListener("click", function(event) { 1.178 + var scoringString = ""; 1.179 + var approvalCount = 0; 1.180 + var disapprovalCount = 0; 1.181 + var sections = mainDiv.childNodes; 1.182 + for (var j=0; j<sections.length; j++) { 1.183 + var section = sections[j]; 1.184 + if (section.className == "approval") approvalCount++; 1.185 + if (section.className == "disapproval") disapprovalCount++; 1.186 + } 1.187 + var approvalIndex = 0; 1.188 + var disapprovalIndex = 0; 1.189 + for (var j=0; j<sections.length; j++) { 1.190 + var section = sections[j]; 1.191 + if ( 1.192 + section.className == "approval" || 1.193 + section.className == "abstention" || 1.194 + section.className == "disapproval" 1.195 + ) { 1.196 + var score; 1.197 + if (section.className == "approval") { 1.198 + score = approvalCount - approvalIndex; 1.199 + approvalIndex++; 1.200 + } else if (section.className == "abstention") { 1.201 + score = 0; 1.202 + } else if (section.className == "disapproval") { 1.203 + score = -1 - disapprovalIndex; 1.204 + disapprovalIndex++; 1.205 + } 1.206 + var entries = section.childNodes; 1.207 + for (var k=0; k<entries.length; k++) { 1.208 + var entry = entries[k]; 1.209 + if (entry.className == "movable") { 1.210 + var id = entry.id.match(/[0-9]+/); 1.211 + var field = document.createElement("input"); 1.212 + scoringString += id + ":" + score + ";"; 1.213 + } 1.214 + } 1.215 + } 1.216 + } 1.217 + var fields = form.childNodes; 1.218 + for (var j=0; j<fields.length; j++) { 1.219 + var field = fields[j]; 1.220 + if (field.name == "scoring") { 1.221 + field.setAttribute("value", scoringString); 1.222 + form.submit(); 1.223 + return; 1.224 + } 1.225 + } 1.226 + alert('Hidden input field named "scoring" not found.'); 1.227 + }, false); 1.228 + } 1.229 + } 1.230 +}, false);