Diferencia entre revisiones de «MediaWiki:Common.js»

De Oroza RO Wiki
Ir a la navegación Ir a la búsqueda
Sin resumen de edición
Sin resumen de edición
Línea 159: Línea 159:


/* =========================================================
/* =========================================================
   RESALTAR MENÚ LATERAL ACTIVO (OROZA WIKI)
   RESALTAR MENÚ LATERAL ACTIVO (VERSIÓN CORREGIDA)
========================================================= */
========================================================= */
function highlightActiveSidebarLink() {
function highlightActiveSidebarLink() {
    // Obtenemos la ruta actual y la decodificamos (para manejar tildes y espacios)
  // Obtenemos la URL actual, la pasamos a minúsculas y quitamos parámetros (? o #)
    var currentPath = decodeURIComponent(window.location.pathname);
  var currentUrl = window.location.href.split('#')[0].split('?')[0].toLowerCase();
    var sidebarLinks = document.querySelectorAll('.oroza-sidebar li a');
  var sidebarLinks = document.querySelectorAll('.oroza-sidebar li a');


    for (var i = 0; i < sidebarLinks.length; i++) {
  for (var i = 0; i < sidebarLinks.length; i++) {
        var linkHref = sidebarLinks[i].getAttribute('href');
    // Obtenemos la URL absoluta del enlace y la limpiamos igual
       
    var linkUrl = sidebarLinks[i].href.split('#')[0].split('?')[0].toLowerCase();
        if (linkHref) {
   
            var decodedHref = decodeURIComponent(linkHref);
    // Si la URL actual coincide con la del enlace, le ponemos la clase active
            // Comparamos si la ruta actual coincide con el enlace del menú
    if (currentUrl === linkUrl) {
            if (currentPath === decodedHref || currentPath.endsWith(decodedHref)) {
      sidebarLinks[i].classList.add('active');
                sidebarLinks[i].classList.add('active');
    } else {
            }
      sidebarLinks[i].classList.remove('active'); // Limpia por si acaso
        }
     }
     }
  }
}
}


// Integración segura con MediaWiki
// Integración segura con el ecosistema de MediaWiki
if (window.mw && mw.hook) {
if (window.mw && mw.hook) {
    mw.hook('wikipage.content').add(highlightActiveSidebarLink);
  mw.hook('wikipage.content').add(highlightActiveSidebarLink);
} else if (document.readyState === 'loading') {
} else if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', highlightActiveSidebarLink);
  document.addEventListener('DOMContentLoaded', highlightActiveSidebarLink);
} else {
} else {
    highlightActiveSidebarLink();
  highlightActiveSidebarLink();
}
}

Revisión del 20:59 6 abr 2026

(function () {
  function safeNormalize(text) {
    text = String(text || '').toLowerCase().trim();

    try {
      return text.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
    } catch (e) {
      return text;
    }
  }

  function hasMark(cellText) {
    cellText = String(cellText || '').replace(/\s+/g, '').trim();

    return (
      cellText !== '' &&
      (
        cellText.indexOf('✔') !== -1 ||
        cellText.indexOf('✅') !== -1 ||
        cellText.indexOf('☑') !== -1
      )
    );
  }

  function getActiveCategoryCol(tableId) {
    var activeBtn = document.querySelector('.oroza-cat-btn.active[data-table="' + tableId + '"]');
    if (!activeBtn) {
      var firstBtn = document.querySelector('.oroza-cat-btn[data-table="' + tableId + '"]');
      if (!firstBtn) return null;
      firstBtn.classList.add('active');
      return firstBtn.getAttribute('data-col'); 
    }
    return activeBtn.getAttribute('data-col'); // Puede retornar "all" en string
  }

  function getSearchTextForRow(row, tableId) {
    var cells = row.getElementsByTagName('td');
    var text = row.getAttribute('data-search') || '';
    // Como añadimos "Prob.", ahora las columnas de información base son 3 en Slot 1 y 4 en Slot 2
    var visibleInfoCols = tableId === 'tableSlot1' ? 3 : 4;

    for (var i = 0; i < visibleInfoCols; i++) {
      if (cells[i]) {
        text += ' ' + (cells[i].textContent || cells[i].innerText || '');
      }
    }

    return safeNormalize(text);
  }

  function updateCategoryView(tableId, inputId) {
    var table = document.getElementById(tableId);
    var input = document.getElementById(inputId);

    if (!table || !table.tBodies.length || !table.tHead || !table.tHead.rows.length) return;

    var activeColRaw = getActiveCategoryCol(tableId);
    if (activeColRaw === null) return;

    var showAll = (activeColRaw === 'all');
    var activeCol = showAll ? -1 : parseInt(activeColRaw, 10);

    var searchText = input ? safeNormalize(input.value) : '';
    var rows = table.tBodies[0].getElementsByTagName('tr');
    var headerRow = table.tHead.rows[0];
    
    // Columnas que SIEMPRE se ven (Atributo, Prob, Rango, etc.)
    var staticCols = tableId === 'tableSlot1' ? 3 : 4;

    // Actualizar Header
    for (var h = 0; h < headerRow.cells.length; h++) {
      headerRow.cells[h].style.display = (showAll || h < staticCols || h === activeCol) ? '' : 'none';
    }

    // Actualizar Body
    for (var i = 0; i < rows.length; i++) {
      var row = rows[i];
      var cells = row.getElementsByTagName('td');
      
      // Si estamos en "Todos", empezamos asumiendo que sí tiene categoría válida
      var matchCategory = showAll;
      var matchText = true;

      // Si se escogió una categoría específica, verificamos si tiene "✔"
      if (!showAll && cells[activeCol]) {
        matchCategory = hasMark(cells[activeCol].textContent || cells[activeCol].innerText);
      }

      // Verificación de búsqueda por texto
      if (searchText !== '') {
        var searchPool = getSearchTextForRow(row, tableId);
        if (searchPool.indexOf(searchText) === -1) {
          matchText = false;
        }
      }

      row.style.display = (matchCategory && matchText) ? '' : 'none';

      for (var c = 0; c < cells.length; c++) {
        cells[c].style.display = (showAll || c < staticCols || c === activeCol) ? '' : 'none';
      }
    }
  }

  function bindCategoryButtons(tableId, inputId) {
    var buttons = document.querySelectorAll('.oroza-cat-btn[data-table="' + tableId + '"]');

    for (var i = 0; i < buttons.length; i++) {
      if (buttons[i].dataset.orozaBound === '1') continue;

      buttons[i].dataset.orozaBound = '1';
      buttons[i].addEventListener('click', function () {
        var sameTableButtons = document.querySelectorAll('.oroza-cat-btn[data-table="' + tableId + '"]');
        for (var j = 0; j < sameTableButtons.length; j++) {
          sameTableButtons[j].classList.remove('active');
        }

        this.classList.add('active');
        updateCategoryView(tableId, inputId);
      });
    }
  }

  function bindSearchInput(tableId, inputId) {
    var input = document.getElementById(inputId);
    if (!input || input.dataset.orozaBound === '1') return;

    input.dataset.orozaBound = '1';
    input.addEventListener('input', function () {
      updateCategoryView(tableId, inputId);
    });
  }

  function initOrozaDropFilters() {
    if (document.getElementById('tableSlot1')) {
      bindCategoryButtons('tableSlot1', 'textSlot1');
      bindSearchInput('tableSlot1', 'textSlot1');
      updateCategoryView('tableSlot1', 'textSlot1');
    }

    if (document.getElementById('tableSlot2')) {
      bindCategoryButtons('tableSlot2', 'textSlot2');
      bindSearchInput('tableSlot2', 'textSlot2');
      updateCategoryView('tableSlot2', 'textSlot2');
    }
  }

  if (window.mw && mw.hook) {
    mw.hook('wikipage.content').add(function () {
      initOrozaDropFilters();
    });
  } else if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initOrozaDropFilters);
  } else {
    initOrozaDropFilters();
  }
})();


/* =========================================================
   RESALTAR MENÚ LATERAL ACTIVO (VERSIÓN CORREGIDA)
========================================================= */
function highlightActiveSidebarLink() {
  // Obtenemos la URL actual, la pasamos a minúsculas y quitamos parámetros (? o #)
  var currentUrl = window.location.href.split('#')[0].split('?')[0].toLowerCase();
  var sidebarLinks = document.querySelectorAll('.oroza-sidebar li a');

  for (var i = 0; i < sidebarLinks.length; i++) {
    // Obtenemos la URL absoluta del enlace y la limpiamos igual
    var linkUrl = sidebarLinks[i].href.split('#')[0].split('?')[0].toLowerCase();
    
    // Si la URL actual coincide con la del enlace, le ponemos la clase active
    if (currentUrl === linkUrl) {
      sidebarLinks[i].classList.add('active');
    } else {
      sidebarLinks[i].classList.remove('active'); // Limpia por si acaso
    }
  }
}

// Integración segura con el ecosistema de MediaWiki
if (window.mw && mw.hook) {
  mw.hook('wikipage.content').add(highlightActiveSidebarLink);
} else if (document.readyState === 'loading') {
  document.addEventListener('DOMContentLoaded', highlightActiveSidebarLink);
} else {
  highlightActiveSidebarLink();
}