-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.js
480 lines (436 loc) · 17.4 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/* Copyright (c) 2017 Felix Bolte */
// define json data as available in qual-o-mat-data
// see https://github.com/gockelhahn/qual-o-mat-data
var data_url = 'https://raw.githubusercontent.com/gockelhahn/qual-o-mat-data/master';
// instead, you can set a relative path, for self hosted data dir
//var data_url = 'data';
var json_list = 'list.json';
var json_overview = 'overview.json';
var json_party = 'party.json';
var json_opinion = 'opinion.json';
// static strings
var allparties = 'Alle Parteien';
// save the states of the selection
var selected = '';
var overview_loaded;
var overview;
var party_loaded;
var party;
var opinion_loaded;
var opinion;
var coalition = null;
var statements = 0;
// reset overview vars
function reset_overview() {
overview_loaded = false;
overview = null;
}
// reset party vars
function reset_party() {
party_loaded = false;
party = null;
}
// reset opinion vars
function reset_opinion() {
opinion_loaded = false;
opinion = null;
}
// clear error messages
function reset_error() {
document.getElementById('error_election').innerHTML = '';
}
// clear header
function reset_header() {
document.getElementById('header_election').innerHTML = '';
}
// clear filter
function reset_filter() {
document.getElementById('filter_party').innerHTML = '';
}
// clear result
function reset_result() {
document.getElementById('result_election').innerHTML = '';
}
// reset all
function reset() {
// clear html dom
reset_error();
reset_header();
reset_filter();
reset_result();
// clear states
reset_overview();
reset_party();
reset_opinion();
coalition = null;
}
// escape html special characters: &,<,>,",'
function escapeHtml(text) {
return String(text)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// fetch json files asynchronously and call callback handler with javascript object
function read_json_from_file(json_file, callback) {
var request = new XMLHttpRequest();
request.overrideMimeType("application/json");
request.open('GET', json_file, true);
request.onreadystatechange = function () {
// if request finished
if (request.readyState === 4) {
var response_json = null;
// if http is OK or NOT MODIFIED
// or code is 0 in case it was opened as local file
// (see https://www.w3.org/TR/XMLHttpRequest/#the-status-attribute)
if (request.status === 0 || request.status === 200 || request.status === 304) {
// uncomment the following line to see successful requests
//console.log('SUCCES: ' + json_file);
// try to parse response as json
try {
response_json = JSON.parse(request.response);
} catch (error) {
// set response_json again to null as the "try" will mangle it
response_json = null;
// log error into console
console.log('Response was: ' + request.response);
console.log('Failed to parse json (' + json_file + '): ' + error);
};
} else {
console.log('Failed to load json (' + json_file + ') - HTTP CODE: ' + request.status);
};
// go back with parsed json or null
callback(response_json);
};
};
// set request timeout to 6 seconds
request.timeout = 6000;
request.ontimeout = function () {
// if request did not finish after set timeout
console.log('Failed to to load json (' + json_file + ') due to a timeout of ' + request.timeout + 'ms');
// still go back with null
callback(null);
};
request.send();
}
// calculate and save result for each coalition
function calculate_result() {
coalition = [];
// sort party array by id asc
party.sort(function(a, b) {
return a.id - b.id;
});
// sort opinion by party asc,statement asc
opinion.sort(function(a, b) {
if (a.party != b.party) {
return a.party - b.party;
};
return a.statement - b.statement;
});
// for two party coalitions
for (var x = 0; x < party.length; x++) {
// only calculate one way (x,y || y,x)
// and skip if party compared to itself
for (var y = x + 1; y < party.length; y++) {
var match = 0;
var obj = {};
obj.first = party[x];
obj.second = party[y];
obj.third = null;
for (var i = 0; i < statements; i++) {
// calculate opinion id by party id and statement id
var opinionx = x * statements + i;
var opiniony = y * statements + i;
// add points for matching statements
if (opinion[opinionx].party === x
&& opinion[opiniony].party === y
&& opinion[opinionx].statement === opinion[opiniony].statement
&& opinion[opinionx].answer === opinion[opiniony].answer) {
match++;
};
};
obj.match = match;
coalition.push(obj);
// for three party coalitions
// only calculate one way (y,z || z,y)
// and skip if party compared to itself
for (var z = y + 1; z < party.length; z++) {
match = 0;
var obj2 = {};
obj2.first = party[x];
obj2.second = party[y];
obj2.third = party[z];
for (var i = 0; i < statements; i++) {
// calculate opinion id by party id and statement id
var opinionx = x * statements + i;
var opiniony = y * statements + i;
var opinionz = z * statements + i;
// add points for matching statements
if (opinion[opinionx].party === x
&& opinion[opiniony].party === y
&& opinion[opinionz].party === z
&& opinion[opinionx].statement === opinion[opiniony].statement
&& opinion[opiniony].statement === opinion[opinionz].statement
&& opinion[opinionx].answer === opinion[opiniony].answer
&& opinion[opiniony].answer === opinion[opinionz].answer) {
match++;
};
};
obj2.match = match;
coalition.push(obj2);
};
};
};
}
// show error message
function show_error(msg) {
if (msg === null) {
msg = 'Failed to load or parse needed data. See console for more info. Please try again!';
};
var final_msg = '<pre>ERROR: ' + msg + '</pre>';
document.getElementById('error_election').innerHTML += final_msg;
}
// fill up the dropdown menu with available elections
function show_list(elections) {
var error = '';
// list not loaded correctly, so show error
if (elections === null) {
show_error(null);
} else if (elections.length == 0) {
show_error('No available elections found.');
} else {
// get dropdown menu
var select_election = document.getElementById('select_election');
for (var i = 0; i < elections.length; i++) {
// we do not need to use escapeHtml here, because option.text takes it literally
var election = elections[i];
var new_option = new Option(election);
select_election.appendChild(new_option);
};
// enable dropdown menu and button to load election
document.getElementById('select_election').disabled = false;
document.getElementById('button_load_election').disabled = false;
};
}
// show brief description about selected election
function show_header() {
// overview not loaded correctly, so show error
if (overview === null) {
show_error('Failed to load or parse the election overview. See console for more info.');
} else {
var header = '<h4>' + escapeHtml(overview.title) + ' (<a target="_blank" href="' + escapeHtml(overview.info) + '">info</a>) am ' + escapeHtml(overview.date.slice(0,10)) + ' (<a target="_blank" href="' + escapeHtml(overview.data_source) + '">quelle</a>)</h4>';
document.getElementById('header_election').innerHTML = header;
};
// enable election loading button only when answer and statement finished loading as well
if (party_loaded
&& opinion_loaded) {
document.getElementById('button_load_election').disabled = false;
};
}
// show filter (select box + button) based on party
function show_filter() {
var error = '';
// party not loaded correctly, so show error
if (party === null) {
show_error(null);
} else if (party.length == 0) {
show_error('No available parties found.');
} else {
// get dropdown menu
// create a filter with all parties
var result = '<span>Filter:</span> ';
result += '<select id="select_party" size="1" disabled></select> ';
result += '<button id="button_filter_party" disabled>Los</button><br>';
result += '<input name="coalition_size" id="radio_two" value="2" checked="" type="radio">' +
'<label for="radio_two">2er-Koalitionen</label>';
result += '<input name="coalition_size" id="radio_three" value="3" type="radio">' +
'<label for="radio_three">3er-Koalitionen</label>';
result += '<input name="coalition_size" id="radio_all" value="0" type="radio">' +
'<label for="radio_all">2er- und 3er-Koalitionen</label>';
document.getElementById('filter_party').innerHTML = result;
// add "alle" and all parties to the select
var select_party = document.getElementById('select_party');
select_party.appendChild(new Option(allparties));
for (var i = 0; i < party.length; i++) {
// we do not need to use escapeHtml here, because option.text takes it literally
var party_name = party[i].name;
var new_option = new Option(party_name);
select_party.appendChild(new_option);
};
// enable dropdown menu and add listener to button
document.getElementById('select_party').disabled = false;
document.getElementById('button_filter_party').addEventListener('click', show_result);
};
}
// helper result line
function assemble_result_line(match, one, two, three) {
var value = '<li><strong><em>';
value += escapeHtml(one);
value += '<span class="plus"> + </span>';
value += escapeHtml(two);
// if "third" argument not existing
if (typeof three != 'undefined') {
value += '<span class="plus"> + </span>';
value += escapeHtml(three);
};
value += '</em></strong>: ';
value += '<span class="points">' + match + '</span>';
value += '/' + statements + ' Punkten</li>';
return value;
}
// show coalitions and their results
function show_result() {
// disable filter button until finished
document.getElementById('button_filter_party').disabled = true;
// not all files loaded correctly, so show error
if (party === null
|| opinion === null) {
show_error(null);
} else {
// enable election loading button only when party and opinion finished loading as well
document.getElementById('button_load_election').disabled = false;
// do counting magic
statements = opinion.length/party.length;
calculate_result();
// sort parties by their result (top down)
coalition.sort(function(a, b) {
return b.match - a.match;
});
// read out applied filter
var filter = document.getElementById('select_party').value;
// read out coalition size to show (0 = all)
var selector = 'input[name="coalition_size"]:checked';
var coalition_size = document.querySelector(selector);
// create numbered list and add all coalitions matching the filter
var result = '<ol type="1">';
for (var i = 0; i < coalition.length; i++) {
if (coalition[i].third != null) {
// skip results of coalitions with 3 parties
// when radio box only wants to see 2 party coalitions
if (parseInt(coalition_size.value) === 2) {
continue;
};
if ((filter === allparties)
|| (filter === coalition[i].first.name)) {
result += assemble_result_line(coalition[i].match,
coalition[i].first.name,
coalition[i].second.name,
coalition[i].third.name);
} else if (filter === coalition[i].second.name) {
result += assemble_result_line(coalition[i].match,
coalition[i].second.name,
coalition[i].first.name,
coalition[i].third.name);
} else if (filter === coalition[i].third.name) {
result += assemble_result_line(coalition[i].match,
coalition[i].third.name,
coalition[i].first.name,
coalition[i].second.name);
};
} else {
// skip results of coalitions with 2 parties
// when radio box only wants to see 3 party coalitions
if (parseInt(coalition_size.value) === 3) {
continue;
};
if ((filter === allparties)
|| (filter === coalition[i].first.name)) {
result += assemble_result_line(coalition[i].match,
coalition[i].first.name,
coalition[i].second.name);
} else if (filter === coalition[i].second.name) {
result += assemble_result_line(coalition[i].match,
coalition[i].second.name,
coalition[i].first.name);
};
};
};
result += '</ol>';
document.getElementById('result_election').innerHTML = result;
// enable filter button
document.getElementById('button_filter_party').disabled = false;
};
// enable election loading button only when overview finished loading as well
if (overview_loaded) {
document.getElementById('button_load_election').disabled = false;
};
// go to the top where the result is displayed
window.scrollTo(0, 0);
}
function callback_load_list(object) {
show_list(object);
}
function callback_load_overview(object) {
overview = object;
show_header();
overview_loaded = true;
}
function callback_load_party(object) {
party = object;
show_filter();
if (opinion_loaded) {
show_result();
};
party_loaded = true;
}
function callback_load_opinion(object) {
opinion = object;
if (party_loaded) {
show_result();
};
opinion_loaded = true;
}
function load_election() {
document.getElementById('button_load_election').disabled = true;
// check if selected option was already loaded before
if (document.getElementById('select_election').value !== selected) {
// save selected election
selected = document.getElementById('select_election').value;
// clear page and reset states
reset();
};
// do not load json if already loaded
if (overview !== null) {
show_header();
} else {
reset_error();
reset_header();
reset_overview();
read_json_from_file(data_url + '/' + selected + '/' + json_overview, callback_load_overview);
};
load_result();
}
function load_result() {
// do not load json if already loaded
if (party !== null && opinion !== null) {
show_result();
} else {
reset_result();
coalition = null;
// if both are null, we have to set their $_loaded to false before calling each's read_json_from_file
if (party === null && opinion === null) {
party_loaded = false;
opinion_loaded = false;
};
if (party === null) {
reset_party();
read_json_from_file(data_url + '/' + selected + '/' + json_party, callback_load_party);
};
if (opinion === null) {
reset_opinion();
read_json_from_file(data_url + '/' + selected + '/' + json_opinion, callback_load_opinion);
};
};
}
function init() {
// clear page and reset states
reset();
// add click action to button
document.getElementById('button_load_election').addEventListener('click', load_election);
// load json list of available elections
read_json_from_file(data_url + '/' + json_list, callback_load_list);
}
// call init when page has loaded
window.addEventListener('load', init);