MediaWiki:CustomFiltered.js: Difference between revisions

From The Seven Sages of Rome
No edit summary
No edit summary
Line 26: Line 26:
     function snapshotSMW() {
     function snapshotSMW() {
       $container.find('tr.filtered-table-item').each(function () {
       $container.find('tr.filtered-table-item').each(function () {
        var inlineDisplay = this.style.display;
         $(this).data('smw-hidden', this.style.display === 'none');
         $(this).data('smw-hidden', inlineDisplay === 'none');
       });
       });
     }
     }
Line 45: Line 44:
         }
         }


         if (!term || $row.text().toLowerCase().indexOf(term) !== -1) {
        // Reset to visible first, then hide if it doesn't match
           $row.show();
        $row.show();
 
         if (term && $row.text().toLowerCase().indexOf(term) === -1) {
           $row.hide();
        } else {
           visible++;
           visible++;
        } else {
          $row.hide();
         }
         }
       });
       });

Revision as of 16:19, 27 February 2026

(function ($) {
  function init() {
    var $container = $('.filtered-views-container');
    if (!$container.length) return;

    var $bar = $('<div>', {
      id: 'smw-filter-bar',
      class: 'd-flex justify-content-between align-items-center w-100 mb-2'
    });

    var $searchInput = $('<input>', {
      type: 'text',
      id: 'smw-search-input',
      placeholder: 'Search all columns…',
      class: 'form-control w-auto'
    });

    var $counter = $('<span>', {
      id: 'smw-filter-counter',
      class: 'text-muted'
    });

    $bar.append($searchInput, $counter);
    $container.before($bar);

    function snapshotSMW() {
      $container.find('tr.filtered-table-item').each(function () {
        $(this).data('smw-hidden', this.style.display === 'none');
      });
    }

    function applySearch() {
      var term = $.trim($searchInput.val()).toLowerCase();
      var total = 0;
      var visible = 0;

      $container.find('tr.filtered-table-item').each(function () {
        var $row = $(this);
        total++;

        if ($row.data('smw-hidden')) {
          $row.hide();
          return;
        }

        // Reset to visible first, then hide if it doesn't match
        $row.show();

        if (term && $row.text().toLowerCase().indexOf(term) === -1) {
          $row.hide();
        } else {
          visible++;
        }
      });

      $counter.text('Showing ' + visible + ' of ' + total + ' entries');
    }

    var smwTimer = null;

    var observer = new MutationObserver(function (mutations) {
      var smwMutation = mutations.some(function (m) {
        return $(m.target).hasClass('filtered-table-item');
      });

      if (!smwMutation) return;

      clearTimeout(smwTimer);
      smwTimer = setTimeout(function () {
        snapshotSMW();
        applySearch();
      }, 50);
    });

    observer.observe($container[0], {
      subtree: true,
      attributes: true,
      attributeFilter: ['style']
    });

    $searchInput.on('input keyup', function () {
      applySearch();
    });

    snapshotSMW();
    applySearch();
  }

  $(document).ready(function () {
    init();
  });

}(jQuery));