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