MediaWiki:Common.js

From SZ
Revision as of 15:03, 24 August 2025 by Test2 (talk | contribs)
Jump to navigation Jump to search

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.
/* Collapsible sidebar sections for MW 1.44 (Vector & Vector 2022) */
var COLLAPSIBLE_SECTIONS = [ 'book1', 'book2' ]; // Keys from MediaWiki:Sidebar
var DEFAULT_OPEN = false;

mw.loader.using(['mediawiki.util']).then(function () {

  function initCollapsiblePortlet(name) {
    var portal = document.getElementById('p-' + name);
    if (!portal) return;

    // Match headings and content for both Vector & Vector 2022
    var heading = portal.querySelector('.vector-menu-heading, h3');
    var content = portal.querySelector('.vector-menu-content, .body');
    if (!heading || !content) return;

    portal.classList.add('mw-custom-collapsible');

    // Get saved state
    var storageKey = 'mwSidebar:' + name;
    var stored = null;
    try { stored = localStorage.getItem(storageKey); } catch (e) {}
    var isOpen = stored === null ? DEFAULT_OPEN : stored === '1';

    function setState(open) {
      portal.classList.toggle('is-open', open);
      heading.setAttribute('aria-expanded', open ? 'true' : 'false');
      content.style.display = open ? '' : 'none';
      try { localStorage.setItem(storageKey, open ? '1' : '0'); } catch (e) {}
    }

    // Accessibility & click events
    heading.setAttribute('role', 'button');
    heading.setAttribute('tabindex', '0');
    heading.addEventListener('click', function () {
      setState(!portal.classList.contains('is-open'));
    });
    heading.addEventListener('keydown', function (e) {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
        setState(!portal.classList.contains('is-open'));
      }
    });

    setState(isOpen);
  }

  function bootCollapsibleSidebars() {
    COLLAPSIBLE_SECTIONS.forEach(initCollapsiblePortlet);
  }

  // Run on page load and after SPA updates
  $(bootCollapsibleSidebars);
  mw.hook('wikipage.content').add(bootCollapsibleSidebars);

});