-
Notifications
You must be signed in to change notification settings - Fork 0
/
crosshairs03.js
349 lines (303 loc) · 12.1 KB
/
crosshairs03.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* ******************************************************************* *
CSS/JavaScript Table Hilighting 0.2
---------------------------
Author: Ryan Scherf
This script gives multiple ways to hilight active columns, rows, and
cells in any give table. It is abstracted enough to ensure that
styles can be modified with simple CSS. Also, the hilighting can
be overridden on any page with simple scripts inside the page.
This script uses the DOM for tables to apply the hilighting very
quickly and efficiently while preserving all other classes and
styles of the table that may have existed.
Tested in: IE6, IE7, FF, and Win Opera 9.
Coming Soon:
- Ability to use multiple colors for different hilighting options
- Support for multiple tables with different hilights on the same page
Known Issues:
- Colspan & rowspan do not function correctly, the script stops
at each of these events and may cause some display problems.
* ******************************************************************* */
// These classes must coincide with your stylesheet
var hilightClass = "hoverHilight"; // Default hilight class
var activeHilightClass = "activeCellHilight"; // Active cell hilight class
var hilightActive = 1; // Hilight active cell beneath cursor differently
var hilightCol = 1; // Hilight active column 0,1
var hilightRow = 1; // Hilight active row 0,1
var hilightTopHeader = 0; // Hilight top header cell 0,1
var hilightLeftHeader = 0; // Hilight leftmost header cell 0,1
var hilightRightHeader = 0; // Hilight rightmost header cell 0,1
var hilightBottomHeader = 0; // Hilight bottom footer cell 0,1
// BETA -- surrounds active cell with styling via JAVASCRIPT, not CSS
var surroundActive = 0; // Surround active cell with different backgrounds
/* ******************************************************************* */
// NO NEED TO EDIT BELOW
/* ******************************************************************* */
/* START THE TABLE HILIGHTING */
/* ******************************************************************* */
function initCrossHairsTable(id, topPad, rightPad, bottomPad, leftPad) {
var tableObj = document.getElementById(id);
/* TODO:
this does not work for header/footers -- why?
this is also broken for ie6 -- setAttribute doesn't work?
*/
var c = tableObj.rows[0].cells.length - rightPad;
var r = tableObj.rows.length - bottomPad;
for (var i = topPad; i < r; i++) {
for (var j = leftPad; j < c; j++) {
if (!document.all) {
tableObj.rows[i].cells[j].setAttribute(
"onmouseover",
"addHilight(this);"
);
tableObj.rows[i].cells[j].setAttribute(
"onmouseout",
"removeHilight(this);"
);
} else {
tableObj.rows[i].cells[j].onmouseover = function() {
addHilight(this);
};
tableObj.rows[i].cells[j].onmouseout = function() {
removeHilight(this);
};
}
}
}
}
/* ******************************************************************* */
/* HILIGHT TOP AND LEFT HEADERS */
/* ******************************************************************* */
function addHilight(cell) {
applyHilight(
cell.parentNode.parentNode.parentNode,
cell.parentNode.rowIndex,
cell.cellIndex,
1
);
}
/* ******************************************************************* */
/* REMOVE MOUSEOVER HILIGHT */
/* ******************************************************************* */
function removeHilight(cell) {
applyHilight(
cell.parentNode.parentNode.parentNode,
cell.parentNode.rowIndex,
cell.cellIndex,
0
);
}
// Global Variables used for swapping and preserving classes - DO NOT EDIT
var oldRowClasses = "";
var oldHeaderClasses = "";
var oldCellClasses = new Array();
/* ******************************************************************* */
/* APPLY THE HILIGHT TO THE CORRECT CELLS */
/* ******************************************************************* */
function applyHilight(obj, rowIndex, colIndex, state) {
// Make sure the browser has W3C DOM support
var W3CDOM = document.createElement && document.getElementsByTagName;
if (!W3CDOM) {
alert("This script requires W3C DOM support");
return;
}
// There is no hilightClass set
if (hilightClass == "") alert("Please set a hilight class.");
// Apply entire row hilighting
if (hilightRow) applyHilightRow(obj, rowIndex, colIndex, state);
// Apply entire column hilighting
if (hilightCol) applyHilightCol(obj, rowIndex, colIndex, state);
// Apply hilighting to top header cell
if (hilightTopHeader) applyHilightTopHeader(obj, rowIndex, colIndex, state);
// Apply hilighting to leftmost header cell
if (hilightLeftHeader) applyHilightLeftHeader(obj, rowIndex, colIndex, state);
// Apply hilighting to rightmost header cell
if (hilightRightHeader)
applyHilightRightHeader(obj, rowIndex, colIndex, state);
// Apply hilighting to bottom footer cell
if (hilightBottomHeader)
applyHilightBottomFooter(obj, rowIndex, colIndex, state);
// Apply hilighting to the active cell beneath the cursor
if (hilightActive) applyHilightActiveCell(obj, rowIndex, colIndex, state);
// Apply hilighting to surround cells (beta)
if (surroundActive)
applySurroundActiveHilight(obj, rowIndex, colIndex, state);
}
// state == 1 when the mouse is hovering the cell
// state == 0 when the mouse has left the cell and move to another
/* ******************************************************************* */
/* HILIGHT LEFTMOST HEADER CELL */
/* ******************************************************************* */
function applyHilightLeftHeader(obj, rowIndex, colIndex, state) {
var classArray;
var rowClasses = "";
if (state == 1) {
obj.rows[rowIndex].cells[0].className += " " + hilightClass;
} else {
classArray = obj.rows[rowIndex].cells[0].className.split(" ");
for (var i = 0; i < classArray.length - 1; i++)
if (classArray[i] != "") rowClasses += " " + classArray[i];
obj.rows[rowIndex].cells[0].className = rowClasses;
}
}
/* ******************************************************************* */
/* HILIGHT RIGHTMOST HEADER CELL */
/* ******************************************************************* */
function applyHilightRightHeader(obj, rowIndex, colIndex, state) {
var classArray;
var rowClasses = "";
if (state == 1) {
obj.rows[rowIndex].cells[obj.rows[rowIndex].cells.length - 1].className +=
" " + hilightClass;
} else {
classArray = obj.rows[rowIndex].cells[
obj.rows[rowIndex].cells.length - 1
].className.split(" ");
for (var i = 0; i < classArray.length - 1; i++)
if (classArray[i] != "") rowClasses += " " + classArray[i];
obj.rows[rowIndex].cells[
obj.rows[rowIndex].cells.length - 1
].className = rowClasses;
}
}
/* ******************************************************************* */
/* HILIGHT TOP HEADER CELL */
/* ******************************************************************* */
function applyHilightTopHeader(obj, rowIndex, colIndex, state) {
var classArray;
var colClasses = "";
if (state == 1) {
obj.rows[0].cells[colIndex].className += " " + hilightClass;
} else {
classArray = obj.rows[0].cells[colIndex].className.split(" ");
for (var i = 0; i < classArray.length - 1; i++)
if (classArray[i] != "") colClasses += " " + classArray[i];
obj.rows[0].cells[colIndex].className = colClasses;
}
}
/* ******************************************************************* */
/* HILIGHT BOTTOM FOOTER CELL */
/* ******************************************************************* */
function applyHilightBottomFooter(obj, rowIndex, colIndex, state) {
var classArray;
var colClasses = "";
if (state == 1) {
obj.rows[obj.rows.length - 1].cells[colIndex].className +=
" " + hilightClass;
} else {
classArray = obj.rows[obj.rows.length - 1].cells[colIndex].className.split(
" "
);
for (var i = 0; i < classArray.length - 1; i++)
if (classArray[i] != "") colClasses += " " + classArray[i];
obj.rows[obj.rows.length - 1].cells[colIndex].className = colClasses;
}
}
/* ******************************************************************* */
/* HILIGHT ENTIRE ROW */
/* ******************************************************************* */
function applyHilightRow(obj, rowIndex, colIndex, state) {
if (state == 1) {
oldRowClasses = obj.rows[rowIndex].className;
obj.rows[rowIndex].className = hilightClass;
} else {
obj.rows[rowIndex].className = oldRowClasses;
}
}
/* ******************************************************************* */
/* HILIGHT ENTIRE COLUMN */
/* ******************************************************************* */
function applyHilightCol(obj, rowIndex, colIndex, state) {
var cellClasses = new Array();
if (state == 1) {
for (var i = 0; i < obj.rows.length; i++) {
cellClasses.push(obj.rows[i].cells[colIndex].className);
obj.rows[i].cells[colIndex].className += " " + hilightClass;
}
oldCellClasses = cellClasses;
} else {
oldCellClasses.reverse();
for (var i = 0; i < obj.rows.length; i++)
obj.rows[i].cells[colIndex].className = oldCellClasses.pop();
}
}
/* ******************************************************************* */
/* HILIGHT ACTIVE CELL IF NEEDED */
/* ******************************************************************* */
var oldActiveCellClasses; // store the previous state of the cell
function applyHilightActiveCell(obj, rowIndex, colIndex, state) {
if (state == 1) {
oldActiveCellClasses = null;
oldActiveCellClasses = obj.rows[rowIndex].cells[colIndex].className;
obj.rows[rowIndex].cells[colIndex].className += " " + activeHilightClass;
} else {
// set the cell back to its previous state
var oldClasses = oldActiveCellClasses.split(" ");
obj.rows[rowIndex].cells[colIndex].className = "";
for (var i = 0; i < oldClasses.length; i++) {
if (oldClasses[i] != hilightClass)
obj.rows[rowIndex].cells[colIndex].className += " " + oldClasses[i];
oldActiveCellClasses = "";
}
}
}
/* ******************************************************************* */
/* SURROUND ACTIVE CELL WITH HILIGHTING (BETA) */
/* ******************************************************************* */
function applySurroundActiveHilight(obj, rowIndex, colIndex, state) {
/* TODO:
- abstract bgcolor to class
- fix for cells on borders
*/
var surroundRadius = 1;
var radiusColor = "#c0dbff";
var cell;
if (!parseInt(surroundRadius)) return;
if (state == 1) {
for (
var i = rowIndex - surroundRadius;
i <= rowIndex + surroundRadius;
i++
) {
if (i < 0) continue;
if (!obj.rows[i]) continue;
for (
var j = colIndex - surroundRadius;
j <= colIndex + surroundRadius;
j++
) {
if (j < 0) continue;
if (!obj.rows[i].cells[j]) continue;
cell = obj.rows[i].cells[j].style;
cell.backgroundColor = radiusColor;
cell.opacity = 0.5;
cell.MozOpacity = 0.5;
cell.KhtmlOpacity = 0.5;
cell.filter = "alpha(opacity=50)";
}
}
obj.rows[rowIndex].cells[colIndex].style.backgroundColor = "";
} else {
for (
var i = rowIndex - surroundRadius;
i <= rowIndex + surroundRadius;
i++
) {
if (i < 0) continue;
if (!obj.rows[i]) continue;
for (
var j = colIndex - surroundRadius;
j <= colIndex + surroundRadius;
j++
) {
if (j < 0) continue;
if (!obj.rows[i].cells[j]) continue;
cell = obj.rows[i].cells[j].style;
cell.backgroundColor = "";
cell.opacity = 1;
cell.MozOpacity = 1;
cell.KhtmlOpacity = 1;
cell.filter = "alpha(opacity=100)";
}
}
}
}