// 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, "'"); } }