489644a33691f5c2f10546f08d785a49b0428e513b611345ee1f244b40878828d42404fcdc32db40144d0dde83bf6b73c1f6e3f5e0a639f368bb5272fbb575 5.5 KB

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