MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
(Undo revision 1647 by Philip (talk))
Tags: Undo Reverted
(Collapsible tables update)
Tag: Reverted
Line 1: Line 1:
/**
function makeCollapsibleMwCollapsible( $content ) {
* Collapsible tables
var $tables = $content
*
.find( 'table.collapsible:not(.mw-collapsible)' )
* Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
.addClass( 'mw-collapsible' );
*
* @version 2.0.3 (2014-03-14)
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
* @author [[User:R. Koot]]
* @author [[User:Krinkle]]
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
* is supported in MediaWiki core.
*/


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() {
    if ( !Table || !Button ) {
$tables.makeCollapsible();
        return false;
} );
    }
}
 
    var Rows = Table.rows;
    var i;
    var $row0 = $(Rows[0]);
 
    if ( Button.firstChild.data === collapseCaption ) {
        for ( i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = 'none';
        }
        Button.firstChild.data = expandCaption;
    } 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( makeCollapsibleMwCollapsible );


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) *************************************
/** Dynamic Navigation Bars (experimental) *************************************

Revision as of 13:37, 11 January 2024

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

 
/** 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 );
  }
}
 
// Add a new section link to the bottom of discussion pages
function addsectionbottom() {
  if (!wgIsArticle) return;
  if ((document.URL.indexOf('?')>=0) && (document.URL.indexOf('action=delete')>=0)) return;
  var caplus;
  if (!(caplus = document.getElementById("ca-addsection"))) return;
  var addsection = document.createElement("span");
  addsection.innerHTML = "<h3> <a " + "href=\"" + caplus.childNodes[0].getAttribute("href") + 
                         "\" title=\"" + caplus.childNodes[0].getAttribute("title") +
                         "\" accesskey=\"" + caplus.childNodes[0].getAttribute("accesskey") + 
                         "\" >" + (skin == "vector" ? "<span> Add topic</span>" : "<span> Add section</span>" ) + "</a> </h3>"
  addsection.className = "noprint";
  addsection.id = "addsectionbottom";
  var content;
  if (!(content = document.getElementById("bodyContent"))) return;
  var catlinks = document.getElementById("catlinks");
  if (catlinks == null)
  {
    content.appendChild(addsection);
  } else {
    content.insertBefore(addsection, catlinks);
  }
}
addOnloadHook(addsectionbottom);
 
// 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 ) {
				rows[l].style.backgroundColor = '#FCC';
			}
			if ( liHTML.indexOf( 'class="newpage"' ) != -1 ) {
				rows[l].style.backgroundColor = '#DFD';
			}
			if ( liHTML.indexOf( 'class="minor"' ) != -1 ) {
				rows[l].style.backgroundColor = '#FEF';
			}
		}
	}
}