MediaWiki:Common.js

From TetrisWiki
Jump to navigation Jump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: 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 () {
	/**
	 * Dynamic Navigation Bars (experimental)
	 *
	 * Description: See [[Wikipedia:NavFrame]].
	 * Maintainers: UNMAINTAINED
	 */

	var collapseCaption = 'hide';
	var expandCaption = 'show';

	// Set up the words in your language
	var navigationBarHide = '[' + collapseCaption + ']';
	var navigationBarShow = '[' + expandCaption + ']';

	/**
	 * Shows and hides content and picture (if available) of navigation bars.
	 *
	 * @param {number} indexNavigationBar The index of navigation bar to be toggled
	 * @param {jQuery.Event} event Event object
	 * @return {boolean}
	 */
	function toggleNavigationBar( indexNavigationBar, event ) {
		var navToggle = document.getElementById( 'NavToggle' + indexNavigationBar );
		var navFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
		var navChild;

		if ( !navFrame || !navToggle ) {
			return false;
		}

		// If shown now
		if ( navToggle.firstChild.data === navigationBarHide ) {
			for ( navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling ) {
				if ( $( navChild ).hasClass( 'NavContent' ) ) {
					navChild.style.display = 'none';
				}
			}
			navToggle.firstChild.data = navigationBarShow;

		// If hidden now
		} else if ( navToggle.firstChild.data === navigationBarShow ) {
			for ( navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling ) {
				if ( $( navChild ).hasClass( 'NavContent' ) ) {
					navChild.style.display = 'block';
				}
			}
			navToggle.firstChild.data = navigationBarHide;
		}

		event.preventDefault();
	}

	/**
	 * Adds show/hide-button to navigation bars.
	 *
	 * @param {jQuery} $content
	 */
	function createNavigationBarToggleButton( $content ) {
		var j, navChild, navToggle, navToggleText, isCollapsed,
			indexNavigationBar = 0;
		// Iterate over all < div >-elements
		var $divs = $content.find( 'div.NavFrame:not(.mw-collapsible)' );
		$divs.each( function ( i, navFrame ) {
			indexNavigationBar++;
			navToggle = document.createElement( 'a' );
			navToggle.className = 'NavToggle';
			navToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
			navToggle.setAttribute( 'href', '#' );
			$( navToggle ).on( 'click', $.proxy( toggleNavigationBar, null, indexNavigationBar ) );

			isCollapsed = $( navFrame ).hasClass( 'collapsed' );
			/**
			 * Check if any children are already hidden.  This loop is here for backwards compatibility:
			 * the old way of making NavFrames start out collapsed was to manually add style="display:none"
			 * to all the NavPic/NavContent elements.  Since this was bad for accessibility (no way to make
			 * the content visible without JavaScript support), the new recommended way is to add the class
			 * "collapsed" to the NavFrame itself, just like with collapsible tables.
			 */
			for ( navChild = navFrame.firstChild; navChild !== null && !isCollapsed; navChild = navChild.nextSibling ) {
				if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
					if ( navChild.style.display === 'none' ) {
						isCollapsed = true;
					}
				}
			}
			if ( isCollapsed ) {
				for ( navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling ) {
					if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
						navChild.style.display = 'none';
					}
				}
			}
			navToggleText = document.createTextNode( isCollapsed ? navigationBarShow : navigationBarHide );
			navToggle.appendChild( navToggleText );

			// Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
			for ( j = 0; j < navFrame.childNodes.length; j++ ) {
				if ( $( navFrame.childNodes[ j ] ).hasClass( 'NavHead' ) ) {
					navToggle.style.color = navFrame.childNodes[ j ].style.color;
					navFrame.childNodes[ j ].appendChild( navToggle );
				}
			}
			navFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
		} );
	}

	mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
} );
/* DO NOT ADD CODE BELOW THIS LINE */