public
Description: jQuery plugin to add interactive and visualization features to a standard table
Homepage: http://www.tonylandis.com
Clone URL: git://github.com/tony-landis/jQuery-bullsEye.git
jQuery-bullsEye / jquery.bullseye.js
100644 292 lines (244 sloc) 8.815 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
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
//
// bullsEye - an interactive table jQuery plugin
//
// (c) Copyright 2009 Tony Landis, website: http://www.tonylandis.com
// License: GPL
// Please leave this header intact.
// If you use this class I would be interested to see your implementation!
//
 
(function($) {
 
 
$.fn.bullseye = function(options) {
 
// override bullseyes's default settings
var opts = $.extend({}, $.fn.bullseye.defaults, options);
$table = this;
 
// set the private max rows/cols values
opts.max_cols = $table.find('th.'+ opts.class_th_col).length - opts.use_cols;
opts.max_rows = $table.find('th.'+ opts.class_th_row).length - opts.use_rows;
 
// set cell row/col
row=-1;
this.find('tr').each(function() {
col = 0;
$(this).find('td').each(function() {
$(this).attr('rel', row + ',' + col);
col += 1;
});
row += 1
});
 
// column heading handling, set column number and listeners
var col = 0;
$table.find('th.'+ opts.class_th_col).each(function() {
 
// set col number for expression lookup later
$(this).attr('rel', col);
 
// set column heading listeners
$(this).bind('mouseover', function() {
var icol = $(this).attr('rel');
 
// loop on all cells
$table.find('td[rel$=,'+ icol +']').each(function(){
var xy = $.fn.bullseye.get_cell_xy( $(this) );
 
if( $(this).attr('class') == opts.class_td_hot) {
// this is a hot cell, highlight it and draw a line to it from the row heading
$table.find('th.'+ opts.class_th_row + '[rel='+ xy.row +']').addClass('th-sel');
$.fn.bullseye.lines_draw_col($table, opts, icol, xy.row);
} else {
// hidden cell
$table.find('th.'+ opts.class_th_row + '[rel='+ xy.row +']').addClass('th-hid');
}
$(this).addClass('sv');
});
}).bind('mouseout', function() {
$table.find('td.sv').each(function(){
$(this).removeClass('sv');
});
$table.find('th.'+ opts.class_th_row).each(function() {
$(this).removeClass('th-sel').removeClass('th-hid');
});
$.fn.bullseye.lines_clear($table);
});
col += 1;
});
 
 
 
 
// row heading handling, set row number and listeners
var row = 0;
$table.find('th.'+ opts.class_th_row ).each(function() {
 
// set col number for expression lookup later
$(this).attr('rel', row);
 
// set column heading listeners
$(this).bind('mouseover', function() {
var irow = $(this).attr('rel');
 
// loop on all cells
$table.find('td[rel^='+ irow +',]').each(function(){
var xy = $.fn.bullseye.get_cell_xy( $(this) );
if( $(this).attr('class') == opts.class_td_hot) {
// this is a hot cell, highlight it and draw a line to it from the row heading
$table.find('th.'+ opts.class_th_col + '[rel='+ xy.col +']').addClass('th-sel');
$.fn.bullseye.lines_draw_row($table, opts, irow, xy.col);
} else {
// hidden cell
$table.find('th.'+ opts.class_th_col + '[rel='+ xy.col +']').addClass('th-hid');
}
$(this).addClass('sv');
});
}).bind('mouseout', function() {
$table.find('td.sv').each(function(){
$(this).removeClass('sv');
});
$table.find('th.'+ opts.class_th_col).each(function() {
$(this).removeClass('th-sel').removeClass('th-hid');
});
$.fn.bullseye.lines_clear($table);
});
row += 1;
});
 
 
 
// set cell-level listeners
return this.find('td.' + opts.class_td_hot).each(function() {
 
$cell = $(this);
var xy = $.fn.bullseye.get_cell_xy($cell);
 
$cell.bind('mouseover', function() {
// add the selected class to this cell's column and row headings
$table.find('th.'+ opts.class_th_col + '[rel='+ xy.col +']').addClass('th-sel');
$table.find('th.'+ opts.class_th_row + '[rel='+ xy.row +']').addClass('th-sel');
// draw the lines from this cell's column and row headings to this cell
$.fn.bullseye.lines_draw_row($table, opts, xy.row, xy.col);
$.fn.bullseye.lines_draw_col($table, opts, xy.col, xy.row);
}).bind('mouseout', function() {
// remove the selected class from this cell's column and row headings
$table.find('th.'+ opts.class_th_col + '[rel='+ xy.col +']').each(function() { $(this).removeClass('th-sel') });
$table.find('th.'+ opts.class_th_row + '[rel='+ xy.row +']').each(function() { $(this).removeClass('th-sel') });
// clear the lines to this cell
$.fn.bullseye.lines_clear($table);
});
 
// Conditional hover action
// Enable with {'cellhover':true}
// Requires Brian Cherne's excellent hoverIntent class: http://cherne.net/brian/resources/jquery.hoverIntent.html
// hoverIntent is included in this distribution for the sake of convenience
if (opts.cell_hover == true)
{
$cell.hoverIntent({
sensitivity: opts.cell_hover_sensitivity,
interval: opts.cell_hover_interval,
over: function(e) {
child = $(this).children(":first")[0];
$.fn.bullseye.set_hover_position($table, opts, this, child);
},
out: function(e) {
var child = $(this).children(":first")[0];
var pos = $(this).position();
$(child).stop().fadeOut('slow');
}
}
);
}
 
});
};
 
 
//
$.fn.bullseye.set_hover_position = function(table, opts, parent, child)
{
xy = $.fn.bullseye.get_cell_xy(parent);
var row = xy.row
var col = xy.col
 
// determine start col
start_col = col - ( (opts.use_cols-1) / 2 );
while ( start_col < 0 ) start_col += 1;
if( start_col >= opts.max_cols) start_col = opts.max_cols;
end_col = start_col + opts.use_cols-1;
 
// determine start row
start_row = row - ( (opts.use_rows-1) / 2 );
while ( start_row < 0 ) start_row += 1;
if( start_row >= opts.max_rows) start_row = opts.max_rows;
end_row = start_row + opts.use_rows-1;
 
// get the start row,col cell
var start = table.find('td[rel='+start_row+','+start_col+']')[0];
var start_pos = $(start).position();
 
// get the end row,col
var end = table.find('td[rel='+end_row+','+end_col+']')[0];
var end_pos = $(end).position();
 
if (opts.cell_hover_effect == 'zoom')
{
// zoom in the cell hover content
var pos = $(parent).position();
$(child).css({
'top': pos.top,
'left': pos.left,
'height': parent.scrollHeight + 'px',
'width': parent.scrollWidth + 'px',
'opacity': .95
});
$(child).animate({
'top': start_pos.top,
'left': start_pos.left,
'margin-top': '-2px',
'margin-left': '-2px',
'height': (end_pos.top - start_pos.top + (end.scrollHeight - 10)) +'px',
'width': (end_pos.left - start_pos.left + (end.scrollWidth - 10)) +'px'
});
 
}
else if (opts.cell_hover_effect == 'fade')
{
// fade in the cell hover content
$(child).css({
'top': start_pos.top,
'left': start_pos.left,
'height': (end_pos.top - start_pos.top + (end.scrollHeight - 10)) +'px',
'width': (end_pos.left - start_pos.left + (end.scrollWidth - 10)) +'px'
}).fadeIn();
 
} else {
// error
}
 
};
 
 
// get the row/col of the selected cell
$.fn.bullseye.get_cell_xy = function(cell) {
var s = $(cell).attr('rel').split(',');
return {
'row': s[0],
'col': s[1]
}
};
 
// draw horizontal lines
$.fn.bullseye.lines_draw_row = function(table, opts, row, col) {
 
// get the height of this row
var parent = table.find('th.' + opts.class_th_row)[0];
var width = parent.clientWidth;
var height = parent.clientHeight;
var center = width/2;
 
var count = 0;
table.find('td[rel$=,'+col+']').each(function(){
if (count < row) {
$(this).addClass("line-col");
}
count++;
});
}
 
// draw vertical lines
$.fn.bullseye.lines_draw_col = function(table, opts, col, row) {
var count = 0;
table.find('td[rel^='+row+'],').each(function(){
if (count < col) $(this).addClass("line-row");
count++;
});
}
 
// clear all lines
$.fn.bullseye.lines_clear = function (table) {
table.find('td[class*=line]').each(function(){
$(this).removeClass("line-col");
$(this).removeClass("line-row");
});
}
 
 
  //
// bullseye default settings
//
$.fn.bullseye.defaults = {
 
// cell hover settings
'cell_hover': true, // (true|false) enable/disable cell level display of extra content on mouseover
'cell_hover_effect': 'fade', // (fade|zoom) the effect to use when showing/hiding the extra content
'cell_hover_sensitivity':1, // (int) passed directly to hoverIntent
'cell_hover_interval':120, // (int) passed directly to hoverIntent
'use_cols':3, // (an uneven number >= 3) how many columns to span
'use_rows':5, // (an uneven number >= 3) how many rows to span
 
// class names
'class_td_hot': 'hot',
'class_th_row': 'row',
'class_th_col': 'col',
 
// private
'max_rows':0,
'max_cols':0
};
 
}) (jQuery);