// ---------- 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) { var row = document.querySelector('tr[data-var-row="' + varId + '"]'); if (!row) return; var type = row.querySelector('select[name="type"]').value; var value = row.querySelector('input[name="value"]').value; var status = document.getElementById('varSaveStatus' + varId); var 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: body, }) .then(function (r) { if (!r.ok) { window.location.reload(); return; } if (status) { status.textContent = 'Enregistré ✓'; setTimeout(function () { status.textContent = ''; }, 2000); } }) .catch(function () { window.location.reload(); }); } function deleteGlobalVariable(varId, name) { if (!confirm('Supprimer la variable « ' + name + ' » ? Toute condition/action qui la référence encore cessera de fonctionner.')) return; var body = new FormData(); fetch('/game/' + GAME_SLUG + '/variables/' + varId + '/delete', { method: 'POST', headers: { 'X-Requested-With': 'fetch' }, body: body, }) .then(function (r) { if (!r.ok) { window.location.reload(); return; } var row = document.querySelector('tr[data-var-row="' + varId + '"]'); if (row) row.remove(); var body2 = document.getElementById('globalVariablesBody'); var emptyHint = document.getElementById('globalVariablesEmptyHint'); if (body2 && emptyHint) emptyHint.style.display = body2.children.length ? 'none' : ''; }) .catch(function () { window.location.reload(); }); } function createGlobalVariable(event) { event.preventDefault(); var form = event.target; var body = new FormData(form); fetch('/game/' + GAME_SLUG + '/variables/new', { method: 'POST', headers: { 'X-Requested-With': 'fetch' }, body: body, }) .then(function (r) { return r.ok ? r.json() : Promise.reject(); }) .then(function (json) { if (!json.row_html) { window.location.reload(); return; } var tbody = document.getElementById('globalVariablesBody'); tbody.insertAdjacentHTML('beforeend', json.row_html); var 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(function () { window.location.reload(); }); return false; }