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:28, 26 July 2026 by Aurelianus (talk | contribs) (Color Herb and Crystal table rows by rarity)

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

/* HarvestWiki admin-only Herb form UI */
(function () {
  'use strict';
  var groups = mw.config.get('wgUserGroups') || [];
  var categories = mw.config.get('wgCategories') || [];
  if (groups.indexOf('sysop') !== -1 || categories.indexOf('Herbs') === -1) {
    return;
  }
  function hideHerbFormActions() {
    ['ca-formedit', 'ca-formcreate', 'ca-formedit-sticky-header', 'ca-formcreate-sticky-header'].forEach(function (id) {
      var element = document.getElementById(id);
      if (element) {
        element.hidden = true;
      }
    });
  }
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', hideHerbFormActions);
  } else {
    hideHerbFormActions();
  }
}());


/* HarvestWiki rarity-colored Herb and Crystal table rows */
(function () {
  'use strict';

  var rarities = {
    common: true,
    uncommon: true,
    rare: true,
    legendary: true,
    mythic: true,
    secret: true,
    mortal: true,
    refined: true,
    spirit: true
  };

  function normalizeRarity(value) {
    var rarity = (value || '').trim().toLowerCase();
    return rarities[rarity] ? rarity : '';
  }

  function applyRarityColors(root) {
    var scope = root && root.querySelectorAll ? root : document;
    var tables = [];

    if (scope.matches && scope.matches('table')) {
      tables.push(scope);
    }
    tables = tables.concat(Array.prototype.slice.call(scope.querySelectorAll('table')));

    tables.forEach(function (table) {
      var headerCells = Array.prototype.slice.call(table.querySelectorAll('tr:first-child th, tr:first-child td'));
      var itemIndex = headerCells.findIndex(function (cell) {
        var label = cell.textContent.trim().toLowerCase();
        return label === 'herb' || label === 'crystal' ||
          cell.classList.contains('field_Herb') || cell.classList.contains('field_Crystal');
      });
      var rarityIndex = headerCells.findIndex(function (cell) {
        return cell.textContent.trim().toLowerCase() === 'rarity' ||
          cell.classList.contains('field_Rarity');
      });

      Array.prototype.slice.call(table.rows).forEach(function (row) {
        var cells = Array.prototype.slice.call(row.cells);
        var itemCell = row.querySelector('td.field_Herb, td.field_Crystal');
        var rarityCell = row.querySelector('td.field_Rarity');

        if (!itemCell && itemIndex >= 0) {
          itemCell = cells[itemIndex];
        }
        if (!rarityCell && rarityIndex >= 0) {
          rarityCell = cells[rarityIndex];
        }
        if (!itemCell || !rarityCell || itemCell.tagName !== 'TD') {
          return;
        }

        var rarity = normalizeRarity(rarityCell.textContent);
        if (!rarity) {
          return;
        }

        row.classList.add('hw-rarity-row');
        row.dataset.hwRarity = rarity;
        itemCell.classList.add('hw-rarity-item');
        rarityCell.classList.add('hw-rarity-label');
      });

      table.dataset.hwRarityColorsReady = '1';
    });
  }

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