MediaWiki:Common.js: Difference between revisions

From Artifacts of Capitalism
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 1: Line 1:
// Replace the Chameleon search-box placeholder and button text
mw.loader.using('mediawiki.util').then(function () {
$(function() {
   function $(s, r){ return (r||document).querySelector(s); }
   // input field
  var inp = document.querySelector('#searchInput, input[name="search"]');
  if ( inp ) {
    inp.placeholder = 'Find an artifact…';    // your new placeholder
  }
  // submit button (magnifying-glass)
  var btn = document.querySelector('#searchButton, button[type="submit"].mw-searchButton');
  if ( btn ) {
    btn.textContent = 'Go';                   // your new label, if you like
  }
});


// Tag login + create-account pages so CSS can target reliably
   var toggler = document.querySelector('.p-navbar .navbar-toggler');
mw.loader.using([]).then(function () {
   if (!toggler) return;
   var sp = mw.config.get('wgCanonicalSpecialPageName');
  if (sp === 'Userlogin' || sp === 'CreateAccount') {
    document.documentElement.classList.add('aoc-auth');
    document.body.classList.add('aoc-auth');
   }
});
mw.loader.using(['mediawiki.util'], function () {
  function qs(sel){ return document.querySelector(sel); }


   var toggler = qs('.p-navbar .navbar-toggler');
  // Resolve the collapse element the button controls
  var collapse = qs('.p-navbar .navbar-collapse');
   var targetSel =
  var search = qs('.p-navbar #p-search');
    toggler.getAttribute('data-target') ||
    toggler.getAttribute('data-bs-target') ||
    (toggler.getAttribute('aria-controls') ? ('#' + toggler.getAttribute('aria-controls')) : null);


   // Drawer toggle
   var collapse = targetSel ? document.querySelector(targetSel) : document.querySelector('.p-navbar .navbar-collapse');
  if (toggler && collapse) {
  if (!collapse) return;
    toggler.addEventListener('click', function (e) {
      e.preventDefault();
      var isOpen = collapse.classList.toggle('show');
      document.body.classList.toggle('offcanvas-open', isOpen);
    });
  }


   // Click outside drawer to close
   // Toggle drawer
  toggler.addEventListener('click', function (e) {
    e.preventDefault();
    var willOpen = !collapse.classList.contains('show');
    collapse.classList.toggle('show', willOpen);
    document.body.classList.toggle('offcanvas-open', willOpen);
    toggler.setAttribute('aria-expanded', String(willOpen));
  });
 
  // Close when clicking outside or pressing Esc
   document.addEventListener('click', function (e) {
   document.addEventListener('click', function (e) {
     if (!collapse || !collapse.classList.contains('show')) return;
     if (!collapse.classList.contains('show')) return;
     var inside = collapse.contains(e.target) || (toggler && toggler.contains(e.target));
     var inside = collapse.contains(e.target) || toggler.contains(e.target);
     if (!inside) {
     if (!inside) {
       collapse.classList.remove('show');
       collapse.classList.remove('show');
       document.body.classList.remove('offcanvas-open');
       document.body.classList.remove('offcanvas-open');
      toggler.setAttribute('aria-expanded', 'false');
    }
  });
  document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape' && collapse.classList.contains('show')) {
      collapse.classList.remove('show');
      document.body.classList.remove('offcanvas-open');
      toggler.setAttribute('aria-expanded', 'false');
     }
     }
   });
   });
  // Lightweight search overlay on mobile
  if (search) {
    var overlay = document.createElement('div');
    overlay.className = 'aoc-search-overlay';
    overlay.innerHTML = search.innerHTML; // reuse existing search form
    document.body.appendChild(overlay);
    search.addEventListener('click', function () {
      overlay.classList.add('show');
      var input = overlay.querySelector('input[type="search"], input[type="text"]');
      if (input) setTimeout(function(){ input.focus(); }, 10);
    });
    // close overlay on submit or outside click/escape
    overlay.addEventListener('submit', function(){ overlay.classList.remove('show'); });
    document.addEventListener('keydown', function(e){ if (e.key === 'Escape') overlay.classList.remove('show'); });
    document.addEventListener('click', function(e){
      if (!overlay.classList.contains('show')) return;
      if (!overlay.contains(e.target) && !search.contains(e.target)) overlay.classList.remove('show');
    });
  }
});
});

Revision as of 23:56, 23 August 2025

mw.loader.using('mediawiki.util').then(function () {
  function $(s, r){ return (r||document).querySelector(s); }

  var toggler = document.querySelector('.p-navbar .navbar-toggler');
  if (!toggler) return;

  // Resolve the collapse element the button controls
  var targetSel =
    toggler.getAttribute('data-target') ||
    toggler.getAttribute('data-bs-target') ||
    (toggler.getAttribute('aria-controls') ? ('#' + toggler.getAttribute('aria-controls')) : null);

  var collapse = targetSel ? document.querySelector(targetSel) : document.querySelector('.p-navbar .navbar-collapse');
  if (!collapse) return;

  // Toggle drawer
  toggler.addEventListener('click', function (e) {
    e.preventDefault();
    var willOpen = !collapse.classList.contains('show');
    collapse.classList.toggle('show', willOpen);
    document.body.classList.toggle('offcanvas-open', willOpen);
    toggler.setAttribute('aria-expanded', String(willOpen));
  });

  // Close when clicking outside or pressing Esc
  document.addEventListener('click', function (e) {
    if (!collapse.classList.contains('show')) return;
    var inside = collapse.contains(e.target) || toggler.contains(e.target);
    if (!inside) {
      collapse.classList.remove('show');
      document.body.classList.remove('offcanvas-open');
      toggler.setAttribute('aria-expanded', 'false');
    }
  });
  document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape' && collapse.classList.contains('show')) {
      collapse.classList.remove('show');
      document.body.classList.remove('offcanvas-open');
      toggler.setAttribute('aria-expanded', 'false');
    }
  });
});