User:Saintrain/S3/colcol.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

//<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");
}

//============================================================================
addOnloadHook( ColColSetup);

//</pre>