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