| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- 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; }; }();
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- var privatePool = new WeakMap();
- /**
- * Calculates indexes of columns to render OR columns that are visible.
- * To redo the calculation, you need to create a new calculator.
- *
- * @class ViewportColumnsCalculator
- */
- var ViewportColumnsCalculator = function () {
- _createClass(ViewportColumnsCalculator, null, [{
- key: 'DEFAULT_WIDTH',
- /**
- * Default column width
- *
- * @type {Number}
- */
- get: function get() {
- return 50;
- }
- /**
- * @param {Number} viewportWidth Width of the viewport
- * @param {Number} scrollOffset Current horizontal scroll position of the viewport
- * @param {Number} totalColumns Total number of rows
- * @param {Function} columnWidthFn Function that returns the width of the column at a given index (in px)
- * @param {Function} overrideFn Function that changes calculated this.startRow, this.endRow (used by MergeCells plugin)
- * @param {Boolean} onlyFullyVisible if `true`, only startRow and endRow will be indexes of rows that are fully in viewport
- * @param {Boolean} stretchH
- * @param {Function} [stretchingColumnWidthFn] Function that returns the new width of the stretched column.
- */
- }]);
- function ViewportColumnsCalculator(viewportWidth, scrollOffset, totalColumns, columnWidthFn, overrideFn, onlyFullyVisible, stretchH) {
- var stretchingColumnWidthFn = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : function (width) {
- return width;
- };
- _classCallCheck(this, ViewportColumnsCalculator);
- privatePool.set(this, {
- viewportWidth: viewportWidth,
- scrollOffset: scrollOffset,
- totalColumns: totalColumns,
- columnWidthFn: columnWidthFn,
- overrideFn: overrideFn,
- onlyFullyVisible: onlyFullyVisible,
- stretchingColumnWidthFn: stretchingColumnWidthFn
- });
- /**
- * Number of rendered/visible columns
- *
- * @type {Number}
- */
- this.count = 0;
- /**
- * Index of the first rendered/visible column (can be overwritten using overrideFn)
- *
- * @type {Number|null}
- */
- this.startColumn = null;
- /**
- * Index of the last rendered/visible column (can be overwritten using overrideFn)
- *
- * @type {null}
- */
- this.endColumn = null;
- /**
- * Position of the first rendered/visible column (in px)
- *
- * @type {Number|null}
- */
- this.startPosition = null;
- this.stretchAllRatio = 0;
- this.stretchLastWidth = 0;
- this.stretch = stretchH;
- this.totalTargetWidth = 0;
- this.needVerifyLastColumnWidth = true;
- this.stretchAllColumnsWidth = [];
- this.calculate();
- }
- /**
- * Calculates viewport
- */
- _createClass(ViewportColumnsCalculator, [{
- key: 'calculate',
- value: function calculate() {
- var sum = 0;
- var needReverse = true;
- var startPositions = [];
- var columnWidth = void 0;
- var priv = privatePool.get(this);
- var onlyFullyVisible = priv.onlyFullyVisible;
- var overrideFn = priv.overrideFn;
- var scrollOffset = priv.scrollOffset;
- var totalColumns = priv.totalColumns;
- var viewportWidth = priv.viewportWidth;
- for (var i = 0; i < totalColumns; i++) {
- columnWidth = this._getColumnWidth(i);
- if (sum <= scrollOffset && !onlyFullyVisible) {
- this.startColumn = i;
- }
- // +1 pixel for row header width compensation for horizontal scroll > 0
- var compensatedViewportWidth = scrollOffset > 0 ? viewportWidth + 1 : viewportWidth;
- if (sum >= scrollOffset && sum + columnWidth <= scrollOffset + compensatedViewportWidth) {
- if (this.startColumn == null) {
- this.startColumn = i;
- }
- this.endColumn = i;
- }
- startPositions.push(sum);
- sum += columnWidth;
- if (!onlyFullyVisible) {
- this.endColumn = i;
- }
- if (sum >= scrollOffset + viewportWidth) {
- needReverse = false;
- break;
- }
- }
- if (this.endColumn === totalColumns - 1 && needReverse) {
- this.startColumn = this.endColumn;
- while (this.startColumn > 0) {
- var viewportSum = startPositions[this.endColumn] + columnWidth - startPositions[this.startColumn - 1];
- if (viewportSum <= viewportWidth || !onlyFullyVisible) {
- this.startColumn--;
- }
- if (viewportSum > viewportWidth) {
- break;
- }
- }
- }
- if (this.startColumn !== null && overrideFn) {
- overrideFn(this);
- }
- this.startPosition = startPositions[this.startColumn];
- if (this.startPosition == void 0) {
- this.startPosition = null;
- }
- if (this.startColumn !== null) {
- this.count = this.endColumn - this.startColumn + 1;
- }
- }
- /**
- * Recalculate columns stretching.
- *
- * @param {Number} totalWidth
- */
- }, {
- key: 'refreshStretching',
- value: function refreshStretching(totalWidth) {
- if (this.stretch === 'none') {
- return;
- }
- this.totalTargetWidth = totalWidth;
- var priv = privatePool.get(this);
- var totalColumns = priv.totalColumns;
- var sumAll = 0;
- for (var i = 0; i < totalColumns; i++) {
- var columnWidth = this._getColumnWidth(i);
- var permanentColumnWidth = priv.stretchingColumnWidthFn(void 0, i);
- if (typeof permanentColumnWidth === 'number') {
- totalWidth -= permanentColumnWidth;
- } else {
- sumAll += columnWidth;
- }
- }
- var remainingSize = totalWidth - sumAll;
- if (this.stretch === 'all' && remainingSize > 0) {
- this.stretchAllRatio = totalWidth / sumAll;
- this.stretchAllColumnsWidth = [];
- this.needVerifyLastColumnWidth = true;
- } else if (this.stretch === 'last' && totalWidth !== Infinity) {
- var _columnWidth = this._getColumnWidth(totalColumns - 1);
- var lastColumnWidth = remainingSize + _columnWidth;
- this.stretchLastWidth = lastColumnWidth >= 0 ? lastColumnWidth : _columnWidth;
- }
- }
- /**
- * Get stretched column width based on stretchH (all or last) setting passed in handsontable instance.
- *
- * @param {Number} column
- * @param {Number} baseWidth
- * @returns {Number|null}
- */
- }, {
- key: 'getStretchedColumnWidth',
- value: function getStretchedColumnWidth(column, baseWidth) {
- var result = null;
- if (this.stretch === 'all' && this.stretchAllRatio !== 0) {
- result = this._getStretchedAllColumnWidth(column, baseWidth);
- } else if (this.stretch === 'last' && this.stretchLastWidth !== 0) {
- result = this._getStretchedLastColumnWidth(column);
- }
- return result;
- }
- /**
- * @param {Number} column
- * @param {Number} baseWidth
- * @returns {Number}
- * @private
- */
- }, {
- key: '_getStretchedAllColumnWidth',
- value: function _getStretchedAllColumnWidth(column, baseWidth) {
- var sumRatioWidth = 0;
- var priv = privatePool.get(this);
- var totalColumns = priv.totalColumns;
- if (!this.stretchAllColumnsWidth[column]) {
- var stretchedWidth = Math.round(baseWidth * this.stretchAllRatio);
- var newStretchedWidth = priv.stretchingColumnWidthFn(stretchedWidth, column);
- if (newStretchedWidth === void 0) {
- this.stretchAllColumnsWidth[column] = stretchedWidth;
- } else {
- this.stretchAllColumnsWidth[column] = isNaN(newStretchedWidth) ? this._getColumnWidth(column) : newStretchedWidth;
- }
- }
- if (this.stretchAllColumnsWidth.length === totalColumns && this.needVerifyLastColumnWidth) {
- this.needVerifyLastColumnWidth = false;
- for (var i = 0; i < this.stretchAllColumnsWidth.length; i++) {
- sumRatioWidth += this.stretchAllColumnsWidth[i];
- }
- if (sumRatioWidth !== this.totalTargetWidth) {
- this.stretchAllColumnsWidth[this.stretchAllColumnsWidth.length - 1] += this.totalTargetWidth - sumRatioWidth;
- }
- }
- return this.stretchAllColumnsWidth[column];
- }
- /**
- * @param {Number} column
- * @returns {Number|null}
- * @private
- */
- }, {
- key: '_getStretchedLastColumnWidth',
- value: function _getStretchedLastColumnWidth(column) {
- var priv = privatePool.get(this);
- var totalColumns = priv.totalColumns;
- if (column === totalColumns - 1) {
- return this.stretchLastWidth;
- }
- return null;
- }
- /**
- * @param {Number} column Column index.
- * @returns {Number}
- * @private
- */
- }, {
- key: '_getColumnWidth',
- value: function _getColumnWidth(column) {
- var width = privatePool.get(this).columnWidthFn(column);
- if (width === void 0) {
- width = ViewportColumnsCalculator.DEFAULT_WIDTH;
- }
- return width;
- }
- }]);
- return ViewportColumnsCalculator;
- }();
- export default ViewportColumnsCalculator;
|