e2a325f28d181f22bb9664ac6e6439c50067c9ccf15656d6185c2b22b40feb47f0f0aa78ac52b352c653d2e7f2604d3c60af4f54d3bd806aea93c4ba98b531 9.6 KB

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