[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/misc/ -> tableheader.js (source)

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7