Jump to content

MediaWiki:Common.js: Difference between revisions

From OnTrackWorld
Pin sidebar open by default for all visitors
Pin sidebar open by default for all visitors
Line 19: Line 19:
   viewports, where a permanently-open sidebar would crowd out the page content. */
   viewports, where a permanently-open sidebar would crowd out the page content. */
( function () {
( function () {
  var attempts = 0;
   function pinMainMenu() {
  var MAX_ATTEMPTS = 32; /* ~8s of enforcement, in case Vector's own later init resets it */
     if ( window.innerWidth < 1000 ) { return; }
   function tick() {
     if ( document.documentElement.classList.contains( 'vector-feature-main-menu-pinned-enabled' ) ) { return; }
     attempts++;
    var pinnedContainer = document.getElementById( 'vector-main-menu-pinned-container' );
    /* window.innerWidth is checked fresh every tick, not once up front -- an early
    var realMenu = document.getElementById( 'vector-main-menu' );
      tick reading a not-yet-settled viewport must not permanently abandon the loop.
    /* Clicking Vector's own pin button relies on its Vue click handler being wired up,
      Re-check and re-click every tick rather than stopping at first success: the
      which isn't reliably ready this early in page load. Instead, replicate what that
      button exists in the DOM from initial render but Vector wires up its click
      handler does directly: the real menu content already lives in the DOM (inside the
      handler, and re-applies its own pin/unpin state from the (empty, for a fresh
      header's collapsed dropdown) -- move it into the always-present pinned-sidebar
      visitor) saved preference, slightly later -- an early click can silently no-op
      slot and flip the one class Vector's CSS keys off of to lay it out as a column. */
      or get overwritten right after. Re-asserting for a few seconds outlasts that. */
    if ( pinnedContainer && realMenu && pinnedContainer.children.length === 0 ) {
     if ( window.innerWidth >= 1000 &&
      pinnedContainer.appendChild( realMenu );
      !document.documentElement.classList.contains( 'vector-feature-main-menu-pinned-enabled' ) ) {
       document.documentElement.classList.remove( 'vector-feature-main-menu-pinned-disabled' );
      var btn = document.querySelector( '#vector-main-menu-dropdown .vector-pinnable-header-pin-button' );
       document.documentElement.classList.add( 'vector-feature-main-menu-pinned-enabled' );
      if ( btn ) { btn.click(); }
      if ( window.console && window.console.log ) {
        console.log( '[otw-pin] tick ' + attempts + ': width=' + window.innerWidth + ' btnFound=' + ( !!btn ) );
       }
    }
    if ( attempts < MAX_ATTEMPTS ) {
       setTimeout( tick, 250 );
     }
     }
   }
   }
   if ( document.readyState === 'loading' ) {
   if ( document.readyState === 'loading' ) {
     document.addEventListener( 'DOMContentLoaded', tick );
     document.addEventListener( 'DOMContentLoaded', pinMainMenu );
   } else {
   } else {
     tick();
     pinMainMenu();
   }
   }
}() );
}() );

Revision as of 19:36, 14 September 2026

/* OPTIONAL. Append to MediaWiki:Common.js only if you want inline highlight-and-annotate
   on long-form pages (white paper, book chapters). Loads the Hypothesis client on pages
   tagged with the "Annotate" category. Readers need a free Hypothesis account to annotate;
   anyone can read annotations. Remove this block to turn it off. */
( function () {
  if ( mw.config.get( 'wgCategories' ).indexOf( 'Annotate' ) === -1 ) { return; }
  if ( mw.config.get( 'wgAction' ) !== 'view' ) { return; }
  var s = document.createElement( 'script' );
  s.src = 'https://hypothes.is/embed.js';
  s.async = true;
  document.head.appendChild( s );
}() );

/* ---- Pin the sidebar (Main menu) open by default for every visitor -----------------
   Vector 2022 only shows Main menu as a permanent left column once a user pins it
   (client preference, saved per-account/browser). This triggers that same built-in
   pin action automatically on every page load, so it defaults to open for everyone
   instead of only whoever has pinned it themselves. Skipped on narrow/mobile
   viewports, where a permanently-open sidebar would crowd out the page content. */
( function () {
  function pinMainMenu() {
    if ( window.innerWidth < 1000 ) { return; }
    if ( document.documentElement.classList.contains( 'vector-feature-main-menu-pinned-enabled' ) ) { return; }
    var pinnedContainer = document.getElementById( 'vector-main-menu-pinned-container' );
    var realMenu = document.getElementById( 'vector-main-menu' );
    /* Clicking Vector's own pin button relies on its Vue click handler being wired up,
       which isn't reliably ready this early in page load. Instead, replicate what that
       handler does directly: the real menu content already lives in the DOM (inside the
       header's collapsed dropdown) -- move it into the always-present pinned-sidebar
       slot and flip the one class Vector's CSS keys off of to lay it out as a column. */
    if ( pinnedContainer && realMenu && pinnedContainer.children.length === 0 ) {
      pinnedContainer.appendChild( realMenu );
      document.documentElement.classList.remove( 'vector-feature-main-menu-pinned-disabled' );
      document.documentElement.classList.add( 'vector-feature-main-menu-pinned-enabled' );
    }
  }
  if ( document.readyState === 'loading' ) {
    document.addEventListener( 'DOMContentLoaded', pinMainMenu );
  } else {
    pinMainMenu();
  }
}() );