-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.js
More file actions
1536 lines (1368 loc) · 57.5 KB
/
Copy pathstats.js
File metadata and controls
1536 lines (1368 loc) · 57.5 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let allData = [];
let filteredData = [];
let debatesData = [];
let filteredDebatesData = [];
let sessionsData = [];
let debateTagsMapping = {}; // Mapping business_number -> tags pour les débats
let partyChartInstance = null;
let typeChartInstance = null;
let yearChartInstance = null;
let debatePartyChartInstance = null;
let debateCouncilChartInstance = null;
function downloadChart(canvasId, filename) {
const canvas = document.getElementById(canvasId);
if (!canvas) return;
const link = document.createElement('a');
link.download = `${filename}.png`;
link.href = canvas.toDataURL('image/png');
link.click();
}
const partyColors = {
'UDC': '#009F4D',
'PSS': '#E53935',
'PS': '#E53935',
'PLR': '#0066CC',
'Le Centre': '#FF9800',
'Centre': '#FF9800',
'M-E': '#FF9800',
'PDC': '#FF9800',
'PBD': '#FF9800',
'CSPO': '#FF9800',
'CVP': '#FF9800',
'BDP': '#FF9800',
'VERT-E-S': '#8BC34A',
'Les Vert-e-s': '#8BC34A',
'Al': '#8BC34A',
'Vert\'libéraux': '#CDDC39',
'pvl': '#CDDC39',
'PVL': '#CDDC39',
'Commissions': '#9E9E9E'
};
const partyLabels = {
'UDC': 'UDC',
'PSS': 'PS',
'PS': 'PS',
'PLR': 'PLR',
'Le Centre': 'Le Centre',
'Centre': 'Le Centre',
'M-E': 'Le Centre',
'PDC': 'Le Centre',
'PBD': 'Le Centre',
'CSPO': 'Le Centre',
'CVP': 'Le Centre',
'BDP': 'Le Centre',
'VERT-E-S': 'VERT-E-S',
'Les Vert-e-s': 'VERT-E-S',
'Al': 'VERT-E-S',
'pvl': 'Vert\'libéraux',
'PVL': 'Vert\'libéraux',
'Commissions': 'Commissions'
};
const typeLabels = {
'Mo.': 'Motion',
'Po.': 'Postulat',
'Ip.': 'Interpellation',
'Fra.': 'Heure des questions',
'A.': 'Question',
'Pa. Iv.': 'Initiative parl.',
'D.Ip.': 'Interpellation urgente',
'BRG': 'Objet du CF'
};
function translateDept(deptDE) {
const translations = {
'EFD': 'DFF',
'EDI': 'DFI',
'UVEK': 'DETEC',
'VBS': 'DDPS',
'EJPD': 'DFJP',
'EDA': 'DFAE',
'WBF': 'DEFR',
'BK': 'ChF',
'BGer': 'TF',
'Parl': 'Parl',
'VBV': 'AF',
'AB-BA': 'AS-MPC'
};
return translations[deptDE] || deptDE;
}
const typeToFilter = {
'Motion': 'Mo.',
'Postulat': 'Po.',
'Interpellation': 'Ip.',
'Heure des questions': 'Fra.',
'Question': 'A.',
'Initiative parl.': 'Pa. Iv.',
'Interpellation urgente': 'D.Ip.',
'Objet du CF': 'BRG'
};
const partyToFilter = {
'PS': 'PS',
'UDC': 'UDC',
'PLR': 'PLR',
'Le Centre': 'Le Centre',
'Verts': 'VERT-E-S',
'Vert\'libéraux': 'pvl',
'Commissions': 'Commissions'
};
async function init() {
try {
// Charger les dates des sessions
const sessionsResponse = await fetch('sessions.json');
const sessionsJson = await sessionsResponse.json();
sessionsData = sessionsJson.sessions || [];
// Charger les données des objets parlementaires et tags manquants
const [response, missingTagsResponse] = await Promise.all([
fetch('cdf_efk_data.json'),
fetch('missing_objects_tags.json').catch(() => ({ json: () => ({ items: [] }) }))
]);
const data = await response.json();
const missingTagsJson = await missingTagsResponse.json();
allData = data.items || [];
filteredData = [...allData];
// Créer le mapping des tags pour les débats
allData.forEach(item => {
if (item.shortId && item.tags) {
debateTagsMapping[item.shortId] = item.tags;
}
});
if (missingTagsJson.items) {
missingTagsJson.items.forEach(item => {
if (item.business_number && item.tags && !debateTagsMapping[item.business_number]) {
debateTagsMapping[item.business_number] = item.tags;
}
});
}
populateObjectFilters();
setupObjectFilterListeners();
renderAllObjectCharts();
// Charger les données des débats
const debatesResponse = await fetch('debates_data.json');
const debatesJson = await debatesResponse.json();
debatesData = debatesJson.items || [];
// Trier du plus récent au plus vieux
debatesData.sort((a, b) => (b.date || '').localeCompare(a.date || ''));
filteredDebatesData = [...debatesData];
populateDebateFilters();
setupDebateFilterListeners();
renderAllDebateCharts();
} catch (error) {
console.error('Error loading data:', error);
}
}
function getCheckedValues(dropdownId) {
const dropdown = document.getElementById(dropdownId);
const checkboxes = dropdown.querySelectorAll('input[type="checkbox"]:not([data-select-all])');
const selectAll = dropdown.querySelector('[data-select-all]');
if (selectAll && selectAll.checked) return [];
return Array.from(checkboxes).filter(cb => cb.checked).map(cb => cb.value);
}
function setupDropdown(dropdownId) {
const dropdown = document.getElementById(dropdownId);
const btn = dropdown.querySelector('.filter-btn');
const menu = dropdown.querySelector('.filter-menu');
const selectAll = dropdown.querySelector('[data-select-all]');
const checkboxes = dropdown.querySelectorAll('input[type="checkbox"]:not([data-select-all])');
const countSpan = dropdown.querySelector('.filter-count');
btn.addEventListener('click', (e) => {
e.stopPropagation();
document.querySelectorAll('.filter-dropdown.open').forEach(d => {
if (d !== dropdown) d.classList.remove('open');
});
dropdown.classList.toggle('open');
});
function updateCount() {
const checkedBoxes = Array.from(checkboxes).filter(cb => cb.checked);
if (selectAll && selectAll.checked) {
countSpan.textContent = '';
} else if (checkedBoxes.length > 0) {
const selectedLabels = checkedBoxes.map(cb => {
const label = cb.parentElement.textContent.trim();
return label;
});
if (selectedLabels.length === 1) {
countSpan.textContent = `: ${selectedLabels[0]}`;
} else if (selectedLabels.length <= 2) {
countSpan.textContent = `: ${selectedLabels.join(', ')}`;
} else {
countSpan.textContent = `: ${selectedLabels[0]} +${selectedLabels.length - 1}`;
}
} else {
countSpan.textContent = '';
}
}
if (selectAll) {
selectAll.addEventListener('change', () => {
checkboxes.forEach(cb => cb.checked = false);
updateCount();
});
}
checkboxes.forEach(cb => {
cb.addEventListener('change', () => {
if (cb.checked && selectAll) selectAll.checked = false;
if (!Array.from(checkboxes).some(c => c.checked) && selectAll) selectAll.checked = true;
updateCount();
});
});
updateCount();
}
function populateObjectFilters() {
// Populer filtre années
const yearMenu = document.getElementById('objectYearMenu');
const years = [...new Set(allData.map(d => d.date ? d.date.substring(0, 4) : null).filter(Boolean))];
if (!years.includes('2026')) years.push('2026');
years.sort().reverse();
years.forEach(year => {
const label = document.createElement('label');
label.innerHTML = `<input type="checkbox" value="${year}"> ${year}`;
yearMenu.appendChild(label);
});
// Populer filtre partis
const partyMenu = document.getElementById('objectPartyMenu');
const parties = [...new Set(allData.map(d => {
const party = d.party || getPartyFromAuthor(d.author);
return normalizeParty(party);
}).filter(Boolean))];
parties.sort((a, b) => a.localeCompare(b, 'fr'));
parties.forEach(party => {
const label = document.createElement('label');
label.innerHTML = `<input type="checkbox" value="${party}"> ${party}`;
partyMenu.appendChild(label);
});
// Populer filtre départements
const deptMenu = document.getElementById('objectDeptMenu');
if (deptMenu) {
const departments = [...new Set(allData.map(d => d.department).filter(Boolean))];
departments.sort((a, b) => translateDept(a).localeCompare(translateDept(b), 'fr'));
departments.forEach(dept => {
const label = document.createElement('label');
const deptFR = translateDept(dept);
label.innerHTML = `<input type="checkbox" value="${dept}"> ${deptFR}`;
deptMenu.appendChild(label);
});
}
// Populer filtre thématiques
const tagsMenu = document.getElementById('objectTagsMenu');
if (tagsMenu) {
const allTags = new Set();
allData.forEach(item => {
if (item.tags) {
item.tags.split('|').forEach(tag => {
if (tag.trim()) allTags.add(tag.trim());
});
}
});
const tagsArray = [...allTags].sort((a, b) => a.localeCompare(b, 'fr'));
tagsArray.forEach(tag => {
const label = document.createElement('label');
label.innerHTML = `<input type="checkbox" value="${tag}"> ${tag}`;
tagsMenu.appendChild(label);
});
}
// Setup dropdowns
setupDropdown('objectYearDropdown');
setupDropdown('objectCouncilDropdown');
setupDropdown('objectPartyDropdown');
setupDropdown('objectDeptDropdown');
setupDropdown('objectTagsDropdown');
setupDropdown('objectLegislatureDropdown');
setupDropdown('objectMentionDropdown');
}
function setupObjectFilterListeners() {
['objectYearDropdown', 'objectCouncilDropdown', 'objectPartyDropdown', 'objectDeptDropdown', 'objectTagsDropdown', 'objectLegislatureDropdown', 'objectMentionDropdown'].forEach(id => {
const el = document.getElementById(id);
if (el) el.addEventListener('change', applyObjectFilters);
});
document.getElementById('resetObjectFilters').addEventListener('click', resetObjectFilters);
}
function resetObjectFilters() {
['objectYearDropdown', 'objectCouncilDropdown', 'objectPartyDropdown', 'objectDeptDropdown', 'objectTagsDropdown', 'objectLegislatureDropdown', 'objectMentionDropdown'].forEach(id => {
const dropdown = document.getElementById(id);
if (!dropdown) return;
const selectAll = dropdown.querySelector('[data-select-all]');
const checkboxes = dropdown.querySelectorAll('input[type="checkbox"]:not([data-select-all])');
if (selectAll) selectAll.checked = true;
checkboxes.forEach(cb => cb.checked = false);
const countSpan = dropdown.querySelector('.filter-count');
if (countSpan) countSpan.textContent = '';
});
applyObjectFilters();
}
function getLegislature(date) {
if (!date) return null;
if (date >= '2023-12-01') return '52';
if (date >= '2019-12-01') return '51';
if (date >= '2015-12-01') return '50';
return null;
}
function getLegislatureFromSession(sessionId) {
if (!sessionId) return null;
const sessionStr = String(sessionId);
if (sessionStr.startsWith('52')) return '52';
if (sessionStr.startsWith('51')) return '51';
if (sessionStr.startsWith('50')) return '50';
return null;
}
function applyObjectFilters() {
const yearFilters = getCheckedValues('objectYearDropdown');
const councilFilters = getCheckedValues('objectCouncilDropdown');
const partyFilters = getCheckedValues('objectPartyDropdown');
const deptFilters = getCheckedValues('objectDeptDropdown');
const tagsFilters = getCheckedValues('objectTagsDropdown');
const legislatureFilters = getCheckedValues('objectLegislatureDropdown');
const mentionFilters = getCheckedValues('objectMentionDropdown');
filteredData = allData.filter(item => {
// Filtre année
if (yearFilters.length > 0 && item.date) {
const year = item.date.substring(0, 4);
if (!yearFilters.includes(year)) return false;
}
// Filtre conseil (NR=N, SR=S)
if (councilFilters.length > 0) {
const councilCode = item.council === 'NR' ? 'N' : item.council === 'SR' ? 'S' : item.council;
if (!councilFilters.includes(councilCode)) return false;
}
// Filtre parti
if (partyFilters.length > 0) {
const itemParty = item.party || getPartyFromAuthor(item.author);
const normalizedParty = normalizeParty(itemParty);
if (!partyFilters.includes(normalizedParty)) return false;
}
// Filtre département
if (deptFilters.length > 0) {
const itemDept = item.department || 'none';
if (!deptFilters.includes(itemDept)) return false;
}
// Filtre thématiques
if (tagsFilters.length > 0) {
const itemTags = item.tags ? item.tags.split('|').map(t => t.trim()) : [];
const hasMatchingTag = itemTags.some(tag => tagsFilters.includes(tag));
if (!hasMatchingTag) return false;
}
// Filtre législature
if (legislatureFilters.length > 0) {
const itemLegislature = getLegislature(item.date);
if (!legislatureFilters.includes(itemLegislature)) return false;
}
// Filtre mention (qui cite le CDF)
if (mentionFilters.length > 0) {
const mentionMap = {
'elu': 'Élu',
'cf': 'Conseil fédéral',
'both': 'Élu & Conseil fédéral'
};
const itemMention = item.mention || '';
const matchesMention = mentionFilters.some(v => mentionMap[v] === itemMention);
if (!matchesMention) return false;
}
return true;
});
renderAllObjectCharts();
}
// Construit l'URL vers objects.html avec tous les filtres actifs + un filtre additionnel
function buildObjectsUrl(additionalFilter = {}) {
const params = new URLSearchParams();
const yearFilters = getCheckedValues('objectYearDropdown');
const councilFilters = getCheckedValues('objectCouncilDropdown');
const partyFilters = getCheckedValues('objectPartyDropdown');
const deptFilters = getCheckedValues('objectDeptDropdown');
const tagsFilters = getCheckedValues('objectTagsDropdown');
const legislatureFilters = getCheckedValues('objectLegislatureDropdown');
const mentionFilters = getCheckedValues('objectMentionDropdown');
if (yearFilters.length > 0) params.set('filter_year', yearFilters.join(','));
if (councilFilters.length > 0) params.set('filter_council', councilFilters.join(','));
if (partyFilters.length > 0) params.set('filter_party', partyFilters.join(','));
if (deptFilters.length > 0) params.set('filter_dept', deptFilters.join(','));
if (tagsFilters.length > 0) params.set('filter_tags', tagsFilters.join(','));
if (legislatureFilters.length > 0) params.set('filter_legislature', legislatureFilters.join(','));
if (mentionFilters.length > 0) params.set('filter_mention', mentionFilters.join(','));
if (additionalFilter.year) params.set('filter_year', additionalFilter.year);
if (additionalFilter.council) params.set('filter_council', additionalFilter.council);
if (additionalFilter.party) params.set('filter_party', additionalFilter.party);
if (additionalFilter.type) params.set('filter_type', additionalFilter.type);
if (additionalFilter.session) params.set('filter_session', additionalFilter.session);
if (additionalFilter.mention) params.set('filter_mention', additionalFilter.mention);
const queryString = params.toString();
return `objects.html${queryString ? '?' + queryString : ''}`;
}
function renderAllObjectCharts() {
renderPartyChart();
renderTypeChart();
renderYearChart();
renderTopAuthors();
updateGlobalSummary();
}
// Mapping des types de sessions (législatures 50, 51, 52)
const sessionTypes = {
// Législature 50 (2015-2019)
'5001': 'Hiver', '5002': 'Printemps', '5003': 'Spéciale', '5004': 'Été', '5005': 'Automne',
'5006': 'Hiver', '5007': 'Printemps', '5008': 'Spéciale', '5009': 'Été', '5010': 'Automne',
'5011': 'Hiver', '5012': 'Printemps', '5013': 'Été', '5014': 'Automne',
'5015': 'Hiver', '5016': 'Printemps', '5017': 'Spéciale', '5018': 'Été', '5019': 'Automne',
// Législature 51 (2019-2023)
'5101': 'Hiver', '5102': 'Printemps', '5103': 'Spéciale', '5104': 'Été', '5105': 'Automne',
'5106': 'Spéciale', '5107': 'Hiver', '5108': 'Printemps', '5109': 'Spéciale', '5110': 'Été',
'5111': 'Automne', '5112': 'Hiver', '5113': 'Printemps', '5114': 'Spéciale', '5115': 'Été',
'5116': 'Automne', '5117': 'Hiver', '5118': 'Printemps', '5119': 'Spéciale', '5120': 'Spéciale',
'5121': 'Été', '5122': 'Automne',
// Législature 52 (2023-)
'5201': 'Hiver', '5202': 'Printemps', '5203': 'Spéciale', '5204': 'Été', '5205': 'Automne',
'5206': 'Hiver', '5207': 'Printemps', '5208': 'Spéciale', '5209': 'Été', '5210': 'Automne',
'5211': 'Hiver', '5212': 'Printemps', '5213': 'Spéciale', '5214': 'Été', '5215': 'Automne',
'5216': 'Hiver', '5217': 'Printemps', '5218': 'Spéciale'
};
function populateDebateFilters() {
// Populer filtre années
const yearMenu = document.getElementById('debateYearMenu');
const years = [...new Set(debatesData.map(d => d.date ? d.date.substring(0, 4) : null).filter(Boolean))];
if (!years.includes('2026')) years.push('2026');
years.sort().reverse();
years.forEach(year => {
const label = document.createElement('label');
label.innerHTML = `<input type="checkbox" value="${year}"> ${year}`;
yearMenu.appendChild(label);
});
// Populer filtre partis
const partyMenu = document.getElementById('debatePartyMenu');
const parties = [...new Set(debatesData.map(d => {
if (!d.party) return 'Conseil fédéral';
return debatePartyLabels[d.party] || d.party;
}))];
parties.sort((a, b) => a.localeCompare(b, 'fr'));
parties.forEach(party => {
const label = document.createElement('label');
label.innerHTML = `<input type="checkbox" value="${party}"> ${party}`;
partyMenu.appendChild(label);
});
// Populer filtre départements
const deptMenu = document.getElementById('debateDeptMenu');
if (deptMenu) {
const departments = [...new Set(debatesData.map(d => d.department).filter(Boolean))];
departments.sort((a, b) => translateDept(a).localeCompare(translateDept(b), 'fr'));
departments.forEach(dept => {
const label = document.createElement('label');
const deptFR = translateDept(dept);
label.innerHTML = `<input type="checkbox" value="${dept}"> ${deptFR}`;
deptMenu.appendChild(label);
});
}
// Populer filtre thématiques
const tagsMenu = document.getElementById('debateTagsMenu');
if (tagsMenu) {
const allTags = new Set();
debatesData.forEach(item => {
const tags = debateTagsMapping[item.business_number];
if (tags) {
tags.split('|').forEach(tag => {
if (tag.trim()) allTags.add(tag.trim());
});
}
});
const tagsArray = [...allTags].sort((a, b) => a.localeCompare(b, 'fr'));
tagsArray.forEach(tag => {
const label = document.createElement('label');
label.innerHTML = `<input type="checkbox" value="${tag}"> ${tag}`;
tagsMenu.appendChild(label);
});
}
// Setup dropdowns
setupDropdown('debateYearDropdown');
setupDropdown('debateSessionDropdown');
setupDropdown('debateCouncilDropdown');
setupDropdown('debatePartyDropdown');
setupDropdown('debateDeptDropdown');
setupDropdown('debateTagsDropdown');
setupDropdown('debateLegislatureDropdown');
}
function setupDebateFilterListeners() {
['debateYearDropdown', 'debateSessionDropdown', 'debateCouncilDropdown', 'debatePartyDropdown', 'debateDeptDropdown', 'debateTagsDropdown', 'debateLegislatureDropdown'].forEach(id => {
const el = document.getElementById(id);
if (el) el.addEventListener('change', applyDebateFilters);
});
document.getElementById('resetDebateFilters').addEventListener('click', resetDebateFilters);
}
function resetDebateFilters() {
['debateYearDropdown', 'debateSessionDropdown', 'debateCouncilDropdown', 'debatePartyDropdown', 'debateDeptDropdown', 'debateTagsDropdown', 'debateLegislatureDropdown'].forEach(id => {
const dropdown = document.getElementById(id);
if (!dropdown) return;
const selectAll = dropdown.querySelector('[data-select-all]');
const checkboxes = dropdown.querySelectorAll('input[type="checkbox"]:not([data-select-all])');
if (selectAll) selectAll.checked = true;
checkboxes.forEach(cb => cb.checked = false);
const countSpan = dropdown.querySelector('.filter-count');
if (countSpan) countSpan.textContent = '';
});
applyDebateFilters();
}
// Filtrer les deux sections par législature depuis le résumé
function filterByLegislature(legValue) {
// Appliquer sur le bloc objets
const objDropdown = document.getElementById('objectLegislatureDropdown');
if (objDropdown) {
const selectAll = objDropdown.querySelector('[data-select-all]');
if (selectAll) selectAll.checked = false;
objDropdown.querySelectorAll('input[type="checkbox"]:not([data-select-all])').forEach(cb => {
cb.checked = (cb.value === legValue);
});
const countSpan = objDropdown.querySelector('.filter-count');
if (countSpan) countSpan.textContent = '(1)';
}
// Appliquer sur le bloc débats
const debDropdown = document.getElementById('debateLegislatureDropdown');
if (debDropdown) {
const selectAll = debDropdown.querySelector('[data-select-all]');
if (selectAll) selectAll.checked = false;
debDropdown.querySelectorAll('input[type="checkbox"]:not([data-select-all])').forEach(cb => {
cb.checked = (cb.value === legValue);
});
const countSpan = debDropdown.querySelector('.filter-count');
if (countSpan) countSpan.textContent = '(1)';
}
applyObjectFilters();
applyDebateFilters();
}
// Filtrer les débats par conseil depuis le résumé
function filterDebatesByCouncil(councilCode) {
const dropdown = document.getElementById('debateCouncilDropdown');
if (!dropdown) return;
// Décocher tout d'abord
const selectAll = dropdown.querySelector('[data-select-all]');
if (selectAll) selectAll.checked = false;
const checkboxes = dropdown.querySelectorAll('input[type="checkbox"]:not([data-select-all])');
checkboxes.forEach(cb => {
cb.checked = (cb.value === councilCode);
});
// Mettre à jour le compteur du filtre
const countSpan = dropdown.querySelector('.filter-count');
if (countSpan) countSpan.textContent = '(1)';
applyDebateFilters();
// Scroller vers la section débats
const debatesSection = document.getElementById('debatesSection');
if (debatesSection) debatesSection.scrollIntoView({ behavior: 'smooth' });
}
function applyDebateFilters() {
const yearFilters = getCheckedValues('debateYearDropdown');
const sessionFilters = getCheckedValues('debateSessionDropdown');
const councilFilters = getCheckedValues('debateCouncilDropdown');
const partyFilters = getCheckedValues('debatePartyDropdown');
const deptFilters = getCheckedValues('debateDeptDropdown');
const tagsFilters = getCheckedValues('debateTagsDropdown');
const legislatureFilters = getCheckedValues('debateLegislatureDropdown');
filteredDebatesData = debatesData.filter(item => {
// Filtre année
if (yearFilters.length > 0 && item.date) {
const year = item.date.substring(0, 4);
if (!yearFilters.includes(year)) return false;
}
// Filtre session (par type)
if (sessionFilters.length > 0) {
const sessionType = sessionTypes[item.id_session];
if (!sessionFilters.includes(sessionType)) return false;
}
// Filtre conseil
if (councilFilters.length > 0 && !councilFilters.includes(item.council)) return false;
// Filtre parti
if (partyFilters.length > 0) {
const itemParty = item.party ? (debatePartyLabels[item.party] || item.party) : 'Conseil fédéral';
if (!partyFilters.includes(itemParty)) return false;
}
// Filtre département
if (deptFilters.length > 0) {
const itemDept = item.department || 'none';
if (!deptFilters.includes(itemDept)) return false;
}
// Filtre thématiques
if (tagsFilters.length > 0) {
const itemTags = debateTagsMapping[item.business_number];
if (!itemTags) return false;
const itemTagsArray = itemTags.split('|').map(t => t.trim());
const hasMatchingTag = tagsFilters.some(tag => itemTagsArray.includes(tag));
if (!hasMatchingTag) return false;
}
// Filtre législature
if (legislatureFilters.length > 0) {
const itemLegislature = getLegislatureFromSession(item.id_session);
if (!legislatureFilters.includes(itemLegislature)) return false;
}
return true;
});
filteredDebatesData.sort((a, b) => (b.date || '').localeCompare(a.date || ''));
renderAllDebateCharts();
}
// Construit l'URL vers debates.html avec tous les filtres actifs + un filtre additionnel
function buildDebatesUrl(additionalFilter = {}) {
const params = new URLSearchParams();
// Récupérer les filtres actifs du bloc débats
const yearFilters = getCheckedValues('debateYearDropdown');
const sessionFilters = getCheckedValues('debateSessionDropdown');
const councilFilters = getCheckedValues('debateCouncilDropdown');
const partyFilters = getCheckedValues('debatePartyDropdown');
const deptFilters = getCheckedValues('debateDeptDropdown');
const tagsFilters = getCheckedValues('debateTagsDropdown');
const legislatureFilters = getCheckedValues('debateLegislatureDropdown');
// Ajouter les filtres existants
if (yearFilters.length > 0) params.set('filter_year', yearFilters.join(','));
if (sessionFilters.length > 0) params.set('filter_session', sessionFilters.join(','));
if (councilFilters.length > 0) params.set('filter_council', councilFilters.join(','));
if (partyFilters.length > 0) params.set('filter_party', partyFilters.join(','));
if (deptFilters.length > 0) params.set('filter_dept', deptFilters.join(','));
if (tagsFilters.length > 0) params.set('filter_tags', tagsFilters.join(','));
if (legislatureFilters.length > 0) params.set('filter_legislature', legislatureFilters.join(','));
// Ajouter le filtre additionnel (celui sur lequel on a cliqué)
if (additionalFilter.council) params.set('filter_council', additionalFilter.council);
if (additionalFilter.party) params.set('filter_party', additionalFilter.party);
const queryString = params.toString();
return `debates.html${queryString ? '?' + queryString : ''}`;
}
function renderAllDebateCharts() {
renderDebatePartyChart();
renderDebateCouncilChart();
renderTopSpeakers();
renderTopSpeakersNoCF();
updateGlobalSummary();
}
function updateGlobalSummary() {
const objectsCountEl = document.getElementById('globalObjectsCount');
const debatesCountEl = document.getElementById('globalDebatesCount');
const periodEl = document.getElementById('globalPeriod');
// Récupérer les filtres communs des deux blocs
const objectYearFilters = getCheckedValues('objectYearDropdown');
const objectLegislatureFilters = getCheckedValues('objectLegislatureDropdown');
const objectCouncilFilters = getCheckedValues('objectCouncilDropdown');
const objectPartyFilters = getCheckedValues('objectPartyDropdown');
const objectDeptFilters = getCheckedValues('objectDeptDropdown');
const objectTagsFilters = getCheckedValues('objectTagsDropdown');
const objectMentionFilters = getCheckedValues('objectMentionDropdown');
const debateYearFilters = getCheckedValues('debateYearDropdown');
const debateLegislatureFilters = getCheckedValues('debateLegislatureDropdown');
const debateCouncilFilters = getCheckedValues('debateCouncilDropdown');
const debatePartyFilters = getCheckedValues('debatePartyDropdown');
const debateDeptFilters = getCheckedValues('debateDeptDropdown');
// Combiner les filtres (union des filtres actifs)
const yearFilters = [...new Set([...objectYearFilters, ...debateYearFilters])];
const legislatureFilters = [...new Set([...objectLegislatureFilters, ...debateLegislatureFilters])];
const councilFilters = [...new Set([...objectCouncilFilters, ...debateCouncilFilters])];
const partyFilters = [...new Set([...objectPartyFilters, ...debatePartyFilters])];
const deptFilters = [...new Set([...objectDeptFilters, ...debateDeptFilters])];
// Filtrer les objets avec les filtres combinés
const globalFilteredObjects = allData.filter(item => {
if (yearFilters.length > 0 && item.date) {
const year = item.date.substring(0, 4);
if (!yearFilters.includes(year)) return false;
}
if (legislatureFilters.length > 0) {
const itemLegislature = getLegislature(item.date);
if (!legislatureFilters.includes(itemLegislature)) return false;
}
if (councilFilters.length > 0) {
const councilCode = item.council === 'NR' ? 'N' : item.council === 'SR' ? 'S' : item.council;
if (!councilFilters.includes(councilCode)) return false;
}
if (partyFilters.length > 0) {
const itemParty = item.party || getPartyFromAuthor(item.author);
const normalizedParty = normalizeParty(itemParty);
if (!partyFilters.includes(normalizedParty)) return false;
}
if (deptFilters.length > 0) {
const itemDept = item.department || 'none';
if (!deptFilters.includes(itemDept)) return false;
}
// Thématiques uniquement pour les objets
if (objectTagsFilters.length > 0) {
const itemTags = item.tags ? item.tags.split('|').map(t => t.trim()) : [];
const hasMatchingTag = itemTags.some(tag => objectTagsFilters.includes(tag));
if (!hasMatchingTag) return false;
}
// Mention filter (qui cite le CDF)
if (objectMentionFilters.length > 0) {
const mentionMap = {
'elu': 'Élu',
'cf': 'Conseil fédéral',
'both': 'Élu & Conseil fédéral'
};
const itemMention = item.mention || '';
const matchesMention = objectMentionFilters.some(v => mentionMap[v] === itemMention);
if (!matchesMention) return false;
}
return true;
});
// Filtrer les débats avec les filtres combinés
const globalFilteredDebates = debatesData.filter(item => {
if (yearFilters.length > 0 && item.date) {
const year = item.date.substring(0, 4);
if (!yearFilters.includes(year)) return false;
}
if (legislatureFilters.length > 0) {
const itemLegislature = getLegislatureFromSession(item.id_session);
if (!legislatureFilters.includes(itemLegislature)) return false;
}
if (councilFilters.length > 0 && !councilFilters.includes(item.council)) return false;
if (partyFilters.length > 0) {
const itemParty = item.party ? (debatePartyLabels[item.party] || item.party) : 'Conseil fédéral';
if (!partyFilters.includes(itemParty)) return false;
}
if (deptFilters.length > 0) {
const itemDept = item.department || 'none';
if (!deptFilters.includes(itemDept)) return false;
}
return true;
});
if (objectsCountEl) {
objectsCountEl.textContent = globalFilteredObjects.length;
}
// Calculer les % de qui cite le CDF (inclusif : "les deux" compte pour chacun)
const pctEluEl = document.getElementById('pctElu');
const pctCFEl = document.getElementById('pctCF');
const bothNoteEl = document.getElementById('mentionBothNote');
if (pctEluEl && pctCFEl && globalFilteredObjects.length > 0) {
const total = globalFilteredObjects.length;
const both = globalFilteredObjects.filter(item => item.mention === 'Élu & Conseil fédéral').length;
// Inclusif : auteur seul + les deux
const eluInclusive = globalFilteredObjects.filter(item => item.mention === 'Élu' || item.mention === 'Élu & Conseil fédéral').length;
// Inclusif : CF seul + les deux
const cfInclusive = globalFilteredObjects.filter(item => item.mention === 'Conseil fédéral' || item.mention === 'Élu & Conseil fédéral').length;
pctEluEl.textContent = eluInclusive;
pctCFEl.textContent = cfInclusive;
if (bothNoteEl && both > 0) {
bothNoteEl.textContent = `dont ${both} par les deux`;
}
}
if (debatesCountEl) {
debatesCountEl.textContent = globalFilteredDebates.length;
}
// Sous-infos débats : répartition CN / CE / AF
const debatesCNEl = document.getElementById('debatesCN');
const debatesCEEl = document.getElementById('debatesCE');
const debatesAFEl = document.getElementById('debatesAF');
if (debatesCNEl && debatesCEEl && globalFilteredDebates.length > 0) {
const cn = globalFilteredDebates.filter(d => d.council === 'N').length;
const ce = globalFilteredDebates.filter(d => d.council === 'S').length;
const af = globalFilteredDebates.filter(d => d.council === 'V').length;
debatesCNEl.textContent = cn;
debatesCEEl.textContent = ce;
if (debatesAFEl) debatesAFEl.textContent = af;
}
if (periodEl) {
const years = new Set();
globalFilteredObjects.forEach(item => {
if (item.date) years.add(item.date.substring(0, 4));
});
globalFilteredDebates.forEach(item => {
if (item.date) years.add(item.date.substring(0, 4));
});
if (years.size === 0) {
periodEl.textContent = '2015 - 2026';
} else {
const sorted = [...years].sort();
if (sorted.length === 1) {
periodEl.textContent = sorted[0];
} else {
periodEl.textContent = `${sorted[0]} - ${sorted[sorted.length - 1]}`;
}
}
}
// Sous-infos période : législatures couvertes
const legislatures = new Set();
globalFilteredObjects.forEach(item => {
const leg = getLegislature(item.date);
if (leg) legislatures.add(leg);
});
globalFilteredDebates.forEach(item => {
const leg = getLegislatureFromSession(item.id_session);
if (leg) legislatures.add(leg);
});
['50', '51', '52'].forEach(num => {
const el = document.getElementById('leg' + num);
if (el) {
const isActive = legislatures.has(num) || legislatures.size === 0;
el.style.opacity = isActive ? '1' : '0.3';
}
});
}
function getPartyFromAuthor(author) {
if (!author) return null;
if (author.includes('FDP') || author.includes('PLR') || author.includes('libéral-radical')) return 'PLR';
if (author.includes('Grünliberale') || author.includes('vert\'libéral')) return 'pvl';
if (author.includes('SVP') || author.includes('UDC') || author.includes('Schweizerischen Volkspartei') || author.includes('Union démocratique')) return 'UDC';
if (author.includes('SP ') || author.includes('PS ') || author.includes('socialiste') || author.includes('Sozialdemokratische')) return 'PSS';
if (author.includes('Grüne') || author.includes('Verts') || author.includes('VERT')) return 'VERT-E-S';
if (author.includes('Mitte') || author.includes('Centre') || author.includes('EVP')) return 'Le Centre';
return null;
}
function normalizeParty(party) {
const normalized = {
'PSS': 'PS',
'PS': 'PS',
'VERT-E-S': 'VERT-E-S',
'Les Vert-e-s': 'VERT-E-S',
'Al': 'VERT-E-S',
'pvl': 'Vert\'libéraux',
'PVL': 'Vert\'libéraux',
'Le Centre': 'Le Centre',
'Centre': 'Le Centre',
'M-E': 'Le Centre',
'PDC': 'Le Centre',
'PBD': 'Le Centre',
'CSPO': 'Le Centre',
'CVP': 'Le Centre',
'BDP': 'Le Centre',
'Commissions': 'Commissions'
};
return normalized[party] || party;
}
function getSessionTypeFromDate(dateStr) {
if (!dateStr || !sessionsData.length) {
return 'autre'; // Hors session si pas de données
}
// Chercher la session correspondante par dates exactes
for (const session of sessionsData) {
if (dateStr >= session.start && dateStr <= session.end) {
// Extraire le type de session depuis l'id (ex: "2024-printemps" -> "printemps")
const parts = session.id.split('-');
if (parts.length >= 2) {
const sessionType = parts[1];
if (sessionType.startsWith('speciale')) return 'speciale';
if (sessionType === 'printemps') return 'printemps';
if (sessionType === 'ete') return 'ete';
if (sessionType === 'automne') return 'automne';
if (sessionType === 'hiver') return 'hiver';
}
return 'autre';
}
}
// Si pas dans une session exacte -> hors session
return 'autre';
}
function renderPartyChart() {
if (partyChartInstance) {
partyChartInstance.destroy();
}
const partyCounts = {};
filteredData.forEach(item => {
let party = item.party || getPartyFromAuthor(item.author);
if (party) {
party = normalizeParty(party);
partyCounts[party] = (partyCounts[party] || 0) + 1;
}
});
const sortedParties = Object.entries(partyCounts)
.sort((a, b) => b[1] - a[1]);
const labels = sortedParties.map(([party]) => party);
const data = sortedParties.map(([, count]) => count);
const colors = labels.map(party => {
for (const [key, color] of Object.entries(partyColors)) {
if (normalizeParty(key) === party) return color;
}
return '#999';
});
const ctx = document.getElementById('partyChart').getContext('2d');
partyChartInstance = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Interventions',
data: data,
backgroundColor: colors,
borderRadius: 4
}]
},
options: {
indexAxis: 'y',
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: false }
},
scales: {
x: {
beginAtZero: true,
ticks: { stepSize: 1 }
}
},
onClick: (event, elements) => {
if (elements.length > 0) {
const index = elements[0].index;
const party = labels[index];
const filterValue = partyToFilter[party] || party;
window.location.href = buildObjectsUrl({ party: filterValue });
}
}
}
});
}
function renderTypeChart() {
if (typeChartInstance) {
typeChartInstance.destroy();
}
const typeCounts = {};
filteredData.forEach(item => {
const type = item.type;
if (type) {
const label = typeLabels[type] || type;
typeCounts[label] = (typeCounts[label] || 0) + 1;
}
});