Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. /**
  2. * We make this a global function, because we also need to bind it to the scroll
  3. * event of div.workspace_results, which is actually created from an underscore
  4. * template (it's in index.html and iframe.html).
  5. */
  6. function scrollFixedTableHeaders() {
  7. var tblWrap = $('.table_wrapper');
  8. if (tblWrap.length) {
  9. var offs = tblWrap[0].getBoundingClientRect();
  10. //console.log(offs);
  11.  
  12. var thead = $('.table_wrapper thead');
  13. var tbody = $('.table_wrapper tbody');
  14. if (thead.length) {
  15. var tHeadFixed = $('div.floatHead', tblWrap); // we do it this way so we can cater for multiple tabs
  16. var tLeftFixed = $('div.floatLeft', tblWrap);
  17.  
  18. // Create our floating header if we haven't done so already.
  19. if (!tHeadFixed.length) {
  20. tHeadFixed = $('<div class="floatHead" />').css({
  21. position: 'fixed',
  22. top: '0',
  23. display: 'none',
  24. 'box-shadow': '2px 2px 5px rgba(0,0,0,0.2)',
  25. 'z-index': 20,
  26. 'white-space': 'nowrap'
  27. });
  28. $('tr', thead[0]).each(function() {
  29. var tr = $(this);
  30. var trHead = $('<div />').css({
  31. display: 'block',
  32. //'border-right': '1px solid #d5d5d5',
  33. //'border-bottom': '1px solid #d5d5d5',
  34. overflow: 'hidden',
  35. 'border-left': tr.css('border-left'),
  36. 'border-right': tr.css('border-right'),
  37. 'border-top': tr.css('border-top'),
  38. 'border-bottom': tr.css('border-bottom'),
  39. height: tr.outerHeight()
  40. });
  41. $('th', this).each(function() {
  42. var th = $(this);
  43. var thHead = $('<div />').css({
  44. display: 'inline-block',
  45. width: th.outerWidth(),
  46. height: th.outerHeight(),
  47. //'border-left': '1px solid #d5d5d5',
  48. //'border-top': '1px solid #d5d5d5',
  49. 'border-left': th.css('border-left'),
  50. 'border-right': th.css('border-right'),
  51. 'border-top': th.css('border-top'),
  52. 'border-bottom': th.css('border-bottom'),
  53. background: th.css('background'),
  54. 'text-align': th.css('text-align'),
  55. 'white-space': 'normal',
  56. 'vertical-align': 'middle',
  57. padding: th.css('padding')
  58. }).html(th.html())
  59. .addClass(th.attr('class'));
  60. trHead.append(thHead);
  61. })
  62. tHeadFixed.append(trHead);
  63. });
  64. tblWrap.append(tHeadFixed);
  65. }
  66.  
  67. // Likewise, create the floating left part if needed
  68. if (!tLeftFixed.length) {
  69. tLeftFixed = $('<div class="floatLeft" />').css({
  70. position: 'fixed',
  71. //top: '0',
  72. display: 'none',
  73. 'box-shadow': '2px 2px 5px rgba(0,0,0,0.2)',
  74. 'z-index': 30,
  75. overflow: 'hidden'
  76. });
  77. $('tr', tbody[0]).each(function() {
  78. var tr = $(this);
  79. var trBody = $('<div />').css({
  80. display: 'block',
  81. //'border-right': '1px solid #d5d5d5',
  82. //'border-bottom': '1px solid #d5d5d5'
  83. 'border-left': tr.css('border-left'),
  84. 'border-right': tr.css('border-right'),
  85. 'border-top': tr.css('border-top'),
  86. 'border-bottom': tr.css('border-bottom'),
  87. height: tr.outerHeight()
  88. });
  89. $('th', tr).each(function() {
  90. var th = $(this);
  91. var thBody = $('<div />').css({
  92. display: 'inline-block',
  93. width: th.outerWidth(),
  94. height: (th.outerHeight()),
  95. //'border-left': '1px solid #d5d5d5',
  96. //'border-top': '1px solid #d5d5d5',
  97. 'border-left': th.css('border-left'),
  98. 'border-right': th.css('border-right'),
  99. 'border-top': th.css('border-top'),
  100. 'border-bottom': th.css('border-bottom'),
  101. background: th.css('background'),
  102. 'text-align': th.css('text-align'),
  103. 'white-space': 'nowrap',
  104. padding: th.css('padding')
  105. }).html(th.html())
  106. .addClass(th.attr('class'));
  107. trBody.append(thBody);
  108. })
  109. tLeftFixed.append(trBody);
  110. });
  111. tblWrap.append(tLeftFixed);
  112. }
  113.  
  114. var showHead, showLeft;
  115.  
  116. if (offs.top < 0 || $('div.workspace_results').scrollTop() > 0) {
  117. tHeadFixed.css({display: 'block'});
  118. thead.css({visibility: 'hidden'});
  119. showHead = true;
  120. }
  121. else {
  122. tHeadFixed.css({display: 'none'});
  123. thead.css({visibility: 'visible'});
  124. showHead = false;
  125. }
  126.  
  127. if (offs.left < 0 || $('div.workspace_results').scrollLeft() > 0) {
  128. tLeftFixed.css({display: 'block'});
  129. tbody.find('th').css({visibility: 'hidden'});
  130. showLeft = true;
  131. }
  132. else {
  133. tLeftFixed.css({display: 'none'});
  134. tbody.find('th').css({visibility: 'visible'});
  135. showLeft = false;
  136. }
  137.  
  138. // The top of the tHeadFixed is the top of the workspace_results div
  139. //tHeadFixed.css({left: offs.left, top: $('div.workspace_results').position().top});
  140. tHeadFixed.css({left: offs.left, top: Math.max($('div.workspace_results')[0].getBoundingClientRect().top, 0)});
  141.  
  142. // The top of the tLeftFixed is the top of the table body
  143. tLeftFixed.css({left: Math.max($('div.workspace_results').position().left, 0), top: tbody[0].getBoundingClientRect().top});
  144.  
  145. // When both the head and left are visible, it looks better to hide the
  146. // column headers above left part (you can see more of the left then).
  147. // these all have class row_header, and are in tHeadFixed.
  148. // if (showHead && showLeft) {
  149. // tHeadFixed.find('div.row_header').css({visibility: 'hidden'});
  150. // }
  151. // else {
  152. // tHeadFixed.find('div.row_header').css({visibility: 'visible'});
  153. // }
  154. }
  155. }
  156. }
  157.  
  158. $(document).ready(function() {
  159. //$(window).scroll(scrollFixedTableHeaders);
  160.  
  161. // Note the last param, true, as we're binding to all descendants also.
  162. // (seems you can't do that in jquery directly, hence this way)
  163. document.addEventListener('scroll', scrollFixedTableHeaders, true);
  164. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement