54c2a3e2356da1c67781f9d2c7e01aba561c9e0b2cd4a07de1c2919a5b221f095357ff737a08ecff1fb347f639e890cfa8baedd2add9072a5635fe1e6c65f8 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const privatePool = new WeakMap();
  2. /**
  3. * Calculates indexes of rows to render OR rows that are visible.
  4. * To redo the calculation, you need to create a new calculator.
  5. *
  6. * @class ViewportRowsCalculator
  7. */
  8. class ViewportRowsCalculator {
  9. /**
  10. * Default row height
  11. *
  12. * @type {Number}
  13. */
  14. static get DEFAULT_HEIGHT() {
  15. return 23;
  16. }
  17. /**
  18. * @param {Number} viewportHeight Height of the viewport
  19. * @param {Number} scrollOffset Current vertical scroll position of the viewport
  20. * @param {Number} totalRows Total number of rows
  21. * @param {Function} rowHeightFn Function that returns the height of the row at a given index (in px)
  22. * @param {Function} overrideFn Function that changes calculated this.startRow, this.endRow (used by MergeCells plugin)
  23. * @param {Boolean} onlyFullyVisible if `true`, only startRow and endRow will be indexes of rows that are fully in viewport
  24. * @param {Number} horizontalScrollbarHeight
  25. */
  26. constructor(viewportHeight, scrollOffset, totalRows, rowHeightFn, overrideFn, onlyFullyVisible, horizontalScrollbarHeight) {
  27. privatePool.set(this, {
  28. viewportHeight,
  29. scrollOffset,
  30. totalRows,
  31. rowHeightFn,
  32. overrideFn,
  33. onlyFullyVisible,
  34. horizontalScrollbarHeight
  35. });
  36. /**
  37. * Number of rendered/visible rows
  38. *
  39. * @type {Number}
  40. */
  41. this.count = 0;
  42. /**
  43. * Index of the first rendered/visible row (can be overwritten using overrideFn)
  44. *
  45. * @type {Number|null}
  46. */
  47. this.startRow = null;
  48. /**
  49. * Index of the last rendered/visible row (can be overwritten using overrideFn)
  50. *
  51. * @type {null}
  52. */
  53. this.endRow = null;
  54. /**
  55. * Position of the first rendered/visible row (in px)
  56. *
  57. * @type {Number|null}
  58. */
  59. this.startPosition = null;
  60. this.calculate();
  61. }
  62. /**
  63. * Calculates viewport
  64. */
  65. calculate() {
  66. let sum = 0;
  67. let needReverse = true;
  68. let startPositions = [];
  69. let priv = privatePool.get(this);
  70. let onlyFullyVisible = priv.onlyFullyVisible;
  71. let overrideFn = priv.overrideFn;
  72. let rowHeightFn = priv.rowHeightFn;
  73. let scrollOffset = priv.scrollOffset;
  74. let totalRows = priv.totalRows;
  75. let viewportHeight = priv.viewportHeight;
  76. let horizontalScrollbarHeight = priv.horizontalScrollbarHeight || 0;
  77. let rowHeight;
  78. // Calculate the number (start and end index) of rows needed
  79. for (let i = 0; i < totalRows; i++) {
  80. rowHeight = rowHeightFn(i);
  81. if (rowHeight === undefined) {
  82. rowHeight = ViewportRowsCalculator.DEFAULT_HEIGHT;
  83. }
  84. if (sum <= scrollOffset && !onlyFullyVisible) {
  85. this.startRow = i;
  86. }
  87. // the row is within the "visible range"
  88. if (sum >= scrollOffset && sum + rowHeight <= scrollOffset + viewportHeight - horizontalScrollbarHeight) {
  89. if (this.startRow === null) {
  90. this.startRow = i;
  91. }
  92. this.endRow = i;
  93. }
  94. startPositions.push(sum);
  95. sum += rowHeight;
  96. if (!onlyFullyVisible) {
  97. this.endRow = i;
  98. }
  99. if (sum >= scrollOffset + viewportHeight - horizontalScrollbarHeight) {
  100. needReverse = false;
  101. break;
  102. }
  103. }
  104. // If the estimation has reached the last row and there is still some space available in the viewport,
  105. // we need to render in reverse in order to fill the whole viewport with rows
  106. if (this.endRow === totalRows - 1 && needReverse) {
  107. this.startRow = this.endRow;
  108. while (this.startRow > 0) {
  109. // rowHeight is the height of the last row
  110. let viewportSum = startPositions[this.endRow] + rowHeight - startPositions[this.startRow - 1];
  111. if (viewportSum <= viewportHeight - horizontalScrollbarHeight || !onlyFullyVisible) {
  112. this.startRow--;
  113. }
  114. if (viewportSum >= viewportHeight - horizontalScrollbarHeight) {
  115. break;
  116. }
  117. }
  118. }
  119. if (this.startRow !== null && overrideFn) {
  120. overrideFn(this);
  121. }
  122. this.startPosition = startPositions[this.startRow];
  123. if (this.startPosition == void 0) {
  124. this.startPosition = null;
  125. }
  126. if (this.startRow !== null) {
  127. this.count = this.endRow - this.startRow + 1;
  128. }
  129. }
  130. }
  131. export default ViewportRowsCalculator;