Advertisement
ToastUltimatum

MediaWiki:Common.js

Mar 27th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. /** Collapsible tables *********************************************************
  2. *
  3. * Description: Allows tables to be collapsed, showing only the header. See
  4. * [[Wikipedia:NavFrame]].
  5. * Taken from Wikipedia's Common.js.
  6. */
  7.  
  8. var autoCollapse = 2;
  9. var collapseCaption = "hide";
  10. var expandCaption = "show";
  11.  
  12. function collapseTable( tableIndex )
  13. {
  14. var Button = document.getElementById( "collapseButton" + tableIndex );
  15. var Table = document.getElementById( "collapsibleTable" + tableIndex );
  16.  
  17. if ( !Table || !Button ) {
  18. return false;
  19. }
  20.  
  21. var Rows = Table.rows;
  22.  
  23. if ( Button.firstChild.data == collapseCaption ) {
  24. for ( var i = 1; i < Rows.length; i++ ) {
  25. Rows[i].style.display = "none";
  26. }
  27. Button.firstChild.data = expandCaption;
  28. } else {
  29. for ( var i = 1; i < Rows.length; i++ ) {
  30. Rows[i].style.display = Rows[0].style.display;
  31. }
  32. Button.firstChild.data = collapseCaption;
  33. }
  34. }
  35.  
  36. function createCollapseButtons()
  37. {
  38. var tableIndex = 0;
  39. var NavigationBoxes = new Object();
  40. var Tables = document.getElementsByTagName( "table" );
  41.  
  42. for ( var i = 0; i < Tables.length; i++ ) {
  43. if ( hasClass( Tables[i], "collapsible" ) ) {
  44.  
  45. /* only add button and increment count if there is a header row to work with */
  46. var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
  47. if (!HeaderRow) continue;
  48. var Header = HeaderRow.getElementsByTagName( "th" )[0];
  49. if (!Header) continue;
  50.  
  51. NavigationBoxes[ tableIndex ] = Tables[i];
  52. Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
  53.  
  54. var Button = document.createElement( "span" );
  55. var ButtonLink = document.createElement( "a" );
  56. var ButtonText = document.createTextNode( collapseCaption );
  57.  
  58. Button.style.styleFloat = "right";
  59. Button.style.cssFloat = "right";
  60. Button.style.fontWeight = "normal";
  61. Button.style.textAlign = "right";
  62. Button.style.width = "6em";
  63.  
  64. ButtonLink.style.color = Header.style.color;
  65. ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
  66. ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );
  67. ButtonLink.appendChild( ButtonText );
  68.  
  69. Button.appendChild( document.createTextNode( "[" ) );
  70. Button.appendChild( ButtonLink );
  71. Button.appendChild( document.createTextNode( "]" ) );
  72.  
  73. Header.insertBefore( Button, Header.childNodes[0] );
  74. tableIndex++;
  75. }
  76. }
  77.  
  78. for ( var i = 0; i < tableIndex; i++ ) {
  79. if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
  80. collapseTable( i );
  81. }
  82. }
  83. }
  84.  
  85. addOnloadHook( createCollapseButtons );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement