Utente:Giudark/CollapseTableColoumn.js

Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.


Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti (per le pagine globali è comunque necessario attendere qualche minuto). Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Chrome: premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menù Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.

/**
 * Collapse tables coloumns
 *
 * @version 0.1
 * @author [[User:Giudark]]
 */
 
 
var autoCollapseColoumn = 5;
var collapseCaption = '+';
var expandCaption = '-';

function collapseColoumnsTable( tableIndex ) {
	var Button = document.getElementById( 'collapseColoumnsButton' + tableIndex );
	var Table = document.getElementById( 'collapsibleColoumnsTable' + tableIndex );

	if ( !Table || !Button ) {
		return false;
	}

	var Cols = Table.coloumns;
	var i;

	if ( Button.firstChild.data === collapseCaption ) {
		for ( i = 1; i < Cols.length; i++ ) {
			Cols[i].style.display = 'none';
		}
		Button.firstChild.data = expandCaption;
	} else {
		for ( i = 1; i < Cols.length; i++ ) {
			Cols[i].style.display = "block";
		}
		Button.firstChild.data = collapseCaption;
	}
}

function createClickHandler( tableIndex ) {
	return function ( e ) {
		e.preventDefault();
		collapseColoumnsTable( tableIndex );
	};
}

function createCollapseButtons() {
	var tableIndex = 0;
	var NavigationBoxes = {};
	var Tables = document.getElementsByTagName( 'table' );
	var i;

	for ( i = 0; i < Tables.length; i++ ) {
		if ( $( Tables[i] ).hasClass( 'collapseColoumns' ) ) {
			/* only add button and increment count if there is a header row to work with */
			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', 'collapsibleColoumnsTable' + tableIndex );

			var Button = document.createElement( 'span' );
			var ButtonLink = document.createElement( 'a' );
			var ButtonText = document.createTextNode( collapseCaption );
 			// TODO: Declare styles in MediaWiki
			// Button.className = 'collapseButton';
			Button.style.styleFloat = 'right';
			Button.style.cssFloat = 'right';
			Button.style.fontWeight = 'normal';
			Button.style.textAlign = 'right';
			Button.style.width = '6em';

			ButtonLink.style.color = Header.style.color;
			ButtonLink.setAttribute( 'id', 'collapseColoumnsButton' + tableIndex );
			ButtonLink.setAttribute( 'href', '#' );
			$( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
			ButtonLink.appendChild( ButtonText );

			Button.appendChild( document.createTextNode( '[' ) );
			Button.appendChild( ButtonLink );
			Button.appendChild( document.createTextNode( ']' ) );

			Header.insertBefore( Button, Header.firstChild );
			tableIndex++;
		}
	}

	for ( i = 0; i < tableIndex; i++ ) {
		if ( $( NavigationBoxes[i] ).hasClass( 'autocollapseColoumns' ) && $( NavigationBoxes[i] ).rows[0].cells.length > autoCollapseColoumn){
			collapseColoumnsTable( i );
		}
	}
}

mw.hook( 'wikipage.content' ).add( createCollapseButtons );