tablecloth.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Tablecloth
  3. written by Alen Grakalic, provided by Css Globe (cssglobe.com)
  4. please visit http://cssglobe.com/lab/tablecloth/
  5. */
  6. this.tablecloth = function(){
  7. // CONFIG
  8. // if set to true then mouseover a table cell will highlight entire column (except sibling headings)
  9. var highlightCols = true;
  10. // if set to true then mouseover a table cell will highlight entire row (except sibling headings)
  11. var highlightRows = true;
  12. // if set to true then click on a table sell will select row or column based on config
  13. var selectable = true;
  14. // this function is called when
  15. // add your own code if you want to add action
  16. // function receives object that has been clicked
  17. this.clickAction = function(obj){
  18. //alert(obj.innerHTML);
  19. };
  20. // END CONFIG (do not edit below this line)
  21. var tableover = false;
  22. this.start = function(){
  23. var tables = document.getElementsByTagName("table");
  24. for (var i=0;i<tables.length;i++){
  25. tables[i].onmouseover = function(){tableover = true};
  26. tables[i].onmouseout = function(){tableover = false};
  27. rows(tables[i]);
  28. };
  29. };
  30. this.rows = function(table){
  31. var css = "";
  32. var tr = table.getElementsByTagName("tr");
  33. for (var i=0;i<tr.length;i++){
  34. css = (css == "odd") ? "even" : "odd";
  35. tr[i].className = css;
  36. var arr = new Array();
  37. for(var j=0;j<tr[i].childNodes.length;j++){
  38. if(tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j]);
  39. };
  40. for (var j=0;j<arr.length;j++){
  41. arr[j].row = i;
  42. arr[j].col = j;
  43. if(arr[j].innerHTML == "&nbsp;" || arr[j].innerHTML == "") arr[j].className += " empty";
  44. arr[j].css = arr[j].className;
  45. arr[j].onmouseover = function(){
  46. over(table,this,this.row,this.col);
  47. };
  48. arr[j].onmouseout = function(){
  49. out(table,this,this.row,this.col);
  50. };
  51. arr[j].onmousedown = function(){
  52. down(table,this,this.row,this.col);
  53. };
  54. arr[j].onmouseup = function(){
  55. up(table,this,this.row,this.col);
  56. };
  57. arr[j].onclick = function(){
  58. click(table,this,this.row,this.col);
  59. };
  60. };
  61. };
  62. };
  63. // appyling mouseover state for objects (th or td)
  64. this.over = function(table,obj,row,col){
  65. if (!highlightCols && !highlightRows) obj.className = obj.css + " over";
  66. if(check1(obj,col)){
  67. if(highlightCols) highlightCol(table,obj,col);
  68. if(highlightRows) highlightRow(table,obj,row);
  69. };
  70. };
  71. // appyling mouseout state for objects (th or td)
  72. this.out = function(table,obj,row,col){
  73. if (!highlightCols && !highlightRows) obj.className = obj.css;
  74. unhighlightCol(table,col);
  75. unhighlightRow(table,row);
  76. };
  77. // appyling mousedown state for objects (th or td)
  78. this.down = function(table,obj,row,col){
  79. obj.className = obj.css + " down";
  80. };
  81. // appyling mouseup state for objects (th or td)
  82. this.up = function(table,obj,row,col){
  83. obj.className = obj.css + " over";
  84. };
  85. // onclick event for objects (th or td)
  86. this.click = function(table,obj,row,col){
  87. if(check1){
  88. if(selectable) {
  89. unselect(table);
  90. if(highlightCols) highlightCol(table,obj,col,true);
  91. if(highlightRows) highlightRow(table,obj,row,true);
  92. document.onclick = unselectAll;
  93. }
  94. };
  95. clickAction(obj);
  96. };
  97. this.highlightCol = function(table,active,col,sel){
  98. var css = (typeof(sel) != "undefined") ? "selected" : "over";
  99. var tr = table.getElementsByTagName("tr");
  100. for (var i=0;i<tr.length;i++){
  101. var arr = new Array();
  102. for(j=0;j<tr[i].childNodes.length;j++){
  103. if(tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j]);
  104. };
  105. var obj = arr[col];
  106. if (check2(active,obj) && check3(obj)) obj.className = obj.css + " " + css;
  107. };
  108. };
  109. this.unhighlightCol = function(table,col){
  110. var tr = table.getElementsByTagName("tr");
  111. for (var i=0;i<tr.length;i++){
  112. var arr = new Array();
  113. for(j=0;j<tr[i].childNodes.length;j++){
  114. if(tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j])
  115. };
  116. var obj = arr[col];
  117. if(check3(obj)) obj.className = obj.css;
  118. };
  119. };
  120. this.highlightRow = function(table,active,row,sel){
  121. var css = (typeof(sel) != "undefined") ? "selected" : "over";
  122. var tr = table.getElementsByTagName("tr")[row];
  123. for (var i=0;i<tr.childNodes.length;i++){
  124. var obj = tr.childNodes[i];
  125. if (check2(active,obj) && check3(obj)) obj.className = obj.css + " " + css;
  126. };
  127. };
  128. this.unhighlightRow = function(table,row){
  129. var tr = table.getElementsByTagName("tr")[row];
  130. for (var i=0;i<tr.childNodes.length;i++){
  131. var obj = tr.childNodes[i];
  132. if(check3(obj)) obj.className = obj.css;
  133. };
  134. };
  135. this.unselect = function(table){
  136. tr = table.getElementsByTagName("tr")
  137. for (var i=0;i<tr.length;i++){
  138. for (var j=0;j<tr[i].childNodes.length;j++){
  139. var obj = tr[i].childNodes[j];
  140. if(obj.className) obj.className = obj.className.replace("selected","");
  141. };
  142. };
  143. };
  144. this.unselectAll = function(){
  145. if(!tableover){
  146. tables = document.getElementsByTagName("table");
  147. for (var i=0;i<tables.length;i++){
  148. unselect(tables[i])
  149. };
  150. };
  151. };
  152. this.check1 = function(obj,col){
  153. return (!(col == 0 && obj.className.indexOf("empty") != -1));
  154. }
  155. this.check2 = function(active,obj){
  156. return (!(active.tagName == "TH" && obj.tagName == "TH"));
  157. };
  158. this.check3 = function(obj){
  159. return (obj.className) ? (obj.className.indexOf("selected") == -1) : true;
  160. };
  161. start();
  162. };
  163. /* script initiates on page load. */
  164. window.onload = tablecloth;