diff options
author | User Content <content@content.events.ccc.de> | 2019-10-30 01:11:47 +0000 |
---|---|---|
committer | User Content <content@content.events.ccc.de> | 2019-10-30 01:11:47 +0000 |
commit | e8b020e28ca377868ff8aad8418372debc910ae6 (patch) | |
tree | 53f318f05093e9cbde83e91f2c58035aedf2d7f0 /static/rater.js |
Initial commit
Diffstat (limited to 'static/rater.js')
-rw-r--r-- | static/rater.js | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/static/rater.js b/static/rater.js new file mode 100644 index 0000000..ec1bcfe --- /dev/null +++ b/static/rater.js | |||
@@ -0,0 +1,337 @@ | |||
1 | function changed_name() { | ||
2 | var username = localStorage.getItem("c3-rating-user"); | ||
3 | var newname = document.getElementById('Username').value; | ||
4 | document.getElementById('Username').classList.toggle('changed', username != newname); | ||
5 | } | ||
6 | |||
7 | function changed_filter() { | ||
8 | var filtertext = document.getElementById('Filter').value.toLowerCase(); | ||
9 | if (filtertext == '') { | ||
10 | document.querySelectorAll('.event-list-item.filtered').forEach(function(ev) { | ||
11 | ev.classList.remove("filtered"); | ||
12 | }); | ||
13 | return; | ||
14 | } | ||
15 | |||
16 | if (!window.inner_texts) { | ||
17 | window.inner_texts = {}; | ||
18 | document.querySelectorAll('.event-list-item').forEach(function(ev) { | ||
19 | inner_texts[ev.getAttribute('id')] = ev.innerText.toLowerCase(); | ||
20 | }); | ||
21 | } | ||
22 | |||
23 | Object.keys(window.inner_texts).forEach(function (eid) { | ||
24 | var elem = document.getElementById(eid); | ||
25 | elem.classList.toggle('filtered', window.inner_texts[eid].indexOf(filtertext) < 0); | ||
26 | }); | ||
27 | } | ||
28 | |||
29 | function confirm_name() { | ||
30 | localStorage.setItem("c3-rating-user", document.getElementById('Username').value); | ||
31 | document.getElementById('Username').classList.remove("changed"); | ||
32 | update_status(); | ||
33 | } | ||
34 | |||
35 | function toggleHidden(name) { | ||
36 | document.getElementById(name).classList.toggle("hidden"); | ||
37 | } | ||
38 | |||
39 | function toggleEdit(eid) { | ||
40 | var username = document.getElementById('Username').value; | ||
41 | if (!username) { | ||
42 | alert( "Please set your name before rating."); | ||
43 | return; | ||
44 | } | ||
45 | |||
46 | var ev = document.getElementById('event-'+eid); | ||
47 | if (ev.classList.contains('editing')) { | ||
48 | ev.classList.toggle('editing', false); | ||
49 | return; | ||
50 | } | ||
51 | |||
52 | var other_in_edit = document.getElementsByClassName('editing'); | ||
53 | if (other_in_edit.length) | ||
54 | other_in_edit[0].classList.remove('editing'); | ||
55 | |||
56 | ev.classList.toggle('editing', true); | ||
57 | |||
58 | var own_rating = document.getElementById('event-own-rating'); | ||
59 | ev.appendChild(own_rating); | ||
60 | |||
61 | var myrating = document.querySelectorAll('div#event-rating-'+eid+'[submitter="'+username+'"]'); | ||
62 | var mycomment = ''; | ||
63 | if (myrating.length) { | ||
64 | mycomment = myrating[0].getElementsByClassName('event-rating-comment')[0].innerHTML; | ||
65 | myrating[0].querySelectorAll('meter').forEach(function(meter) { | ||
66 | var category = meter.getAttribute('category'); | ||
67 | document.querySelectorAll('#event-own-rating .slider input[category='+category+']')[0].value = meter.value; | ||
68 | changeVal('event-'+category+'-output', meter.value); | ||
69 | }); | ||
70 | } else { | ||
71 | document.querySelectorAll('#event-own-rating .slider input').forEach(function(sl) { | ||
72 | sl.value = 0; | ||
73 | var category = sl.getAttribute('category'); | ||
74 | changeVal('event-'+category+'-output', '0'); | ||
75 | }); | ||
76 | } | ||
77 | document.getElementById('event-comment').value = mycomment; | ||
78 | own_rating.querySelectorAll('button.remove-rating')[0].classList.toggle('hidden', !myrating.length); | ||
79 | } | ||
80 | |||
81 | function changeVal(el, value) { | ||
82 | document.getElementById(el).innerHTML = value.toString() + " %"; | ||
83 | } | ||
84 | |||
85 | function twocol() { | ||
86 | document.body.classList.toggle('two-column'); | ||
87 | document.body.classList.remove('three-column'); | ||
88 | document.body.classList.remove('four-column'); | ||
89 | } | ||
90 | function threecol() { | ||
91 | document.body.classList.remove('two-column'); | ||
92 | document.body.classList.toggle('three-column'); | ||
93 | document.body.classList.remove('four-column'); | ||
94 | } | ||
95 | function fourcol() { | ||
96 | document.body.classList.remove('two-column'); | ||
97 | document.body.classList.remove('three-column'); | ||
98 | document.body.classList.toggle('four-column'); | ||
99 | } | ||
100 | |||
101 | function invert_sort() { | ||
102 | var evl = document.getElementById('event-list'); | ||
103 | var nodes = Array.prototype.slice.call(evl.getElementsByClassName('event-list-item')).reverse().forEach(function(el) { | ||
104 | evl.appendChild(el); | ||
105 | }); | ||
106 | } | ||
107 | |||
108 | function sort_by(order_function) { | ||
109 | var evl = document.getElementById('event-list'); | ||
110 | Array.prototype.slice.call(evl.getElementsByClassName('event-list-item')).sort(order_function).forEach(function(el) { | ||
111 | evl.appendChild(el); | ||
112 | }); | ||
113 | } | ||
114 | |||
115 | function myrating_count_sort(elem1, elem2) { | ||
116 | var username = document.getElementById('Username').value; | ||
117 | return elem2.querySelectorAll('.event-rating[submitter="'+username+'"]').length - elem1.querySelectorAll('.event-rating[submitter="'+username+'"]').length; | ||
118 | } | ||
119 | |||
120 | function random_sort(elem1, elem2) { return !!Math.floor(Math.random() * 2) ? -1 : 1; } | ||
121 | function rating_count_sort(elem1, elem2) { return elem2.querySelectorAll('.event-rating').length - elem1.querySelectorAll('.event-rating').length; } | ||
122 | function rating_1_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-0')[0].getAttribute('value') - elem1.getElementsByClassName('meter-0')[0].getAttribute('value'); } | ||
123 | function rating_2_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-1')[0].getAttribute('value') - elem1.getElementsByClassName('meter-1')[0].getAttribute('value'); } | ||
124 | function rating_3_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-2')[0].getAttribute('value') - elem1.getElementsByClassName('meter-2')[0].getAttribute('value'); } | ||
125 | function rating_4_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-3')[0].getAttribute('value') - elem1.getElementsByClassName('meter-3')[0].getAttribute('value'); } | ||
126 | function rating_5_sort(elem1, elem2) { return elem2.getElementsByClassName('meter-4')[0].getAttribute('value') - elem1.getElementsByClassName('meter-4')[0].getAttribute('value'); } | ||
127 | function coordinator_sort(elem1, elem2) { return get_coordinator(elem1).localeCompare(get_coordinator(elem2)); } | ||
128 | function state_sort(elem1, elem2) { return elem2.getAttribute('event_state').localeCompare(elem1.getAttribute('event_state')); } | ||
129 | |||
130 | function get_coordinator(elem) { | ||
131 | var coordinator = elem.getElementsByClassName('event-coordinator'); | ||
132 | if (coordinator.length) | ||
133 | return coordinator[0].getAttribute('coordinator'); | ||
134 | return ''; | ||
135 | } | ||
136 | |||
137 | function do_remove_rating() { | ||
138 | if (confirm('are you sure?') == false) | ||
139 | return; | ||
140 | |||
141 | var in_edit = document.getElementsByClassName('editing'); | ||
142 | if (!in_edit.length) | ||
143 | return; | ||
144 | var eid = in_edit[0].getAttribute('id').replace(/^event-/, ''); | ||
145 | |||
146 | var xhttp = new XMLHttpRequest(); | ||
147 | xhttp.open("POST", "api/remove_rating/"+eid, true); | ||
148 | xhttp.setRequestHeader("Content-type", "application/json"); | ||
149 | |||
150 | var username = document.getElementById('Username').value; | ||
151 | |||
152 | xhttp.onreadystatechange = function() { | ||
153 | if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) { | ||
154 | var myrating = document.querySelectorAll('div#event-rating-'+eid+'[submitter="'+username+'"]'); | ||
155 | if(myrating.length) | ||
156 | myrating[0].parentNode.removeChild(myrating[0]); | ||
157 | toggleEdit(eid); | ||
158 | update_status(); | ||
159 | } | ||
160 | } | ||
161 | xhttp.send( JSON.stringify( { 'author': username } ) ); | ||
162 | } | ||
163 | |||
164 | function do_remove_event(eid) { | ||
165 | var ev = document.getElementById('event-'+eid); | ||
166 | var xhttp = new XMLHttpRequest(); | ||
167 | xhttp.open("POST", "api/remove_event/"+eid, true); | ||
168 | xhttp.setRequestHeader("Content-type", "application/json"); | ||
169 | xhttp.onreadystatechange = function() { | ||
170 | if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) { | ||
171 | ev.parentNode.removeChild(ev); | ||
172 | } | ||
173 | } | ||
174 | xhttp.send(); | ||
175 | } | ||
176 | |||
177 | function do_rate() { | ||
178 | var in_edit = document.getElementsByClassName('editing'); | ||
179 | if (!in_edit.length) | ||
180 | return; | ||
181 | var eid = in_edit[0].getAttribute('id').replace(/^event-/, ''); | ||
182 | |||
183 | var username = document.getElementById('Username').value; | ||
184 | if (!username) { | ||
185 | alert( "Please set your name before rating."); | ||
186 | return; | ||
187 | } | ||
188 | |||
189 | var xhttp = new XMLHttpRequest(); | ||
190 | xhttp.open("POST", "api/add_rating/"+eid, true); | ||
191 | xhttp.setRequestHeader("Content-type", "application/json"); | ||
192 | |||
193 | ratings = {}; | ||
194 | document.getElementById('event-own-rating').querySelectorAll('.category-slider input').forEach(function(ev) { | ||
195 | ratings[ev.getAttribute("category")] = ev.value; | ||
196 | }); | ||
197 | var comment = document.getElementById('event-comment').value; | ||
198 | |||
199 | xhttp.onreadystatechange = function() { | ||
200 | if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) { | ||
201 | var myrating = document.querySelectorAll('div#event-rating-'+eid+'[submitter="'+username+'"]'); | ||
202 | if (myrating.length) { | ||
203 | myrating = myrating[0]; | ||
204 | } else { | ||
205 | myrating = document.getElementById('event-rating-new').cloneNode(true); | ||
206 | myrating.setAttribute('id', 'event-rating-'+eid); | ||
207 | myrating.classList.remove('hidden'); | ||
208 | myrating.setAttribute('submitter', username); | ||
209 | myrating.getElementsByClassName('event-rating-submitter')[0].innerHTML = username + ':'; | ||
210 | document.querySelectorAll('#event-'+eid+' .event-ratings')[0].append(myrating); | ||
211 | } | ||
212 | myrating.getElementsByClassName('event-rating-comment')[0].innerHTML = comment; | ||
213 | for (category in ratings) { | ||
214 | myrating.querySelectorAll('meter[category='+category+']')[0].value = ratings[category]; | ||
215 | myrating.querySelectorAll('.event-rating-category-output[category='+category+']')[0].innerHTML = ' ' + categories[category] + ' ' + ratings[category] + ' %'; | ||
216 | } | ||
217 | |||
218 | toggleEdit(eid); | ||
219 | update_status(); | ||
220 | } | ||
221 | } | ||
222 | |||
223 | xhttp.send( JSON.stringify( { | ||
224 | 'author': username, | ||
225 | 'comment': comment, | ||
226 | 'ratings': ratings | ||
227 | } ) ); | ||
228 | } | ||
229 | |||
230 | function do_set_state(eid, state) { | ||
231 | if ( state == document.getElementById('event-'+eid).getAttribute('event_state')) | ||
232 | state = ''; | ||
233 | |||
234 | var xhttp = new XMLHttpRequest(); | ||
235 | xhttp.open("POST", "api/set_event_state/"+eid, true); | ||
236 | xhttp.setRequestHeader("Content-type", "application/json"); | ||
237 | xhttp.onreadystatechange = function() { | ||
238 | if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) { | ||
239 | document.getElementById('event-'+eid).setAttribute('event_state', state); | ||
240 | update_status(); | ||
241 | } | ||
242 | } | ||
243 | xhttp.send( JSON.stringify( { 'state': state } ) ); | ||
244 | } | ||
245 | |||
246 | function do_take(eid) { | ||
247 | var username = document.getElementById('Username').value; | ||
248 | if (!username) { | ||
249 | alert( "Please set your name before taking an event."); | ||
250 | return; | ||
251 | } | ||
252 | |||
253 | var ev = document.getElementById('event-'+eid); | ||
254 | if (ev.classList.contains('i-am-coordinator')) | ||
255 | username = ''; | ||
256 | |||
257 | var xhttp = new XMLHttpRequest(); | ||
258 | xhttp.open("POST", "api/set_event_coordinator/"+eid, true); | ||
259 | xhttp.setRequestHeader("Content-type", "application/json"); | ||
260 | xhttp.onreadystatechange = function() { | ||
261 | if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) { | ||
262 | var coor = ev.getElementsByClassName('event-coordinator'); | ||
263 | if (coor.length) { | ||
264 | if (username) { | ||
265 | coor[0].innerHTML = '<em>coordinator: </em> '+username; | ||
266 | coor[0].setAttribute('coordinator', username); | ||
267 | } else | ||
268 | coor[0].parentNode.removeChild(coor[0]); | ||
269 | } else { | ||
270 | if (username) { | ||
271 | var pers = ev.getElementsByClassName('event-persons'); | ||
272 | coor = document.createElement('div'); | ||
273 | coor.classList.toggle('event-coordinator', true); | ||
274 | coor.setAttribute('coordinator', username); | ||
275 | coor.innerHTML = '<em>coordinator: </em> '+username; | ||
276 | pers[0].insertBefore(coor, pers[0].firstChild); | ||
277 | } | ||
278 | } | ||
279 | update_status(); | ||
280 | } | ||
281 | } | ||
282 | xhttp.send( JSON.stringify( { 'coordinator': username } ) ); | ||
283 | } | ||
284 | |||
285 | function update_status() { | ||
286 | var accepted_count = document.querySelectorAll('.event-list-item[event_state=accepted]').length; | ||
287 | var rejected_count = document.querySelectorAll('.event-list-item[event_state=rejected]').length; | ||
288 | var taken_count = document.querySelectorAll('.event-list-item .event-coordinator').length; | ||
289 | var total_count = document.getElementsByClassName('event-list-item').length; | ||
290 | var total_voted_count = document.querySelectorAll('.event-rating:first-child').length; | ||
291 | var username = document.getElementById('Username').value; | ||
292 | var own_voted_count = 0; | ||
293 | if (username) | ||
294 | own_voted_count = document.querySelectorAll('.event-rating[submitter="'+username+'"]').length; | ||
295 | document.getElementById('status').innerHTML = total_count + ' events. ' + accepted_count + ' accepted. ' + rejected_count + ' rejected. ' + (total_count - own_voted_count) + ' todo. ' + (total_count - total_voted_count) + ' unvoted. ' + (total_count - taken_count) + ' untaken.'; | ||
296 | |||
297 | /* Do the math */ | ||
298 | document.querySelectorAll('.event-list-item').forEach(function(ev) { | ||
299 | if (username) { | ||
300 | ev.classList.toggle('has-own-rating', ev.querySelectorAll('.event-rating[submitter="'+username+'"]').length > 0); | ||
301 | ev.classList.toggle('i-am-coordinator', ev.querySelectorAll('.event-coordinator[coordinator="'+username+'"]').length > 0); | ||
302 | } | ||
303 | |||
304 | var counts = {}; | ||
305 | var meters = ev.querySelectorAll('.event-rating meter'); | ||
306 | |||
307 | if (!meters.length) { | ||
308 | ev.querySelectorAll('.top-meter').forEach(function(meter) { | ||
309 | meter.setAttribute('value', 0); | ||
310 | }); | ||
311 | return; | ||
312 | } | ||
313 | |||
314 | meters.forEach(function(rat) { | ||
315 | var tmp = counts[rat.getAttribute('category')] || 0; | ||
316 | counts[rat.getAttribute('category')] = tmp + parseInt(rat.getAttribute('value')); | ||
317 | }); | ||
318 | var total = 0, i = 0, divisor = meters.length / Object.keys(counts).length; | ||
319 | for (category in counts) { | ||
320 | var dest_meter = ev.getElementsByClassName('meter-'+i)[0]; | ||
321 | dest_meter.setAttribute('value', counts[category] / divisor); | ||
322 | dest_meter.setAttribute('title', category + ': ' + counts[category] / divisor + ' %' ); | ||
323 | total += counts[category] / divisor; | ||
324 | i++; | ||
325 | } | ||
326 | ev.getElementsByClassName('meter-4')[0].setAttribute('value', total / Object.keys(counts).length); | ||
327 | }); | ||
328 | } | ||
329 | |||
330 | document.addEventListener('DOMContentLoaded', function () { | ||
331 | var username = localStorage.getItem("c3-rating-user"); | ||
332 | if (username) | ||
333 | document.getElementById('Username').value = username; | ||
334 | if (window.safari !== undefined) | ||
335 | document.body.classList.add('safari'); | ||
336 | update_status(); | ||
337 | }); | ||