Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
Add automatic multi-location filters for Cargo herb archive
 
Hide Herb form actions from non-administrators
Line 99: Line 99:
     initHerbLocationFilters($content && $content[0] ? $content[0] : document);
     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();
  }
}());
}());

Revision as of 17:17, 26 July 2026

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