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