Jump to content

User:Func/wpfunc/searchhelp.js

From Wikipedia, the free encyclopedia
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.
	//	Currently, restructures the checkboxes in a table,
	//	adding multiple-select checkboxes:
	//
	//		√ all        √ all      √ everything
	//		√ (Main)     √ ...talk  √ both
	//		√ User       √ ...talk  √ both
	//		√ Wikipedia  √ ...talk  √ both
	//		√ Image      √ ...talk  √ both
	//		√ MediaWiki  √ ...talk  √ both
	//		√ Template   √ ...talk  √ both
	//		√ Help       √ ...talk  √ both
	//		√ Category   √ ...talk  √ both
	//		√ Portal     √ ...talk  √ both
	//
	//	More stuff to add in the future....
	//
function SearchHelp()
{
	var form = document.getElementById( 'powersearch' ); if ( ! form ) return;

		//	Stupid ECMA....
		//
		//	Eh, good place to do something else here anyway.
		//
	var labels = [], temp = form.getElementsByTagName( 'label' );
	for ( var x = 0; x < temp.length; x++ )
	{
		labels[ x ] = temp[ x ];
		
		labels[ x ].onclick = function()
		{	this.checked = ! this.checked;
			SearchHelpUpdate();
			return true;
		}
	}

	form.labels = labels; // allow event handlers to access, numerically

	var i, text, input, label, td, tr, table;

	for ( i = 1; i < labels.length; i += 2 )
		labels[ i ].lastChild.nodeValue = '...talk';

	table = document.createElement( 'table' );

	//	can't see what's going on....
	//
form.insertBefore( table, form.firstChild.nextSibling );

		tr = document.createElement( 'tr' );

			td = document.createElement( 'td' );

				label = document.createElement( 'label' );

					input = document.createElement( 'input' );
					input.type = 'checkbox';
					form.col1 = input;
					input.onclick = function()
					{	var checked = ! this.checked;
						for ( var i = 0; i < this.form.labels.length; i += 2 )
							this.form.labels[ i ].firstChild.checked = checked;
						this.checked = checked;
						SearchHelpUpdate( this.form );
						return true;
					}

				label.appendChild( input );
				label.appendChild( document.createTextNode( 'all' ) );
				label.style.fontStyle = 'italic';

			td.appendChild( label );

		tr.appendChild( td );

			td = document.createElement( 'td' );

				label = document.createElement( 'label' );

					input = document.createElement( 'input' );
					input.type = 'checkbox';
					form.col2 = input;
					input.onclick = function()
					{	var checked = ! this.checked;
						for ( var i = 1; i < this.form.labels.length; i += 2 )
							this.form.labels[ i ].firstChild.checked = checked;
						this.checked = checked;
						SearchHelpUpdate( this.form );
						return true;
					}

				label.appendChild( input );
				label.appendChild( document.createTextNode( 'all' ) );
				label.style.fontStyle = 'italic';

			td.appendChild( label );

		tr.appendChild( td );

			td = document.createElement( 'td' );

				label = document.createElement( 'label' );

					input = document.createElement( 'input' );
					input.type = 'checkbox';
					form.allVals = input;
					input.onclick = function()
					{	for ( var i = 0; i < this.form.labels.length; i += 2 )
						{	this.form.labels[ i     ].firstChild.checked = this.checked;
							this.form.labels[ i + 1 ].firstChild.checked = this.checked;
							//this.form[ 'both' + i ]  .firstChild.checked = checked;
						}
						this.form.col1.checked = this.checked;
						this.form.col2.checked = this.checked;
						return true;
					}

				label.appendChild( input );
				label.appendChild( document.createTextNode( 'everything' ) );
				label.style.fontStyle = 'italic';

			td.appendChild( label );

		tr.appendChild( td );

	table.appendChild( tr );

	for ( i = 0; i < labels.length; i += 2 )
	{
			tr = document.createElement( 'tr' );

				td = document.createElement( 'td' ); td.appendChild( labels[ i ] );

			tr.appendChild( td );

				td = document.createElement( 'td' ); td.appendChild( labels[ i + 1 ] );

			tr.appendChild( td );

				td = document.createElement( 'td' );

					label = document.createElement( 'label' );

						input = document.createElement( 'input' );
						input.type = 'checkbox';
						form[ 'both' + i ] = input;
						input.box1 = labels[ i     ].firstChild;
						input.box2 = labels[ i + 1 ].firstChild;
						input.onclick = function()
						{	this.box1.checked =
							this.box2.checked = ! this.checked;
							this.checked = ! this.checked;
							SearchHelpUpdate( this.form );
							return true;
						}

					label.appendChild( input );
					label.appendChild( document.createTextNode( 'both' ) );
					label.style.fontStyle = 'italic';
					
				td.appendChild( label );

			tr.appendChild( td );

		table.appendChild( tr );

	}
	SearchHelpUpdate( form );
}

function SearchHelpUpdate( form )
{
	var val1, val2, col1 = true, col2 = true;

	var labels = form.labels;

	for ( var i = 0; i < form.labels.length; i += 2 )
	{
		val1 = labels[ i     ].firstChild.checked;
		val2 = labels[ i + 1 ].firstChild.checked;

		form[ 'both' + i ].checked = ( val1 && val2 );

		if ( ! val1 ) col1 = false;
		if ( ! val2 ) col2 = false;
	}

	form.col1.checked = col1;
	form.col2.checked = col2;

	form.allVals.checked = col1 && col2;
}

if ( window.addEventListener ) window.addEventListener( 'load', SearchHelp, false );
else if ( window.attachEvent ) window.attachEvent(    'onload', SearchHelp );