From f9a9bf1ae4fac9317fd71f0bcb207544b000e6f1 Mon Sep 17 00:00:00 2001 From: polo Date: Thu, 23 Oct 2025 14:15:01 +0200 Subject: =?UTF-8?q?t=C3=A9l=C3=A9chargement=20d'assets=20dans=20head,=20he?= =?UTF-8?q?ader=20et=20footer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/js/InputFile.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 public/js/InputFile.js (limited to 'public/js/InputFile.js') diff --git a/public/js/InputFile.js b/public/js/InputFile.js new file mode 100644 index 0000000..f5e450c --- /dev/null +++ b/public/js/InputFile.js @@ -0,0 +1,51 @@ +// étendre une classe parente avec InputFile? +class InputFile{ + constructor(name){ + this.name = name; + this.parent = document.getElementById(name); + } + open(){ + this.parent.querySelector('#' + this.name + '_img').classList.add('hidden'); + this.parent.querySelector('#' + this.name + '_input').classList.remove('hidden'); + this.parent.querySelector('#' + this.name + '_open').classList.add('hidden'); + this.parent.querySelector('#' + this.name + '_submit').classList.remove('hidden'); + this.parent.querySelector('#' + this.name + '_cancel').classList.remove('hidden'); + } + close(){ + this.parent.querySelector('#' + this.name + '_img').classList.remove('hidden'); + this.parent.querySelector('#' + this.name + '_input').classList.add('hidden'); + this.parent.querySelector('#' + this.name + '_open').classList.remove('hidden'); + this.parent.querySelector('#' + this.name + '_submit').classList.add('hidden'); + this.parent.querySelector('#' + this.name + '_cancel').classList.add('hidden'); + } + submit(){ + const file = this.parent.querySelector('#' + this.name + '_input').files[0]; + if(!file){ + console.error("Erreur: aucun fichier sélectionné."); + return; + } + const form_data = new FormData(); + form_data.append('file', file); + + fetch('index.php?head_foot_image=' + this.name, { + method: 'POST', // apparemment il faudrait utiliser PUT + body: form_data + }) + .then(response => response.json()) + .then(data => { + if(data.success){ + this.parent.querySelector('#' + this.name + '_img').src = data.location; + this.close(this.name); + } + else{ + console.error("Erreur: le serveur n'a pas enregistré l'image'."); + } + }) + .catch(error => { + console.error('Erreur:', error); + }); + } + cancel(){ + this.close(this.name); + } +} \ No newline at end of file -- cgit v1.2.3