MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
(Removed outdated hook possibly)
Tag: Reverted
(Rebuilding from scratch)
Tags: Mobile edit Mobile web edit
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
/**
/**
* Collapsible tables
* 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]].
*
*
* Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
* @version 2.0.3 (2014-03-14)
* @version 3.0.0 (2018-05-20)
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
* @author [[User:R. Koot]]
* @author [[User:R. Koot]]
* @author [[User:Krinkle]]
* @author [[User:Krinkle]]
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
* @author [[User:TheDJ]]
* is supported in MediaWiki core.
* @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' );


var autoCollapse = 2;
$.each( $tables, function ( index, table ) {
var collapseCaption = 'hide';
// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
var expandCaption = 'show';
if ( $( table ).hasClass( 'collapsed' ) ) {
var tableIndex = 0;
$( table ).addClass( 'mw-collapsed' );
 
// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
function collapseTable( tableIndex ) {
}
    var Button = document.getElementById( 'collapseButton' + tableIndex );
} );
    var Table = document.getElementById( 'collapsibleTable' + tableIndex );
if ( $tables.length > 0 ) {
mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
$tables.makeCollapsible();
} );
}
}
mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );


    if ( !Table || !Button ) {
/**
        return false;
* Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
    }
*
 
* Maintainers: TheDJ
    var Rows = Table.rows;
*/
    var i;
function mwCollapsibleSetup( $collapsibleContent ) {
    var $row0 = $(Rows[0]);
var $element,
 
$toggle,
    if ( Button.firstChild.data === collapseCaption ) {
autoCollapseThreshold = 2;
        for ( i = 1; i < Rows.length; i++ ) {
$.each( $collapsibleContent, function ( index, element ) {
            Rows[i].style.display = 'none';
$element = $( element );
        }
if ( $element.hasClass( 'collapsible' ) ) {
        Button.firstChild.data = expandCaption;
$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
    } else {
        for ( i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = $row0.css( 'display' );
        }
        Button.firstChild.data = collapseCaption;
    }
}
 
function createClickHandler( tableIndex ) {
    return function ( e ) {
        e.preventDefault();
        collapseTable( tableIndex );
    };
}
 
function createCollapseButtons( $content ) {
    var NavigationBoxes = {};
    var $Tables = $content.find( 'table' );
    var i;
 
    $Tables.each( function( i, table ) {
        if ( $(table).hasClass( 'collapsible' ) ) {
 
            /* only add button and increment count if there is a header row to work with */
            var HeaderRow = table.getElementsByTagName( 'tr' )[0];
            if ( !HeaderRow ) {
                return;
            }
            var Header = table.getElementsByTagName( 'th' )[0];
            if ( !Header ) {
                return;
            }
 
            NavigationBoxes[ tableIndex ] = table;
            table.setAttribute( 'id', 'collapsibleTable' + tableIndex );
 
            var Button    = document.createElement( 'span' );
            var ButtonLink = document.createElement( 'a' );
            var ButtonText = document.createTextNode( collapseCaption );
            // Styles are declared in [[MediaWiki:Common.css]]
            Button.className = 'collapseButton';
 
            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
            ButtonLink.setAttribute( 'href', '#' );
            $( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
            ButtonLink.appendChild( ButtonText );
 
            Button.appendChild( document.createTextNode( '[' ) );
            Button.appendChild( ButtonLink );
            Button.appendChild( document.createTextNode( ']' ) );
 
            Header.insertBefore( Button, Header.firstChild );
            tableIndex++;
        }
    } );
 
    for ( i = 0;  i < tableIndex; i++ ) {
        if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||
            ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) )
        ) {
            collapseTable( i );
        }
        else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
            var element = NavigationBoxes[i];
            while ((element = element.parentNode)) {
                if ( $( element ).hasClass( 'outercollapse' ) ) {
                    collapseTable ( i );
                    break;
                }
            }
        }
    }
}
 
mw.hook( 'wikipage.content' ).add( createCollapseButtons );
/** Test if an element has a certain class **************************************
*
* Description: Uses regular expressions and caching for better performance.
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
*/
var hasClass = ( function() {
var reCache = {};
return function( element, className ) {
return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
};
})();
/** Dynamic Navigation Bars (experimental) *************************************
*
*  Description: See [[w:NavFrame]].
*  Maintainers: UNMAINTAINED
*/
// set up the words in your language
var NavigationBarHide = '[' + collapseCaption + ']';
var NavigationBarShow = '[' + expandCaption + ']';
// shows and hides content and picture (if available) of navigation bars
// Parameters:
//    indexNavigationBar: the index of navigation bar to be toggled
function toggleNavigationBar(indexNavigationBar)
{
    var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
    var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
    if (!NavFrame || !NavToggle) {
        return false;
    }
    // if shown now
    if (NavToggle.firstChild.data == NavigationBarHide) {
        for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
            if (hasClass(NavChild, 'NavContent') || hasClass(NavChild, 'NavPic')) {
                NavChild.style.display = 'none';
            }
        }
    NavToggle.firstChild.data = NavigationBarShow;
    // if hidden now
    } else if (NavToggle.firstChild.data == NavigationBarShow) {
        for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
            if (hasClass(NavChild, 'NavContent') || hasClass(NavChild, 'NavPic')) {
                NavChild.style.display = 'block';
            }
        }
        NavToggle.firstChild.data = NavigationBarHide;
    }
}
// adds show/hide-button to navigation bars
function createNavigationBarToggleButton()
{
    var indexNavigationBar = 0;
    // iterate over all < div >-elements
    var divs = document.getElementsByTagName("div");
    for (var i = 0; NavFrame = divs[i]; i++) {
        // if found a navigation bar
        if (hasClass(NavFrame, "NavFrame")) {
            indexNavigationBar++;
            var NavToggle = document.createElement("a");
            NavToggle.className = 'NavToggle';
            NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
            NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
            var isCollapsed = hasClass( NavFrame, "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 (var NavChild = NavFrame.firstChild; NavChild != null && !isCollapsed; NavChild = NavChild.nextSibling) {
                if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
                    if ( NavChild.style.display == 'none' ) {
                        isCollapsed = true;
                    }
                }
            }
            if (isCollapsed) {
                for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
                    if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
                        NavChild.style.display = 'none';
                    }
                }
            }
            var 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(var j=0; j < NavFrame.childNodes.length; j++) {
                if (hasClass(NavFrame.childNodes[j], "NavHead")) {
                    NavToggle.style.color = NavFrame.childNodes[j].style.color;
                    NavFrame.childNodes[j].appendChild(NavToggle);
                }
            }
            NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
        }
    }
}
addOnloadHook( createNavigationBarToggleButton );
/* displayTimer.js taken from http://bulbapedia.bulbagarden.net/wiki/MediaWiki:Monobook.js
  Add the following to [[Special:MyPage/vector.js|your vector.js]] file for customisation:
  timerDisplay = false; // de-activates the timer
  timerTimezone = [offset]; // set the timer offset, e.g. timerTimezone = +10; for AEST */
hookEvent( 'load', displayTimer );
function displayTimer ()
{
  if ( typeof( timerDisplay ) !== 'undefined' && timerDisplay === false )
    return;
  var date;
  var timerParent = document.getElementById( 'p-personal' ).getElementsByTagName( 'ul' )[0];
  var timerLink  = document.createElement( 'a' );
  var timerObj    = document.createElement( 'li' );
  timerLink.href              = '/index.php?title=' + wgPageName + '&action=purge';
  timerLink.title              = 'Purge the server cache and update the contents of this page.'
  timerObj.id                  = 'pt-timer';
  timerObj.style.textTransform = 'none';
  timerObj.style.fontWeight    = 'bold';
  timerObj.style.fontSize      = '72%';
  timerObj.appendChild( timerLink );
  timerParent.insertBefore( timerObj, timerParent.firstChild );
  function actualizeUTC ()
  {
    timerDate          = new Date();
    timerLink.innerHTML = ( timerDate.getUTCHours()  < 10 ? '0' : '' ) + timerDate.getUTCHours()  + ':'
                        + ( timerDate.getUTCMinutes() < 10 ? '0' : '' ) + timerDate.getUTCMinutes() + ':'
                        + ( timerDate.getUTCSeconds() < 10 ? '0' : '' ) + timerDate.getUTCSeconds() + ' (UTC)';
  }
  function actualizeCustom ()
  {
    timerDate          = new Date();
    timerDate.setMinutes( timerDate.getMinutes() + timerDate.getTimezoneOffset() + timerTimezone * 60 );
    timerLink.innerHTML = ( timerDate.getHours()  < 10 ? '0' : '' ) + timerDate.getHours()  + ':'
                        + ( timerDate.getMinutes() < 10 ? '0' : '' ) + timerDate.getMinutes() + ':'
                        + ( timerDate.getSeconds() < 10 ? '0' : '' ) + timerDate.getSeconds()
                        + ' (UTC' + ( timerTimezone < 0 ? '' : '+' ) + timerTimezone + ')';
  }
  // start
  if ( typeof( timerTimezone ) !== 'number' )
  {
    actualizeUTC();
    setInterval( actualizeUTC, 1000 );
  }
  else
  {
    actualizeCustom();
    setInterval( actualizeCustom, 1000 );
  }
}
 
// Highlight recent changes
hookEvent( 'load', recentChangesHighlights );
function recentChangesHighlights() {
if ( wgPageName == 'Special:RecentChanges' ) {
var targetContent = document.getElementById( 'bodyContent' );
var rows = targetContent.getElementsByTagName( 'li' );
for ( var l = 0; l < rows.length; l++ ) {
liHTML = rows[l].innerHTML
liText = rows[l].textContent;
if ( liHTML.indexOf( '/index.php?title=Special:Log/' ) != -1 ) {
rows[l].style.backgroundColor = '#DDF';
}
}
if ( liHTML.indexOf( '/index.php?title=Special:Log/block' ) != -1 || liHTML.indexOf( '/index.php?title=Special:Log/delete' ) != -1 ) {
if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
rows[l].style.backgroundColor = '#FCC';
$element.data( 'mw-collapsible' ).collapse();
} else if ( $element.hasClass( 'innercollapse' ) ) {
if ( $element.parents( '.outercollapse' ).length > 0 ) {
$element.data( 'mw-collapsible' ).collapse();
}
}
}
if ( liHTML.indexOf( 'class="newpage"' ) != -1 ) {
// because of colored backgrounds, style the link in the text color
rows[l].style.backgroundColor = '#DFD';
// 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' );
}
}
}
if ( liHTML.indexOf( 'class="minor"' ) != -1 ) {
} );
rows[l].style.backgroundColor = '#FEF';
}
}
}
}
}
 
mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );

Latest revision as of 15:04, 11 January 2024

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