Jump to content

User:TheDJ/foldrefs.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Amalthea (talk | contribs) at 07:26, 13 June 2014 (Quickfix: Copied createNavigationBarToggleButton from MediaWiki:Common.js, it's no longer a global function.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<pre><nowiki>

mw.loader.using( ['mediawiki.util','mediawiki.legacy.wikibits'], function () {
	
	/* adds show/hide-button to navigation bars */
	/* Copied from [[MediaWiki:Common.js]] rv 609161585, it's no longer global ... there's certainly a better fix for this. */
	function createNavigationBarToggleButton() {
	    var indexNavigationBar = 0;
	    var NavFrame;
	    var NavChild;
	    /* iterate over all < div >-elements */
	    var divs = document.getElementsByTagName( 'div' );
	    for ( var i = 0; (NavFrame = divs[i]); i++ ) {
	        /* if found a navigation bar */
	        if ( $( NavFrame ).hasClass( 'NavFrame' ) ) {
	 
	            indexNavigationBar++;
	            var NavToggle = document.createElement( 'a' );
	            NavToggle.className = 'NavToggle';
	            NavToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
	            NavToggle.setAttribute( 'href', '#' );
	            $( NavToggle ).on( 'click', $.proxy( window.toggleNavigationBar, window, indexNavigationBar ) );
	 
	            var 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';
	                    }
	                }
	            }
	            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 ( $( NavFrame.childNodes[j] ).hasClass( 'NavHead' ) ) {
	                    NavToggle.style.color = NavFrame.childNodes[j].style.color;
	                    NavFrame.childNodes[j].appendChild( NavToggle );
	                }
	            }
	            NavFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
	        }
	    }
	}

  var ref_list_list = getElementsByClassName(document, "OL", "references");

  if( ref_list_list && ref_list_list[0] )
  {
    var ref_list = ref_list_list[0];
    if( hasClass( ref_list.parentNode, "references-small" ) )
        ref_list = ref_list.parentNode;

    var refdiv = document.createElement( "DIV" );
    refdiv.className = "references-folded NavFrame collapsed"
    refdiv.style.border = "none";
    refdiv.style.margin = 0;
    refdiv.style.padding = 0;
    refdiv.style.fontSize = "100%";
    refdiv.innerHTML = '<div class="NavHead" style="background:transparent;">Folded references<div></div></div><div class="NavContent" style="text-align:left;"></div>';

    ref_list.parentNode.insertBefore( refdiv, ref_list );
    var last = refdiv.lastChild
    if( last ) last.appendChild( ref_list );
  }
  createNavigationBarToggleButton();
});
//</pre></nowiki>