// ---------- Onglet "Variables" — CRUD en AJAX ---------- // Demande explicite : modifier/supprimer une variable rechargeait toute // la page (formulaires classiques + navigation pjax) — remplacé par de // vrais appels fetch(), sans jamais recharger ni naviguer. // Enregistre automatiquement dès qu'un champ (type/valeur) change — plus // besoin d'un bouton "Enregistrer" séparé (demande explicite : "que la // modification s'enregistre automatiquement"). function saveGlobalVariable(varId) { const row = document.querySelector(`tr[data-var-row="${varId}"]`); if (!row) return; const type = row.querySelector('select[name="type"]').value; const { value } = row.querySelector('input[name="value"]'); const status = document.getElementById(`varSaveStatus${varId}`); const body = new FormData(); body.append('type', type); body.append('value', value); fetch(`/game/${GAME_SLUG}/variables/${varId}/edit`, { method: 'POST', headers: { 'X-Requested-With': 'fetch' }, body, }) .then((r) => { if (!r.ok) { window.location.reload(); return; } if (status) { status.textContent = 'Enregistré ✓'; setTimeout(() => { status.textContent = ''; }, 2000); } }) .catch(() => { window.location.reload(); }); } function deleteGlobalVariable(varId, name) { if (!window.confirm(`Supprimer la variable « ${name} » ? Toute condition/action qui la référence encore cessera de fonctionner.`)) return; const body = new FormData(); fetch(`/game/${GAME_SLUG}/variables/${varId}/delete`, { method: 'POST', headers: { 'X-Requested-With': 'fetch' }, body, }) .then((r) => { if (!r.ok) { window.location.reload(); return; } const row = document.querySelector(`tr[data-var-row="${varId}"]`); if (row) row.remove(); const body2 = document.getElementById('globalVariablesBody'); const emptyHint = document.getElementById('globalVariablesEmptyHint'); if (body2 && emptyHint) emptyHint.style.display = body2.children.length ? 'none' : ''; }) .catch(() => { window.location.reload(); }); } function createGlobalVariable(event) { event.preventDefault(); const form = event.target; const body = new FormData(form); fetch(`/game/${GAME_SLUG}/variables/new`, { method: 'POST', headers: { 'X-Requested-With': 'fetch' }, body, }) .then((r) => (r.ok ? r.json() : Promise.reject(new Error(`HTTP ${r.status}`)))) .then((json) => { if (!json.row_html) { window.location.reload(); return; } const tbody = document.getElementById('globalVariablesBody'); tbody.insertAdjacentHTML('beforeend', json.row_html); const emptyHint = document.getElementById('globalVariablesEmptyHint'); if (emptyHint) emptyHint.style.display = 'none'; form.reset(); form.style.display = 'none'; // même comportement que toggleDashCreate() après création }) .catch(() => { window.location.reload(); }); return false; }