Jump to content

MediaWiki:Gadget-CollapseAdvancedSearch.js

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.
/* Right-aligned "Filters" button in the tabs; on-demand init; no flash */
(function (mw, $) {
  'use strict';
  if (window.ICWFiltersToggleLoaded) return;
  window.ICWFiltersToggleLoaded = true;

  if (mw.config.get('wgCanonicalSpecialPageName') !== 'Search') return;

  $(function () {
    var fs = document.getElementById('mw-searchoptions');
    if (!fs) return;

    var $ul = $('.mw-search-profile-tabs .search-types ul');
    if (!$ul.length) return;

    // Avoid duplicates if something re-runs
    var $li = $ul.find('li.filters-toggle');
    if (!$li.length) {
      $li = $('<li>', { class: 'filters-toggle' });
      var $btn = $('<button>', {
        type: 'button',
        class: 'icw-filters-btn',
        'aria-controls': 'mw-searchoptions',
        'aria-expanded': 'false',
        text: 'Show filters'
      });
      $li.append($btn);
      // Append to UL; margin-left:auto on li pushes it to the right
      $ul.append($li);
    }
    var $btn = $li.find('button.icw-filters-btn');

    var initialized = false;

    function ensureInitExpanded() {
      if (initialized) return;
      fs.classList.add('mw-collapsible');                 // expanded by default
      fs.setAttribute('data-expandtext', 'show');
      fs.setAttribute('data-collapsetext', 'hide');
      $(fs).makeCollapsible();                            // create internal structure
      fs.classList.remove('mw-collapsed');                // force expanded if set elsewhere
      var c = fs.querySelector('.mw-collapsible-content');
      if (c) c.style.display = '';                        // ensure visible
      initialized = true;
    }

    function openPanel() {
      ensureInitExpanded();
      fs.classList.add('icw-open');                       // reveal via CSS gate
      $btn.attr('aria-expanded', 'true').text('Hide filters');
      $li.addClass('is-open');
    }

    function closePanel() {
      fs.classList.remove('icw-open');                    // hide entire fieldset
      $btn.attr('aria-expanded', 'false').text('Show filters');
      $li.removeClass('is-open');
    }

    $btn.on('click', function () {
      if (fs.classList.contains('icw-open')) closePanel(); else openPanel();
    });

    // Optional: Alt+F toggles filters
    $(document).on('keydown', function (e) {
      if ((e.altKey || e.metaKey) && !e.shiftKey && !e.ctrlKey && (e.key === 'f' || e.key === 'F')) {
        e.preventDefault();
        $btn.trigger('click');
      }
    });
  });
}(mediaWiki, jQuery));