ce1fe8c7de3adf55a77563730415e65efe71a74ea33e944888be7bef02b81f8aadad1822657f5d8bfab9f21081f3440308c65ae243e5dd7b0a5cf6b2c1bf91 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. const privatePool = new WeakMap();
  2. /**
  3. * Calculates indexes of columns to render OR columns that are visible.
  4. * To redo the calculation, you need to create a new calculator.
  5. *
  6. * @class ViewportColumnsCalculator
  7. */
  8. class ViewportColumnsCalculator {
  9. /**
  10. * Default column width
  11. *
  12. * @type {Number}
  13. */
  14. static get DEFAULT_WIDTH() {
  15. return 50;
  16. }
  17. /**
  18. * @param {Number} viewportWidth Width of the viewport
  19. * @param {Number} scrollOffset Current horizontal scroll position of the viewport
  20. * @param {Number} totalColumns Total number of rows
  21. * @param {Function} columnWidthFn Function that returns the width of the column 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 {Boolean} stretchH
  25. * @param {Function} [stretchingColumnWidthFn] Function that returns the new width of the stretched column.
  26. */
  27. constructor(viewportWidth, scrollOffset, totalColumns, columnWidthFn, overrideFn, onlyFullyVisible, stretchH,
  28. stretchingColumnWidthFn = (width) => width) {
  29. privatePool.set(this, {
  30. viewportWidth,
  31. scrollOffset,
  32. totalColumns,
  33. columnWidthFn,
  34. overrideFn,
  35. onlyFullyVisible,
  36. stretchingColumnWidthFn,
  37. });
  38. /**
  39. * Number of rendered/visible columns
  40. *
  41. * @type {Number}
  42. */
  43. this.count = 0;
  44. /**
  45. * Index of the first rendered/visible column (can be overwritten using overrideFn)
  46. *
  47. * @type {Number|null}
  48. */
  49. this.startColumn = null;
  50. /**
  51. * Index of the last rendered/visible column (can be overwritten using overrideFn)
  52. *
  53. * @type {null}
  54. */
  55. this.endColumn = null;
  56. /**
  57. * Position of the first rendered/visible column (in px)
  58. *
  59. * @type {Number|null}
  60. */
  61. this.startPosition = null;
  62. this.stretchAllRatio = 0;
  63. this.stretchLastWidth = 0;
  64. this.stretch = stretchH;
  65. this.totalTargetWidth = 0;
  66. this.needVerifyLastColumnWidth = true;
  67. this.stretchAllColumnsWidth = [];
  68. this.calculate();
  69. }
  70. /**
  71. * Calculates viewport
  72. */
  73. calculate() {
  74. let sum = 0;
  75. let needReverse = true;
  76. let startPositions = [];
  77. let columnWidth;
  78. let priv = privatePool.get(this);
  79. let onlyFullyVisible = priv.onlyFullyVisible;
  80. let overrideFn = priv.overrideFn;
  81. let scrollOffset = priv.scrollOffset;
  82. let totalColumns = priv.totalColumns;
  83. let viewportWidth = priv.viewportWidth;
  84. for (let i = 0; i < totalColumns; i++) {
  85. columnWidth = this._getColumnWidth(i);
  86. if (sum <= scrollOffset && !onlyFullyVisible) {
  87. this.startColumn = i;
  88. }
  89. // +1 pixel for row header width compensation for horizontal scroll > 0
  90. let compensatedViewportWidth = scrollOffset > 0 ? viewportWidth + 1 : viewportWidth;
  91. if (sum >= scrollOffset && sum + columnWidth <= scrollOffset + compensatedViewportWidth) {
  92. if (this.startColumn == null) {
  93. this.startColumn = i;
  94. }
  95. this.endColumn = i;
  96. }
  97. startPositions.push(sum);
  98. sum += columnWidth;
  99. if (!onlyFullyVisible) {
  100. this.endColumn = i;
  101. }
  102. if (sum >= scrollOffset + viewportWidth) {
  103. needReverse = false;
  104. break;
  105. }
  106. }
  107. if (this.endColumn === totalColumns - 1 && needReverse) {
  108. this.startColumn = this.endColumn;
  109. while (this.startColumn > 0) {
  110. let viewportSum = startPositions[this.endColumn] + columnWidth - startPositions[this.startColumn - 1];
  111. if (viewportSum <= viewportWidth || !onlyFullyVisible) {
  112. this.startColumn--;
  113. }
  114. if (viewportSum > viewportWidth) {
  115. break;
  116. }
  117. }
  118. }
  119. if (this.startColumn !== null && overrideFn) {
  120. overrideFn(this);
  121. }
  122. this.startPosition = startPositions[this.startColumn];
  123. if (this.startPosition == void 0) {
  124. this.startPosition = null;
  125. }
  126. if (this.startColumn !== null) {
  127. this.count = this.endColumn - this.startColumn + 1;
  128. }
  129. }
  130. /**
  131. * Recalculate columns stretching.
  132. *
  133. * @param {Number} totalWidth
  134. */
  135. refreshStretching(totalWidth) {
  136. if (this.stretch === 'none') {
  137. return;
  138. }
  139. this.totalTargetWidth = totalWidth;
  140. let priv = privatePool.get(this);
  141. let totalColumns = priv.totalColumns;
  142. let sumAll = 0;
  143. for (let i = 0; i < totalColumns; i++) {
  144. let columnWidth = this._getColumnWidth(i);
  145. let permanentColumnWidth = priv.stretchingColumnWidthFn(void 0, i);
  146. if (typeof permanentColumnWidth === 'number') {
  147. totalWidth -= permanentColumnWidth;
  148. } else {
  149. sumAll += columnWidth;
  150. }
  151. }
  152. let remainingSize = totalWidth - sumAll;
  153. if (this.stretch === 'all' && remainingSize > 0) {
  154. this.stretchAllRatio = totalWidth / sumAll;
  155. this.stretchAllColumnsWidth = [];
  156. this.needVerifyLastColumnWidth = true;
  157. } else if (this.stretch === 'last' && totalWidth !== Infinity) {
  158. let columnWidth = this._getColumnWidth(totalColumns - 1);
  159. let lastColumnWidth = remainingSize + columnWidth;
  160. this.stretchLastWidth = lastColumnWidth >= 0 ? lastColumnWidth : columnWidth;
  161. }
  162. }
  163. /**
  164. * Get stretched column width based on stretchH (all or last) setting passed in handsontable instance.
  165. *
  166. * @param {Number} column
  167. * @param {Number} baseWidth
  168. * @returns {Number|null}
  169. */
  170. getStretchedColumnWidth(column, baseWidth) {
  171. let result = null;
  172. if (this.stretch === 'all' && this.stretchAllRatio !== 0) {
  173. result = this._getStretchedAllColumnWidth(column, baseWidth);
  174. } else if (this.stretch === 'last' && this.stretchLastWidth !== 0) {
  175. result = this._getStretchedLastColumnWidth(column);
  176. }
  177. return result;
  178. }
  179. /**
  180. * @param {Number} column
  181. * @param {Number} baseWidth
  182. * @returns {Number}
  183. * @private
  184. */
  185. _getStretchedAllColumnWidth(column, baseWidth) {
  186. let sumRatioWidth = 0;
  187. let priv = privatePool.get(this);
  188. let totalColumns = priv.totalColumns;
  189. if (!this.stretchAllColumnsWidth[column]) {
  190. let stretchedWidth = Math.round(baseWidth * this.stretchAllRatio);
  191. let newStretchedWidth = priv.stretchingColumnWidthFn(stretchedWidth, column);
  192. if (newStretchedWidth === void 0) {
  193. this.stretchAllColumnsWidth[column] = stretchedWidth;
  194. } else {
  195. this.stretchAllColumnsWidth[column] = isNaN(newStretchedWidth) ? this._getColumnWidth(column) : newStretchedWidth;
  196. }
  197. }
  198. if (this.stretchAllColumnsWidth.length === totalColumns && this.needVerifyLastColumnWidth) {
  199. this.needVerifyLastColumnWidth = false;
  200. for (let i = 0; i < this.stretchAllColumnsWidth.length; i++) {
  201. sumRatioWidth += this.stretchAllColumnsWidth[i];
  202. }
  203. if (sumRatioWidth !== this.totalTargetWidth) {
  204. this.stretchAllColumnsWidth[this.stretchAllColumnsWidth.length - 1] += this.totalTargetWidth - sumRatioWidth;
  205. }
  206. }
  207. return this.stretchAllColumnsWidth[column];
  208. }
  209. /**
  210. * @param {Number} column
  211. * @returns {Number|null}
  212. * @private
  213. */
  214. _getStretchedLastColumnWidth(column) {
  215. let priv = privatePool.get(this);
  216. let totalColumns = priv.totalColumns;
  217. if (column === totalColumns - 1) {
  218. return this.stretchLastWidth;
  219. }
  220. return null;
  221. }
  222. /**
  223. * @param {Number} column Column index.
  224. * @returns {Number}
  225. * @private
  226. */
  227. _getColumnWidth(column) {
  228. let width = privatePool.get(this).columnWidthFn(column);
  229. if (width === void 0) {
  230. width = ViewportColumnsCalculator.DEFAULT_WIDTH;
  231. }
  232. return width;
  233. }
  234. }
  235. export default ViewportColumnsCalculator;