Jump to content

MediaWiki:Common.js

From the Vrienden Universe, a fictional wiki

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.
/**
 * 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 */
} );

/**
 * Add Vrienden Universe custom themes to Vector 2022's color options.
 *
 * Vector exposes os/day/night as native client preferences. Custom themes
 * select Vector's night mode underneath, then add their own root CSS class.
 */
( function () {
	'use strict';

	var root = document.documentElement,
		themes = [
			{
				name: 'Noord',
				value: 'noord',
				storageKey: 'vu-theme-noord',
				rootClass: 'vu-theme-noord'
			},
			{
				name: 'Paap',
				value: 'paap',
				storageKey: 'vu-theme-paap',
				rootClass: 'vu-theme-paap'
			},
			{
				name: 'Van Hetten',
				value: 'van-hetten',
				storageKey: 'vu-theme-van-hetten',
				rootClass: 'vu-theme-van-hetten'
			},
			{
				name: 'Hoos',
				value: 'hoos',
				storageKey: 'vu-theme-hoos',
				rootClass: 'vu-theme-hoos'
			},
			{
				name: 'Schroeter',
				value: 'schroeter',
				storageKey: 'vu-theme-schroeter',
				rootClass: 'vu-theme-schroeter'
			},
			{
				name: 'Hugerinus',
				value: 'hugerinus',
				storageKey: 'vu-theme-hugerinus',
				rootClass: 'vu-theme-hugerinus'
			}
		];

	function inputId( theme ) {
		return 'skin-client-pref-skin-theme-value-' + theme.value;
	}

	function isCustomThemeValue( value ) {
		var i;

		for ( i = 0; i < themes.length; i++ ) {
			if ( themes[ i ].value === value ) {
				return true;
			}
		}

		return false;
	}

	function readStoredTheme() {
		var i;

		try {
			for ( i = themes.length - 1; i >= 0; i-- ) {
				if ( window.localStorage.getItem( themes[ i ].storageKey ) === '1' ) {
					return themes[ i ];
				}
			}
		} catch ( error ) {
			/* Continue without a stored custom theme. */
		}

		return null;
	}

	function applyTheme( selectedTheme ) {
		var i,
			theme,
			enabled;

		for ( i = 0; i < themes.length; i++ ) {
			theme = themes[ i ];
			enabled = selectedTheme === theme;
			root.classList.toggle( theme.rootClass, enabled );

			try {
				if ( enabled ) {
					window.localStorage.setItem( theme.storageKey, '1' );
				} else {
					window.localStorage.removeItem( theme.storageKey );
				}
			} catch ( error ) {
				/* The selected theme still works when storage is blocked. */
			}
		}
	}

	function makeThemeControl( theme, nightInput ) {
		var container = document.createElement( 'div' ),
			input = document.createElement( 'input' ),
			icon = document.createElement( 'span' ),
			label = document.createElement( 'label' ),
			labelText = document.createElement( 'span' );

		container.className = 'cdx-radio';
		input.className = 'cdx-radio__input';
		input.type = 'radio';
		input.name = nightInput.name;
		input.id = inputId( theme );
		input.value = theme.value;
		input.setAttribute( 'data-event-name', input.id );
		icon.className = 'cdx-radio__icon';
		label.className = 'cdx-label cdx-radio__label';
		label.setAttribute( 'for', input.id );
		labelText.className = 'cdx-label__label__text';
		labelText.textContent = theme.name;

		label.appendChild( labelText );
		container.appendChild( input );
		container.appendChild( icon );
		container.appendChild( label );

		input.addEventListener( 'change', function () {
			if ( !input.checked ) {
				return;
			}
			nightInput.click();
			input.checked = true;
			applyTheme( theme );
		} );

		return container;
	}

	function mountControls() {
		var nightInput = document.getElementById( 'skin-client-pref-skin-theme-value-night' ),
			storedTheme = readStoredTheme(),
			form,
			control,
			inputs,
			i,
			theme,
			selectedInput;

		if ( !nightInput ) {
			return;
		}

		form = nightInput.form || nightInput.closest( 'form' );
		if ( !form ) {
			return;
		}

		for ( i = 0; i < themes.length; i++ ) {
			theme = themes[ i ];
			if ( !document.getElementById( inputId( theme ) ) ) {
				control = makeThemeControl( theme, nightInput );
				form.appendChild( control );
			}
		}

		if ( form.getAttribute( 'data-vu-theme-listeners' ) !== '1' ) {
			form.setAttribute( 'data-vu-theme-listeners', '1' );
			inputs = form.querySelectorAll( 'input[type="radio"]' );
			for ( i = 0; i < inputs.length; i++ ) {
				if ( !isCustomThemeValue( inputs[ i ].value ) ) {
					inputs[ i ].addEventListener( 'change', function ( event ) {
						if ( event.target.checked ) {
							applyTheme( null );
						}
					} );
				}
			}
		}

		if ( storedTheme ) {
			if ( !root.classList.contains( 'skin-theme-clientpref-night' ) ) {
				nightInput.click();
			}
			selectedInput = document.getElementById( inputId( storedTheme ) );
			if ( selectedInput ) {
				selectedInput.checked = true;
			}
			applyTheme( storedTheme );
		}
	}

	applyTheme( readStoredTheme() );

	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', mountControls );
	} else {
		mountControls();
	}

	new MutationObserver( mountControls ).observe( document.documentElement, {
		childList: true,
		subtree: true
	} );
}() );
/* DO NOT ADD CODE BELOW THIS LINE */