2007-02-06 08:35:13 +00:00
|
|
|
// $Id$
|
|
|
|
|
2007-07-01 15:37:10 +00:00
|
|
|
Drupal.behaviors.tableHeader = function (context) {
|
2007-08-30 18:56:18 +00:00
|
|
|
// This breaks in anything less than IE 7. Prevent it from running.
|
|
|
|
if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-09 09:56:39 +00:00
|
|
|
// Keep track of all cloned table headers.
|
|
|
|
var headers = [];
|
2007-02-06 08:35:13 +00:00
|
|
|
|
2007-07-01 15:37:10 +00:00
|
|
|
$('table thead:not(.tableHeader-processed)', context).each(function () {
|
2008-01-13 21:21:53 +00:00
|
|
|
// Clone thead so it inherits original jQuery properties.
|
|
|
|
var headerClone = $(this).clone(true).insertBefore(this.parentNode).wrap('<table class="sticky-header"></table>').parent().css({
|
2008-01-09 09:56:39 +00:00
|
|
|
position: 'fixed',
|
|
|
|
top: '0px'
|
|
|
|
});
|
2008-01-10 17:59:38 +00:00
|
|
|
|
2008-01-09 09:56:39 +00:00
|
|
|
var headerClone = $(headerClone)[0];
|
|
|
|
headers.push(headerClone);
|
2007-02-06 08:35:13 +00:00
|
|
|
|
2008-01-09 09:56:39 +00:00
|
|
|
// Store parent table.
|
|
|
|
var table = $(this).parent('table')[0];
|
|
|
|
headerClone.table = table;
|
|
|
|
// Finish initialzing header positioning.
|
|
|
|
headerClone.resizeWidths = true;
|
|
|
|
tracker(headerClone);
|
2007-02-06 08:35:13 +00:00
|
|
|
|
2008-01-09 09:56:39 +00:00
|
|
|
$(table).addClass('sticky-table');
|
2007-07-01 15:37:10 +00:00
|
|
|
$(this).addClass('tableHeader-processed');
|
2007-02-06 08:35:13 +00:00
|
|
|
});
|
|
|
|
|
2008-01-09 09:56:39 +00:00
|
|
|
// Track positioning and visibility.
|
|
|
|
function tracker(e) {
|
|
|
|
// Save positioning data.
|
|
|
|
var viewHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
|
|
|
|
if (e.viewHeight != viewHeight || e.resizeWidths) {
|
|
|
|
e.viewHeight = viewHeight;
|
|
|
|
e.vPosition = $(e.table).offset().top;
|
|
|
|
e.hPosition = $(e.table).offset().left;
|
|
|
|
e.vLength = $(e.table).height();
|
2008-01-13 21:21:53 +00:00
|
|
|
e.resizeWidths = true;
|
2008-01-09 09:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Track horizontal positioning relative to the viewport and set visibility.
|
|
|
|
var hScroll = document.documentElement.scrollLeft || document.body.scrollLeft;
|
|
|
|
var vScroll = document.documentElement.scrollTop || document.body.scrollTop;
|
|
|
|
var vOffset = vScroll - e.vPosition - 4;
|
|
|
|
var visState = (vOffset > 0 && vOffset < e.vLength - 100) ? 'visible' : 'hidden';
|
|
|
|
$(e).css({left: -hScroll + e.hPosition +'px', visibility: visState});
|
|
|
|
|
|
|
|
// Resize cell widths.
|
|
|
|
if (e.resizeWidths) {
|
|
|
|
var cellCount = 0;
|
|
|
|
$('th', e).each(function() {
|
|
|
|
var cellWidth = parseInt($('th', e.table).eq(cellCount).css('width'));
|
|
|
|
// Exception for IE7.
|
|
|
|
if (!cellWidth) {
|
|
|
|
var cellWidth = $('th', e.table).eq(cellCount).width();
|
|
|
|
}
|
|
|
|
cellCount++;
|
|
|
|
$(this).css('width', cellWidth +'px');
|
|
|
|
});
|
|
|
|
$(e).css('width', $(e.table).width() +'px');
|
|
|
|
e.resizeWidths = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-02-06 08:35:13 +00:00
|
|
|
// Track scrolling.
|
|
|
|
var scroll = function() {
|
2008-01-09 09:56:39 +00:00
|
|
|
$(headers).each(function () {
|
|
|
|
tracker(this);
|
2007-02-06 08:35:13 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
$(window).scroll(scroll);
|
|
|
|
$(document.documentElement).scroll(scroll);
|
|
|
|
|
|
|
|
// Track resizing.
|
|
|
|
var time = null;
|
|
|
|
var resize = function () {
|
|
|
|
// Ensure minimum time between adjustments.
|
|
|
|
if (time) {
|
2007-04-07 03:03:40 +00:00
|
|
|
return;
|
2007-02-06 08:35:13 +00:00
|
|
|
}
|
|
|
|
time = setTimeout(function () {
|
2008-01-09 09:56:39 +00:00
|
|
|
$(headers).each(function () {
|
|
|
|
this.resizeWidths = true;
|
|
|
|
tracker(this);
|
2007-06-01 09:05:45 +00:00
|
|
|
});
|
2007-04-07 03:03:40 +00:00
|
|
|
// Reset timer
|
|
|
|
time = null;
|
2007-02-06 08:35:13 +00:00
|
|
|
}, 250);
|
|
|
|
};
|
|
|
|
$(window).resize(resize);
|
2007-07-01 15:37:10 +00:00
|
|
|
};
|