MediaWiki:Apri-chiudi.js

Da Wikivoyage.

Nota: dopo aver salvato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tenere premuto il tasto delle maiuscole e fare clic su Ricarica, oppure premere Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premere Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer: tenere premuto il tasto Ctrl mentre si fa clic su Refresh, oppure premere Ctrl-F5
  • Opera: svuotare completamente la cache dal menu Strumenti → Preferenze
/** Tabella collassabile
 *
 * Descrizione: Permette alle tabelle di espandersi e contrarsi, mostrando solo il titolo. Vedi [[:en:Wikipedia:NavFrame]].
 * Mantenimento: [[:en:User:R. Koot]]
 */
 
var autoCollapse = 2;
var collapseCaption = "▲ Nascondi";
var expandCaption = "▼ Espandi";
 
window.collapseTable = function( tableIndex ){
	var Button = document.getElementById( "collapseButton" + tableIndex );
	var Table = document.getElementById( "collapsibleTable" + tableIndex );
 
	if ( !Table || !Button ) {
		return false;
	}
 
	var Rows = Table.rows;
 
	if ( Button.firstChild.data == collapseCaption ) {
		for ( var i = 1; i < Rows.length; i++ ) {
			Rows[i].style.display = "none";
		}
		Button.firstChild.data = expandCaption;
	} else {
		for ( var i = 1; i < Rows.length; i++ ) {
			Rows[i].style.display = Rows[0].style.display;
		}
		Button.firstChild.data = collapseCaption;
	}
}

function hasClass( el, selector ) {
	var className = " " + selector + " ";
	if ( ( " " + el.className + " " ).replace( /[\n\t]/g, " " ).indexOf( className ) > -1 ) {
		return true;
	}
	return false;
}

function createCollapseButtons(){
	var tableIndex = 0;
	var NavigationBoxes = new Object();
	var Tables = document.getElementsByTagName( "table" );
 
	for ( var i = 0; i < Tables.length; i++ ) {
		if ( hasClass( Tables[i], "collapsible" ) ) {
 
			/* aggiunge soltanto il pulsante e il numero di incremento se c'è una riga di intestazione per lavorarci */
			var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
			if (!HeaderRow) continue;
			var Header = HeaderRow.getElementsByTagName( "th" )[0];
			if (!Header) continue;
 
			NavigationBoxes[ tableIndex ] = Tables[i];
			Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
 
			var Button	 = document.createElement( "span" );
			var ButtonLink = document.createElement( "a" );
			var ButtonText = document.createTextNode( collapseCaption );
 
			Button.className = "collapseButton"; //Gli stili sono dichiarati nel Common.css
 
			ButtonLink.style.color = Header.style.color;
			ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
			ButtonLink.setAttribute( "href", "#" );
			addHandler( ButtonLink, "click", new Function( "evt", "collapseTable(" + tableIndex + " ); return false;") );
			ButtonLink.appendChild( ButtonText );
 
			Button.appendChild( document.createTextNode( "[" ) );
			Button.appendChild( ButtonLink );
			Button.appendChild( document.createTextNode( "]" ) );
 
			Header.insertBefore( Button, Header.firstChild );
			tableIndex++;
		}
	}
 
	for ( var i = 0; i < tableIndex; i++ ) {
		if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
			collapseTable( i );
		} 
		else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {
			var element = NavigationBoxes[i];
			while (element = element.parentNode) {
				if ( hasClass( element, "outercollapse" ) ) {
					collapseTable ( i );
					break;
				}
			}
		}
	}
}
 
$( createCollapseButtons );

/**
 * da jQuery makeCollapsible 
 * 
 * script iniziale : //www.mediawiki.org/wiki/RL/DM#jQuery.makeCollapsible
 * autore iniziale : Krinkle <krinklemail@gmail.com>
 *
 * modifiche eseguite da : //fr.wikipedia.org/wiki/Utilisateur:Lgd
 *
 * Doppia licenza:
 * @licenza CC-BY 3.0 <//creativecommons.org/licenses/by/3.0>
 * @licenza GPL2 <//www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 */
 
var newCollapsible = function($) {
 
	return $('.it-collapsible').each(function() {
		var _fn = 'jquery.newCollapsible> ';
 
		// Define reused variables and functions
		var	$that = $(this).addClass( 'it-collapsible' ), // case: $( '#myAJAXelement' ).newCollapsible()
			that = this,
			collapsetext = $(this).attr( 'data-collapsetext' ),
			expandtext = $(this).attr( 'data-expandtext' ),
			toggleElement = function( $collapsible, action, $defaultToggle, instantHide ) {
				// Validate parameters
				if ( !$collapsible.jquery ) { // $collapsible must be an instance of jQuery
					return;
				}
				if ( action != 'expand' && action != 'collapse' ) {
					// action must be string with 'expand' or 'collapse'
					return;
				}
				if ( typeof $defaultToggle == 'undefined' ) {
					$defaultToggle = null;
				}
				if ( $defaultToggle !== null && !($defaultToggle instanceof $) ) {
					// is optional (may be undefined), but if defined it must be an instance of jQuery.
					// If it's not, abort right away.
					// After this $defaultToggle is either null or a valid jQuery instance.
					return;
				}
 
				var $containers = null;
 
				if ( action == 'collapse' ) {
 
					// Collapse the element
					if ( $collapsible.is( 'table' ) && $collapsible.find('caption').length ) { // only table with caption
						// Hide all table rows of this table
						// Slide doens't work with tables, but fade does as of jQuery 1.1.3
						// http://stackoverflow.com/questions/467336#920480
						$containers = $collapsible.find( '>tbody>tr' );
						if ( $defaultToggle ) {
							$containers.stop(true, true).fadeOut();
						} else {
							if ( instantHide ) {
								$containers.hide();
							} else {
								$containers.stop( true, true ).fadeOut();
							}
						}
 
					} else if ( !$collapsible.is( 'ul' ) && !$collapsible.is( 'ol' ) && !$that.is( 'table' ) ) { // <div>, <p> etc. but not ul ol and tables without caption
						var $collapsibleContent = $collapsible.find( '> .it-collapsible-content' );
 
						// If a collapsible-content is defined, collapse it
						if ( $collapsibleContent.length ) {
							if ( instantHide ) {
								$collapsibleContent.hide();
							} else {
								$collapsibleContent.slideUp();
							}
 
						// Otherwise assume this is a customcollapse with a remote toggle
						// .. and there is no collapsible-content because the entire element should be toggled
						} else {
							if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
								$collapsible.fadeOut();
							} else {
								$collapsible.slideUp();
							}
						}
					}
 
				} else {
 
					// Expand the element
					if ( $collapsible.is( 'table' ) && $collapsible.find('caption').length ) {
						$containers = $collapsible.find( '>tbody>tr' );
						if ( $defaultToggle ) {
							$containers.stop(true, true).fadeIn();
						} else {
							$containers.stop(true, true).fadeIn();
						}
 
					} else if ( !$collapsible.is( 'ul' ) && !$collapsible.is( 'ol' ) && !$that.is( 'table' ) ) { // <div>, <p> etc.but not ul ol and not tables without caption
						var $collapsibleContent = $collapsible.find( '> .it-collapsible-content' );
 
						// If a collapsible-content is defined, collapse it
						if ( $collapsibleContent.length ) {
							$collapsibleContent.slideDown();
 
						// Otherwise assume this is a customcollapse with a remote toggle
						// .. and there is no collapsible-content because the entire element should be toggled
						} else {
							if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
								$collapsible.fadeIn();
							} else {
								$collapsible.slideDown();
							}
						}
					}
				}
			},
			// Toggles collapsible and togglelink class and updates text label
			toggleLinkDefault = function( that, e ) {
				var	$that = $(that),
					$collapsible = $that.closest( '.it-collapsible.it-made-collapsible' ).toggleClass( 'it-collapsed' );
				e.preventDefault();
				e.stopPropagation();
 
				// It's expanded right now
				if ( !$that.hasClass( 'it-collapsible-toggle-collapsed' ) ) {
					// Change link to "Show"
					$that.removeClass( 'it-collapsible-toggle-expanded' ).addClass( 'it-collapsible-toggle-collapsed' );
					if ( $that.find( '> a' ).length ) {
						$that.find( '> a' ).text( expandtext );
					} else {
						$that.text( expandtext );
					}
					// Collapse element
					toggleElement( $collapsible, 'collapse', $that );
 
				// It's collapsed right now
				} else {
					// Change link to "Hide"
					$that.removeClass( 'it-collapsible-toggle-collapsed' ).addClass( 'it-collapsible-toggle-expanded' );
					if ( $that.find( '> a' ).length ) {
						$that.find( '> a' ).text( collapsetext );
					} else {
						$that.text( collapsetext );
					}
					// Expand element
					toggleElement( $collapsible, 'expand', $that );
				}
				return;
			},
			// Toggles collapsible and togglelink class
			toggleLinkPremade = function( $that, e ) {
				var	$collapsible = $that.eq(0).closest( '.it-collapsible.it-made-collapsible' ).toggleClass( 'it-collapsed' );
				if ( $(e.target).is('a') ) {
					return true;
				}
				e.preventDefault();
				e.stopPropagation();
 
				// It's expanded right now
				if ( !$that.hasClass( 'it-collapsible-toggle-collapsed' ) ) {
					// Change toggle to collapsed
					$that.removeClass( 'it-collapsible-toggle-expanded' ).addClass( 'it-collapsible-toggle-collapsed' );
					// Collapse element
					toggleElement( $collapsible, 'collapse', $that );
 
				// It's collapsed right now
				} else {
					// Change toggle to expanded
					$that.removeClass( 'it-collapsible-toggle-collapsed' ).addClass( 'it-collapsible-toggle-expanded' );
					// Expand element
					toggleElement( $collapsible, 'expand', $that );
				}
				return;
			},
			// Toggles customcollapsible
			toggleLinkCustom = function( $that, e, $collapsible ) {
				// For the initial state call of customtogglers there is no event passed
				if (e) {
					e.preventDefault();
				e.stopPropagation();
				}
				// Get current state and toggle to the opposite
				var action = $collapsible.hasClass( 'it-collapsed' ) ? 'expand' : 'collapse';
				$collapsible.toggleClass( 'it-collapsed' );
				toggleElement( $collapsible, action, $that );
 
			};
 
		// Use custom text or default ?
		if( !collapsetext ) {
			//collapsetext = mw.message( 'collapsible-collapse' );
			collapsetext = 'chiudere';
		}
		if ( !expandtext ) {
			//expandtext = mw.message( 'collapsible-expand' );
			expandtext = 'aprire';
		}
 
		// Create toggle link with a space around the brackets (&nbsp;[text]&nbsp;)
		var $toggleLink =
			$( '<a href="#"></a>' )
				.text( collapsetext )
				.wrap( '<span class="it-collapsible-toggle"></span>' )
				.parent()
				.on( 'click.it-collapse', function(e) {
					toggleLinkDefault( this, e );
				} );
 
		// Return if it has been enabled already.
		if ( $that.hasClass( 'it-made-collapsible' ) ) {
			return;
		} else {
			$that.addClass( 'it-made-collapsible' );
		}
 
		// Check if this element has a custom position for the toggle link
		// (ie. outside the container or deeper inside the tree)
		// Then: Locate the custom toggle link(s) and bind them
		if ( ( $that.attr( 'id' ) || '' ).indexOf( 'it-customcollapsible-' ) === 0 ) {
 
			var thatId = $that.attr( 'id' ),
				$customTogglers = $( '.' + thatId.replace( 'it-customcollapsible', 'it-customtoggle' ) );
			mw.log( _fn + 'Found custom collapsible: #' + thatId );
 
			// Double check that there is actually a customtoggle link
			if ( $customTogglers.length ) {
				$customTogglers.on( 'click.it-collapse', function( e ) {
					toggleLinkCustom( $(this), e, $that );
				} );
			} else {
				mw.log( _fn + '#' + thatId + ': Missing toggler!' );
			}
 
			// Initial state
			if ( $that.hasClass( 'it-collapsed' ) ) {
				$that.removeClass( 'it-collapsed' );
				toggleLinkCustom( $customTogglers, null, $that );
			}
 
		// If this is not a custom case, do the default:
		// Wrap the contents add the toggle link
		} else {
 
			// Elements are treated differently
			if ( $that.is( 'table' ) && $that.find('caption').length ) { // only table with caption
				// The toggle-link will be in the caption
				var	$caption = $( 'caption', that ),
					$toggle = $caption.find( '> .it-collapsible-toggle' );
 
				// If theres no toggle link, add it to the caption
				if ( !$toggle.length ) {
					$caption.eq(-1).prepend( $toggleLink );
				} else {
					$toggleLink = $toggle.off( 'click.it-collapse' ).on( 'click.it-collapse', function( e ) {
						toggleLinkPremade( $toggle, e );
					} );
				}
 
			} else if ( !$that.is( 'ul' ) && !$that.is( 'ol' ) && !$that.is( 'table' ) ) { // <div>, <p> etc.but not ol ul and not tables without caption
 
				// The toggle-link will be the first child of the element
				var $toggle = $that.find( '> .it-collapsible-toggle' );
 
				// If a direct child .content-wrapper does not exists, create it
				if ( !$that.find( '> .it-collapsible-content' ).length ) {
					$that.wrapInner( '<div class="it-collapsible-content"></div>' );
				}
 
				// If theres no toggle link, add it
				if ( !$toggle.length ) {
					$that.prepend( $toggleLink );
				} else {
					$toggleLink = $toggle.off( 'click.it-collapse' ).on( 'click.it-collapse', function( e ) {
						toggleLinkPremade( $toggle, e );
					} );
				}
			}
		}
 
		// Initial state (only for those that are not custom)
		if ( $that.hasClass( 'it-collapsed' ) && ( $that.attr( 'id' ) || '').indexOf( 'it-customcollapsible-' ) !== 0 ) {
			$that.removeClass( 'it-collapsed' );
			// The collapsible element could have multiple togglers
			// To toggle the initial state only click one of them (ie. the first one, eq(0) )
			// Else it would go like: hide,show,hide,show for each toggle link.
			toggleElement( $that, 'collapse', $toggleLink.eq(0), /* instantHide = */ true );
			$toggleLink.eq(0).click();
		}
	} );
};
var newAutoCollapse = function($) {
	// autocollaspe for "palettes"
	var $navboxnew = $('#content .navboxnew');
	if ($navboxnew.length == 1) {
		$navboxnew.filter('.it-collapsed').not('.uncollapsed, .collapsed').each(function() {
			$(this).find('span.it-collapsible-toggle a').first().click();
		});
	}
};
var newBeKindToIE = function($) {
	var ieVersion = $.client.profile().versionBase;
	// gestion des listes horizontalisées 
	/* remove bullet from last list items in IE 8 7 6 */
	if ( ieVersion < 9 ) {
		$('#content .navboxnew_inline').find('li:last-child').addClass('navboxnew_last');
	}
	/* navbox for IE < 8 */
	if ( ieVersion < 8 ) {
		$('#content .navboxnew').removeClass('navboxnew').addClass('navboxnew_oldie');
	}
};
 
var newCollapsibleKeyboard = function($) {
	$('.it-collapsible-toggle, .it-collapsible-toggle-keyboard').attr('tabindex',0).keypress(function(event){
		if ( event.which == 13 ) {
			$(this).click()
		}
	});
};
 
var newCollapsibleGroup = function($) {
	$('.it-collapsible-group').each(function(){
		var $that = $(this);
		var text = 'Aprire tutto';
		var $tooglelink = $('<a class="it-collapsible-toggle it-collapsible-toggle-collapsed it-collapsible-group-toogle-all" href="#">Aprire tutto</a>');
		$that.find('.it-collapsible-group-toogle:first').append($tooglelink).click(function(event){
			if(text == 'Aprire tutto') {
				text = 'Chiudere tutto';
				$that.find('.it-collapsible-toggle-collapsed:not(".it-collapsible-group-toogle-all")').click();
			} else {
				text = 'Aprire tutto';
				$that.find('.it-collapsible-toggle-expanded:not(".it-collapsible-group-toogle-all")').click();
			}
			$tooglelink.text(text);
			return false;
		});
	});
};
 
if ( $.client.profile().name == 'msie' ) {
	$(document).ready(newBeKindToIE);
}
$(document).ready(newCollapsible);
$(document).ready(newAutoCollapse);
$(document).ready(newCollapsibleKeyboard);
$(document).ready(newCollapsibleGroup);