diff options
Diffstat (limited to 'public/js/calendar.js')
-rw-r--r-- | public/js/calendar.js | 277 |
1 files changed, 10 insertions, 267 deletions
diff --git a/public/js/calendar.js b/public/js/calendar.js index 154cdfd..fb26e80 100644 --- a/public/js/calendar.js +++ b/public/js/calendar.js | |||
@@ -1,19 +1,3 @@ | |||
1 | /* // à utiliser plus tard? | ||
2 | const current_view = calendar.view; | ||
3 | switch(current_view.type){ | ||
4 | case 'dayGridMonth': | ||
5 | console.log('mois'); | ||
6 | break; | ||
7 | case 'timeGridWeek': | ||
8 | console.log('semaine'); | ||
9 | break; | ||
10 | case 'timeGridDay': | ||
11 | console.log('jour'); | ||
12 | break; | ||
13 | default: | ||
14 | console.log('erreur'); | ||
15 | }*/ | ||
16 | |||
17 | document.addEventListener('DOMContentLoaded', function(){ | 1 | document.addEventListener('DOMContentLoaded', function(){ |
18 | const calendarEl = document.getElementById('calendar'); | 2 | const calendarEl = document.getElementById('calendar'); |
19 | let selected_start_string = null; | 3 | let selected_start_string = null; |
@@ -50,90 +34,13 @@ document.addEventListener('DOMContentLoaded', function(){ | |||
50 | nowIndicator: true, // barre rouge pour maintenant | 34 | nowIndicator: true, // barre rouge pour maintenant |
51 | 35 | ||
52 | select: function(info){ | 36 | select: function(info){ |
53 | const aside = document.querySelector('aside'); | 37 | selected_start_string = info.startStr; // variable "globale" |
54 | let checked = ''; | ||
55 | let input = 'datetime-local'; | ||
56 | |||
57 | // on veut des chaines de la forme 2025-05-20T07:05 | ||
58 | // il faut retirer les secondes et le fuseau horaire du format ISO, c'est chiant | ||
59 | // on enverra par contre une chaine ISO au serveur pour avoir un enregistrement correct | ||
60 | |||
61 | let start_value; | ||
62 | let end_value; | ||
63 | const end = new Date(info.endStr); | ||
64 | |||
65 | console.log(info.startStr); | ||
66 | console.log(info.endStr); | ||
67 | |||
68 | if(calendar.view.type == 'dayGridMonth'){ | ||
69 | start_value = info.startStr + 'T10:00'; | ||
70 | end.setDate(end.getDate() - 1); // jour de fin modifié pour ne pas faire bizarre pour l'utilisateur | ||
71 | end.setHours(11); | ||
72 | end_value = end.toISOString().split('T')[0] + 'T11:00'; | ||
73 | } | ||
74 | else if(calendar.view.type == 'timeGridWeek' || calendar.view.type == 'timeGridDay'){ | ||
75 | const start_array = info.startStr.split("T"); | ||
76 | const end_array = info.endStr.split("T"); | ||
77 | |||
78 | // clic sur la ligne "Journée", = 'dayGridMonth' | ||
79 | if(start_array.length == 1){ | ||
80 | checked = 'checked'; | ||
81 | input = 'date'; | ||
82 | start_value = info.startStr; | ||
83 | end.setDate(end.getDate() - 1); | ||
84 | end_value = end.toISOString().split('T')[0]; | ||
85 | } | ||
86 | else if(start_array.length == 2){ | ||
87 | start_value = start_array[0] + "T" + start_array[1].substr(0,5); // format 2025-06-12T10:00 | ||
88 | end_value = end_array[0] + "T" + end_array[1].substr(0,5); | ||
89 | } | ||
90 | else{ | ||
91 | console.log('pas censé se produire'); | ||
92 | return; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | selected_start_string = start_value; // variable "globale" | ||
97 | |||
98 | const aside_content = `<div class="form_event"> | ||
99 | <div class="event_title_box"> | ||
100 | <h2>Nouvel évènement</h2> | ||
101 | </div> | ||
102 | <div class=""> | ||
103 | <label for="event_title">Nom</label> | ||
104 | <input type="text" id="event_title"> | ||
105 | </div> | ||
106 | <div class=""> | ||
107 | <label for="event_description">Description</label> | ||
108 | <textarea id="event_description" cols="27"></textarea> | ||
109 | </div> | ||
110 | <div class=""> | ||
111 | <input type="checkbox" id="event_all_day" class="event_all_day" ` + checked + `> | ||
112 | <label for="event_all_day">Journée entière</label> | ||
113 | </div> | ||
114 | <div class=""> | ||
115 | <label for="event_start">Début</label> | ||
116 | <input type="` + input + `" id="event_start" value="` + start_value + `"> | ||
117 | </div> | ||
118 | <div class=""> | ||
119 | <label for="event_end">Fin</label> | ||
120 | <input type="` + input + `" id="event_end" value="` + end_value + `"> | ||
121 | </div> | ||
122 | <div class=""> | ||
123 | <label for="event_color">Couleur</label> | ||
124 | <input type="color" id="event_color" value="#3788D8"> | ||
125 | </div> | ||
126 | <button class="submit_new_event">Enregistrer</button> | ||
127 | <button class="event_close_button">Annuler</button> | ||
128 | </div>`; | ||
129 | aside.innerHTML = aside_content; | ||
130 | calendar.updateSize(); | ||
131 | }, | 38 | }, |
132 | //unselect: function(event, view){}, | 39 | //unselect: function(event, view){}, |
40 | |||
133 | eventClick: function(info){ | 41 | eventClick: function(info){ |
134 | const aside = document.querySelector('aside'); | 42 | const aside = document.querySelector('aside'); |
135 | const checked = info.event.allDay ? 'checked' : ''; | 43 | const checked = info.event.allDay ? 'checked' : ''; |
136 | const input = info.event.allDay ? 'date' : 'datetime-local'; | ||
137 | 44 | ||
138 | // change des objets Date en chaînes compatibles avec les input | 45 | // change des objets Date en chaînes compatibles avec les input |
139 | function formatDate(date){ | 46 | function formatDate(date){ |
@@ -148,38 +55,14 @@ document.addEventListener('DOMContentLoaded', function(){ | |||
148 | const formated_start = formatDate(info.event.start); | 55 | const formated_start = formatDate(info.event.start); |
149 | const formated_end = formatDate(info.event.allDay ? getEndMinusOneDay(info.event.end) : info.event.end, info.event.allDay); | 56 | const formated_end = formatDate(info.event.allDay ? getEndMinusOneDay(info.event.end) : info.event.end, info.event.allDay); |
150 | 57 | ||
151 | const aside_content = `<div class="form_event"> | 58 | const aside_content = `<div class="event" style="border-color: ` + info.event.backgroundColor +`;"> |
152 | <div class="event_title_box"> | 59 | <h3>` + info.event.title + `</h3> |
153 | <h2>Modifier un évènement</h2> | 60 | <p><i>` + info.event.extendedProps.description + `</i></p> |
154 | </div> | 61 | <p>Journée entière: ` + (checked ? 'oui' : 'non') + `</p> |
155 | <div class=""> | 62 | <p>Début: ` + formated_start + `</p> |
156 | <label for="event_title">Nom</label> | 63 | <p>Fin: ` + formated_end + `</p> |
157 | <input type="text" id="event_title" value="` + info.event.title + `"> | 64 | </div>`; |
158 | <input type="hidden" id="event_id" value="` + info.event.id + `"> | 65 | |
159 | </div> | ||
160 | <div class=""> | ||
161 | <label for="event_description">Description</label> | ||
162 | <textarea id="event_description" cols="27">` + info.event.extendedProps.description + `</textarea> | ||
163 | </div> | ||
164 | <div class=""> | ||
165 | <input type="checkbox" id="event_all_day" class="event_all_day" ` + checked + `> | ||
166 | <label for="event_all_day">Journée entière</label> | ||
167 | </div> | ||
168 | <div class=""> | ||
169 | <label for="event_start">Début</label> | ||
170 | <input type="` + input + `" id="event_start" value="` + formated_start + `"> | ||
171 | </div> | ||
172 | <div class=""> | ||
173 | <label for="event_end">Fin</label> | ||
174 | <input type="` + input + `" id="event_end" value="` + formated_end + `"> | ||
175 | </div> | ||
176 | <div class=""> | ||
177 | <label for="event_color">Couleur</label> | ||
178 | <input type="color" id="event_color" value="` + info.event.backgroundColor + `"> | ||
179 | </div> | ||
180 | <button class="submit_update_event">Modifier</button> | ||
181 | <button class="event_close_button">Annuler</button> | ||
182 | </div>`; | ||
183 | aside.innerHTML = aside_content; | 66 | aside.innerHTML = aside_content; |
184 | calendar.updateSize(); | 67 | calendar.updateSize(); |
185 | }, | 68 | }, |
@@ -198,137 +81,6 @@ document.addEventListener('DOMContentLoaded', function(){ | |||
198 | calendar.updateSize(); | 81 | calendar.updateSize(); |
199 | } | 82 | } |
200 | 83 | ||
201 | function submitEvent(new_event){ | ||
202 | const event_title = document.getElementById('event_title').value; | ||
203 | const event_description = document.getElementById('event_description').value; | ||
204 | const event_all_day = document.getElementById('event_all_day').checked; | ||
205 | let event_start = document.getElementById('event_start').value; | ||
206 | let event_end = document.getElementById('event_end').value; | ||
207 | const event_color = document.getElementById('event_color').value; // #3788d8 par défaut | ||
208 | const event_id = new_event ? '' : document.getElementById('event_id').value; | ||
209 | |||
210 | if(event_title.length !== 0 && event_start.length !== 0 && event_end.length !== 0 && event_color.length !== 0 | ||
211 | && (new_event || event_id.length !== 0)) | ||
212 | { | ||
213 | if(event_all_day){ | ||
214 | // on remet le jour de fin exclu | ||
215 | const tmp_object = new Date(event_end); | ||
216 | tmp_object.setDate(tmp_object.getDate() + 1); | ||
217 | event_end = tmp_object.toISOString().split('T')[0]; | ||
218 | } | ||
219 | else{ | ||
220 | event_start = new Date(event_start).toISOString(); | ||
221 | event_end = new Date(event_end).toISOString(); | ||
222 | } | ||
223 | console.log(event_end); | ||
224 | |||
225 | if(event_start > event_end || (!event_all_day && event_start == event_end)){ | ||
226 | return; | ||
227 | } | ||
228 | |||
229 | // création | ||
230 | if(new_event){ | ||
231 | const event = { | ||
232 | title: event_title, | ||
233 | description: event_description, | ||
234 | allDay: event_all_day, | ||
235 | start: event_start, | ||
236 | end: event_end, | ||
237 | color: event_color | ||
238 | }; | ||
239 | |||
240 | fetch('../src/post-ajax.php', { | ||
241 | method: 'POST', | ||
242 | headers: { | ||
243 | 'Content-Type': 'application/json', | ||
244 | }, | ||
245 | body: JSON.stringify(event), | ||
246 | }) | ||
247 | .then(response => response.json()) | ||
248 | .then(data => { | ||
249 | if(data.success){ | ||
250 | event.id = data.id; | ||
251 | calendar.addEvent(event); | ||
252 | hideModal(); | ||
253 | } | ||
254 | }) | ||
255 | .catch((error) => { | ||
256 | console.error('Error:', error); | ||
257 | }); | ||
258 | |||
259 | } | ||
260 | // modification | ||
261 | else{ | ||
262 | const event = calendar.getEventById(event_id); | ||
263 | |||
264 | if(event){ | ||
265 | const event_copy = { | ||
266 | id: parseInt(event.id), | ||
267 | description: event_description, | ||
268 | title: event_title, | ||
269 | allDay: event_all_day, | ||
270 | start: event_start, | ||
271 | end: event_end, | ||
272 | color: event_color | ||
273 | }; | ||
274 | |||
275 | fetch('../src/post-ajax.php', { | ||
276 | method: 'POST', | ||
277 | headers: { | ||
278 | 'Content-Type': 'application/json', | ||
279 | }, | ||
280 | body: JSON.stringify(event_copy), | ||
281 | }) | ||
282 | .then(response => response.json()) | ||
283 | .then(data => { | ||
284 | if(data.success){ | ||
285 | event.setProp('title', event_title); | ||
286 | event.setExtendedProp('description', event_description); | ||
287 | event.setAllDay(event_all_day); | ||
288 | event.setStart(event_start); | ||
289 | event.setEnd(event_end); | ||
290 | event.setProp('color', event_color); | ||
291 | hideModal(); | ||
292 | } | ||
293 | }) | ||
294 | .catch((error) => { | ||
295 | console.error('Error:', error); | ||
296 | }); | ||
297 | } | ||
298 | else{ | ||
299 | console.log("Événement non trouvé !"); | ||
300 | } | ||
301 | } | ||
302 | } | ||
303 | else{ | ||
304 | // notif input vide | ||
305 | console.log('erreur: input vide'); | ||
306 | } | ||
307 | } | ||
308 | |||
309 | function checkAllDay(){ | ||
310 | const event_start_input = document.getElementById('event_start'); | ||
311 | const event_end_input = document.getElementById('event_end'); | ||
312 | |||
313 | const start = event_start_input.value; | ||
314 | const end = event_end_input.value; | ||
315 | |||
316 | if(document.getElementById('event_all_day').checked){ | ||
317 | event_start_input.type = 'date'; | ||
318 | event_end_input.type = 'date'; | ||
319 | |||
320 | event_start_input.value = start.split('T')[0]; | ||
321 | event_end_input.value = end.split('T')[0]; | ||
322 | } | ||
323 | else{ | ||
324 | event_start_input.type = 'datetime-local'; | ||
325 | event_end_input.type = 'datetime-local'; | ||
326 | |||
327 | event_start_input.value = start + 'T10:00'; | ||
328 | event_end_input.value = end + 'T11:00'; | ||
329 | } | ||
330 | } | ||
331 | |||
332 | document.addEventListener('keydown', function(event){ | 84 | document.addEventListener('keydown', function(event){ |
333 | if(event.key === 'Escape') { | 85 | if(event.key === 'Escape') { |
334 | hideModal(); | 86 | hideModal(); |
@@ -340,15 +92,6 @@ document.addEventListener('DOMContentLoaded', function(){ | |||
340 | if(event.target.classList.contains('event_close_button')){ | 92 | if(event.target.classList.contains('event_close_button')){ |
341 | hideModal(); | 93 | hideModal(); |
342 | } | 94 | } |
343 | else if(event.target.classList.contains('event_all_day')){ | ||
344 | checkAllDay(); | ||
345 | } | ||
346 | else if(event.target.classList.contains('submit_new_event')){ | ||
347 | submitEvent(true); | ||
348 | } | ||
349 | else if(event.target.classList.contains('submit_update_event')){ | ||
350 | submitEvent(false); | ||
351 | } | ||
352 | }); | 95 | }); |
353 | 96 | ||
354 | calendar.render(); | 97 | calendar.render(); |