-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
463 lines (405 loc) · 21.3 KB
/
index.html
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
<!DOCTYPE html>
<html lang="sk">
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-QXR2KE4GRN"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/item-series.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<style>
.chart-container {
width: 100%;
height: 100%;
position: relative;
}
.btn {
display: flex;
align-items: center;
justify-content: center;
}
.material-icons {
vertical-align: middle;
}
</style>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-QXR2KE4GRN');
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:image" content="https://politicalparties.azurewebsites.net/static/politicalPartiesBanner2.jpg" />
<title>Politické strany a Mandáty</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</head>
<body>
<div class="container">
<h4 class="center-align">Politické strany a Mandáty</h4>
<h6 class="center-align">Výpočet možných kombinácií potrebných na prekročenie 75 mandátov - vládnej väčšiny</h6>
<div class="chart-container">
<div id="myChart"></canvas>
</div>
</div>
<div class="row valign-wrapper">
<div class="input-field col s6">
<select id="source" onchange="fetchParties()">
{{range .}}
<option value="{{.}}">{{.}}</option>
{{end}}
</select>
<label>Prieskumy</label>
</div>
<div class="input-field col s6">
<button class="btn waves-effect waves-light" type="submit" form="party-form"
style="width: 100%;">Odoslať
<i class="material-icons right">send</i>
</button>
</div>
</div>
<div class="row valign-wrapper" style="background-color: #e0e0e0; border-radius: 5px; padding: 10px 0;">
<div class="col s5" style="display: flex; align-items: center; padding-left: 20px;">
Prípadne zadefinuj vzájomné vylúčenia
</div>
<div class="col s1" style="display: flex; align-items: center; justify-content: center;">
<i class="material-icons">arrow_forward</i>
</div>
<div class="col s6">
<button class="btn waves-effect waves-light" type="button" onclick="exclusionsForm(event)"
style="width: 100%;">Vzájomné Vylúčenia</button>
</div>
</div>
<form id="party-form">
<div class="row">
<div class="input-field col s6">
<input type="text" id="party1" required>
<label for="party1">Strana 1</label>
</div>
<div class="input-field col s5">
<input type="number" id="seats1" min="0" required>
<label for="seats1">Mandáty strany 1</label>
</div>
<div class="input-field col s1">
<input type="color" id="color1" value="#1e88e5">
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input type="text" id="party2" required>
<label for="party2">Strana 2</label>
</div>
<div class="input-field col s5">
<input type="number" id="seats2" min="0" required>
<label for="seats2">Mandáty strany 2</label>
</div>
<div class="input-field col s1">
<input type="color" id="color2" value="#5e35b1">
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input type="text" id="party3" required>
<label for="party3">Strana 3</label>
</div>
<div class="input-field col s5">
<input type="number" id="seats3" min="0" required>
<label for="seats3">Mandáty strany 3</label>
</div>
<div class="input-field col s1">
<input type="color" id="color3" value="#f4511e">
</div>
</div>
<div id="extra-fields"></div>
<div class="row">
<div class="input-field col s6">
<button class="btn waves-effect waves-light" type="button" id="add-another-party"
onclick="addPartyField()" style="width: 80%;">Pridaj stranu</button>
</div>
<div class="input-field col s2">
</div>
</div>
</form>
<!-- Footer -->
<footer class="page-footer" style="background-color: #575757; color: white; padding: 1px 0;">
<div class="footer-copyright">
<div class="container">
© 2023 All Rights Reserved
<span class="grey-text text-lighten-4 right">Contact <a
href="https://www.facebook.com/martin.huba.921" target="_blank"><i
class="fab fa-facebook-f"></i> Facebook</a></span>
</div>
</div>
</footer>
<script>
let fieldCounter = 3;
let myChart = null;
// Initialize totalSeats variable to keep track of the total number of seats for activated parties
let totalSeats = 150;
// Define seatsLabel outside the fetchParties function
let seatsLabel;
async function fetchParties() {
try {
const selectedValue = document.getElementById('source').value;
const agency = selectedValue.split(" - ")[0]; // Extract only the agency name
// put into local storage info which agency was fetched
localStorage.setItem('selectedOption', agency);
const response = await fetch(`/fetch?source=${agency}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
const parties = result.parties;
const date = result.date;
localStorage.setItem('agencyDate', date);
// Reset totalSeats to 150
totalSeats = 150;
const chartData = parties.map(party => {
return {
name: party.Name,
y: party.Seats,
color: party.Color,
label: `${party.Name} (${party.Seats})`,
isActive: true, // Initialize isActive to true
originalColor: party.Color // Store the original color
};
});
// Remove all existing rows
const extraFields = document.getElementById("extra-fields");
extraFields.innerHTML = '';
// Reset fieldCounter
fieldCounter = 3;
parties.forEach((party, index) => {
if (index < 3) {
document.getElementById(`party${index + 1}`).value = party.Name;
document.getElementById(`seats${index + 1}`).value = party.Seats;
document.getElementById(`color${index + 1}`).value = party.Color; // Add this line
document.querySelector(`label[for="party${index + 1}"]`).classList.add('active');
document.querySelector(`label[for="seats${index + 1}"]`).classList.add('active');
} else {
addPartyField();
document.getElementById(`party${index + 1}`).value = party.Name;
document.getElementById(`seats${index + 1}`).value = party.Seats;
document.getElementById(`color${index + 1}`).value = party.Color; // Add this line
document.querySelector(`label[for="party${index + 1}"]`).classList.add('active');
document.querySelector(`label[for="seats${index + 1}"]`).classList.add('active');
}
// since it's fetched, hide "add another party button"
document.getElementById("add-another-party").style.display = "none";
});
if (myChart) {
const series = myChart.series[0];
// If new data has more points, add the extra points
while (series.data.length < chartData.length) {
series.addPoint({ y: 0 }, false); // Add a dummy point with value 0, without redrawing
}
// If new data has fewer points, remove the extra points
while (series.data.length > chartData.length) {
series.data[series.data.length - 1].remove(false); // Remove the last point, without redrawing
}
// Update the data for the existing points
for (let i = 0; i < chartData.length; i++) {
series.data[i].update(chartData[i], false); // Update the point, without redrawing
}
myChart.setTitle({ text: `${agency} - ${date}` });
// Update the seatsLabel text
if (seatsLabel) {
seatsLabel.attr({
text: `Spolu: ${totalSeats}`
});
}
myChart.redraw(); // Redraw the chart once after all updates
} else {
// Create the chart if it doesn't exist
myChart = Highcharts.chart('myChart', {
chart: {
type: 'item'
},
title: {
text: `${agency} - ${date}`
},
series: [{
name: 'Seats',
keys: ['name', 'y', 'color', 'label'],
data: chartData,
dataLabels: {
enabled: true,
format: '{point.label}'
},
center: ['50%', '88%'],
size: '150%',
startAngle: -100,
endAngle: 100,
point: {
events: {
legendItemClick: function (event) {
if (this.isActive) {
totalSeats -= this.y;
this.update({ color: '#cccccc' }, false); // Update color to grey
} else {
totalSeats += this.y;
this.update({ color: this.options.originalColor }, false); // Update color to original
}
this.isActive = !this.isActive;
seatsLabel.attr({
text: `Spolu: ${totalSeats}`
});
myChart.redraw(); // Redraw the chart to apply color changes
event.preventDefault(); // Prevent the default action of hiding the series
},
click: function (event) {
if (this.isActive) {
totalSeats -= this.y;
this.update({ color: '#cccccc' }, false); // Update color to grey
} else {
totalSeats += this.y;
this.update({ color: this.options.originalColor }, false); // Update color to original
}
this.isActive = !this.isActive;
seatsLabel.attr({
text: `Spolu: ${totalSeats}`
});
myChart.redraw(); // Redraw the chart to apply color changes
event.preventDefault(); // Prevent the default action of hiding the series
}
}
}
}],
legend: {
itemStyle: {
color: '#000000',
fontSize: '16px', // Increase font size
//fontWeight: 'bold' // Make it bold if you like
},
itemHiddenStyle: {
color: '#cccccc'
},
itemEvents: {
click: function (event) {
const point = this.series.chart.get(this.name);
if (point.isActive) {
totalSeats -= point.y;
point.update({ color: '#cccccc' }, false); // Update color to grey
} else {
totalSeats += point.y;
point.update({ color: point.options.color }, false); // Update color to original
}
point.isActive = !point.isActive;
// Update the total number of seats for activated parties
document.getElementById('total-seats-count').innerText = totalSeats;
myChart.redraw(); // Redraw the chart to apply color changes
// Prevent the default action of hiding the series
return false;
}
}
}
});
// Create the seatsLabel if it doesn't exist
if (!seatsLabel) {
seatsLabel = myChart.renderer.text(`Spolu: ${totalSeats}`, 40, 40)
.css({
fontSize: '20px'
})
.add();
}
}
} catch (error) {
console.error("Fetch error:", error);
}
}
function getRandomColor() {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function populateFormAndSubmit(parties) {
const partiesInput = document.getElementById("parties");
partiesInput.value = JSON.stringify(parties);
const form = document.getElementById("main-form");
form.submit();
}
function submitForm(event) {
event.preventDefault();
let parties = [];
for (let i = 1; i <= fieldCounter; i++) {
let party = document.getElementById(`party${i}`).value;
let seats = parseInt(document.getElementById(`seats${i}`).value, 10);
let color = document.getElementById(`color${i}`).value;
parties.push({ name: party, seats: seats, color: color });
}
fetch('/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `parties=${encodeURIComponent(JSON.stringify(parties))}`,
})
.then((response) => response.json())
.then((data) => {
localStorage.setItem('chartData', JSON.stringify(data));
window.location.href = '/results';
})
.catch((error) => console.error(error));
}
function exclusionsForm(event) {
event.preventDefault();
let partyNames = [];
for (let i = 1; i <= fieldCounter; i++) {
let party = document.getElementById(`party${i}`).value;
partyNames.push(party);
}
let partiesData = [];
for (let i = 1; i <= fieldCounter; i++) {
let party = document.getElementById(`party${i}`).value;
let seats = parseInt(document.getElementById(`seats${i}`).value, 10);
let color = document.getElementById(`color${i}`).value;
partiesData.push({ name: party, seats: seats, color: color });
}
localStorage.setItem('partyNames', JSON.stringify(partyNames));
localStorage.setItem('partiesData', JSON.stringify(partiesData));
window.location.href = '/exclusions';
}
function addPartyField() {
fieldCounter++;
let extraFields = document.getElementById("extra-fields");
let rowDiv = document.createElement("div");
rowDiv.className = "row";
let partyDiv = document.createElement("div");
partyDiv.className = "input-field col s6";
partyDiv.innerHTML = `
<input type="text" id="party${fieldCounter}" required>
<label for="party${fieldCounter}">Strana ${fieldCounter}</label>`;
rowDiv.appendChild(partyDiv);
let seatsDiv = document.createElement("div");
seatsDiv.className = "input-field col s5";
seatsDiv.innerHTML = `
<input type="number" id="seats${fieldCounter}" min="0" required>
<label for="seats${fieldCounter}">Mandáty strany ${fieldCounter}</label>`;
rowDiv.appendChild(seatsDiv);
let colorDiv = document.createElement("div");
colorDiv.className = "input-field col s1";
colorDiv.innerHTML = `
<input type="color" id="color${fieldCounter}" value="${getRandomColor()}">`;
rowDiv.appendChild(colorDiv);
extraFields.appendChild(rowDiv);
}
document.getElementById("party-form").addEventListener("submit", submitForm);
document.addEventListener('DOMContentLoaded', function () {
var elems = document.querySelectorAll('select');
var instances = M.FormSelect.init(elems);
// Automatically select the newest poll and fetch data for it
const dropdown = document.getElementById("source");
dropdown.selectedIndex = 0; // select the first option
fetchParties();
});
</script>
</body>
</html>