user_search_and_inputs.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // User search & conditional inputs (sin cambios funcionales)
  2. (function(){
  3. const i18n = window.__AUD_I18N__ || {};
  4. const conditionalInputs = {};
  5. let allUsers = [];
  6. let filteredUsers = [];
  7. let selectedUserIndex = -1;
  8. let elements = {};
  9. function cacheDom(){
  10. elements.form = document.getElementById('audit-form');
  11. elements.resultsContainer = document.getElementById('audit-results-container');
  12. elements.auditTypeSelect = document.getElementById('audit-type');
  13. elements.usernameSelect = document.getElementById('username-filter');
  14. elements.startDateInput = document.getElementById('start-date');
  15. elements.endDateInput = document.getElementById('end-date');
  16. elements.startDateContainer = document.getElementById('start-date-container');
  17. elements.endDateContainer = document.getElementById('end-date-container');
  18. elements.userSearchInput = document.getElementById('user-search-input');
  19. elements.userDropdown = document.getElementById('user-dropdown');
  20. elements.userOptionsContainer = document.getElementById('user-options-container');
  21. conditionalInputs.user = document.getElementById('user-filter-container');
  22. conditionalInputs.user_optional = document.getElementById('user-filter-container');
  23. conditionalInputs.keyword = document.getElementById('keyword-input-container');
  24. conditionalInputs.ip = document.getElementById('ip-input-container');
  25. conditionalInputs.response_code = document.getElementById('response-code-input-container');
  26. conditionalInputs.social_media = document.getElementById('social-media-input-container');
  27. }
  28. function initDates(){
  29. const todayString = window.getLocalDateString();
  30. elements.startDateInput.value = todayString;
  31. elements.endDateInput.value = todayString;
  32. }
  33. function renderUserOptions(users, searchTerm=''){
  34. elements.userOptionsContainer.innerHTML = '';
  35. if (!searchTerm){
  36. const allOption = document.createElement('div');
  37. allOption.className = 'px-3 py-2 hover:bg-blue-50 cursor-pointer text-gray-700 border-b border-gray-100';
  38. allOption.textContent = i18n.allUsersOption || '-- Todos --';
  39. allOption.dataset.value = '';
  40. allOption.addEventListener('click', ()=>selectUser('', '-- Todos --'));
  41. elements.userOptionsContainer.appendChild(allOption);
  42. }
  43. users.forEach((user,index)=>{
  44. const option = document.createElement('div');
  45. option.className = 'px-3 py-2 hover:bg-blue-50 cursor-pointer text-gray-700';
  46. option.textContent = user;
  47. option.dataset.value = user;
  48. option.dataset.index = index;
  49. option.addEventListener('click', ()=>selectUser(user, user));
  50. if (searchTerm){
  51. const regex = new RegExp(`(${searchTerm})`, 'gi');
  52. option.innerHTML = user.replace(regex, '<mark class="bg-yellow-200">$1</mark>');
  53. }
  54. elements.userOptionsContainer.appendChild(option);
  55. });
  56. if (users.length===0 && searchTerm){
  57. const noResults = document.createElement('div');
  58. noResults.className = 'px-3 py-2 text-gray-500 italic';
  59. noResults.textContent = i18n.noUsersFound || 'No se encontraron usuarios';
  60. elements.userOptionsContainer.appendChild(noResults);
  61. }
  62. }
  63. function selectUser(value, displayText){
  64. elements.userSearchInput.value = displayText;
  65. elements.usernameSelect.value = value;
  66. elements.userDropdown.classList.add('hidden');
  67. selectedUserIndex = -1;
  68. }
  69. function filterUsers(term){
  70. if (!term) filteredUsers = [...allUsers];
  71. else filteredUsers = allUsers.filter(u=>u.toLowerCase().includes(term.toLowerCase()));
  72. renderUserOptions(filteredUsers, term);
  73. }
  74. function updateSelectedOption(options){
  75. options.forEach((opt,i)=>opt.classList.toggle('bg-blue-100', i===selectedUserIndex));
  76. }
  77. function bindUserSearchEvents(){
  78. const input = elements.userSearchInput;
  79. input.addEventListener('input', e=>{ filterUsers(e.target.value); elements.userDropdown.classList.remove('hidden'); selectedUserIndex=-1; });
  80. input.addEventListener('focus', ()=>{ filterUsers(input.value); elements.userDropdown.classList.remove('hidden'); });
  81. input.addEventListener('keydown', e=>{
  82. const options = elements.userOptionsContainer.querySelectorAll('[data-value]');
  83. if (e.key==='ArrowDown'){ e.preventDefault(); selectedUserIndex = Math.min(selectedUserIndex+1, options.length-1); updateSelectedOption(options);}
  84. else if (e.key==='ArrowUp'){ e.preventDefault(); selectedUserIndex = Math.max(selectedUserIndex-1, -1); updateSelectedOption(options);}
  85. else if (e.key==='Enter'){ e.preventDefault(); if (selectedUserIndex>=0 && options[selectedUserIndex]){ const sel=options[selectedUserIndex]; selectUser(sel.dataset.value, sel.textContent);} }
  86. else if (e.key==='Escape'){ elements.userDropdown.classList.add('hidden'); selectedUserIndex=-1; }
  87. });
  88. document.addEventListener('click', e=>{ if(!input.contains(e.target) && !elements.userDropdown.contains(e.target)){ elements.userDropdown.classList.add('hidden'); selectedUserIndex=-1; }});
  89. }
  90. function fetchUsers(){
  91. fetch('/api/all-users')
  92. .then(r=>r.json())
  93. .then(users=>{
  94. allUsers = users; filteredUsers=[...users];
  95. users.forEach(u=>{ const opt=document.createElement('option'); opt.value=u; opt.textContent=u; elements.usernameSelect.appendChild(opt); });
  96. renderUserOptions(filteredUsers);
  97. })
  98. .catch(()=> toastr.error(i18n.errorLoadingUsers || 'Error al cargar la lista de usuarios', i18n.loadError || 'Error de carga'));
  99. }
  100. function toggleConditionalInputs(){
  101. elements.resultsContainer.innerHTML = window.__AUD_FORM__?.defaultResultsHTML || elements.resultsContainer.innerHTML;
  102. Object.values(conditionalInputs).forEach(el=>{ if(el) el.style.display='none'; });
  103. const selectedOption = elements.auditTypeSelect.options[elements.auditTypeSelect.selectedIndex];
  104. const auditType = selectedOption.value;
  105. const required = (selectedOption.dataset.requires||'').split(',').filter(Boolean);
  106. required.forEach(id=>{ if(conditionalInputs[id]) conditionalInputs[id].style.display='block'; });
  107. if (auditType === 'daily_activity'){
  108. elements.endDateContainer.style.display='none';
  109. elements.startDateContainer.querySelector('label').textContent = i18n.dateLabel || 'Fecha';
  110. } else {
  111. elements.endDateContainer.style.display='block';
  112. elements.startDateContainer.querySelector('label').textContent = i18n.startDateLabel || 'Fecha de Inicio';
  113. }
  114. if (auditType === 'user_summary' || auditType === 'daily_activity') {
  115. elements.userSearchInput.placeholder = i18n.selectUserPlaceholder || 'Seleccionar usuario...';
  116. elements.userSearchInput.value='';
  117. elements.usernameSelect.value='';
  118. elements.userSearchInput.disabled=false;
  119. } else {
  120. elements.userSearchInput.placeholder = i18n.searchUserOptionalPlaceholder || 'Buscar usuario... (Opcional)';
  121. elements.userSearchInput.value='-- Todos --';
  122. elements.usernameSelect.value='';
  123. elements.userSearchInput.disabled=false;
  124. }
  125. }
  126. function initConditionalInputs(){
  127. elements.auditTypeSelect.addEventListener('change', toggleConditionalInputs);
  128. toggleConditionalInputs();
  129. }
  130. function initUserSearch(){
  131. bindUserSearchEvents();
  132. fetchUsers();
  133. }
  134. function initUserAndInputs(){
  135. cacheDom();
  136. initDates();
  137. initConditionalInputs();
  138. initUserSearch();
  139. }
  140. window.__AUD_USER_INPUTS__ = { initUserAndInputs };
  141. })();