confirm_modal.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Confirm Modal Component Logic
  2. let confirmCallback = null;
  3. // Low-level: open the modal assuming confirmMessage is already populated.
  4. function openConfirmModal(callback, options = {}) {
  5. confirmCallback = callback;
  6. const cancelBtn = document.getElementById('confirmCancelBtn');
  7. const confirmBtn = document.getElementById('confirmConfirmBtn');
  8. if (options.onlyClose) {
  9. cancelBtn.classList.add('hidden');
  10. confirmBtn.textContent = 'Cerrar';
  11. confirmBtn.classList.remove('bg-red-500', 'hover:bg-red-600');
  12. confirmBtn.classList.add('bg-blue-500', 'hover:bg-blue-600');
  13. } else {
  14. cancelBtn.classList.remove('hidden');
  15. confirmBtn.textContent = 'Confirmar';
  16. confirmBtn.classList.remove('bg-blue-500', 'hover:bg-blue-600');
  17. confirmBtn.classList.add('bg-red-500', 'hover:bg-red-600');
  18. }
  19. document.getElementById('confirmModal').classList.remove('hidden');
  20. }
  21. // High-level: set a plain-text message and open the modal.
  22. function showCustomConfirm(message, callback, options = {}) {
  23. document.getElementById('confirmMessage').textContent = message;
  24. openConfirmModal(callback, options);
  25. }
  26. function hideConfirmModal() {
  27. document.getElementById('confirmModal').classList.add('hidden');
  28. }
  29. function executeConfirmCallback() {
  30. if (confirmCallback) {
  31. confirmCallback();
  32. confirmCallback = null;
  33. }
  34. hideConfirmModal();
  35. }
  36. // Close modal on Escape key
  37. document.addEventListener('keydown', (event) => {
  38. if (event.key === 'Escape' && !document.getElementById('confirmModal').classList.contains('hidden')) {
  39. hideConfirmModal();
  40. }
  41. });