Jump to content

MediaWiki:Common.js

From Yusupov's House
Revision as of 19:15, 2 October 2025 by Mvuijlst (talk | contribs)

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.
/* Promote infobox image to a 10:7 hero above the title (touch devices only) */
(function () {
  if (!window.matchMedia || !matchMedia('(pointer: coarse)').matches) return;
  if (document.querySelector('.article-hero')) return;

  function run() {
    // Find the infobox image cell
    var imgCell = document.querySelector('table.infobox .infobox-image-cell');
    if (!imgCell) return;

    // What we'll move: prefer a wrapper (span/a/figure) so we keep the link
    var moveNode =
      imgCell.querySelector('span, figure, a, picture, img') || imgCell.firstElementChild;
    if (!moveNode) return;

    // Where to put the hero
    var header = document.querySelector('.mw-body-header') || document.getElementById('firstHeading');
    if (!header) return;

    // Create the hero *first* so space is reserved immediately
    var hero = document.createElement('div');
    hero.className = 'article-hero';
    header.parentNode.insertBefore(hero, header);

    // Batch DOM mutation in the next frame to avoid extra reflows
    requestAnimationFrame(function () {
      // Move (not clone) the existing node — avoids a second download/decoding
      hero.appendChild(moveNode);

      // Optional: mark infobox for any extra styling you might want later
      var infobox = imgCell.closest('table.infobox');
      if (infobox) infobox.classList.add('infobox--image-promoted');
    });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', run, { once: true });
  } else {
    run();
  }
})();


/* Quick facts: collapse infobox under first paragraph on touch devices */
mw.hook('wikipage.content').add(function ($content) {
  // Only on touch devices
  if (!window.matchMedia || !window.matchMedia('(pointer: coarse)').matches) return;

  var $ibox = $content.find('table.infobox').first();
  if (!$ibox.length) return;

  // First real paragraph in article content
  var $firstP = $content
    .find('.mw-parser-output > p')
    .filter(function () { return $(this).text().trim().length > 0; })
    .first();
  if (!$firstP.length) return;

  // Build <details> wrapper
  var $details = $('<details>', { 'class': 'quickfacts', 'aria-label': 'Quick facts' });
  var $summary = $('<summary>', { 'class': 'quickfacts-summary', text: 'Quick facts' });

  $details.append($summary);

  // Mark infobox so CSS can remove float/width inside quickfacts
  $ibox.addClass('infobox--in-quickfacts');

  // Move the infobox inside the details (not cloning; avoids duplicate content for SR)
  $details.append($ibox);

  // Insert directly after the first paragraph
  $details.insertAfter($firstP);
});