autoColumnSize.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  2. var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  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. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  6. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  7. import BasePlugin from './../_base';
  8. import { arrayEach, arrayFilter, arrayReduce, arrayMap } from './../../helpers/array';
  9. import { cancelAnimationFrame, requestAnimationFrame } from './../../helpers/feature';
  10. import { isVisible } from './../../helpers/dom/element';
  11. import GhostTable from './../../utils/ghostTable';
  12. import { isObject, objectEach, hasOwnProperty } from './../../helpers/object';
  13. import { valueAccordingPercent, rangeEach } from './../../helpers/number';
  14. import { registerPlugin } from './../../plugins';
  15. import SamplesGenerator from './../../utils/samplesGenerator';
  16. import { isPercentValue } from './../../helpers/string';
  17. import { ViewportColumnsCalculator } from './../../3rdparty/walkontable/src';
  18. var privatePool = new WeakMap();
  19. /**
  20. * @plugin AutoColumnSize
  21. *
  22. * @description
  23. * This plugin allows to set column widths based on their widest cells.
  24. *
  25. * By default, the plugin is declared as `undefined`, which makes it enabled (same as if it was declared as `true`).
  26. * Enabling this plugin may decrease the overall table performance, as it needs to calculate the widths of all cells to
  27. * resize the columns accordingly.
  28. * If you experience problems with the performance, try turning this feature off and declaring the column widths manually.
  29. *
  30. * Column width calculations are divided into sync and async part. Each of this parts has their own advantages and
  31. * disadvantages. Synchronous calculations are faster but they block the browser UI, while the slower asynchronous operations don't
  32. * block the browser UI.
  33. *
  34. * To configure the sync/async distribution, you can pass an absolute value (number of columns) or a percentage value to a config object:
  35. * ```js
  36. * ...
  37. * // as a number (300 columns in sync, rest async)
  38. * autoColumnSize: {syncLimit: 300},
  39. * ...
  40. *
  41. * ...
  42. * // as a string (percent)
  43. * autoColumnSize: {syncLimit: '40%'},
  44. * ...
  45. * ```
  46. *
  47. * To configure this plugin see {@link Options#autoColumnSize}.
  48. *
  49. * @example
  50. * ```js
  51. * ...
  52. * var hot = new Handsontable(document.getElementById('example'), {
  53. * date: getData(),
  54. * autoColumnSize: true
  55. * });
  56. * // Access to plugin instance:
  57. * var plugin = hot.getPlugin('autoColumnSize');
  58. *
  59. * plugin.getColumnWidth(4);
  60. *
  61. * if (plugin.isEnabled()) {
  62. * // code...
  63. * }
  64. * ...
  65. * ```
  66. */
  67. var AutoColumnSize = function (_BasePlugin) {
  68. _inherits(AutoColumnSize, _BasePlugin);
  69. _createClass(AutoColumnSize, null, [{
  70. key: 'CALCULATION_STEP',
  71. get: function get() {
  72. return 50;
  73. }
  74. }, {
  75. key: 'SYNC_CALCULATION_LIMIT',
  76. get: function get() {
  77. return 50;
  78. }
  79. }]);
  80. function AutoColumnSize(hotInstance) {
  81. _classCallCheck(this, AutoColumnSize);
  82. var _this = _possibleConstructorReturn(this, (AutoColumnSize.__proto__ || Object.getPrototypeOf(AutoColumnSize)).call(this, hotInstance));
  83. privatePool.set(_this, {
  84. /**
  85. * Cached column header names. It is used to diff current column headers with previous state and detect which
  86. * columns width should be updated.
  87. *
  88. * @private
  89. * @type {Array}
  90. */
  91. cachedColumnHeaders: []
  92. });
  93. /**
  94. * Cached columns widths.
  95. *
  96. * @type {Array}
  97. */
  98. _this.widths = [];
  99. /**
  100. * Instance of {@link GhostTable} for rows and columns size calculations.
  101. *
  102. * @type {GhostTable}
  103. */
  104. _this.ghostTable = new GhostTable(_this.hot);
  105. /**
  106. * Instance of {@link SamplesGenerator} for generating samples necessary for columns width calculations.
  107. *
  108. * @type {SamplesGenerator}
  109. */
  110. _this.samplesGenerator = new SamplesGenerator(function (row, col) {
  111. return _this.hot.getDataAtCell(row, col);
  112. });
  113. /**
  114. * `true` only if the first calculation was performed
  115. *
  116. * @type {Boolean}
  117. */
  118. _this.firstCalculation = true;
  119. /**
  120. * `true` if the size calculation is in progress.
  121. *
  122. * @type {Boolean}
  123. */
  124. _this.inProgress = false;
  125. // moved to constructor to allow auto-sizing the columns when the plugin is disabled
  126. _this.addHook('beforeColumnResize', function (col, size, isDblClick) {
  127. return _this.onBeforeColumnResize(col, size, isDblClick);
  128. });
  129. return _this;
  130. }
  131. /**
  132. * Check if the plugin is enabled in the handsontable settings.
  133. *
  134. * @returns {Boolean}
  135. */
  136. _createClass(AutoColumnSize, [{
  137. key: 'isEnabled',
  138. value: function isEnabled() {
  139. return this.hot.getSettings().autoColumnSize !== false && !this.hot.getSettings().colWidths;
  140. }
  141. /**
  142. * Enable plugin for this Handsontable instance.
  143. */
  144. }, {
  145. key: 'enablePlugin',
  146. value: function enablePlugin() {
  147. var _this2 = this;
  148. if (this.enabled) {
  149. return;
  150. }
  151. var setting = this.hot.getSettings().autoColumnSize;
  152. if (setting && setting.useHeaders != null) {
  153. this.ghostTable.setSetting('useHeaders', setting.useHeaders);
  154. }
  155. this.addHook('afterLoadData', function () {
  156. return _this2.onAfterLoadData();
  157. });
  158. this.addHook('beforeChange', function (changes) {
  159. return _this2.onBeforeChange(changes);
  160. });
  161. this.addHook('beforeRender', function (force) {
  162. return _this2.onBeforeRender(force);
  163. });
  164. this.addHook('modifyColWidth', function (width, col) {
  165. return _this2.getColumnWidth(col, width);
  166. });
  167. this.addHook('afterInit', function () {
  168. return _this2.onAfterInit();
  169. });
  170. _get(AutoColumnSize.prototype.__proto__ || Object.getPrototypeOf(AutoColumnSize.prototype), 'enablePlugin', this).call(this);
  171. }
  172. /**
  173. * Update plugin state.
  174. */
  175. }, {
  176. key: 'updatePlugin',
  177. value: function updatePlugin() {
  178. var changedColumns = this.findColumnsWhereHeaderWasChanged();
  179. if (changedColumns.length) {
  180. this.clearCache(changedColumns);
  181. }
  182. _get(AutoColumnSize.prototype.__proto__ || Object.getPrototypeOf(AutoColumnSize.prototype), 'updatePlugin', this).call(this);
  183. }
  184. /**
  185. * Disable plugin for this Handsontable instance.
  186. */
  187. }, {
  188. key: 'disablePlugin',
  189. value: function disablePlugin() {
  190. _get(AutoColumnSize.prototype.__proto__ || Object.getPrototypeOf(AutoColumnSize.prototype), 'disablePlugin', this).call(this);
  191. }
  192. /**
  193. * Calculate a columns width.
  194. *
  195. * @param {Number|Object} colRange Column range object.
  196. * @param {Number|Object} rowRange Row range object.
  197. * @param {Boolean} [force=false] If `true` force calculate width even when value was cached earlier.
  198. */
  199. }, {
  200. key: 'calculateColumnsWidth',
  201. value: function calculateColumnsWidth() {
  202. var colRange = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { from: 0, to: this.hot.countCols() - 1 };
  203. var _this3 = this;
  204. var rowRange = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { from: 0, to: this.hot.countRows() - 1 };
  205. var force = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  206. if (typeof colRange === 'number') {
  207. colRange = { from: colRange, to: colRange };
  208. }
  209. if (typeof rowRange === 'number') {
  210. rowRange = { from: rowRange, to: rowRange };
  211. }
  212. rangeEach(colRange.from, colRange.to, function (col) {
  213. if (force || _this3.widths[col] === void 0 && !_this3.hot._getColWidthFromSettings(col)) {
  214. var samples = _this3.samplesGenerator.generateColumnSamples(col, rowRange);
  215. samples.forEach(function (sample, col) {
  216. return _this3.ghostTable.addColumn(col, sample);
  217. });
  218. }
  219. });
  220. if (this.ghostTable.columns.length) {
  221. this.ghostTable.getWidths(function (col, width) {
  222. _this3.widths[col] = width;
  223. });
  224. this.ghostTable.clean();
  225. }
  226. }
  227. /**
  228. * Calculate all columns width.
  229. *
  230. * @param {Object|Number} rowRange Row range object.
  231. */
  232. }, {
  233. key: 'calculateAllColumnsWidth',
  234. value: function calculateAllColumnsWidth() {
  235. var _this4 = this;
  236. var rowRange = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { from: 0, to: this.hot.countRows() - 1 };
  237. var current = 0;
  238. var length = this.hot.countCols() - 1;
  239. var timer = null;
  240. this.inProgress = true;
  241. var loop = function loop() {
  242. // When hot was destroyed after calculating finished cancel frame
  243. if (!_this4.hot) {
  244. cancelAnimationFrame(timer);
  245. _this4.inProgress = false;
  246. return;
  247. }
  248. _this4.calculateColumnsWidth({
  249. from: current,
  250. to: Math.min(current + AutoColumnSize.CALCULATION_STEP, length)
  251. }, rowRange);
  252. current = current + AutoColumnSize.CALCULATION_STEP + 1;
  253. if (current < length) {
  254. timer = requestAnimationFrame(loop);
  255. } else {
  256. cancelAnimationFrame(timer);
  257. _this4.inProgress = false;
  258. // @TODO Should call once per render cycle, currently fired separately in different plugins
  259. _this4.hot.view.wt.wtOverlays.adjustElementsSize(true);
  260. // tmp
  261. if (_this4.hot.view.wt.wtOverlays.leftOverlay.needFullRender) {
  262. _this4.hot.view.wt.wtOverlays.leftOverlay.clone.draw();
  263. }
  264. }
  265. };
  266. // sync
  267. if (this.firstCalculation && this.getSyncCalculationLimit()) {
  268. this.calculateColumnsWidth({ from: 0, to: this.getSyncCalculationLimit() }, rowRange);
  269. this.firstCalculation = false;
  270. current = this.getSyncCalculationLimit() + 1;
  271. }
  272. // async
  273. if (current < length) {
  274. loop();
  275. } else {
  276. this.inProgress = false;
  277. }
  278. }
  279. /**
  280. * Set the sampling options.
  281. *
  282. * @private
  283. */
  284. }, {
  285. key: 'setSamplingOptions',
  286. value: function setSamplingOptions() {
  287. var setting = this.hot.getSettings().autoColumnSize;
  288. var samplingRatio = setting && hasOwnProperty(setting, 'samplingRatio') ? this.hot.getSettings().autoColumnSize.samplingRatio : void 0;
  289. var allowSampleDuplicates = setting && hasOwnProperty(setting, 'allowSampleDuplicates') ? this.hot.getSettings().autoColumnSize.allowSampleDuplicates : void 0;
  290. if (samplingRatio && !isNaN(samplingRatio)) {
  291. this.samplesGenerator.setSampleCount(parseInt(samplingRatio, 10));
  292. }
  293. if (allowSampleDuplicates) {
  294. this.samplesGenerator.setAllowDuplicates(allowSampleDuplicates);
  295. }
  296. }
  297. /**
  298. * Recalculate all columns width (overwrite cache values).
  299. */
  300. }, {
  301. key: 'recalculateAllColumnsWidth',
  302. value: function recalculateAllColumnsWidth() {
  303. if (this.hot.view && isVisible(this.hot.view.wt.wtTable.TABLE)) {
  304. this.clearCache();
  305. this.calculateAllColumnsWidth();
  306. }
  307. }
  308. /**
  309. * Get value which tells how many columns should be calculated synchronously. Rest of the columns will be calculated asynchronously.
  310. *
  311. * @returns {Number}
  312. */
  313. }, {
  314. key: 'getSyncCalculationLimit',
  315. value: function getSyncCalculationLimit() {
  316. /* eslint-disable no-bitwise */
  317. var limit = AutoColumnSize.SYNC_CALCULATION_LIMIT;
  318. var colsLimit = this.hot.countCols() - 1;
  319. if (isObject(this.hot.getSettings().autoColumnSize)) {
  320. limit = this.hot.getSettings().autoColumnSize.syncLimit;
  321. if (isPercentValue(limit)) {
  322. limit = valueAccordingPercent(colsLimit, limit);
  323. } else {
  324. // Force to Number
  325. limit >>= 0;
  326. }
  327. }
  328. return Math.min(limit, colsLimit);
  329. }
  330. /**
  331. * Get the calculated column width.
  332. *
  333. * @param {Number} col Column index.
  334. * @param {Number} [defaultWidth] Default column width. It will be picked up if no calculated width found.
  335. * @param {Boolean} [keepMinimum=true] If `true` then returned value won't be smaller then 50 (default column width).
  336. * @returns {Number}
  337. */
  338. }, {
  339. key: 'getColumnWidth',
  340. value: function getColumnWidth(col) {
  341. var defaultWidth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : void 0;
  342. var keepMinimum = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  343. var width = defaultWidth;
  344. if (width === void 0) {
  345. width = this.widths[col];
  346. if (keepMinimum && typeof width === 'number') {
  347. width = Math.max(width, ViewportColumnsCalculator.DEFAULT_WIDTH);
  348. }
  349. }
  350. return width;
  351. }
  352. /**
  353. * Get the first visible column.
  354. *
  355. * @returns {Number} Returns column index or -1 if table is not rendered.
  356. */
  357. }, {
  358. key: 'getFirstVisibleColumn',
  359. value: function getFirstVisibleColumn() {
  360. var wot = this.hot.view.wt;
  361. if (wot.wtViewport.columnsVisibleCalculator) {
  362. return wot.wtTable.getFirstVisibleColumn();
  363. }
  364. if (wot.wtViewport.columnsRenderCalculator) {
  365. return wot.wtTable.getFirstRenderedColumn();
  366. }
  367. return -1;
  368. }
  369. /**
  370. * Get the last visible column.
  371. *
  372. * @returns {Number} Returns column index or -1 if table is not rendered.
  373. */
  374. }, {
  375. key: 'getLastVisibleColumn',
  376. value: function getLastVisibleColumn() {
  377. var wot = this.hot.view.wt;
  378. if (wot.wtViewport.columnsVisibleCalculator) {
  379. return wot.wtTable.getLastVisibleColumn();
  380. }
  381. if (wot.wtViewport.columnsRenderCalculator) {
  382. return wot.wtTable.getLastRenderedColumn();
  383. }
  384. return -1;
  385. }
  386. /**
  387. * Collects all columns which titles has been changed in comparison to the previous state.
  388. *
  389. * @returns {Array} It returns an array of physical column indexes.
  390. */
  391. }, {
  392. key: 'findColumnsWhereHeaderWasChanged',
  393. value: function findColumnsWhereHeaderWasChanged() {
  394. var columnHeaders = this.hot.getColHeader();
  395. var _privatePool$get = privatePool.get(this),
  396. cachedColumnHeaders = _privatePool$get.cachedColumnHeaders;
  397. var changedColumns = arrayReduce(columnHeaders, function (acc, columnTitle, physicalColumn) {
  398. var cachedColumnsLength = cachedColumnHeaders.length;
  399. if (cachedColumnsLength - 1 < physicalColumn || cachedColumnHeaders[physicalColumn] !== columnTitle) {
  400. acc.push(physicalColumn);
  401. }
  402. if (cachedColumnsLength - 1 < physicalColumn) {
  403. cachedColumnHeaders.push(columnTitle);
  404. } else {
  405. cachedColumnHeaders[physicalColumn] = columnTitle;
  406. }
  407. return acc;
  408. }, []);
  409. return changedColumns;
  410. }
  411. /**
  412. * Clear cache of calculated column widths. If you want to clear only selected columns pass an array with their indexes.
  413. * Otherwise whole cache will be cleared.
  414. *
  415. * @param {Array} [columns=[]] List of column indexes (physical indexes) to clear.
  416. */
  417. }, {
  418. key: 'clearCache',
  419. value: function clearCache() {
  420. var _this5 = this;
  421. var columns = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  422. if (columns.length) {
  423. arrayEach(columns, function (physicalIndex) {
  424. _this5.widths[physicalIndex] = void 0;
  425. });
  426. } else {
  427. this.widths.length = 0;
  428. }
  429. }
  430. /**
  431. * Check if all widths were calculated. If not then return `true` (need recalculate).
  432. *
  433. * @returns {Boolean}
  434. */
  435. }, {
  436. key: 'isNeedRecalculate',
  437. value: function isNeedRecalculate() {
  438. return !!arrayFilter(this.widths, function (item) {
  439. return item === void 0;
  440. }).length;
  441. }
  442. /**
  443. * On before render listener.
  444. *
  445. * @private
  446. */
  447. }, {
  448. key: 'onBeforeRender',
  449. value: function onBeforeRender() {
  450. var force = this.hot.renderCall;
  451. var rowsCount = this.hot.countRows();
  452. // Keep last column widths unchanged for situation when all rows was deleted or trimmed (pro #6)
  453. if (!rowsCount) {
  454. return;
  455. }
  456. this.calculateColumnsWidth({ from: this.getFirstVisibleColumn(), to: this.getLastVisibleColumn() }, void 0, force);
  457. if (this.isNeedRecalculate() && !this.inProgress) {
  458. this.calculateAllColumnsWidth();
  459. }
  460. }
  461. /**
  462. * On after load data listener.
  463. *
  464. * @private
  465. */
  466. }, {
  467. key: 'onAfterLoadData',
  468. value: function onAfterLoadData() {
  469. var _this6 = this;
  470. if (this.hot.view) {
  471. this.recalculateAllColumnsWidth();
  472. } else {
  473. // first load - initialization
  474. setTimeout(function () {
  475. if (_this6.hot) {
  476. _this6.recalculateAllColumnsWidth();
  477. }
  478. }, 0);
  479. }
  480. }
  481. /**
  482. * On before change listener.
  483. *
  484. * @private
  485. * @param {Array} changes
  486. */
  487. }, {
  488. key: 'onBeforeChange',
  489. value: function onBeforeChange(changes) {
  490. var _this7 = this;
  491. var changedColumns = arrayMap(changes, function (_ref) {
  492. var _ref2 = _slicedToArray(_ref, 2),
  493. row = _ref2[0],
  494. column = _ref2[1];
  495. return _this7.hot.propToCol(column);
  496. });
  497. this.clearCache(changedColumns);
  498. }
  499. /**
  500. * On before column resize listener.
  501. *
  502. * @private
  503. * @param {Number} col
  504. * @param {Number} size
  505. * @param {Boolean} isDblClick
  506. * @returns {Number}
  507. */
  508. }, {
  509. key: 'onBeforeColumnResize',
  510. value: function onBeforeColumnResize(col, size, isDblClick) {
  511. if (isDblClick) {
  512. this.calculateColumnsWidth(col, void 0, true);
  513. size = this.getColumnWidth(col, void 0, false);
  514. }
  515. return size;
  516. }
  517. /**
  518. * On after Handsontable init fill plugin with all necessary values.
  519. *
  520. * @private
  521. */
  522. }, {
  523. key: 'onAfterInit',
  524. value: function onAfterInit() {
  525. privatePool.get(this).cachedColumnHeaders = this.hot.getColHeader();
  526. }
  527. /**
  528. * Destroy plugin instance.
  529. */
  530. }, {
  531. key: 'destroy',
  532. value: function destroy() {
  533. this.ghostTable.clean();
  534. _get(AutoColumnSize.prototype.__proto__ || Object.getPrototypeOf(AutoColumnSize.prototype), 'destroy', this).call(this);
  535. }
  536. }]);
  537. return AutoColumnSize;
  538. }(BasePlugin);
  539. registerPlugin('autoColumnSize', AutoColumnSize);
  540. export default AutoColumnSize;