diff options
Diffstat (limited to 'public/js/calendar.js')
-rw-r--r-- | public/js/calendar.js | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/public/js/calendar.js b/public/js/calendar.js new file mode 100644 index 0000000..2193272 --- /dev/null +++ b/public/js/calendar.js | |||
@@ -0,0 +1,110 @@ | |||
1 | // js/calendar.js | ||
2 | |||
3 | document.addEventListener('DOMContentLoaded', function(){ | ||
4 | const calendarEl = document.getElementById('calendar'); | ||
5 | let selected_start_string = null; | ||
6 | |||
7 | const calendar = new FullCalendar.Calendar(calendarEl,{ | ||
8 | editable: true, | ||
9 | locale: 'fr', | ||
10 | //timeZone: 'local', // à modifier pour être à l'heure d'un autre pays | ||
11 | initialView: 'dayGridMonth', | ||
12 | headerToolbar:{ | ||
13 | left: 'prev,next today', | ||
14 | center: 'title', | ||
15 | right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek' | ||
16 | //right: 'dayGridMonth,timeGridWeek' | ||
17 | }, | ||
18 | slotMinWidth: 70, | ||
19 | defaultAllDay: false, | ||
20 | |||
21 | // numéros de semaine | ||
22 | //weekNumbers: true, | ||
23 | //weekText: 's', | ||
24 | |||
25 | // vue mois | ||
26 | contentHeight: 600, // après initialisation: calendar.setOption('contentHeight', 650); | ||
27 | //aspectRatio: 1.5, // après initialisation: calendar.setOption('aspectRatio', 1.8); | ||
28 | // pour recalculer la taille au redimensionnement du parent, exécuter: calendar.updateSize() | ||
29 | stickyHeaderDates: true, // garder les en-tête de colonnes lors du scroll | ||
30 | fixedWeekCount: false, // avec false, affiche 4, 5 ou 6 semaines selon le mois | ||
31 | selectable: true, // sélection de jours multiples | ||
32 | navLinks: true, // numéros de jour et de semaines clicables | ||
33 | |||
34 | // vue semaine | ||
35 | slotEventOverlap: true, // superposition (limitée) de deux évènements simultanés | ||
36 | allDayContent: 'Journée', // texte dans la case "toute la journée" | ||
37 | nowIndicator: true, // barre rouge pour maintenant | ||
38 | |||
39 | // params en plus: https://fullcalendar.io/docs/events-json-feed | ||
40 | events: 'index.php?action=get_events', // fichier PHP qui retourne les événements | ||
41 | |||
42 | select: function(info){ | ||
43 | selected_start_string = info.startStr; // variable "globale" | ||
44 | hideModal(); | ||
45 | }, | ||
46 | //unselect: function(event, view){}, | ||
47 | |||
48 | eventClick: function(info){ | ||
49 | const aside = document.querySelector('aside'); | ||
50 | const checked = info.event.allDay ? 'checked' : ''; | ||
51 | |||
52 | // change des objets Date en chaînes compatibles avec les input | ||
53 | function formatDate(date){ | ||
54 | return date.getFullYear() + '-' + (date.getMonth() + 1).toString().padStart(2, '0') + '-' + date.getDate().toString().padStart(2, '0') | ||
55 | + (info.event.allDay ? '' : 'T' + date.getHours().toString().padStart(2, '0') + ':' + date.getMinutes().toString().padStart(2, '0')); | ||
56 | } | ||
57 | function minusOneDay(date){ | ||
58 | date.setDate(date.getDate() - 1); // jour de fin modifié pour ne pas faire bizarre pour l'utilisateur | ||
59 | return date; | ||
60 | } | ||
61 | |||
62 | const start = formatDate(info.event.start); | ||
63 | const start_date = start.split('T')[0]; | ||
64 | const start_hour = (info.event.allDay ? '' : '<br>à ' + start.split('T')[1]).replace(":", "h"); | ||
65 | const formated_start = 'le ' + start_date.split('-')[2] + '/' + start_date.split('-')[1] + '/' + start_date.split('-')[0] + start_hour; | ||
66 | const end = formatDate(info.event.allDay ? minusOneDay(info.event.end) : info.event.end, info.event.allDay); | ||
67 | const end_date = end.split('T')[0]; | ||
68 | const end_hour = (info.event.allDay ? '' : '<br>à ' + end.split('T')[1]).replace(":", "h"); | ||
69 | const formated_end = 'le ' + end_date.split('-')[2] + '/' + end_date.split('-')[1] + '/' + end_date.split('-')[0] + end_hour; | ||
70 | |||
71 | const aside_content = `<div class="event" style="border-color: ` + info.event.backgroundColor +`;"> | ||
72 | <h3>` + info.event.title + `</h3> | ||
73 | <p><i>` + info.event.extendedProps.description + `</i></p> | ||
74 | <p>Journée entière: <br>` + (checked ? 'oui' : 'non') + `</p> | ||
75 | <p>Début: <br>` + formated_start + `</p> | ||
76 | <p>Fin: <br> ` + formated_end + `</p> | ||
77 | </div>`; | ||
78 | |||
79 | aside.innerHTML = aside_content; | ||
80 | calendar.updateSize(); | ||
81 | }, | ||
82 | viewDidMount: function(info){ // déclenché lorsque qu'une nouvelle vue est chargée (mois, semaine...) | ||
83 | if(selected_start_string){ | ||
84 | calendar.gotoDate(new Date(selected_start_string)); | ||
85 | } | ||
86 | }, | ||
87 | //datesSet: function(info){}, // déclenché lorsque des dates affichées sont chargées (= comme viewDidMount + changement de date) | ||
88 | }); | ||
89 | |||
90 | function hideModal(){ | ||
91 | const aside = document.querySelector('aside'); | ||
92 | aside.innerHTML = ''; | ||
93 | calendar.updateSize(); | ||
94 | } | ||
95 | |||
96 | document.addEventListener('keydown', function(event){ | ||
97 | if(event.key === 'Escape') { | ||
98 | hideModal(); | ||
99 | } | ||
100 | }); | ||
101 | |||
102 | // technique de la délégation d'événements pour utiliser un bouton ajouté dynamiquement | ||
103 | document.addEventListener('click', function(event){ | ||
104 | if(event.target.classList.contains('event_close_button')){ | ||
105 | hideModal(); | ||
106 | } | ||
107 | }); | ||
108 | |||
109 | calendar.render(); | ||
110 | }); \ No newline at end of file | ||