MediaWiki:Common.js: Difference between revisions

From SZ
Jump to navigation Jump to search
No edit summary
Tag: Manual revert
No edit summary
Line 1: Line 1:
/* Collapsible sidebar sections for MW 1.44 (Vector & Vector 2022) */
mw.loader.using('jquery', function () {
var COLLAPSIBLE_SECTIONS = [ 'book1', 'book2' ]; // Keys from MediaWiki:Sidebar
    $(document).ready(function () {
var DEFAULT_OPEN = false;
        $('.portal .body > ul > li').each(function () {
 
            var $this = $(this);
mw.loader.using(['mediawiki.util']).then(function () {
            if ($this.find('ul').length) {
 
                $this.addClass('collapsible-header');
  function initCollapsiblePortlet(name) {
                $this.click(function (e) {
    var portal = document.getElementById('p-' + name);
                    // Prevent following links on header click
    if (!portal) return;
                    if (e.target.tagName !== 'A') {
 
                        e.preventDefault();
    // Match headings and content for both Vector & Vector 2022
                        $this.toggleClass('open');
    var heading = portal.querySelector('.vector-menu-heading, h3');
                        $this.children('ul').slideToggle(200);
    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);
});
});

Revision as of 10:06, 25 August 2025

mw.loader.using('jquery', function () {
    $(document).ready(function () {
        $('.portal .body > ul > li').each(function () {
            var $this = $(this);
            if ($this.find('ul').length) {
                $this.addClass('collapsible-header');
                $this.click(function (e) {
                    // Prevent following links on header click
                    if (e.target.tagName !== 'A') {
                        e.preventDefault();
                        $this.toggleClass('open');
                        $this.children('ul').slideToggle(200);
                    }
                });
            }
        });
    });
});