MediaWiki:Common.js

MediaWiki interface page
Revision as of 13:40, 11 January 2024 by Philip (talk | contribs) (Removed experimental navbar code)

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