conexiones.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // conexiones.js – Actions for the conexiones partial
  2. // Uses event delegation on document so it works after innerHTML replacements.
  3. var _actionUsername = null;
  4. var _actionIp = null;
  5. // ── CSRF ──────────────────────────────────────────────────────
  6. function _getCsrf() {
  7. var meta = document.querySelector('meta[name="csrf-token"]');
  8. return meta ? meta.getAttribute('content') : '';
  9. }
  10. // ── Modal helpers ────────────────────────────────────────────
  11. function closeAllModals() {
  12. ['modal-block', 'modal-unblock', 'modal-throttle', 'modal-unthrottle', 'modal-reset'].forEach(function(id) {
  13. var el = document.getElementById(id);
  14. if (el) el.classList.add('hidden');
  15. });
  16. }
  17. function _openModal(modalId, subtitleId, username, ip) {
  18. _actionUsername = username;
  19. _actionIp = ip;
  20. var sub = document.getElementById(subtitleId);
  21. if (sub) sub.textContent = username + ' · ' + ip;
  22. var modal = document.getElementById(modalId);
  23. if (modal) modal.classList.remove('hidden');
  24. }
  25. // ── Delay pools loader ────────────────────────────────────────
  26. function loadDelayPools() {
  27. var select = document.getElementById('throttle-pool-select');
  28. if (!select) return;
  29. select.innerHTML = '<option value="">Cargando...</option>';
  30. fetch('/api/delay-pools')
  31. .then(function(r) { return r.json(); })
  32. .then(function(data) {
  33. var pools = data.pools || [];
  34. if (pools.length === 0) {
  35. select.innerHTML = '<option value="">No hay delay pools configurados</option>';
  36. return;
  37. }
  38. select.innerHTML = '';
  39. pools.forEach(function(pool) {
  40. var opt = document.createElement('option');
  41. opt.value = pool.pool_number;
  42. opt.textContent = 'Pool #' + pool.pool_number + ' — clase ' + (pool.class || '?') + ' · ' + (pool.parameters || '?');
  43. select.appendChild(opt);
  44. });
  45. })
  46. .catch(function() {
  47. var s = document.getElementById('throttle-pool-select');
  48. if (s) s.innerHTML = '<option value="">Error al cargar los pools</option>';
  49. });
  50. }
  51. // ── API action ────────────────────────────────────────────────
  52. function _apiAction(url, body) {
  53. fetch(url, {
  54. method: 'POST',
  55. headers: {
  56. 'Content-Type': 'application/json',
  57. 'X-CSRFToken': _getCsrf()
  58. },
  59. body: JSON.stringify(body)
  60. })
  61. .then(function(r) { return r.json(); })
  62. .then(function(data) {
  63. showToast(
  64. data.status === 'success',
  65. data.message || data.error || (data.status === 'success' ? 'Operación exitosa' : 'Error')
  66. );
  67. })
  68. .catch(function() {
  69. showToast(false, 'Error de conexión con el servidor');
  70. });
  71. }
  72. // ── Toast ─────────────────────────────────────────────────────
  73. var _toastTimer = null;
  74. function showToast(success, message) {
  75. var toast = document.getElementById('action-toast');
  76. var icon = document.getElementById('action-toast-icon');
  77. var msg = document.getElementById('action-toast-message');
  78. if (!toast || !icon || !msg) return;
  79. toast.className = toast.className.replace(/bg-\S+|text-\S+/g, '').trim();
  80. if (success) {
  81. toast.classList.add('bg-green-50', 'text-green-800', 'border', 'border-green-200');
  82. icon.className = 'fas fa-circle-check text-green-500 text-lg';
  83. } else {
  84. toast.classList.add('bg-red-50', 'text-red-800', 'border', 'border-red-200');
  85. icon.className = 'fas fa-circle-exclamation text-red-500 text-lg';
  86. }
  87. msg.textContent = message;
  88. toast.classList.remove('hidden');
  89. if (_toastTimer) clearTimeout(_toastTimer);
  90. _toastTimer = setTimeout(function() { toast.classList.add('hidden'); }, 4500);
  91. }
  92. // ── Accordion ─────────────────────────────────────────────────
  93. function restoreAccordionState() {
  94. var accordionState = JSON.parse(localStorage.getItem('accordion-state') || '{}');
  95. document.querySelectorAll('[data-accordion-content]').forEach(function(content) {
  96. var user = JSON.parse(content.getAttribute('data-user'));
  97. var index = content.id.replace('content-', '');
  98. var icon = document.getElementById('icon-' + index);
  99. var isOpen = accordionState[user] !== false;
  100. content.style.display = isOpen ? '' : 'none';
  101. if (icon) icon.classList[isOpen ? 'add' : 'remove']('rotate-180');
  102. });
  103. }
  104. function _toggleAccordion(index) {
  105. var content = document.getElementById('content-' + index);
  106. var icon = document.getElementById('icon-' + index);
  107. if (!content) return;
  108. var user = JSON.parse(content.getAttribute('data-user'));
  109. var opening = content.style.display === 'none';
  110. content.style.display = opening ? '' : 'none';
  111. if (icon) icon.classList[opening ? 'add' : 'remove']('rotate-180');
  112. var state = JSON.parse(localStorage.getItem('accordion-state') || '{}');
  113. state[user] = opening;
  114. localStorage.setItem('accordion-state', JSON.stringify(state));
  115. }
  116. // ── Actions menu ──────────────────────────────────────────────
  117. function _closeAllActionsMenus() {
  118. document.querySelectorAll('[data-actions-menu]').forEach(function(m) {
  119. m.classList.add('hidden');
  120. });
  121. }
  122. function _toggleActionsMenu(index) {
  123. var menu = document.querySelector('[data-actions-menu="' + index + '"]');
  124. if (!menu) return;
  125. var wasOpen = !menu.classList.contains('hidden');
  126. _closeAllActionsMenus();
  127. if (!wasOpen) menu.classList.remove('hidden');
  128. }
  129. // ── Event delegation (single listener on document) ───────────
  130. document.addEventListener('click', function(e) {
  131. var target = e.target;
  132. // 1. Accordion toggle
  133. var accBtn = target.closest('[data-accordion-btn]');
  134. if (accBtn) {
  135. e.stopPropagation();
  136. _toggleAccordion(accBtn.getAttribute('data-accordion-btn'));
  137. return;
  138. }
  139. // 2. Actions menu toggle button
  140. var actBtn = target.closest('[data-actions-toggle]');
  141. if (actBtn) {
  142. e.stopPropagation();
  143. _toggleActionsMenu(actBtn.getAttribute('data-actions-toggle'));
  144. return;
  145. }
  146. // 3. Action item inside the menu
  147. var actionItem = target.closest('[data-action]');
  148. if (actionItem) {
  149. var action = actionItem.getAttribute('data-action');
  150. var user = actionItem.getAttribute('data-user');
  151. var ip = actionItem.getAttribute('data-ip');
  152. _closeAllActionsMenus();
  153. if (action === 'block') { _openModal('modal-block', 'modal-block-subtitle', user, ip); }
  154. if (action === 'unblock') { _openModal('modal-unblock', 'modal-unblock-subtitle', user, ip); }
  155. if (action === 'throttle') { _openModal('modal-throttle', 'modal-throttle-subtitle', user, ip); loadDelayPools(); }
  156. if (action === 'unthrottle') { _openModal('modal-unthrottle', 'modal-unthrottle-subtitle', user, ip); }
  157. if (action === 'reset') { _openModal('modal-reset', 'modal-reset-subtitle', user, ip); }
  158. return;
  159. }
  160. // 4. Modal cancel button
  161. if (target.closest('[data-modal-cancel]')) {
  162. closeAllModals();
  163. return;
  164. }
  165. // 5. Modal confirm buttons
  166. var confirmBtn = target.closest('[data-modal-confirm]');
  167. if (confirmBtn) {
  168. var type = confirmBtn.getAttribute('data-modal-confirm');
  169. closeAllModals();
  170. if (type === 'block') { _apiAction('/api/connections/block', { username: _actionUsername, ip: _actionIp }); }
  171. if (type === 'unblock') { _apiAction('/api/connections/unblock', { username: _actionUsername, ip: _actionIp }); }
  172. if (type === 'unthrottle') { _apiAction('/api/connections/unthrottle', { username: _actionUsername, ip: _actionIp }); }
  173. if (type === 'reset') { _apiAction('/api/connections/reset', { ip: _actionIp }); }
  174. if (type === 'throttle') {
  175. var sel = document.getElementById('throttle-pool-select');
  176. var poolNum = sel ? parseInt(sel.value) : NaN;
  177. if (!poolNum) { showToast(false, 'Selecciona un delay pool válido'); return; }
  178. _apiAction('/api/connections/throttle', { username: _actionUsername, ip: _actionIp, pool_number: poolNum });
  179. }
  180. return;
  181. }
  182. // 6. Modal backdrop click
  183. var modalIds = ['modal-block', 'modal-unblock', 'modal-throttle', 'modal-unthrottle', 'modal-reset'];
  184. if (modalIds.includes(target.id)) {
  185. closeAllModals();
  186. return;
  187. }
  188. // 7. Click outside any open actions menu → close all
  189. if (!target.closest('[data-actions-menu]') && !target.closest('[data-actions-toggle]')) {
  190. _closeAllActionsMenus();
  191. }
  192. });