Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
453 lines (389 loc) · 16.9 KB
/
Copy pathscript.js
File metadata and controls
453 lines (389 loc) · 16.9 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
// Element Selections
const dateForm = document.getElementById('dateForm');
const dateDebutInput = document.getElementById('dateDebut');
const dateFinInput = document.getElementById('dateFin');
const countryCodeSelect = document.getElementById('countryCode');
const resultsPlaceholder = document.getElementById('resultsPlaceholder');
const resultsContent = document.getElementById('resultsContent');
const workRatioTrack = document.getElementById('workRatioTrack');
const dateDebutTitle = document.getElementById('dateDebutTitle');
const dateFinTitle = document.getElementById('dateFinTitle');
const detailBetweenTwoDatesSpan = document.getElementById('detailBetweenTwoDatesSpan');
const numbersOfDays = document.getElementById('numbersOfDays');
const workDaysSpan = document.getElementById('workDaysSpan');
const weekendDaysSpan = document.getElementById('weekendDaysSpan');
const workRatioBar = document.getElementById('workRatioBar');
const holidaysCountSpan = document.getElementById('holidaysCountSpan');
const holidaysList = document.getElementById('holidaysList');
const holidaysDetails = document.getElementById('holidaysDetails');
const holidaysCountryName = document.getElementById('holidaysCountryName');
const holidaysLoader = document.getElementById('holidaysLoader');
const totalMonthsSpan = document.getElementById('totalMonthsSpan');
const totalWeeksSpan = document.getElementById('totalWeeksSpan');
const totalHoursSpan = document.getElementById('totalHoursSpan');
const totalMinutesSpan = document.getElementById('totalMinutesSpan');
const startWeekdaySpan = document.getElementById('startWeekdaySpan');
const endWeekdaySpan = document.getElementById('endWeekdaySpan');
const leapYearsCountSpan = document.getElementById('leapYearsCountSpan');
const leapYearsList = document.getElementById('leapYearsList');
const leapYearsDetails = document.getElementById('leapYearsDetails');
// Init
document.addEventListener('DOMContentLoaded', () => {
const currentYearEl = document.getElementById('current-year');
if (currentYearEl) currentYearEl.innerText = new Date().getFullYear();
fetchAndInitCountries();
});
// Form Submit
dateForm.addEventListener('submit', (e) => {
e.preventDefault();
analyzeDates();
});
// Main Analyze
async function analyzeDates() {
const dateDebutVal = dateDebutInput.value;
const dateFinVal = dateFinInput.value;
if (!dateDebutVal || !dateFinVal) {
showToast('Veuillez sélectionner deux dates valides.');
return;
}
const dateDebut = new Date(dateDebutVal);
const dateFin = new Date(dateFinVal);
dateDebut.setHours(0, 0, 0, 0);
dateFin.setHours(0, 0, 0, 0);
if (dateDebut > dateFin) {
showToast('La date de début doit être antérieure ou égale à la date de fin.');
return;
}
resultsPlaceholder.classList.add('hidden');
resultsContent.classList.remove('hidden');
resultsContent.classList.remove('is-visible');
void resultsContent.offsetWidth;
resultsContent.classList.add('is-visible');
const options = { day: '2-digit', month: '2-digit', year: 'numeric' };
dateDebutTitle.innerText = dateDebut.toLocaleDateString('fr-FR', options);
dateFinTitle.innerText = dateFin.toLocaleDateString('fr-FR', options);
const duration = getDuration(dateDebut, dateFin);
let durationStr = '';
if (duration.years > 0) durationStr += `${duration.years} an${duration.years > 1 ? 's' : ''} `;
if (duration.months > 0) durationStr += `${duration.months} mois `;
if (duration.days > 0 || (!duration.years && !duration.months)) {
durationStr += `${duration.days} jour${duration.days > 1 ? 's' : ''}`;
}
detailBetweenTwoDatesSpan.innerText = durationStr.trim();
const totalDays = getNumberOfDays(dateDebut, dateFin);
numbersOfDays.innerText = formatNumber(totalDays);
const { workDays, weekendDays } = calcWeeklySplit(dateDebut, dateFin);
workDaysSpan.innerText = formatNumber(workDays);
weekendDaysSpan.innerText = formatNumber(weekendDays);
const pct = (workDays + weekendDays) > 0 ? (workDays / (workDays + weekendDays)) * 100 : 0;
workRatioBar.style.width = `${pct}%`;
if (workRatioTrack) {
workRatioTrack.setAttribute('aria-valuenow', Math.round(pct));
}
totalMonthsSpan.innerText = formatNumber((totalDays / 30.437).toFixed(1));
totalWeeksSpan.innerText = formatNumber((totalDays / 7).toFixed(1));
totalHoursSpan.innerText = formatNumber(totalDays * 24);
totalMinutesSpan.innerText = formatNumber(totalDays * 24 * 60);
startWeekdaySpan.innerText = capitalize(dateDebut.toLocaleDateString('fr-FR', { weekday: 'long' }));
endWeekdaySpan.innerText = capitalize(dateFin.toLocaleDateString('fr-FR', { weekday: 'long' }));
const leapYears = getLeapYears(dateDebut.getFullYear(), dateFin.getFullYear());
leapYearsCountSpan.innerText = leapYears.length;
if (leapYears.length > 0) {
leapYearsDetails.open = false;
leapYearsDetails.style.display = 'block';
leapYearsList.innerHTML = leapYears
.map(y => `<li><span>Année ${y}</span> <small>366 jours</small></li>`)
.join('');
} else {
leapYearsDetails.style.display = 'none';
leapYearsList.innerHTML = '';
}
const countryCode = countryCodeSelect.value;
const countryLabel = countryCodeSelect.options[countryCodeSelect.selectedIndex].text.trim();
holidaysCountryName.innerText = countryLabel ? `— ${countryLabel}` : '';
setHolidaysLoading(true);
try {
const holidays = await fetchHolidays(countryCode, dateDebut, dateFin);
setHolidaysLoading(false);
holidaysCountSpan.innerText = holidays.length;
if (holidays.length > 0) {
holidaysDetails.style.display = 'block';
holidaysList.innerHTML = holidays
.map(h => `<li><span>${escapeHtml(h.localName)}</span> <small>${new Date(h.date + 'T00:00:00').toLocaleDateString('fr-FR', options)}</small></li>`)
.join('');
} else {
holidaysDetails.style.display = 'none';
holidaysList.innerHTML = '';
}
} catch {
setHolidaysLoading(false);
holidaysCountSpan.innerText = '—';
holidaysDetails.style.display = 'none';
holidaysList.innerHTML = '';
showToast('Impossible de récupérer les jours fériés. Vérifiez votre connexion.');
}
}
// Nager.Date API
async function fetchHolidays(countryCode, startDate, endDate) {
const startYear = startDate.getFullYear();
const endYear = endDate.getFullYear();
const requests = [];
for (let y = startYear; y <= endYear; y++) {
requests.push(
fetch(`https://date.nager.at/api/v3/PublicHolidays/${y}/${countryCode}`)
.then(r => r.ok ? r.json() : [])
);
}
const results = await Promise.all(requests);
const all = results.flat();
return all.filter(h => {
const d = new Date(h.date + 'T00:00:00');
return d >= startDate && d <= endDate;
}).sort((a, b) => new Date(a.date) - new Date(b.date));
}
// Loading State
function setHolidaysLoading(loading) {
if (loading) {
holidaysCountSpan.innerText = '';
holidaysLoader.classList.remove('hidden');
holidaysDetails.style.display = 'none';
holidaysList.innerHTML = '';
} else {
holidaysLoader.classList.add('hidden');
}
}
// Calculations
function getNumberOfDays(start, end) {
return Math.round(Math.abs(end - start) / (1000 * 60 * 60 * 24));
}
function getDuration(start, end) {
let years = end.getFullYear() - start.getFullYear();
let months = end.getMonth() - start.getMonth();
let days = end.getDate() - start.getDate();
if (days < 0) {
months--;
days += new Date(end.getFullYear(), end.getMonth(), 0).getDate();
}
if (months < 0) { years--; months += 12; }
return { years, months, days };
}
function calcWeeklySplit(start, end) {
let workDays = 0, weekendDays = 0;
const curr = new Date(start);
while (curr <= end) {
const day = curr.getDay();
(day === 0 || day === 6) ? weekendDays++ : workDays++;
curr.setDate(curr.getDate() + 1);
}
return { workDays, weekendDays };
}
function getLeapYears(startYear, endYear) {
const years = [];
for (let y = startYear; y <= endYear; y++) {
if ((y % 4 === 0 && y % 100 !== 0) || y % 400 === 0) years.push(y);
}
return years;
}
// Helpers
function formatNumber(num) {
return new Intl.NumberFormat('fr-FR').format(num);
}
function capitalize(str) {
return str ? str.charAt(0).toUpperCase() + str.slice(1) : '';
}
function escapeHtml(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
function showToast(message) {
const container = document.getElementById('toastContainer');
const toast = document.createElement('div');
toast.className = 'toast';
toast.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="12"></line><line x1="12" y1="16" x2="12.01" y2="16"></line></svg>
<span>${escapeHtml(message)}</span>
`;
container.appendChild(toast);
setTimeout(() => {
toast.style.opacity = '0';
toast.style.transform = 'translateX(12px)';
toast.style.transition = 'all 0.35s ease';
setTimeout(() => toast.remove(), 350);
}, 3500);
}
// Country Selector & API integration
let allCountries = [];
let currentSelectedCode = 'BJ';
// Helper: Convert country code to flag emoji
function getFlagEmoji(countryCode) {
if (!countryCode || countryCode.length !== 2) return '🌐';
const codePoints = countryCode
.toUpperCase()
.split('')
.map(char => 127397 + char.charCodeAt(0));
try {
return String.fromCodePoint(...codePoints);
} catch {
return '🌐';
}
}
// Populate the native select for compatibility
function populateNativeSelect(list) {
countryCodeSelect.innerHTML = list
.map(c => `<option value="${c.countryCode}">${getFlagEmoji(c.countryCode)} ${c.name}</option>`)
.join('');
}
// Select a country and update all UI elements
function selectCountry(code) {
const c = allCountries.find(x => x.countryCode === code);
if (!c) return;
currentSelectedCode = code;
// Update native select
countryCodeSelect.value = code;
// Update trigger UI
const selectedValueEl = document.querySelector('#selectTrigger .selected-value');
if (selectedValueEl) {
selectedValueEl.innerHTML = `${getFlagEmoji(c.countryCode)} ${c.name}`;
}
// Update active class in dropdown items
document.querySelectorAll('.option-item').forEach(item => {
if (item.getAttribute('data-value') === code) {
item.classList.add('is-selected');
} else {
item.classList.remove('is-selected');
}
});
}
// Render option items in custom dropdown list
function renderCustomOptions(list) {
const optionsList = document.getElementById('optionsList');
if (!optionsList) return;
if (list.length === 0) {
optionsList.innerHTML = `<li class="no-results">Aucun pays trouvé</li>`;
return;
}
optionsList.innerHTML = list
.map(c => `
<li class="option-item ${c.countryCode === currentSelectedCode ? 'is-selected' : ''}" data-value="${c.countryCode}">
<span class="option-flag">${getFlagEmoji(c.countryCode)}</span>
<span class="option-name">${escapeHtml(c.name)}</span>
</li>
`)
.join('');
// Add click listeners to items
optionsList.querySelectorAll('.option-item').forEach(item => {
item.addEventListener('click', (e) => {
const code = item.getAttribute('data-value');
selectCountry(code);
// Close dropdown
const customSelectContainer = document.getElementById('customSelectContainer');
if (customSelectContainer) {
customSelectContainer.classList.remove('is-open');
document.getElementById('selectTrigger').setAttribute('aria-expanded', 'false');
}
});
});
}
// Initialize custom select event handlers
function initCustomSelect() {
const selectTrigger = document.getElementById('selectTrigger');
const customSelectContainer = document.getElementById('customSelectContainer');
const countrySearchInput = document.getElementById('countrySearchInput');
if (!selectTrigger || !customSelectContainer) return;
// Toggle dropdown on click
selectTrigger.addEventListener('click', (e) => {
e.stopPropagation();
const isOpen = customSelectContainer.classList.toggle('is-open');
selectTrigger.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
if (isOpen) {
if (countrySearchInput) {
countrySearchInput.value = '';
renderCustomOptions(allCountries);
countrySearchInput.focus();
}
}
});
// Filter list on search input
if (countrySearchInput) {
countrySearchInput.addEventListener('input', (e) => {
const query = e.target.value.toLowerCase().trim();
const filtered = allCountries.filter(c =>
c.name.toLowerCase().includes(query) ||
c.countryCode.toLowerCase().includes(query)
);
renderCustomOptions(filtered);
});
}
// Close dropdown when clicking outside
document.addEventListener('click', (e) => {
if (!customSelectContainer.contains(e.target)) {
customSelectContainer.classList.remove('is-open');
selectTrigger.setAttribute('aria-expanded', 'false');
}
});
}
// Fetch all available countries from Nager.Date API with local fallback
async function fetchAndInitCountries() {
const fallbackCountries = [
{ countryCode: "BJ", name: "Bénin" },
{ countryCode: "CI", name: "Côte d'Ivoire" },
{ countryCode: "CD", name: "Congo (RDC)" },
{ countryCode: "CG", name: "Congo" },
{ countryCode: "GA", name: "Gabon" },
{ countryCode: "GH", name: "Ghana" },
{ countryCode: "KE", name: "Kenya" },
{ countryCode: "MA", name: "Maroc" },
{ countryCode: "MG", name: "Madagascar" },
{ countryCode: "NE", name: "Niger" },
{ countryCode: "NG", name: "Nigeria" },
{ countryCode: "SC", name: "Seychelles" },
{ countryCode: "SN", name: "Sénégal" },
{ countryCode: "TN", name: "Tunisie" },
{ countryCode: "ZA", name: "Afrique du Sud" },
{ countryCode: "BE", name: "Belgique" },
{ countryCode: "CH", name: "Suisse" },
{ countryCode: "DE", name: "Allemagne" },
{ countryCode: "ES", name: "Espagne" },
{ countryCode: "FR", name: "France" },
{ countryCode: "GB", name: "Royaume-Uni" },
{ countryCode: "IT", name: "Italie" },
{ countryCode: "LU", name: "Luxembourg" },
{ countryCode: "NL", name: "Pays-Bas" },
{ countryCode: "PT", name: "Portugal" },
{ countryCode: "BR", name: "Brésil" },
{ countryCode: "CA", name: "Canada" },
{ countryCode: "MX", name: "Mexique" },
{ countryCode: "US", name: "États-Unis" },
{ countryCode: "AU", name: "Australie" },
{ countryCode: "CN", name: "Chine" },
{ countryCode: "JP", name: "Japon" },
{ countryCode: "SG", name: "Singapour" }
];
try {
const response = await fetch('https://date.nager.at/api/v3/AvailableCountries');
if (!response.ok) throw new Error('Network error');
const apiCountries = await response.json();
// Map and merge French names for existing fallback countries
allCountries = apiCountries.map(ac => {
const matched = fallbackCountries.find(fc => fc.countryCode.toUpperCase() === ac.countryCode.toUpperCase());
return {
countryCode: ac.countryCode,
name: matched ? matched.name : ac.name
};
});
} catch (err) {
console.warn('API error fetching countries, using local fallback:', err);
allCountries = [...fallbackCountries];
}
// Sort alphabetically by name
allCountries.sort((a, b) => a.name.localeCompare(b.name, 'fr'));
// Populate native and custom UIs
populateNativeSelect(allCountries);
initCustomSelect();
// Select default Bénin (BJ) or first available
const defaultSelection = allCountries.find(c => c.countryCode === 'BJ') || allCountries[0];
if (defaultSelection) {
selectCountry(defaultSelection.countryCode);
}
}