MediaWiki:Common.js: Difference between revisions

From SZ
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 1: Line 1:
/* Debugging: Collapsible sidebar for Vector & Vector 2022 */
* Collapsible sidebar sections for MW 1.44 (Vector & Vector 2022) List the exact section keys from MediaWiki:Sidebar here: */ var COLLAPSIBLE_SECTIONS = [ 'book1', 'book2' ]; /* Default open/closed; set true to open by default */ var DEFAULT_OPEN = false; function initCollapsiblePortlet(name) { var portal = document.getElementById('p-' + name); if (!portal) return; // Choose heading/content across skins var heading = portal.querySelector('.vector-menu-heading') || portal.querySelector('h3'); var content = portal.querySelector('.vector-menu-content') || portal.querySelector('.body'); if (!heading || !content) return; portal.classList.add('mw-custom-collapsible'); // State from storage 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) {} } // A11y & interactions 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 load and on SPA-like updates if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', bootCollapsibleSidebars); } else { bootCollapsibleSidebars(); } mw.hook('wikipage.content').add(bootCollapsibleSidebars);
var COLLAPSIBLE_SECTIONS = ['book1', 'book2'];
var DEFAULT_OPEN = false;
 
mw.loader.using(['mediawiki.util']).then(function () {
 
  function initCollapsiblePortlet(name) {
    var portal = document.getElementById('p-' + name);
    if (!portal) {
      console.warn('CollapsibleSidebar: Portal not found for', name);
      return;
    }
 
    // Try multiple selectors for compatibility
    var heading = portal.querySelector('.vector-menu-heading, h3');
    var content = portal.querySelector('.vector-menu-content, .body');
 
    if (!heading) {
      console.warn('CollapsibleSidebar: Heading not found in portal', name, portal);
      return;
    }
    if (!content) {
      console.warn('CollapsibleSidebar: Content not found in portal', name, portal);
      return;
    }
 
    console.log('CollapsibleSidebar: Initializing portal', name);
 
    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) {
      console.log('CollapsibleSidebar: Setting state', name, 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() {
    console.log('CollapsibleSidebar: Booting for sections', COLLAPSIBLE_SECTIONS);
    COLLAPSIBLE_SECTIONS.forEach(initCollapsiblePortlet);
  }
 
  $(bootCollapsibleSidebars);
  mw.hook('wikipage.content').add(bootCollapsibleSidebars);
 
});

Revision as of 09:59, 25 August 2025

* Collapsible sidebar sections for MW 1.44 (Vector & Vector 2022) List the exact section keys from MediaWiki:Sidebar here: */ var COLLAPSIBLE_SECTIONS = [ 'book1', 'book2' ]; /* Default open/closed; set true to open by default */ var DEFAULT_OPEN = false; function initCollapsiblePortlet(name) { var portal = document.getElementById('p-' + name); if (!portal) return; // Choose heading/content across skins var heading = portal.querySelector('.vector-menu-heading') || portal.querySelector('h3'); var content = portal.querySelector('.vector-menu-content') || portal.querySelector('.body'); if (!heading || !content) return; portal.classList.add('mw-custom-collapsible'); // State from storage 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) {} } // A11y & interactions 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 load and on SPA-like updates if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', bootCollapsibleSidebars); } else { bootCollapsibleSidebars(); } mw.hook('wikipage.content').add(bootCollapsibleSidebars);