summaryrefslogtreecommitdiff
path: root/misc/tableheader.js
blob: 0568e56a90fa70219aba60d9ecfe2443e04a718c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// $Id$

Drupal.behaviors.tableHeader = function (context) {
  // This breaks in anything less than IE 7. Prevent it from running.
  if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
    return;
  }

  // Keep track of all header cells.
  var cells = [];

  var z = 0;
  $('table thead:not(.tableHeader-processed)', context).each(function () {
    // Find table height.
    var table = $(this).parent('table')[0];
    var height = $(table).addClass('sticky-table').height();
    var i = 0;

    // Find all header cells.
    $('th', this).each(function () {

      // Ensure each cell has an element in it.
      var html = $(this).html();
      if (html == ' ') {
        html = '&nbsp;';
      }
      if ($(this).children().size() == 0) {
        html = '<span>'+ html +'</span>';
      }

      // Clone and wrap cell contents in sticky wrapper that overlaps the cell's padding.
      $('<div class="sticky-header" style="position: fixed; visibility: hidden; top: 0px;">'+ html +'</div>').prependTo(this);
      var div = $('div.sticky-header', this).css({
        'marginLeft': '-'+ $(this).css('paddingLeft'),
        'marginRight': '-'+ $(this).css('paddingRight'),
        'paddingLeft': $(this).css('paddingLeft'),
        'paddingTop': $(this).css('paddingTop'),
        'paddingBottom': $(this).css('paddingBottom'),
        'z-index': ++z
      })[0];
      cells.push(div);

      // Adjust width to fit cell/table.
      var ref = this;
      if (!i++) {
        // The first cell is as wide as the table to prevent gaps.
        ref = table;
        div.wide = true;
      }
      $(div).width(Math.max(0, $(ref).width() - parseInt($(div).css('paddingLeft'))));

      // Get position and store.
      div.cell = this;
      div.table = table;
      div.stickyMax = height;
      div.stickyPosition = $(this).offset().top;
    });
    $(this).addClass('tableHeader-processed');
  });

  // Track scrolling.
  var scroll = function() {
    $(cells).each(function () {
      // Fetch scrolling position.
      var scroll = document.documentElement.scrollTop || document.body.scrollTop;
      var offset = scroll - this.stickyPosition - 4;
      if (offset > 0 && offset < this.stickyMax - 100) {
        $(this).css('visibility', 'visible');
      }
      else {
        $(this).css('visibility', 'hidden');
      }
    });
  };
  $(window).scroll(scroll);
  $(document.documentElement).scroll(scroll);

  // Track resizing.
  var time = null;
  var resize = function () {
    // Ensure minimum time between adjustments.
    if (time) {
      return;
    }
    time = setTimeout(function () {

      // Precalculate table heights
      $('table.sticky-table').each(function () {
        this.savedHeight = $(this).height();
      });

      $('table.sticky-table div.sticky-header').each(function () {
        // Get position.
        this.stickyPosition = $(this.cell).offset().top;
        this.stickyMax = this.table.savedHeight;

        // Reflow the cell.
        var ref = this.cell;
        if (this.wide) {
          // Resize the first cell to fit the table.
          ref = this.table;
        }
        $(this).width(Math.max(0, $(ref).width() - parseInt($(this).css('paddingLeft'))));
      });

      // Reset timer
      time = null;
    }, 250);
  };
  $(window).resize(resize);
};