|
1 // $Id: tableheader.js,v 1.16.2.2 2009/03/30 12:48:09 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 // Define the anchor holding var. |
|
39 var prevAnchor = ''; |
|
40 |
|
41 // Track positioning and visibility. |
|
42 function tracker(e) { |
|
43 // Save positioning data. |
|
44 var viewHeight = document.documentElement.scrollHeight || document.body.scrollHeight; |
|
45 if (e.viewHeight != viewHeight) { |
|
46 e.viewHeight = viewHeight; |
|
47 e.vPosition = $(e.table).offset().top - 4; |
|
48 e.hPosition = $(e.table).offset().left; |
|
49 e.vLength = e.table.clientHeight - 100; |
|
50 // Resize header and its cell widths. |
|
51 var parentCell = $('th', e.table); |
|
52 $('th', e).each(function(index) { |
|
53 var cellWidth = parentCell.eq(index).css('width'); |
|
54 // Exception for IE7. |
|
55 if (cellWidth == 'auto') { |
|
56 cellWidth = parentCell.get(index).clientWidth +'px'; |
|
57 } |
|
58 $(this).css('width', cellWidth); |
|
59 }); |
|
60 $(e).css('width', $(e.table).css('width')); |
|
61 } |
|
62 |
|
63 // Track horizontal positioning relative to the viewport and set visibility. |
|
64 var hScroll = document.documentElement.scrollLeft || document.body.scrollLeft; |
|
65 var vOffset = (document.documentElement.scrollTop || document.body.scrollTop) - e.vPosition; |
|
66 var visState = (vOffset > 0 && vOffset < e.vLength) ? 'visible' : 'hidden'; |
|
67 $(e).css({left: -hScroll + e.hPosition +'px', visibility: visState}); |
|
68 |
|
69 // Check the previous anchor to see if we need to scroll to make room for the header. |
|
70 // Get the height of the header table and scroll up that amount. |
|
71 if (prevAnchor != location.hash) { |
|
72 if (location.hash != '') { |
|
73 var offset = $('td' + location.hash).offset(); |
|
74 if (offset) { |
|
75 var top = offset.top; |
|
76 var scrollLocation = top - $(e).height(); |
|
77 $('body, html').scrollTop(scrollLocation); |
|
78 } |
|
79 } |
|
80 prevAnchor = location.hash; |
|
81 } |
|
82 } |
|
83 |
|
84 // Only attach to scrollbars once, even if Drupal.attachBehaviors is called |
|
85 // multiple times. |
|
86 if (!$('body').hasClass('tableHeader-processed')) { |
|
87 $('body').addClass('tableHeader-processed'); |
|
88 $(window).scroll(Drupal.tableHeaderDoScroll); |
|
89 $(document.documentElement).scroll(Drupal.tableHeaderDoScroll); |
|
90 } |
|
91 |
|
92 // Track scrolling. |
|
93 Drupal.tableHeaderOnScroll = function() { |
|
94 $(headers).each(function () { |
|
95 tracker(this); |
|
96 }); |
|
97 }; |
|
98 |
|
99 // Track resizing. |
|
100 var time = null; |
|
101 var resize = function () { |
|
102 // Ensure minimum time between adjustments. |
|
103 if (time) { |
|
104 return; |
|
105 } |
|
106 time = setTimeout(function () { |
|
107 $('table.sticky-header').each(function () { |
|
108 // Force cell width calculation. |
|
109 this.viewHeight = 0; |
|
110 tracker(this); |
|
111 }); |
|
112 // Reset timer |
|
113 time = null; |
|
114 }, 250); |
|
115 }; |
|
116 $(window).resize(resize); |
|
117 }; |