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 1: Línea 1:
function safeNormalize(text) {
(function () {
  text = String(text || '').toLowerCase().trim();
  function safeNormalize(text) {
    text = String(text || '').toLowerCase().trim();


  try {
    try {
    return text.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
      return text.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
  } catch (e) {
    } catch (e) {
     return text;
      return text;
    }
  }
 
  function hasMark(cellText) {
    cellText = String(cellText || '').replace(/\s+/g, '').trim();
 
     return (
      cellText !== '' &&
      (
        cellText.indexOf('✔') !== -1 ||
        cellText.indexOf('✅') !== -1 ||
        cellText.indexOf('☑') !== -1
      )
    );
   }
   }
}


function hasMark(cellText) {
  function getActiveCategoryCol(tableId) {
  cellText = String(cellText || '').replace(/\s+/g, '').trim();
    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 parseInt(firstBtn.getAttribute('data-col'), 10);
    }
    return parseInt(activeBtn.getAttribute('data-col'), 10);
  }


   return (
   function getSearchTextForRow(row, tableId) {
     cellText !== '' &&
     var cells = row.getElementsByTagName('td');
     (
     var text = row.getAttribute('data-search') || '';
      cellText.indexOf('') !== -1 ||
    var visibleInfoCols = tableId === 'tableSlot1' ? 2 : 3;
      cellText.indexOf('') !== -1 ||
      cellText.indexOf('') !== -1
    )
  );
}


function filterMatrix(tableId, selectId, inputId) {
    for (var i = 0; i < visibleInfoCols; i++) {
  var table = document.getElementById(tableId);
      if (cells[i]) {
  var select = document.getElementById(selectId);
        text += ' ' + (cells[i].textContent || cells[i].innerText || '');
  var input = document.getElementById(inputId);
      }
    }


  if (!table || !select || !input || !table.tBodies.length) return;
    return safeNormalize(text);
  }


   var selectColIndex = select.value;
   function updateCategoryView(tableId, inputId) {
  var inputText = safeNormalize(input.value);
    var table = document.getElementById(tableId);
  var rows = table.tBodies[0].getElementsByTagName('tr');
    var input = document.getElementById(inputId);


  for (var i = 0; i < rows.length; i++) {
    if (!table || !table.tBodies.length || !table.tHead || !table.tHead.rows.length) return;
    var row = rows[i];
    var tds = row.getElementsByTagName('td');


     var matchCategory = true;
     var activeCol = getActiveCategoryCol(tableId);
     var matchText = true;
     if (activeCol === null) return;


     if (selectColIndex !== '') {
     var searchText = input ? safeNormalize(input.value) : '';
      var colIdx = parseInt(selectColIndex, 10);
    var rows = table.tBodies[0].getElementsByTagName('tr');
      var categoryCell = tds[colIdx];
    var headerRow = table.tHead.rows[0];
    var staticCols = tableId === 'tableSlot1' ? 2 : 3;


      if (!categoryCell || !hasMark(categoryCell.textContent || categoryCell.innerText)) {
    for (var h = 0; h < headerRow.cells.length; h++) {
        matchCategory = false;
      headerRow.cells[h].style.display = (h < staticCols || h === activeCol) ? '' : 'none';
      }
     }
     }


     if (inputText !== '') {
     for (var i = 0; i < rows.length; i++) {
       var rowContent = '';
      var row = rows[i];
      var cells = row.getElementsByTagName('td');
      var matchCategory = false;
       var matchText = true;


       if (tableId === 'tableSlot1') {
       if (cells[activeCol]) {
        rowContent += (tds[0] ? (tds[0].textContent || tds[0].innerText) : '') + ' ';
         matchCategory = hasMark(cells[activeCol].textContent || cells[activeCol].innerText);
        rowContent += (tds[1] ? (tds[1].textContent || tds[1].innerText) : '');
      } else {
         rowContent += (tds[0] ? (tds[0].textContent || tds[0].innerText) : '') + ' ';
        rowContent += (tds[1] ? (tds[1].textContent || tds[1].innerText) : '') + ' ';
        rowContent += (tds[2] ? (tds[2].textContent || tds[2].innerText) : '');
       }
       }


       rowContent = safeNormalize(rowContent);
       if (searchText !== '') {
        var searchPool = getSearchTextForRow(row, tableId);
        if (searchPool.indexOf(searchText) === -1) {
          matchText = false;
        }
      }


       if (rowContent.indexOf(inputText) === -1) {
       row.style.display = (matchCategory && matchText) ? '' : 'none';
         matchText = false;
 
      for (var c = 0; c < cells.length; c++) {
         cells[c].style.display = (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');
        }


    row.style.display = (matchCategory && matchText) ? '' : 'none';
        this.classList.add('active');
        updateCategoryView(tableId, inputId);
      });
    }
   }
   }
}


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


  if (!table || !select || !input) return;
    input.dataset.orozaBound = '1';
    input.addEventListener('input', function () {
      updateCategoryView(tableId, inputId);
    });
  }


   select.addEventListener('change', function () {
   function initOrozaDropFilters() {
    filterMatrix(tableId, selectId, inputId);
    if (document.getElementById('tableSlot1')) {
  });
      bindCategoryButtons('tableSlot1', 'textSlot1');
      bindSearchInput('tableSlot1', 'textSlot1');
      updateCategoryView('tableSlot1', 'textSlot1');
    }


  input.addEventListener('input', function () {
    if (document.getElementById('tableSlot2')) {
    filterMatrix(tableId, selectId, inputId);
      bindCategoryButtons('tableSlot2', 'textSlot2');
  });
      bindSearchInput('tableSlot2', 'textSlot2');
}
      updateCategoryView('tableSlot2', 'textSlot2');
    }
  }


function initOrozaDropFilters() {
  if (window.mw && mw.hook) {
  bindFilter('tableSlot1', 'catSlot1', 'textSlot1');
    mw.hook('wikipage.content').add(function () {
   bindFilter('tableSlot2', 'catSlot2', 'textSlot2');
      initOrozaDropFilters();
}
    });
 
   } else if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initOrozaDropFilters);
    document.addEventListener('DOMContentLoaded', initOrozaDropFilters);
  } else {
    initOrozaDropFilters();
  }
})();

Revisión del 23:48 3 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 parseInt(firstBtn.getAttribute('data-col'), 10);
    }
    return parseInt(activeBtn.getAttribute('data-col'), 10);
  }

  function getSearchTextForRow(row, tableId) {
    var cells = row.getElementsByTagName('td');
    var text = row.getAttribute('data-search') || '';
    var visibleInfoCols = tableId === 'tableSlot1' ? 2 : 3;

    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 activeCol = getActiveCategoryCol(tableId);
    if (activeCol === null) return;

    var searchText = input ? safeNormalize(input.value) : '';
    var rows = table.tBodies[0].getElementsByTagName('tr');
    var headerRow = table.tHead.rows[0];
    var staticCols = tableId === 'tableSlot1' ? 2 : 3;

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

    for (var i = 0; i < rows.length; i++) {
      var row = rows[i];
      var cells = row.getElementsByTagName('td');
      var matchCategory = false;
      var matchText = true;

      if (cells[activeCol]) {
        matchCategory = hasMark(cells[activeCol].textContent || cells[activeCol].innerText);
      }

      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 = (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();
  }
})();