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:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using(['mediawiki.util']).then(function () {
  function initCollapsiblePortlet(name) {
    var portal = document.getElementById('p-' + name);
    if (!portal) return;


/* Collapsible sidebar sections for MW 1.44 (Vector & Vector 2022)
    var heading = portal.querySelector('.vector-menu-heading, h3');
  List the exact section keys from MediaWiki:Sidebar here: */
    var content = portal.querySelector('.vector-menu-content, .body');
var COLLAPSIBLE_SECTIONS = [ 'book1', 'book2' ];
    if (!heading || !content) return;


/* Default open/closed; set true to open by default */
    // Debugging: confirm elements
var DEFAULT_OPEN = false;
    console.log('Found portal:', name, heading, content);


function initCollapsiblePortlet(name) {
    // Rest of your code unchanged...
  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
   function bootCollapsibleSidebars() {
  var storageKey = 'mwSidebar:' + name;
     COLLAPSIBLE_SECTIONS.forEach(initCollapsiblePortlet);
  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
   mw.hook('wikipage.content').add(bootCollapsibleSidebars);
  heading.setAttribute('role', 'button');
   $(bootCollapsibleSidebars);
  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);

Revision as of 14:59, 24 August 2025

/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using(['mediawiki.util']).then(function () {
  function initCollapsiblePortlet(name) {
    var portal = document.getElementById('p-' + name);
    if (!portal) return;

    var heading = portal.querySelector('.vector-menu-heading, h3');
    var content = portal.querySelector('.vector-menu-content, .body');
    if (!heading || !content) return;

    // Debugging: confirm elements
    console.log('Found portal:', name, heading, content);

    // Rest of your code unchanged...
  }

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

  mw.hook('wikipage.content').add(bootCollapsibleSidebars);
  $(bootCollapsibleSidebars);
});