comparison misc/tableheader.js @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 589fb7c02327
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 // $Id: tableheader.js,v 1.16 2008/01/30 10:17:39 goba Exp $
2
3 Drupal.tableHeaderDoScroll = function() {
4 if (typeof(Drupal.tableHeaderOnScroll)=='function') {
5 Drupal.tableHeaderOnScroll();
6 }
7 };
8
9 Drupal.behaviors.tableHeader = function (context) {
10 // This breaks in anything less than IE 7. Prevent it from running.
11 if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7) {
12 return;
13 }
14
15 // Keep track of all cloned table headers.
16 var headers = [];
17
18 $('table.sticky-enabled thead:not(.tableHeader-processed)', context).each(function () {
19 // Clone thead so it inherits original jQuery properties.
20 var headerClone = $(this).clone(true).insertBefore(this.parentNode).wrap('<table class="sticky-header"></table>').parent().css({
21 position: 'fixed',
22 top: '0px'
23 });
24
25 headerClone = $(headerClone)[0];
26 headers.push(headerClone);
27
28 // Store parent table.
29 var table = $(this).parent('table')[0];
30 headerClone.table = table;
31 // Finish initialzing header positioning.
32 tracker(headerClone);
33
34 $(table).addClass('sticky-table');
35 $(this).addClass('tableHeader-processed');
36 });
37
38 // Track positioning and visibility.
39 function tracker(e) {
40 // Save positioning data.
41 var viewHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
42 if (e.viewHeight != viewHeight) {
43 e.viewHeight = viewHeight;
44 e.vPosition = $(e.table).offset().top - 4;
45 e.hPosition = $(e.table).offset().left;
46 e.vLength = e.table.clientHeight - 100;
47 // Resize header and its cell widths.
48 var parentCell = $('th', e.table);
49 $('th', e).each(function(index) {
50 var cellWidth = parentCell.eq(index).css('width');
51 // Exception for IE7.
52 if (cellWidth == 'auto') {
53 cellWidth = parentCell.get(index).clientWidth +'px';
54 }
55 $(this).css('width', cellWidth);
56 });
57 $(e).css('width', $(e.table).css('width'));
58 }
59
60 // Track horizontal positioning relative to the viewport and set visibility.
61 var hScroll = document.documentElement.scrollLeft || document.body.scrollLeft;
62 var vOffset = (document.documentElement.scrollTop || document.body.scrollTop) - e.vPosition;
63 var visState = (vOffset > 0 && vOffset < e.vLength) ? 'visible' : 'hidden';
64 $(e).css({left: -hScroll + e.hPosition +'px', visibility: visState});
65 }
66
67 // Only attach to scrollbars once, even if Drupal.attachBehaviors is called
68 // multiple times.
69 if (!$('body').hasClass('tableHeader-processed')) {
70 $('body').addClass('tableHeader-processed');
71 $(window).scroll(Drupal.tableHeaderDoScroll);
72 $(document.documentElement).scroll(Drupal.tableHeaderDoScroll);
73 }
74
75 // Track scrolling.
76 Drupal.tableHeaderOnScroll = function() {
77 $(headers).each(function () {
78 tracker(this);
79 });
80 };
81
82 // Track resizing.
83 var time = null;
84 var resize = function () {
85 // Ensure minimum time between adjustments.
86 if (time) {
87 return;
88 }
89 time = setTimeout(function () {
90 $('table.sticky-header').each(function () {
91 // Force cell width calculation.
92 this.viewHeight = 0;
93 tracker(this);
94 });
95 // Reset timer
96 time = null;
97 }, 250);
98 };
99 $(window).resize(resize);
100 };