Difference between revisions of "MediaWiki:Common.js"

From The Viking Age Compendium
Jump to: navigation, search
m
m
Line 3: Line 3:
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
/* Any JavaScript here will be loaded for all users on every page load. */
  
 
/* 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);
 
    };
 
})();
 
  
 
/**
 
/**
  * Collapsible tables *********************************************************
+
  * Dynamic Navigation Bars. See [[Wikipedia:NavFrame]]
 +
*  
 +
* Based on script from en.wikipedia.org, 2008-09-15.
 
  *
 
  *
  * Description: Allows tables to be collapsed, showing only the header. See
+
  * @source www.mediawiki.org/wiki/MediaWiki:Gadget-NavFrame.js
  *             [[Wikipedia:NavFrame]].
+
  * @maintainer Helder.wiki, 2012–2013
  * Maintainers: [[User:R. Koot]]
+
  * @maintainer Krinkle, 2013
 
  */
 
  */
 +
( function () {
 
   
 
   
var autoCollapse = 2;
+
// Set up the words in your language
 
var collapseCaption = 'hide';
 
var collapseCaption = 'hide';
 
var expandCaption = 'show';
 
var expandCaption = 'show';
 
   
 
   
window.collapseTable = function ( tableIndex ) {
+
var navigationBarHide = '[' + collapseCaption + ']';
    var Button = document.getElementById( 'collapseButton' + tableIndex );
+
var navigationBarShow = '[' + expandCaption + ']';
    var Table = document.getElementById( 'collapsibleTable' + tableIndex );
+
 
   
 
   
    if ( !Table || !Button ) {
+
/**
        return false;
+
* 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} e Event object
 +
*/
 +
function toggleNavigationBar( indexNavigationBar, e ) {
 +
var navChild,
 +
navToggle = document.getElementById( 'NavToggle' + indexNavigationBar ),
 +
navFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
 
   
 
   
    var Rows = Table.rows;
+
// Prevent browser from jumping to href "#"
    var i;
+
e.preventDefault();
 
   
 
   
    if ( Button.firstChild.data === collapseCaption ) {
+
if ( !navFrame || !navToggle ) {
        for ( i = 1; i < Rows.length; i++ ) {
+
return false;
            Rows[i].style.display = 'none';
+
}
        }
+
        Button.firstChild.data = expandCaption;
+
    } else {
+
        for ( i = 1; i < Rows.length; i++ ) {
+
            Rows[i].style.display = Rows[0].style.display;
+
        }
+
        Button.firstChild.data = collapseCaption;
+
    }
+
};
+
 
   
 
   
function createCollapseButtons() {
+
// If shown now
    var tableIndex = 0;
+
if ( navToggle.firstChild.data == navigationBarHide ) {
    var NavigationBoxes = {};
+
for ( navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling ) {
    var Tables = document.getElementsByTagName( 'table' );
+
if ( hasClass( navChild, 'NavPic' ) ) {
    var i;
+
navChild.style.display = 'none';
 +
}
 +
if ( hasClass( navChild, 'NavContent' ) ) {
 +
navChild.style.display = 'none';
 +
}
 +
}
 +
navToggle.firstChild.data = navigationBarShow;
 
   
 
   
    function handleButtonLink( index, e ) {
+
// If hidden now
        window.collapseTable( index );
+
} else if ( navToggle.firstChild.data == navigationBarShow ) {
        e.preventDefault();
+
for ( navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling ) {
    }
+
if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
 +
navChild.style.display = 'block';
 +
}
 +
}
 +
navToggle.firstChild.data = navigationBarHide;
 +
}
 +
}
 
   
 
   
    for ( i = 0; i < Tables.length; i++ ) {
+
/**
        if ( $( Tables[i] ).hasClass( 'collapsible' ) ) {
+
* Adds show/hide-button to navigation bars.
 +
*
 +
* @param {jQuery} $content
 +
*/
 +
function createNavigationBarToggleButton( $content ) {
 +
var i, j, navFrame, navToggle, navToggleText, navChild,
 +
indexNavigationBar = 0,
 +
navFrames = $content.find( 'div.NavFrame' ).toArray();
 
   
 
   
            /* only add button and increment count if there is a header row to work with */
+
// Iterate over all (new) nav frames
            var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
+
for ( i = 0; i < navFrames.length; i++ ) {
            if ( !HeaderRow ) continue;
+
navFrame = navFrames[i];
            var Header = HeaderRow.getElementsByTagName( 'th' )[0];
+
// If found a navigation bar
            if ( !Header ) continue;
+
indexNavigationBar++;
 +
navToggle = document.createElement( 'a' );
 +
navToggle.className = 'NavToggle';
 +
navToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
 +
navToggle.setAttribute( 'href', '#' );
 +
$( navToggle ).on( 'click', $.proxy( toggleNavigationBar, null, indexNavigationBar ) );
 
   
 
   
            NavigationBoxes[ tableIndex ] = Tables[i];
+
navToggleText = document.createTextNode( navigationBarHide );
            Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
+
for ( navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling ) {
 +
if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
 +
if ( navChild.style.display == 'none' ) {
 +
navToggleText = document.createTextNode( navigationBarShow );
 +
break;
 +
}
 +
}
 +
}
 
   
 
   
            var Button    = document.createElement( 'span' );
+
navToggle.appendChild( navToggleText );
            var ButtonLink = document.createElement( 'a' );
+
// Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
            var ButtonText = document.createTextNode( collapseCaption );
+
for ( j = 0; j < navFrame.childNodes.length; j++ ) {
+
if ( $( navFrame.childNodes[j] ).hasClass( 'NavHead' ) ) {
            Button.className = 'collapseButton'; /* Styles are declared in Common.css */
+
navFrame.childNodes[j].appendChild( navToggle );
+
}
            ButtonLink.style.color = Header.style.color;
+
}
            ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
+
navFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
            ButtonLink.setAttribute( 'href', '#' );
+
}
            $( ButtonLink ).on( 'click', $.proxy( handleButtonLink, ButtonLink, tableIndex ) );
+
}
            ButtonLink.appendChild( ButtonText );
+
+
            Button.appendChild( document.createTextNode( '[' ) );
+
            Button.appendChild( ButtonLink );
+
            Button.appendChild( document.createTextNode( ']' ) );
+
 
   
 
   
            Header.insertBefore( Button, Header.firstChild );
+
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
            tableIndex++;
+
        }
+
    }
+
+
    for ( i = 0;  i < tableIndex; i++ ) {
+
        if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) || ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) ) ) {
+
            window.collapseTable( i );
+
        }
+
        else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
+
            var element = NavigationBoxes[i];
+
            while ((element = element.parentNode)) {
+
                if ( $( element ).hasClass( 'outercollapse' ) ) {
+
                    window.collapseTable ( i );
+
                    break;
+
                }
+
            }
+
        }
+
    }
+
}
+
 
   
 
   
mw.hook( 'wikipage.content' ).add( createCollapseButtons );
+
}());

Revision as of 13:34, 16 March 2014

importScript('HarvardReferences.js');

/* Any JavaScript here will be loaded for all users on every page load. */


/**
 * Dynamic Navigation Bars. See [[Wikipedia:NavFrame]]
 * 
 * Based on script from en.wikipedia.org, 2008-09-15.
 *
 * @source www.mediawiki.org/wiki/MediaWiki:Gadget-NavFrame.js
 * @maintainer Helder.wiki, 2012–2013
 * @maintainer Krinkle, 2013
 */
( function () {
 
// Set up the words in your language
var collapseCaption = 'hide';
var expandCaption = 'show';
 
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} e Event object
 */
function toggleNavigationBar( indexNavigationBar, e ) {
	var navChild,
		navToggle = document.getElementById( 'NavToggle' + indexNavigationBar ),
		navFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
 
	// Prevent browser from jumping to href "#"
	e.preventDefault();
 
	if ( !navFrame || !navToggle ) {
		return false;
	}
 
	// If shown now
	if ( navToggle.firstChild.data == navigationBarHide ) {
		for ( navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling ) {
			if ( hasClass( navChild, 'NavPic' ) ) {
				navChild.style.display = 'none';
			}
			if ( hasClass( navChild, '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( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
				navChild.style.display = 'block';
			}
		}
		navToggle.firstChild.data = navigationBarHide;
	}
}
 
/**
 * Adds show/hide-button to navigation bars.
 *
 * @param {jQuery} $content
 */
function createNavigationBarToggleButton( $content ) {
	var i, j, navFrame, navToggle, navToggleText, navChild,
		indexNavigationBar = 0,
		navFrames = $content.find( 'div.NavFrame' ).toArray();
 
	// Iterate over all (new) nav frames
	for ( i = 0; i < navFrames.length; i++ ) {
		navFrame = navFrames[i];
		// If found a navigation bar
		indexNavigationBar++;
		navToggle = document.createElement( 'a' );
		navToggle.className = 'NavToggle';
		navToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
		navToggle.setAttribute( 'href', '#' );
		$( navToggle ).on( 'click', $.proxy( toggleNavigationBar, null, indexNavigationBar ) );
 
		navToggleText = document.createTextNode( navigationBarHide );
		for ( navChild = navFrame.firstChild; navChild != null; navChild = navChild.nextSibling ) {
			if ( $( navChild ).hasClass( 'NavPic' ) || $( navChild ).hasClass( 'NavContent' ) ) {
				if ( navChild.style.display == 'none' ) {
					navToggleText = document.createTextNode( navigationBarShow );
					break;
				}
			}
		}
 
		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' ) ) {
				navFrame.childNodes[j].appendChild( navToggle );
			}
		}
		navFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
	}
}
 
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
 
}());