Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 17:06, 26 July 2026 by Aurelianus (talk | contribs) (Add automatic multi-location filters for Cargo herb archive)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

/* HarvestWiki dynamic Cargo archive filters */
(function () {
  'use strict';

  function initHerbLocationFilters(root) {
    var catalogs = [];
    if (root && root.matches && root.matches('.hw-herb-catalog')) {
      catalogs.push(root);
    }
    if (root && root.querySelectorAll) {
      catalogs = catalogs.concat(Array.prototype.slice.call(root.querySelectorAll('.hw-herb-catalog')));
    }

    catalogs.forEach(function (catalog) {
      if (catalog.dataset.hwLocationFiltersReady === '1') {
        return;
      }

      var table = catalog.querySelector('table');
      var filterBar = document.querySelector('.hw-herb-filter');
      if (!table || !filterBar) {
        return;
      }

      var headers = Array.prototype.slice.call(table.querySelectorAll('thead th'));
      var locationIndex = headers.findIndex(function (header) {
        return header.textContent.trim().toLowerCase() === 'location';
      });
      if (locationIndex < 0) {
        return;
      }

      var rows = Array.prototype.slice.call(table.querySelectorAll('tbody tr'));
      var counts = {};
      rows.forEach(function (row) {
        var cell = row.querySelectorAll('td')[locationIndex];
        var location = cell ? cell.textContent.trim() : '';
        row.dataset.hwLocation = location;
        if (location) {
          counts[location] = (counts[location] || 0) + 1;
        }
      });

      var locations = Object.keys(counts).sort();
      var enabled = new Set(locations);
      filterBar.innerHTML = '';

      var showAll = document.createElement('button');
      showAll.type = 'button';
      showAll.className = 'hw-filter-chip hw-filter-chip--all';
      showAll.textContent = 'Show all';
      filterBar.appendChild(showAll);

      var locationButtons = {};
      locations.forEach(function (location) {
        var button = document.createElement('button');
        button.type = 'button';
        button.className = 'hw-filter-chip is-active';
        button.setAttribute('aria-pressed', 'true');
        button.textContent = location + ' (' + counts[location] + ')';
        button.addEventListener('click', function () {
          if (enabled.has(location)) {
            enabled.delete(location);
          } else {
            enabled.add(location);
          }
          applyFilters();
        });
        locationButtons[location] = button;
        filterBar.appendChild(button);
      });

      function applyFilters() {
        rows.forEach(function (row) {
          row.hidden = !enabled.has(row.dataset.hwLocation);
        });
        locations.forEach(function (location) {
          var active = enabled.has(location);
          locationButtons[location].classList.toggle('is-active', active);
          locationButtons[location].setAttribute('aria-pressed', active ? 'true' : 'false');
        });
        showAll.disabled = enabled.size === locations.length;
        showAll.classList.toggle('is-active', enabled.size === locations.length);
      }

      showAll.addEventListener('click', function () {
        enabled = new Set(locations);
        applyFilters();
      });

      catalog.dataset.hwLocationFiltersReady = '1';
      applyFilters();
    });
  }

  mw.hook('wikipage.content').add(function ($content) {
    initHerbLocationFilters($content && $content[0] ? $content[0] : document);
  });
}());