Guest User

Untitled

a guest
Jul 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import debounce from 'lodash/debounce';
  2.  
  3. document.addEventListener('DOMContentLoaded', function() {
  4. let tables = document.querySelectorAll('.js-responsive-table');
  5.  
  6. tables.forEach((table) => {
  7. new ResponsiveTable(table);
  8. });
  9. });
  10.  
  11. class ResponsiveTable {
  12. constructor(table) {
  13. this.tableState = 'desktop';
  14. this.table = table;
  15. this.headers = table.querySelectorAll('thead th');
  16. this.body = table.querySelectorAll('tbody > tr');
  17. this.desktopLayout = table.cloneNode(true);
  18. this.mobileLayout = null;
  19. this.breakpoint = this.table.dataset.breakpoint ? parseInt(this.table.dataset.breakpoint) : 600;
  20.  
  21. // wrap table in a div
  22. this.wrapper = document.createElement('div');
  23. this.wrapper.classList = 'responsive-table responsive-table--desktop';
  24. this.table.parentNode.insertBefore(this.wrapper, this.table);
  25. this.wrapper.appendChild(this.table);
  26.  
  27. window.addEventListener('resize', debounce(() => {
  28. this.checkScreenSize();
  29. }, 100));
  30.  
  31. this.checkScreenSize();
  32. }
  33.  
  34. checkScreenSize() {
  35. if (window.matchMedia('(max-width: ' + (this.breakpoint - 1) + 'px)').matches && this.tableState !== 'mobile') {
  36. this.mobileTable();
  37. }
  38.  
  39. if (window.matchMedia('(min-width: ' + this.breakpoint + 'px)').matches && this.tableState !== 'desktop') {
  40. this.desktopTable();
  41. }
  42. }
  43.  
  44. toggleWrapperClasses() {
  45. this.wrapper.classList.toggle('responsive-table--desktop');
  46. this.wrapper.classList.toggle('responsive-table--mobile');
  47. }
  48.  
  49. desktopTable() {
  50. this.wrapper.replaceChild(this.desktopLayout, this.wrapper.firstChild);
  51. this.tableState = 'desktop';
  52. this.toggleWrapperClasses();
  53. }
  54.  
  55. mobileTable() {
  56. if (this.mobileLayout) {
  57. this.wrapper.replaceChild(this.mobileLayout, this.wrapper.firstChild);
  58. } else {
  59. let rows = [];
  60. let headers = [];
  61.  
  62. this.headers.forEach((header) => {
  63. headers.push(header);
  64.  
  65. if (header.colSpan > 1) {
  66. for (let i = 0; i < (header.colSpan - 1); i++) {
  67. headers.push(null);
  68. }
  69. }
  70. });
  71.  
  72. headers.forEach((header, index) => {
  73. rows.push(this.readRow(header, index));
  74. });
  75.  
  76. this.buildTable(rows);
  77. }
  78.  
  79. this.tableState = 'mobile';
  80. this.toggleWrapperClasses();
  81. }
  82.  
  83. readRow(header, index) {
  84. let row = [
  85. header
  86. ];
  87.  
  88. this.body.forEach((desktopRow) => {
  89. row.push(desktopRow.children[index]);
  90. });
  91.  
  92. return row;
  93. }
  94.  
  95. buildTable(rows) {
  96. this.table.querySelector('thead').remove();
  97. let tbody = this.table.querySelector('tbody');
  98.  
  99. rows.forEach((rowItems) => {
  100. tbody.appendChild(this.buildRow(rowItems));
  101. });
  102.  
  103. this.table.appendChild(tbody);
  104.  
  105. this.mobileLayout = this.table.cloneNode(true);
  106. }
  107.  
  108. buildRow(rowItems) {
  109. let tr = document.createElement('tr');
  110.  
  111. rowItems.forEach((item) => {
  112. if (item) {
  113. if (item.colSpan > 1) {
  114. item.rowSpan = item.colSpan;
  115. item.colSpan = 1;
  116. }
  117. tr.appendChild(item);
  118. }
  119. });
  120.  
  121. return tr;
  122. }
  123. }
Add Comment
Please, Sign In to add comment