From e3a42c8342bba0db15e2ca9a78911121e5d539da Mon Sep 17 00:00:00 2001 From: polo Date: Thu, 29 Jan 2026 22:51:19 +0100 Subject: =?UTF-8?q?classe=20CalendarModalView,=20contr=C3=B4les=20getEleme?= =?UTF-8?q?ntOrThrow=20et=20assertElementType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/js/CalendarModalView.js | 120 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 public/js/CalendarModalView.js (limited to 'public/js/CalendarModalView.js') diff --git a/public/js/CalendarModalView.js b/public/js/CalendarModalView.js new file mode 100644 index 0000000..a7afba9 --- /dev/null +++ b/public/js/CalendarModalView.js @@ -0,0 +1,120 @@ +// js/CalendarModalView.js + +class CalendarModalView { + constructor(data) { + this.modal = ''; + if (data.mode !== 'show' && data.mode !== 'new' && data.mode !== 'edit') { + throw new Error("unknown mode in CalendarModal's constructor"); + } + this.data = data; + } + getView() { + switch (this.data.mode) { + case 'show': + this.makeShowView(); + break; + case 'new': + case 'edit': + this.makeEditView(); + break; + default: + throw new Error("unknown mode set in CalendarModal"); + } + return this.modal; + } + makeShowView() { + if (this.data.mode !== 'show') { + throw new Error(''); + } + if (this.data.all_day) { + this.data.end.setDate(this.data.end.getDate() - 1); // jour de fin modifié pour ne pas faire bizarre pour l'utilisateur + } + this.modal += `
+

` + this.escapeHtml(this.data.title) + `

+

` + this.escapeHtml(this.data.description) + `

`; + // allDay un jour + if (this.data.all_day && (this.data.start.getTime() === this.data.end.getTime())) { // comparaison des timestamps + this.modal += `

le ` + this.escapeHtml(this.displayDate(this.data.start)) + `

`; // affichage simplifié évènement d'un jour + } + // allDay plusieurs jours + else if (this.data.all_day) { + this.modal += `

du ` + this.escapeHtml(this.displayDate(this.data.start)) + `
+ au ` + this.escapeHtml(this.displayDate(this.data.end)) + `

`; + } + // non allDay + else { + this.modal += `

du ` + this.escapeHtml(this.displayDate(this.data.start)) + `
+ à ` + this.escapeHtml(this.displayHour(this.data.start)) + `

+

au ` + this.escapeHtml(this.displayDate(this.data.end)) + `
+ à ` + this.escapeHtml(this.displayHour(this.data.end)) + `

`; + } + this.modal += ` +
`; + } + displayDate(date) { + return date.getDate().toString().padStart(2, '0') + '/' + (date.getMonth() + 1).toString().padStart(2, '0') + '/' + date.getFullYear(); + } + displayHour(date) { + return date.getHours().toString().padStart(2, '0') + 'h' + date.getMinutes().toString().padStart(2, '0'); + } + makeEditView() { + if (this.data.mode === 'show') { + throw new Error(''); + } + if (this.data.all_day) { + this.data.end.setDate(this.data.end.getDate() - 1); // jour de fin modifié pour ne pas faire bizarre pour l'utilisateur + } + const title = this.data.mode === 'edit' ? this.data.title : ''; + const id = this.data.mode === 'edit' ? this.data.id : ''; + const description = this.data.mode === 'edit' ? this.data.description : ''; + const color = this.data.mode === 'edit' ? this.data.color : '#3788D8'; // bleu par défaut + this.modal += `
+
+

` + (this.data.mode === 'edit' ? "Modifier un" : "Nouvel") + ` évènement

+
+
+ + + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
` + + (this.data.mode === 'edit' + ? '' + : '') + + ` +
`; + } + makeInputValue(date) { + return date.getFullYear() + '-' + (date.getMonth() + 1).toString().padStart(2, '0') + '-' + date.getDate().toString().padStart(2, '0') + + (this.data.all_day ? '' : 'T' + date.getHours().toString().padStart(2, '0') + ':' + date.getMinutes().toString().padStart(2, '0')); + } + escapeHtml(html) { + if (html === null || html === undefined) + return ''; + return String(html) + .replace(/&/g, "&") // & en premier + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); + } +} -- cgit v1.2.3