Files
GF/js/indices.js
T

795 lines
17 KiB
JavaScript
Raw Normal View History

"use strict";
/*
* =========================================================
* CONFIGURATION
* =========================================================
*/
const API_URL = "../php/api_list_full_table_indice.php";
/*
* API de création.
*
* A créer côté PHP.
*/
const API_CREATE_URL = "../php/api_create_indice.php";
const tableauIndices = document.getElementById("tableauIndices");
const compteur = document.getElementById("compteur");
const message = document.getElementById("message");
const btnActualiser = document.getElementById("btnActualiser");
const btnNouveau = document.getElementById("btnNouveau");
const filtreIndice = document.getElementById("filtreIndice");
/*
* =========================================================
* MODAL
* =========================================================
*/
const modalNouvelEnregistrement = document.getElementById(
"modalNouvelEnregistrement",
);
const formNouvelEnregistrement = document.getElementById(
"formNouvelEnregistrement",
);
const btnFermerModal = document.getElementById("btnFermerModal");
const btnAnnuler = document.getElementById("btnAnnuler");
const btnSauvegarder = document.getElementById("btnSauvegarder");
const messageNouveau = document.getElementById("messageNouveau");
/*
* =========================================================
* CHAMPS FORMULAIRE
* =========================================================
*/
const nouvelIndice = document.getElementById("nouvelIndice");
const nouveauNom = document.getElementById("nouveauNom");
const nouvelIsin = document.getElementById("nouvelIsin");
const nouveauSymbole = document.getElementById("nouveauSymbole");
const nouvelActif = document.getElementById("nouvelActif");
const nouvelAfficher = document.getElementById("nouvelAfficher");
/*
* =========================================================
* DONNEES
* =========================================================
*/
let tousLesEnregistrements = [];
/*
* =========================================================
* AFFICHER MESSAGE
* =========================================================
*/
function afficherMessage(texte, type = "info") {
message.textContent = texte;
message.classList.remove(
"hidden",
"bg-emerald-900/50",
"border-emerald-700",
"text-emerald-300",
"bg-rose-900/50",
"border-rose-700",
"text-rose-300",
"bg-slate-900",
"border-slate-700",
"text-slate-300",
);
2026-08-20 11:14:25 +00:00
message.classList.add("border");
2026-08-20 16:01:58 +00:00
if (type === "success") {
message.classList.add(
"bg-emerald-900/50",
"border-emerald-700",
"text-emerald-300",
);
} else if (type === "error") {
message.classList.add("bg-rose-900/50", "border-rose-700", "text-rose-300");
} else {
message.classList.add("bg-slate-900", "border-slate-700", "text-slate-300");
}
}
2026-08-20 16:01:58 +00:00
/*
* =========================================================
* CACHER MESSAGE
* =========================================================
*/
function cacherMessage() {
message.classList.add("hidden");
}
2026-08-20 11:14:25 +00:00
/*
* =========================================================
* ECHAPPER HTML
* =========================================================
*/
2026-08-20 16:01:58 +00:00
function echapperHTML(valeur) {
if (valeur === null || valeur === undefined) {
return "";
}
2026-08-20 16:01:58 +00:00
return String(valeur)
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
2026-08-20 11:14:25 +00:00
/*
* =========================================================
* CHECKBOX
* =========================================================
*/
2026-08-20 16:01:58 +00:00
function creerCheckbox(valeur) {
const checked = Number(valeur) === 1 || valeur === true || valeur === "1";
2026-08-20 16:01:58 +00:00
return `
<div class="flex justify-center items-center">
2026-08-20 16:01:58 +00:00
<input
type="checkbox"
${checked ? "checked" : ""}
disabled
class="w-5 h-5 accent-emerald-500 cursor-not-allowed opacity-90"
/>
</div>
`;
2026-08-20 07:56:32 +00:00
}
/*
* =========================================================
* CREER UNE LIGNE
* =========================================================
*/
2026-08-20 16:01:58 +00:00
function creerLigne(enregistrement) {
return `
<tr
class="hover:bg-slate-700/30 transition-colors"
>
2026-08-20 16:01:58 +00:00
<td
class="px-4 py-3 text-sm text-slate-500 whitespace-nowrap"
>
${echapperHTML(enregistrement.id)}
</td>
2026-08-20 07:56:32 +00:00
<td
class="px-4 py-3 whitespace-nowrap"
>
<span
class="inline-flex items-center
px-2.5 py-1
rounded-lg
bg-slate-700
border border-slate-600
text-xs font-semibold
text-slate-200"
>
${echapperHTML(enregistrement.indice)}
</span>
</td>
2026-08-20 16:01:58 +00:00
2026-08-20 11:14:25 +00:00
<td
class="px-4 py-3
text-sm font-medium
text-white
whitespace-nowrap"
>
${echapperHTML(enregistrement.nom)}
</td>
2026-08-20 16:01:58 +00:00
2026-08-20 11:14:25 +00:00
<td
class="px-4 py-3
text-sm text-slate-300
font-mono
whitespace-nowrap"
>
${echapperHTML(enregistrement.isin)}
</td>
2026-08-20 16:01:58 +00:00
<td
class="px-4 py-3
text-sm text-slate-300
font-semibold
whitespace-nowrap"
>
${echapperHTML(enregistrement.symbole)}
</td>
2026-08-20 16:01:58 +00:00
2026-08-20 11:14:25 +00:00
<td class="px-4 py-3 text-center">
${creerCheckbox(enregistrement.actif)}
</td>
2026-08-20 16:01:58 +00:00
2026-08-20 11:14:25 +00:00
<td class="px-4 py-3 text-center">
${creerCheckbox(enregistrement.afficher)}
</td>
2026-08-20 16:01:58 +00:00
<td
class="px-4 py-3
text-xs text-slate-400
whitespace-nowrap"
>
${echapperHTML(enregistrement.updated_at)}
</td>
2026-08-20 16:01:58 +00:00
<td
class="px-4 py-3
text-xs text-slate-500
whitespace-nowrap"
>
${echapperHTML(enregistrement.created_at)}
</td>
2026-08-20 16:01:58 +00:00
</tr>
`;
}
2026-08-20 16:01:58 +00:00
/*
* =========================================================
* CONSTRUIRE FILTRES
* =========================================================
*/
function construireFiltres() {
const indices = [
...new Set(
tousLesEnregistrements
.map(function (enregistrement) {
return enregistrement.indice;
})
.filter(function (indice) {
return (
indice !== null &&
indice !== undefined &&
String(indice).trim() !== ""
);
}),
),
];
indices.sort(function (a, b) {
return String(a).localeCompare(String(b), "fr", {
sensitivity: "base",
});
});
2026-08-20 16:01:58 +00:00
const filtreActuel = filtreIndice.value;
2026-08-20 16:01:58 +00:00
filtreIndice.innerHTML = "";
2026-08-20 16:01:58 +00:00
/*
* Tout
*/
2026-08-20 16:01:58 +00:00
const optionTout = document.createElement("option");
2026-08-20 16:01:58 +00:00
optionTout.value = "tout";
optionTout.textContent = "Tout";
2026-08-20 16:01:58 +00:00
filtreIndice.appendChild(optionTout);
2026-08-20 16:01:58 +00:00
/*
* Afficher
*/
2026-08-20 16:01:58 +00:00
const optionAfficher = document.createElement("option");
2026-08-20 07:56:32 +00:00
optionAfficher.value = "afficher";
optionAfficher.textContent = "Afficher";
2026-08-20 16:01:58 +00:00
filtreIndice.appendChild(optionAfficher);
2026-08-20 16:01:58 +00:00
/*
* Inactif
*/
2026-08-20 16:01:58 +00:00
const optionInactif = document.createElement("option");
2026-08-20 16:01:58 +00:00
optionInactif.value = "inactif";
optionInactif.textContent = "Inactif";
2026-08-20 16:01:58 +00:00
filtreIndice.appendChild(optionInactif);
2026-08-20 16:01:58 +00:00
/*
* Indices
*/
2026-08-20 16:01:58 +00:00
indices.forEach(function (indice) {
const option = document.createElement("option");
2026-08-20 16:01:58 +00:00
option.value = "indice:" + indice;
option.textContent = indice;
2026-08-20 16:01:58 +00:00
filtreIndice.appendChild(option);
});
2026-08-20 16:01:58 +00:00
/*
* Restaurer filtre
*/
2026-08-20 16:01:58 +00:00
const optionExiste = Array.from(filtreIndice.options).some(function (option) {
return option.value === filtreActuel;
});
2026-08-20 16:01:58 +00:00
if (optionExiste) {
filtreIndice.value = filtreActuel;
} else {
filtreIndice.value = "tout";
}
}
2026-08-20 16:01:58 +00:00
/*
* =========================================================
* APPLIQUER FILTRE
* =========================================================
*/
function appliquerFiltre() {
const filtre = filtreIndice.value;
let enregistrementsFiltres;
/*
* Tout
*/
if (filtre === "tout") {
enregistrementsFiltres = tousLesEnregistrements;
} else if (filtre === "afficher") {
/*
* Afficher
*/
enregistrementsFiltres = tousLesEnregistrements.filter(
function (enregistrement) {
return (
Number(enregistrement.afficher) === 1 ||
enregistrement.afficher === true ||
enregistrement.afficher === "1"
);
},
);
} else if (filtre === "inactif") {
/*
* Inactif
*/
enregistrementsFiltres = tousLesEnregistrements.filter(
function (enregistrement) {
return !(
Number(enregistrement.actif) === 1 ||
enregistrement.actif === true ||
enregistrement.actif === "1"
);
},
);
} else if (filtre.startsWith("indice:")) {
/*
* Indice
*/
const indiceRecherche = filtre.substring("indice:".length);
enregistrementsFiltres = tousLesEnregistrements.filter(
function (enregistrement) {
return (
String(enregistrement.indice).toLowerCase() ===
String(indiceRecherche).toLowerCase()
);
},
);
} else {
enregistrementsFiltres = tousLesEnregistrements;
}
2026-08-20 16:01:58 +00:00
afficherDonneesFiltrees(enregistrementsFiltres);
}
2026-08-20 16:01:58 +00:00
/*
* =========================================================
* AFFICHER DONNEES FILTREES
* =========================================================
*/
2026-08-20 16:01:58 +00:00
function afficherDonneesFiltrees(enregistrements) {
tableauIndices.innerHTML = "";
2026-08-20 16:01:58 +00:00
if (!Array.isArray(enregistrements) || enregistrements.length === 0) {
compteur.textContent = "0 enregistrement";
2026-08-20 16:01:58 +00:00
tableauIndices.innerHTML = `
<tr>
2026-08-20 16:01:58 +00:00
<td
colspan="9"
class="px-6 py-10 text-center
text-sm text-slate-500"
>
Aucun enregistrement correspondant
au filtre sélectionné.
</td>
2026-08-20 16:01:58 +00:00
</tr>
`;
2026-08-20 16:01:58 +00:00
return;
}
2026-08-20 16:01:58 +00:00
/*
* =======================================================
* TRI PAR ID CROISSANT
* =======================================================
*/
2026-08-20 16:01:58 +00:00
const donneesTriees = [...enregistrements].sort(function (a, b) {
return Number(a.id) - Number(b.id);
});
2026-08-20 16:01:58 +00:00
let html = "";
2026-08-20 16:01:58 +00:00
donneesTriees.forEach(function (enregistrement) {
html += creerLigne(enregistrement);
});
2026-08-20 16:01:58 +00:00
tableauIndices.innerHTML = html;
2026-08-20 16:01:58 +00:00
compteur.textContent =
donneesTriees.length +
(donneesTriees.length > 1 ? " enregistrements" : " enregistrement");
}
2026-08-20 16:01:58 +00:00
/*
* =========================================================
* AFFICHER DONNEES
* =========================================================
*/
2026-08-20 16:01:58 +00:00
function afficherDonnees(data) {
tousLesEnregistrements = Array.isArray(data) ? data : [];
2026-08-20 16:01:58 +00:00
construireFiltres();
2026-08-20 16:01:58 +00:00
appliquerFiltre();
}
2026-08-20 16:01:58 +00:00
/*
* =========================================================
* CHARGER DONNEES
* =========================================================
*/
2026-08-20 16:01:58 +00:00
async function chargerDonnees() {
btnActualiser.disabled = true;
2026-08-20 16:01:58 +00:00
btnActualiser.textContent = "↻ Chargement...";
2026-08-20 16:01:58 +00:00
afficherMessage("Chargement des indices et des actions...", "info");
2026-08-20 16:01:58 +00:00
try {
const response = await fetch(API_URL, {
method: "GET",
2026-08-20 16:01:58 +00:00
credentials: "same-origin",
2026-08-20 16:01:58 +00:00
cache: "no-store",
2026-08-20 16:01:58 +00:00
headers: {
Accept: "application/json",
},
});
2026-08-20 16:01:58 +00:00
if (!response.ok) {
throw new Error("Erreur HTTP " + response.status);
}
2026-08-20 16:01:58 +00:00
const result = await response.json();
2026-08-20 16:01:58 +00:00
if (!result || result.status !== "success") {
throw new Error(
result && result.message
? result.message
: "L'API a retourné une erreur.",
);
}
2026-08-20 16:01:58 +00:00
afficherDonnees(result.data);
2026-08-20 16:01:58 +00:00
cacherMessage();
} catch (error) {
console.error("Erreur chargement indices/actions :", error);
2026-08-20 16:01:58 +00:00
tableauIndices.innerHTML = `
<tr>
2026-08-20 16:01:58 +00:00
<td
colspan="9"
class="px-6 py-10
text-center
text-rose-400"
2026-08-20 16:01:58 +00:00
>
Impossible de charger les données.
</td>
2026-08-20 16:01:58 +00:00
</tr>
2026-08-20 11:14:25 +00:00
`;
compteur.textContent = "Erreur de chargement";
afficherMessage(
"Erreur lors de la récupération des données : " + error.message,
"error",
);
} finally {
btnActualiser.disabled = false;
btnActualiser.textContent = "↻ Actualiser";
2026-08-20 07:56:32 +00:00
}
}
/*
* =========================================================
* OUVRIR MODAL
* =========================================================
*/
2026-08-20 16:01:58 +00:00
function ouvrirModal() {
formNouvelEnregistrement.reset();
2026-08-20 11:14:25 +00:00
/*
* Valeurs par défaut
*/
2026-08-20 16:01:58 +00:00
nouvelActif.checked = true;
nouvelAfficher.checked = true;
2026-08-20 16:01:58 +00:00
messageNouveau.classList.add("hidden");
2026-08-20 16:01:58 +00:00
modalNouvelEnregistrement.classList.remove("hidden");
2026-08-20 07:56:32 +00:00
modalNouvelEnregistrement.classList.add("flex");
2026-08-20 16:01:58 +00:00
setTimeout(function () {
nouvelIndice.focus();
}, 50);
}
2026-08-20 16:01:58 +00:00
/*
* =========================================================
* FERMER MODAL
* =========================================================
*/
2026-08-20 16:01:58 +00:00
function fermerModal() {
modalNouvelEnregistrement.classList.add("hidden");
2026-08-20 11:14:25 +00:00
modalNouvelEnregistrement.classList.remove("flex");
}
2026-08-20 16:01:58 +00:00
/*
* =========================================================
* MESSAGE MODAL
* =========================================================
*/
function afficherMessageNouveau(texte, type = "error") {
messageNouveau.textContent = texte;
messageNouveau.classList.remove(
"hidden",
"bg-rose-900/50",
"border-rose-700",
"text-rose-300",
"bg-emerald-900/50",
"border-emerald-700",
"text-emerald-300",
);
2026-08-20 16:01:58 +00:00
messageNouveau.classList.add("border");
2026-08-20 16:01:58 +00:00
if (type === "success") {
messageNouveau.classList.add(
"bg-emerald-900/50",
"border-emerald-700",
"text-emerald-300",
);
} else {
messageNouveau.classList.add(
"bg-rose-900/50",
"border-rose-700",
"text-rose-300",
);
}
}
2026-08-20 07:56:32 +00:00
/*
* =========================================================
* SAUVEGARDER NOUVEL ENREGISTREMENT
* =========================================================
*/
2026-08-20 16:01:58 +00:00
async function sauvegarderNouvelEnregistrement(event) {
event.preventDefault();
2026-08-20 11:14:25 +00:00
/*
* Lecture des champs
*/
2026-08-20 07:56:32 +00:00
const donnees = {
indice: nouvelIndice.value.trim(),
2026-08-20 16:01:58 +00:00
nom: nouveauNom.value.trim(),
2026-08-20 16:01:58 +00:00
isin: nouvelIsin.value.trim().toUpperCase(),
2026-08-20 16:01:58 +00:00
symbole: nouveauSymbole.value.trim().toUpperCase(),
2026-08-20 07:56:32 +00:00
actif: nouvelActif.checked ? 1 : 0,
2026-08-20 16:01:58 +00:00
afficher: nouvelAfficher.checked ? 1 : 0,
};
2026-08-20 16:01:58 +00:00
/*
* Vérification
*/
2026-08-20 07:56:32 +00:00
if (!donnees.indice || !donnees.nom || !donnees.isin || !donnees.symbole) {
afficherMessageNouveau("Tous les champs texte sont obligatoires.");
2026-08-20 16:01:58 +00:00
2026-08-20 07:56:32 +00:00
return;
}
/*
* Désactivation bouton
*/
2026-08-20 16:01:58 +00:00
btnSauvegarder.disabled = true;
2026-08-20 16:01:58 +00:00
btnSauvegarder.textContent = "Sauvegarde...";
2026-08-20 16:01:58 +00:00
messageNouveau.classList.add("hidden");
2026-08-20 16:01:58 +00:00
try {
const response = await fetch(API_CREATE_URL, {
method: "POST",
2026-08-20 16:01:58 +00:00
credentials: "same-origin",
2026-08-20 08:29:28 +00:00
headers: {
"Content-Type": "application/json",
2026-08-20 16:01:58 +00:00
Accept: "application/json",
},
2026-08-20 16:01:58 +00:00
body: JSON.stringify(donnees),
});
2026-08-20 16:01:58 +00:00
if (!response.ok) {
throw new Error("Erreur HTTP " + response.status);
}
2026-08-20 16:01:58 +00:00
const result = await response.json();
2026-08-20 16:01:58 +00:00
if (!result || result.status !== "success") {
throw new Error(
result && result.message
? result.message
: "Impossible de créer l'enregistrement.",
);
}
2026-08-20 16:01:58 +00:00
/*
* Fermeture
*/
fermerModal();
/*
* Message
*/
afficherMessage("L'enregistrement a été créé avec succès.", "success");
2026-08-20 08:29:28 +00:00
/*
* Rechargement
*/
2026-08-20 16:01:58 +00:00
await chargerDonnees();
} catch (error) {
console.error("Erreur création enregistrement :", error);
2026-08-20 16:01:58 +00:00
afficherMessageNouveau(error.message, "error");
} finally {
btnSauvegarder.disabled = false;
2026-08-20 16:01:58 +00:00
btnSauvegarder.textContent = "Sauvegarder";
}
2026-08-20 07:56:32 +00:00
}
/*
* =========================================================
* EVENEMENTS
* =========================================================
*/
2026-08-20 16:01:58 +00:00
filtreIndice.addEventListener("change", function () {
appliquerFiltre();
});
2026-08-20 16:01:58 +00:00
btnActualiser.addEventListener("click", function () {
chargerDonnees();
});
2026-08-20 16:01:58 +00:00
btnNouveau.addEventListener("click", function () {
ouvrirModal();
});
2026-08-20 16:01:58 +00:00
btnFermerModal.addEventListener("click", function () {
fermerModal();
});
2026-08-20 16:01:58 +00:00
btnAnnuler.addEventListener("click", function () {
fermerModal();
});
2026-08-20 16:01:58 +00:00
formNouvelEnregistrement.addEventListener(
"submit",
sauvegarderNouvelEnregistrement,
);
2026-08-20 16:01:58 +00:00
/*
* Fermer en cliquant sur le fond
*/
2026-08-20 16:01:58 +00:00
modalNouvelEnregistrement.addEventListener("click", function (event) {
if (event.target === modalNouvelEnregistrement) {
fermerModal();
}
});
/*
* Touche Echap
*/
2026-08-20 16:01:58 +00:00
document.addEventListener("keydown", function (event) {
if (
event.key === "Escape" &&
!modalNouvelEnregistrement.classList.contains("hidden")
) {
fermerModal();
2026-08-20 16:01:58 +00:00
}
});
/*
* =========================================================
* INITIALISATION
* =========================================================
*/
document.addEventListener("DOMContentLoaded", function () {
chargerDonnees();
});