MediaWiki:Common.js: Difference between revisions

From New wiki
Jump to navigation Jump to search
Content deleted Content added
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 279: Line 279:
})();
})();


/* 1. Reset the wrapper to be a simple container */
/* Force the wrapper to handle layout */
.quiet-btn-wrapper {
.quiet-btn-wrapper {
display: inline-block;
display: inline-block !important;
padding: 0 !important;
/* No padding here, we want the link inside to handle the size */
}
}


/* 2. Style the actual link inside to look like the Button */
/* Target the link inside with Maximum Priority */
/* We use #mw-content-text to overpower default wiki styles */
.quiet-btn-wrapper a {
#mw-content-text .quiet-btn-wrapper a,
/* Layout & Box Model */
.quiet-btn-wrapper a,
display: inline-flex; /* Ensures vertical centering */
.quiet-btn-wrapper a.external {
align-items: center;
box-sizing: border-box;
/* 1. FORCE THE BOX SHAPE */
min-height: 32px; /* Exact Codex button height */
display: inline-block !important;
padding: 0 12px; /* Standard horizontal padding */
min-height: 32px !important;
border-radius: 2px; /* The distinct 'square-ish' rounded corners */
padding: 6px 12px !important;
border: 1px solid transparent; /* Keeps sizing consistent with bordered buttons */
box-sizing: border-box !important;

border-radius: 2px !important;
/* Typography */
border: 1px solid transparent !important;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Lato, Helvetica, Arial, sans-serif;
font-size: 0.875rem; /* 14px - Exact interface size */
/* 2. FORCE THE COLORS (Overrides the Blue Link) */
font-weight: 700; /* Bold text */
background-color: #f8f9fa !important;
line-height: 1.6; /* Standard line-height */
color: #202122 !important;
text-decoration: none; /* No underline */
text-decoration: none !important;

background-image: none !important; /* Removes external link icon */
/* Default Colors (Codex Neutral Quiet) */
background-color: #f8f9fa; /* Light gray background */
/* 3. FONTS */
color: #202122 !important; /* Dark gray/black text (forced) */
font-family: sans-serif !important;

/* Smooth Transition Effect */
font-size: 14px !important; /* 0.875rem */
font-weight: 700 !important;
transition: background-color 100ms ease, color 100ms ease, border-color 100ms ease, box-shadow 100ms ease;
cursor: pointer;
line-height: 1.4 !important;
}
}


/* HOVER EFFECT */
/* 3. Hover State (Mouse over) */
#mw-content-text .quiet-btn-wrapper a:hover,
.quiet-btn-wrapper a:hover {
.quiet-btn-wrapper a:hover {
background-color: #eaecf0; /* Slightly darker gray */
background-color: #eaecf0 !important;
color: #202122 !important;
color: #202122 !important;
text-decoration: none;
text-decoration: none !important;
}

/* 4. Active State (Clicking down) */
.quiet-btn-wrapper a:active {
background-color: #c8ccd1; /* Even darker gray for click feedback */
box-shadow: none;
}

/* 5. Focus State (Accessibility/Keyboard navigation) */
.quiet-btn-wrapper a:focus {
outline: 2px solid #36c; /* Standard Wiki focus blue */
outline-offset: 2px;
}
}



Revision as of 04:58, 30 November 2025

/* Any JavaScript here will be loaded for all users on every page load. */
/**
 * Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
 * loaded for all users on every wiki page. If possible create a gadget that is
 * enabled by default instead of adding it here (since gadgets are fully
 * optimized ResourceLoader modules with possibility to add dependencies etc.)
 *
 * Since Common.js isn't a gadget, there is no place to declare its
 * dependencies, so we have to lazy load them with mw.loader.using on demand and
 * then execute the rest in the callback. In most cases these dependencies will
 * be loaded (or loading) already and the callback will not be delayed. In case a
 * dependency hasn't arrived yet it'll make sure those are loaded before this.
 */

/* global mw, $ */
/* jshint strict:false, browser:true */

mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
	/* Begin of mw.loader.using callback */

	/**
	 * Map addPortletLink to mw.util
	 * @deprecated: Use mw.util.addPortletLink instead.
	 */
	mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );

	/**
	 * @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
	 * @rev 6
	 */
	var extraCSS = mw.util.getParamValue( 'withCSS' ),
		extraJS = mw.util.getParamValue( 'withJS' );

	if ( extraCSS ) {
		if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
			mw.loader.load( '/w/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
		}
	}

	if ( extraJS ) {
		if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
			mw.loader.load( '/w/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
		}
	}

	/**
	 * Collapsible tables; reimplemented with mw-collapsible
	 * Styling is also in place to avoid FOUC
	 *
	 * Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
	 * @version 3.0.0 (2018-05-20)
	 * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
	 * @author [[User:R. Koot]]
	 * @author [[User:Krinkle]]
	 * @author [[User:TheDJ]]
	 * @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
	 * is supported in MediaWiki core. Shimmable since MediaWiki 1.32
	 *
	 * @param {jQuery} $content
	 */
	function makeCollapsibleMwCollapsible( $content ) {
		var $tables = $content
			.find( 'table.collapsible:not(.mw-collapsible)' )
			.addClass( 'mw-collapsible' );

		$.each( $tables, function ( index, table ) {
			// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
			if ( $( table ).hasClass( 'collapsed' ) ) {
				$( table ).addClass( 'mw-collapsed' );
				// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
			}
		} );
		if ( $tables.length > 0 ) {
			mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
				$tables.makeCollapsible();
			} );
		}
	}
	mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );

	/**
	 * Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
	 *
	 * Maintainers: TheDJ
	 */
	function mwCollapsibleSetup( $collapsibleContent ) {
		var $element,
			$toggle,
			autoCollapseThreshold = 2;
		$.each( $collapsibleContent, function ( index, element ) {
			$element = $( element );
			if ( $element.hasClass( 'collapsible' ) ) {
				$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
			}
			if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
				$element.data( 'mw-collapsible' ).collapse();
			} else if ( $element.hasClass( 'innercollapse' ) ) {
				if ( $element.parents( '.outercollapse' ).length > 0 ) {
					$element.data( 'mw-collapsible' ).collapse();
				}
			}
			// because of colored backgrounds, style the link in the text color
			// to ensure accessible contrast
			$toggle = $element.find( '.mw-collapsible-toggle' );
			if ( $toggle.length ) {
				// Make the toggle inherit text color (Updated for T333357 2023-04-29)
				if ( $toggle.parent()[ 0 ].style.color ) {
					$toggle.css( 'color', 'inherit' );
					$toggle.find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
				}
			}
		} );
	}

	mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );

	/* End of mw.loader.using callback */
} );
// CapSach — Mobile TOC overlay (all skins; phone widths)
(function () {
  // Don’t run on very wide screens (tablet/desktop have native TOC)
  if (window.matchMedia('(min-width: 768px)').matches) return;

  // Only on normal content pages
  if (window.mw && mw.config && mw.config.get) {
    var isArticle = !!mw.config.get('wgIsArticle');
    if (!isArticle) return;
  }

  // Find the content root; MobileFrontend restructures DOM, so be flexible
  var root =
    document.querySelector('#mw-content-text .mw-parser-output') ||
    document.querySelector('.mw-parser-output') ||
    document.getElementById('mw-content-text') ||
    document.querySelector('#content') ||
    document.body;

  // Collect headings (H2–H6). Prefer spans with .mw-headline (stable anchor ids)
  var items = [];
  var headings = root.querySelectorAll('h2, h3, h4, h5, h6');
  headings.forEach(function (h) {
    var level = parseInt(h.tagName.slice(1), 10);
    if (level < 2 || level > 6) return;
    var headline = h.querySelector('.mw-headline') || h;
    var id = headline.id || h.id;
    var text = (headline.textContent || h.textContent || '').trim();
    if (!id || !text) return;
    items.push({ id: id, text: text, level: level });
  });

  // Show only if there are enough headings to be useful (match core default)
  if (items.length < 3) return;

  // Create trigger button (bottom-left; avoids “Back to top” on bottom-right)
  var btn = document.createElement('button');
  btn.id = 'cps-open-toc';
  btn.type = 'button';
  btn.setAttribute('aria-label', 'Open table of contents');
  btn.innerHTML = '<span class="icon" aria-hidden="true">≡</span><span class="label">TOC</span>';
  document.body.appendChild(btn);

  // Overlay + panel
  var overlay = document.createElement('div');
  overlay.id = 'cps-toc-overlay';
  overlay.setAttribute('aria-hidden', 'true');

  var panel = document.createElement('div');
  panel.id = 'cps-toc-panel';
  panel.setAttribute('role', 'dialog');
  panel.setAttribute('aria-modal', 'true');
  panel.setAttribute('aria-label', 'Table of contents');

  var header = document.createElement('div');
  header.id = 'cps-toc-header';
  header.innerHTML =
    '<h2 id="cps-toc-title">Contents</h2>' +
    '<button id="cps-toc-close" type="button" aria-label="Close">×</button>';

  var list = document.createElement('ul');
  list.id = 'cps-toc-list';

  items.forEach(function (it) {
    var li = document.createElement('li');
    li.setAttribute('data-level', String(it.level));
    var a = document.createElement('a');
    a.href = '#' + it.id;
    a.textContent = it.text;
    li.appendChild(a);
    list.appendChild(li);
  });

  panel.appendChild(header);
  panel.appendChild(list);
  overlay.appendChild(panel);
  document.body.appendChild(overlay);

  // Focus handling
  var lastFocus = null;
  function openOverlay() {
    lastFocus = document.activeElement;
    overlay.classList.add('is-open');
    overlay.setAttribute('aria-hidden', 'false');
    document.body.style.overflow = 'hidden';
    // Focus first link for accessibility
    var firstLink = list.querySelector('a');
    if (firstLink) firstLink.focus({ preventScroll: true });
  }
  function closeOverlay() {
    overlay.classList.remove('is-open');
    overlay.setAttribute('aria-hidden', 'true');
    document.body.style.overflow = '';
    if (lastFocus && lastFocus.focus) lastFocus.focus({ preventScroll: true });
  }

  btn.style.display = 'flex'; // reveal trigger now that we know we have headings
  btn.addEventListener('click', openOverlay);

  overlay.addEventListener('click', function (e) {
    // Click outside the bottom sheet closes
    if (e.target === overlay) closeOverlay();
  });
  overlay.querySelector('#cps-toc-close').addEventListener('click', closeOverlay);

  overlay.addEventListener('keydown', function (e) {
    if (e.key === 'Escape') closeOverlay();
  });

  // Navigate and try to ensure mobile-collapsed sections are visible
  list.addEventListener('click', function (e) {
    var a = e.target.closest('a');
    if (!a) return;
    e.preventDefault();

    var targetId = a.getAttribute('href').slice(1);
    var target = document.getElementById(targetId);
    closeOverlay();

    if (target) {
      try {
        target.scrollIntoView({ behavior: 'smooth', block: 'start' });
      } catch (_) {
        target.scrollIntoView(true);
      }

      // Update URL hash after a tick (so browser back works)
      setTimeout(function () {
        if (history && history.replaceState) {
          history.replaceState(null, '', '#' + targetId);
        } else {
          location.hash = targetId;
        }
      }, 200);

      // MobileFrontend: headings may be inside collapsed sections.
      // Heuristic: click the nearest toggle if present.
      var maybeToggle = target.closest('.collapsible-block, .mf-section') ||
                        target.closest('section');
      if (maybeToggle && maybeToggle.classList.contains('collapsed')) {
        // Try to open; fallback by clicking the first heading inside
        var headingToggle = maybeToggle.querySelector('.section-heading, h2, h3, h4, h5, h6');
        if (headingToggle) headingToggle.click();
      }
    }
  });

  // Re-hide on rotation/resize to tablet/desktop
  window.addEventListener('resize', function () {
    if (window.matchMedia('(min-width: 768px)').matches) {
      btn.style.display = 'none';
      closeOverlay();
    } else {
      btn.style.display = 'flex';
    }
  }, { passive: true });
})();

/* Force the wrapper to handle layout */
.quiet-btn-wrapper {
    display: inline-block !important;
    padding: 0 !important;
}

/* Target the link inside with Maximum Priority */
/* We use #mw-content-text to overpower default wiki styles */
#mw-content-text .quiet-btn-wrapper a,
.quiet-btn-wrapper a, 
.quiet-btn-wrapper a.external {
    
    /* 1. FORCE THE BOX SHAPE */
    display: inline-block !important; 
    min-height: 32px !important;
    padding: 6px 12px !important;
    box-sizing: border-box !important;
    border-radius: 2px !important;
    border: 1px solid transparent !important;
    
    /* 2. FORCE THE COLORS (Overrides the Blue Link) */
    background-color: #f8f9fa !important;
    color: #202122 !important;
    text-decoration: none !important;
    background-image: none !important; /* Removes external link icon */
    
    /* 3. FONTS */
    font-family: sans-serif !important;
    font-size: 14px !important; /* 0.875rem */
    font-weight: 700 !important;
    line-height: 1.4 !important;
}

/* HOVER EFFECT */
#mw-content-text .quiet-btn-wrapper a:hover,
.quiet-btn-wrapper a:hover {
    background-color: #eaecf0 !important;
    color: #202122 !important;
    text-decoration: none !important;
}

/* DO NOT ADD CODE BELOW THIS LINE */