e056b741378059ca8871879372bc8aeba04124a2e4f9902dfe3a9a29740b291cd23fd0596b6d191c42206c381b7b547a497099eb6be304cbddf69f42511407 9.5 KB

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