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