ShowHide/code.js

/* * Copyright © 2009, Daniel Friesen * All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: *    * Redistributions of source code must retain the above copyright *      notice, this list of conditions and the following disclaimer. *    * Redistributions in binary form must reproduce the above copyright *      notice, this list of conditions and the following disclaimer in the *      documentation and/or other materials provided with the distribution. *    * Neither the name of the script nor the *      names of its contributors may be used to endorse or promote products *      derived from this software without specific prior written permission. *  * THIS SOFTWARE IS PROVIDED BY DANIEL FRIESEN &apos;&apos;AS IS&apos;&apos; AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL DANIEL FRIESEN BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ( function( $ ) { 	// CONFIG 	var config = window.ShowHideConfig = $.extend( true, { autoCollapse: 2, userLang: true, brackets: &apos;[]&apos;, linkBefore: false, // Bulgarian bg: { show: "Покажи", hide: "Скрий", showAll: "Покажи всички", hideAll: "Скрий всички" }, 		// German de: { show: "anzeigen", hide: "verbergen", showAll: "alle anzeigen", hideAll: "alle verbergen" }, 		// English en: { show: "show", hide: "hide", showAll: "show all", hideAll: "hide all" }, 		// Spanish es: { show: "Mostrar", hide: "Ocultar", showAll: "Mostrar todo", hideAll: "Ocultar todo" }, 		// French fr: { show: "afficher", hide: "masquer", showAll: "tout afficher", hideAll: "tout masquer" }, 		// Hungarian hu: { show: "kibontás", hide: "elrejtés", showAll: "összes kibontása", hideAll: "összes elrejtése" }, 		// Italian it: { show: "Mostra", hide: "Nascondi", showAll: "Mostra tutti", hideAll: "Nascondi tutti" }, 		// Japanese ja: { show: "表示", hide: "非表示", showAll: "すべて表示", hideAll: "すべて非表示" }, 		// Korean ko: { show: "보이기", hide: "숨기기", showAll: "모두 보이기", hideAll: "모두 숨기기" }, 		// Dutch nl: { show: "tonen", hide: "verbergen", showAll: "alles tonen", hideAll: "alles verbergen" }, 		// Polish pl: { show: "Pokaż", hide: "Ukryj", showAll: "Pokaż wszystko", hideAll: "Ukryj wszystko" }, 		// Portuguese pt: { show: "Mostrar", hide: "Esconder", showAll: "Expandir Todos", hideAll: "Esconder Todos" }, 		// Brazilian Portuguese &apos;pt-br&apos;: { show: "Mostrar", hide: "Esconder", showAll: "Expandir Todos", hideAll: "Esconder Todos" }, 		// Russian ru: { show: "Открыть", hide: "Скрыть", showAll: "Открыть все", hideAll: "Скрыть все" }, 		// Chinese zh: { show: "显示", hide: "隐藏", showAll: "全部显示", hideAll: "全部隐藏" } 		// Make a post on the talkpage if you have i18n updates }, window.ShowHideConfig || {} ); 	// i18n function 	function msg( name ) { 		if ( config.userLang &amp;&amp; wgUserLanguage in config &amp;&amp; name in config[wgUserLanguage] ) { 			return config[wgUserLanguage][name]; 		} 		if ( wgContentLanguage in config &amp;&amp; name in config[wgContentLanguage] ) { 			return config[wgContentLanguage][name]; 		} 		return config.en[name]; 	} 	// common 	$.fn.onLink = function( fn ) { 		return this.bind( &apos;click keypress&apos;, function(e) { if ( e.type === &apos;click&apos; || ( e.type === &apos;keypress&apos; &amp;&amp; ( e.keyCode === 13 || e.charCode === 32 ) ) ) { fn.call(this, e); } 		} ); 	}; 	/** Collapsible tables using jQuery 	 * 	 * Description: Allows tables to be collapsed, showing only the header. 	 */ 	function collapseTable( node, state ) { 		var	$table = $( node ), 			$button = $table.find( &apos;tr:first &gt; th:last .collapseLink&apos; ); 		if ( !$table.length || !$button.length ) { 			return false; 		} 		if ( typeof state === &apos;boolean&apos; ) { 			$table.toggleClass( &apos;collapsed&apos;, !state ); 		} else { 			$table.toggleClass( &apos;collapsed&apos; ); 		} 		var hidden = $table.hasClass( &apos;collapsed&apos; ); 		$table.find( &apos;&gt; * &gt; tr&apos; ).not( &apos;:first, .nocollapse&apos; )[hidden?"hide":"show"]; 		$button.text( msg( hidden ? "show" : "hide" ) ); 		return true; 	} 	function createCollapseButtons { 		var NavigationBoxes = []; 		$( &apos;table.collapsible&apos; ).each( function  { NavigationBoxes.push(this); var	$buttonLink = $( &apos;&lt;span tabIndex=0 class=collapseLink /&gt;&apos; ).text( msg( "hide" ) ).css({ cursor: "pointer" }).onLink( function( e ) { collapseTable( $(this).closest(&apos;table&apos;) ); } ), $button = $( "&lt;span class=collapseButton /&gt;" ).css( { 				"float": "right", 				textAlign: "right", 				fontWeight: "normal", 				width: "6em", 				marginLeft: "-100%" 			} ); $button.append( document.createTextNode(config.brackets.substr(0, config.brackets.length/2)), $buttonLink, config.brackets.substr(config.brackets.length/2) ); var $header = $( this ).find( &apos;tr:first &gt; th:last&apos; ).prepend($button); } ); 		// if more Navigation Bars found than Default: hide all 		if ( $( NavigationBoxes ).filter( &apos;.autocollapse&apos; ).length &gt;= config.autoCollapse ) { 			$( NavigationBoxes ).filter( &apos;.autocollapse&apos; ).each( function { collapseTable( this, false ); } ); 		} 		$( NavigationBoxes ).filter( &apos;.collapsed&apos; ).each( function  { collapseTable( this, false ); } ); 	} 	$( createCollapseButtons ); 	/** Dynamic Navigation Bars with jQuery 	 * 	 *  Base Description: See Wikipedia:Wikipedia:NavFrame. 	 */ 	// shows and hides content and picture (if available) of navigation bars 	function toggleNavigationBar( node ) { 		var	$navFrame = $( node ), 			$navToggle = $navFrame.find( &apos;.NavHead:first .collapseLink&apos; ); 		if ( !$navFrame.length || !$navToggle.length ) { 			return false; 		} 		$navFrame.toggleClass( &apos;NavVisible&apos; ); 		$navFrame.find( &apos;.NavPic, .NavContent&apos; ).not( $navFrame.find( &apos;.NavFrame .NavPic&apos; ) ).not( $navFrame.find( &apos;.NavFrame .NavContent&apos; ) ).slideToggle; $navToggle.text( msg( $navFrame.hasClass( &apos;NavVisible&apos; ) ? "hide" : "show" ) ); return true; } 	// adds show/hide-button to navigation bars function createNavigationBarToggleButton { var NavFrames = $( &apos;.NavFrame&apos; ).addClass( &apos;NavVisible&apos; ).each( function { 			var	$navHead = $( this ).find( &apos;.NavHead:first&apos; ), 				$buttonLink = $( &apos;&lt;span tabIndex=0 class=collapseLink /&gt;&apos; ).text( msg( "hide" ) ).onLink( function ( e ) { toggleNavigationBar( $( this ).closest( &apos;.NavFrame&apos; ) ); } ), 				$button = $( &apos;&lt;span class="NavToggle collapseButton" /&gt;&apos; ); 			$navHead.filter( &apos;legend&apos; ).append( &apos; - &apos; ); 			if ( config.brackets ) { 				$button.append( document.createTextNode(config.brackets.substr(0, config.brackets.length/2)), $buttonLink, config.brackets.substr(config.brackets.length/2) ); 			} else { 				$button.append( $buttonLink ); 			} 			$navHead[config.linkBefore?"prepend":"append"]($button); 		} ); // if more Navigation Bars found than Default: hide all if ( NavFrames.length &gt;= config.autoCollapse ) { NavFrames.not( &apos;.noautocollapse&apos; ).each( function { toggleNavigationBar(this); } ); } else { NavFrames.filter( &apos;.collapsed&apos; ).each( function { toggleNavigationBar(this); } ); } 		return true; } 	$( createNavigationBarToggleButton ); $( function { 		$( &apos;.NavGlobal&apos; ).each( function  { $( &apos;&lt;span class=NavGlobalShow /&gt;&apos; ).append( 				document.createTextNode( &apos;[&apos; ), 				$( &apos;&lt;span tabIndex=0 class=collapseLink /&gt;&apos; ).text( msg( "showAll" ) ).onLink( function ( e ) { $( &apos;.NavFrame&apos; ).each( function { if ( !$( this ).hasClass( &apos;NavVisible&apos; ) ) toggleNavigationBar(this); } ); } ), 				&apos;]&apos; 			).appendTo( this ); $( this ).append( &apos; &apos; ); $(&apos;&lt;span class=NavGlobalHide /&gt;&apos;).append( 				document.createTextNode( &apos;[&apos; ), 				$( &apos;&lt;span tabIndex=0 class=collapseLink /&gt;&apos; ).text( msg( "hideAll" ) ).onLink( function ( e ) { $( &apos;.NavFrame&apos; ).each( function { if ( $( this ).hasClass( &apos;NavVisible&apos; ) ) toggleNavigationBar(this); } ); } ), 				&apos;]&apos; 			).appendTo( this ); } ); 	} ); } )( jQuery ); /*