liquid_feedback_frontend

view 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
author bsw/jbe
date Fri Dec 25 12:00:00 2009 +0100 (2009-12-25)
parents
children 00d1004545f1
line source
1 function setCategoryHeadings() {
2 var approvalCount = 0;
3 var disapprovalCount = 0;
4 var sections = document.getElementById("voting").childNodes;
5 for (var i=0; i<sections.length; i++) {
6 var section = sections[i];
7 if (section.className == "approval") approvalCount++;
8 if (section.className == "disapproval") disapprovalCount++;
9 }
10 var approvalIndex = 0;
11 var disapprovalIndex = 0;
12 for (var i=0; i<sections.length; i++) {
13 var section = sections[i];
14 if (
15 section.className == "approval" ||
16 section.className == "abstention" ||
17 section.className == "disapproval"
18 ) {
19 var setHeading = function(heading) {
20 var headingNodes = section.childNodes;
21 for (var j=0; j<headingNodes.length; j++) {
22 var headingNode = headingNodes[j];
23 if (headingNode.className == "cathead") {
24 headingNode.textContent = heading;
25 }
26 }
27 }
28 var count = 0;
29 var entries = section.childNodes;
30 for (var j=0; j<entries.length; j++) {
31 var entry = entries[j];
32 if (entry.className == "movable") count++;
33 }
34 if (section.className == "approval") {
35 if (approvalCount > 1) {
36 if (approvalIndex == 0) {
37 if (count == 1) setHeading("Zustimmung (Erstwunsch)");
38 else setHeading("Zustimmung (Erstwünsche)");
39 } else if (approvalIndex == 1) {
40 if (count == 1) setHeading("Zustimmung (Zweitwunsch)");
41 else setHeading("Zustimmung (Zweitwünsche)");
42 } else if (approvalIndex == 2) {
43 if (count == 1) setHeading("Zustimmung (Drittwunsch)");
44 else setHeading("Zustimmung (Drittwünsche)");
45 } else {
46 if (count == 1) setHeading("Zustimmung (" + (approvalIndex+1) + ".-Wunsch)");
47 else setHeading("Zustimmung (" + (approvalIndex+1) + ".-Wünsche)");
48 }
49 } else {
50 setHeading("Zustimmung");
51 }
52 approvalIndex++;
53 } else if (section.className == "abstention") {
54 setHeading("Enthaltung");
55 } else if (section.className == "disapproval") {
56 if (disapprovalCount > disapprovalIndex + 2) {
57 setHeading("Ablehnung (jedoch Bevorzugung gegenüber unteren Ablehnungsblöcken)")
58 } else if (disapprovalCount == 2 && disapprovalIndex == 0) {
59 setHeading("Ablehnung (jedoch Bevorzugung gegenüber unterem Ablehnungsblock)")
60 } else if (disapprovalIndex == disapprovalCount - 2) {
61 setHeading("Ablehnung (jedoch Bevorzugung gegenüber letztem Ablehnungsblock)")
62 } else {
63 setHeading("Ablehnung");
64 }
65 disapprovalIndex++;
66 }
67 }
68 }
69 }
70 function elementDropped(element, dropX, dropY) {
71 var oldParent = element.parentNode;
72 var centerY = dropY + element.clientHeight / 2
73 var approvalCount = 0;
74 var disapprovalCount = 0;
75 var mainDiv = document.getElementById("voting");
76 var sections = mainDiv.childNodes;
77 for (var i=0; i<sections.length; i++) {
78 var section = sections[i];
79 if (section.className == "approval") approvalCount++;
80 if (section.className == "disapproval") disapprovalCount++;
81 }
82 for (var i=0; i<sections.length; i++) {
83 var section = sections[i];
84 if (
85 section.className == "approval" ||
86 section.className == "abstention" ||
87 section.className == "disapproval"
88 ) {
89 if (
90 centerY >= section.offsetTop &&
91 centerY < section.offsetTop + section.clientHeight
92 ) {
93 var entries = section.childNodes;
94 for (var j=0; j<entries.length; j++) {
95 var entry = entries[j];
96 if (entry.className == "movable") {
97 if (centerY < entry.offsetTop + entry.clientHeight / 2) {
98 if (element != entry) {
99 oldParent.removeChild(element);
100 section.insertBefore(element, entry);
101 }
102 break;
103 }
104 }
105 }
106 if (j == entries.length) {
107 oldParent.removeChild(element);
108 section.appendChild(element);
109 }
110 break;
111 }
112 }
113 }
114 if (i == sections.length) {
115 var newSection = document.createElement("div");
116 var cathead = document.createElement("div");
117 cathead.setAttribute("class", "cathead");
118 newSection.appendChild(cathead);
119 for (var i=0; i<sections.length; i++) {
120 var section = sections[i];
121 if (
122 section.className == "approval" ||
123 section.className == "abstention" ||
124 section.className == "disapproval"
125 ) {
126 if (centerY < section.offsetTop + section.clientHeight / 2) {
127 if (section.className == "disapproval") {
128 newSection.setAttribute("class", "disapproval");
129 disapprovalCount++;
130 } else {
131 newSection.setAttribute("class", "approval");
132 approvalCount++;
133 }
134 mainDiv.insertBefore(newSection, section);
135 break;
136 }
137 }
138 }
139 if (i == sections.length) {
140 newSection.setAttribute("class", "disapproval");
141 disapprovalCount++;
142 mainDiv.appendChild(newSection);
143 }
144 oldParent.removeChild(element);
145 newSection.appendChild(element);
146 }
147 sections = mainDiv.childNodes;
148 for (i=0; i<sections.length; i++) {
149 var section = sections[i];
150 if (
151 (section.className == "approval" && approvalCount > 1) ||
152 (section.className == "disapproval" && disapprovalCount > 1)
153 ) {
154 var entries = section.childNodes;
155 for (var j=0; j<entries.length; j++) {
156 var entry = entries[j];
157 if (entry.className == "movable") break;
158 }
159 if (j == entries.length) {
160 section.parentNode.removeChild(section);
161 }
162 }
163 }
164 setCategoryHeadings();
165 }
166 window.addEventListener("load", function(event) {
167 setCategoryHeadings();
168 var mainDiv = document.getElementById("voting");
169 var form = document.getElementById("voting_form");
170 var elements = document.getElementsByTagName("input");
171 for (var i=0; i<elements.length; i++) {
172 var element = elements[i];
173 if (element.className == "voting_done") {
174 element.addEventListener("click", function(event) {
175 var scoringString = "";
176 var approvalCount = 0;
177 var disapprovalCount = 0;
178 var sections = mainDiv.childNodes;
179 for (var j=0; j<sections.length; j++) {
180 var section = sections[j];
181 if (section.className == "approval") approvalCount++;
182 if (section.className == "disapproval") disapprovalCount++;
183 }
184 var approvalIndex = 0;
185 var disapprovalIndex = 0;
186 for (var j=0; j<sections.length; j++) {
187 var section = sections[j];
188 if (
189 section.className == "approval" ||
190 section.className == "abstention" ||
191 section.className == "disapproval"
192 ) {
193 var score;
194 if (section.className == "approval") {
195 score = approvalCount - approvalIndex;
196 approvalIndex++;
197 } else if (section.className == "abstention") {
198 score = 0;
199 } else if (section.className == "disapproval") {
200 score = -1 - disapprovalIndex;
201 disapprovalIndex++;
202 }
203 var entries = section.childNodes;
204 for (var k=0; k<entries.length; k++) {
205 var entry = entries[k];
206 if (entry.className == "movable") {
207 var id = entry.id.match(/[0-9]+/);
208 var field = document.createElement("input");
209 scoringString += id + ":" + score + ";";
210 }
211 }
212 }
213 }
214 var fields = form.childNodes;
215 for (var j=0; j<fields.length; j++) {
216 var field = fields[j];
217 if (field.name == "scoring") {
218 field.setAttribute("value", scoringString);
219 form.submit();
220 return;
221 }
222 }
223 alert('Hidden input field named "scoring" not found.');
224 }, false);
225 }
226 }
227 }, false);

Impressum / About Us