User:Saintrain/S3/colcol.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Saintrain/S3/colcol. |
//<pre>
/*********************** Collapsible columns *********************************
* Description: Allows table columns to be collapsed, showing only the header.
*
* "Adapted" from [[WikiMedia:Common.js]]:"Collapsible tables". Maintainers: User:R. Koot
*******************************************************************************/
//====================================== hasClass ========================================================>
var hasClass = (function () {
var reCache = {};
return function (element, className) {
return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
};
})();
//====================================== ColColToggle ========================================================>
function ColColToggle( s )
{
var BUG = false, BUGT = false;
if(BUGT)console.time("CCT");
var ccx = document.getElementById( 'ccExpand' + s );
var ccc = document.getElementById( 'ccColumn' + s );
if( !ccx || !ccc ) { if(BUG)console.log(' CCT: ccx={',ccx,'} ccc={',ccc,'}' ); return; }
if( ccx.style.display == "none" ) {
ccx.style.display = "inline";
ccc.style.display = "none";
}
else {
ccx.style.display = "none";
ccc.style.display = "inline";
}
if(BUGT)console.timeEnd("CCT");
}
//====================================== ColColSetup ========================================================>
function ColColSetup()
{
var BUG = false, BUGT = false;
if(BUGT)console.time("CCS");
var ccws = getElementsByClass( "ccWrapper", null, "TD" );
if( ! ccws ) { if(BUG)if(BUG)console.log('CCS: no ccws'); return; }
if(BUG)console.log('ccws.length=',ccws.length );
for(s=0; s<ccws.length; s++) {
ccw = ccws[s];
if(BUG)console.log(' ccw=',ccw, ' tag=',ccw.tagName);
var ccx = getElementsByClass( "ccExpand", ccw )[0];
var ccc = getElementsByClass( "ccColumn", ccw )[0];
var cch = getElementsByClass( "ccColHdr", ccw )[0];
if( !ccx || !ccc || !cch ) {
if(BUG)console.log(' CCT: ccx={',ccx,'} ccc={',ccc,'} cch={',cch,'}' );
return;
}
var hdg = cch.textContent;
var hds = "";
for(i=0; i<hdg.length; i++) {
if( (" " != hdg[i]) && ("\'" != hdg[i]) )
hds += hdg[i] + "<br/>";
}
// set attrs for column header
/// console.log('s='+s+ ' c='+cch.style.color+ ' b='+cch.style.backgroundColor );
cch.setAttribute( "title", "Click to collapse" );
var ssh = "";
if( null != cch.getAttribute("style") )
ssh = cch.getAttribute("style");
ssh += "color:blue; cursor:pointer; white-space:nowrap;";
cch.setAttribute( "style", ssh );
/// console.log('s='+s+ ' c='+cch.style.color+ ' b='+cch.style.backgroundColor );
// set attrs for column contents
ccc.setAttribute( "id", "ccColumn" + s );
ccc.onclick = new Function( "ColColToggle(" + s + ");" );
// set attrs for expand button
ccx.innerHTML = hds;
ccx.setAttribute( "id", "ccExpand" + s );
ccx.setAttribute( "title", "Click to expand" );
/*
var ssx = "";
if( null != ccx.getAttribute("style") )
ssx = ccx.getAttribute("style");
ssx += "color:blue; cursor:pointer;";
if(BUG)console.log('ccx.style.ssx={',ssx,'}');
ccx.setAttribute( "style", ssx );
if(BUG)console.log('ccx.style.ckd={',ccx.getAttribute("style"),'}');
/* */
/// console.log('s='+s+ ' c='+cch.style.color+ ' b='+cch.style.backgroundColor );
/// console.log(' c='+cch.style.color+ ' !c='+ ! cch.style.color );
ccx.style.backgroundColor = cch.style.color;
if( cch.style.backgroundColor ) {
/// console.log(' using bgc={'+cch.style.backgroundColor+'}');
ccx.style.color = cch.style.backgroundColor; // "blue"; //
}
else
ccx.style.color = "white"; // ! ccx.style.backgroundColor;
ccx.style.cursor = "pointer";
/// console.log(' c='+ccx.style.color+ ' b='+ccx.style.backgroundColor );
ccx.onclick = new Function( "ColColToggle(" + s + ")" );
if( hasClass( ccw, "ccCollapse" ) ) {
if(BUG)console.log('CCS: collapsing col[' + s + ']' );
ColColToggle( s );
}
if(BUG)console.log(' ');
}
if(BUGT)console.timeEnd("CCS");
}
//============================================================================
$( ColColSetup);
//</pre>