manualColumnResize.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. 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. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. 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; }
  5. 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; }
  6. import BasePlugin from './../_base.js';
  7. import { addClass, hasClass, removeClass, outerHeight } from './../../helpers/dom/element';
  8. import EventManager from './../../eventManager';
  9. import { pageX, pageY } from './../../helpers/dom/event';
  10. import { arrayEach } from './../../helpers/array';
  11. import { rangeEach } from './../../helpers/number';
  12. import { registerPlugin } from './../../plugins';
  13. // Developer note! Whenever you make a change in this file, make an analogous change in manualRowResize.js
  14. /**
  15. * @description
  16. * ManualColumnResize Plugin.
  17. *
  18. * Has 2 UI components:
  19. * - handle - the draggable element that sets the desired width of the column.
  20. * - guide - the helper guide that shows the desired width as a vertical guide.
  21. *
  22. * @plugin ManualColumnResize
  23. */
  24. var ManualColumnResize = function (_BasePlugin) {
  25. _inherits(ManualColumnResize, _BasePlugin);
  26. function ManualColumnResize(hotInstance) {
  27. _classCallCheck(this, ManualColumnResize);
  28. var _this = _possibleConstructorReturn(this, (ManualColumnResize.__proto__ || Object.getPrototypeOf(ManualColumnResize)).call(this, hotInstance));
  29. _this.currentTH = null;
  30. _this.currentCol = null;
  31. _this.selectedCols = [];
  32. _this.currentWidth = null;
  33. _this.newSize = null;
  34. _this.startY = null;
  35. _this.startWidth = null;
  36. _this.startOffset = null;
  37. _this.handle = document.createElement('DIV');
  38. _this.guide = document.createElement('DIV');
  39. _this.eventManager = new EventManager(_this);
  40. _this.pressed = null;
  41. _this.dblclick = 0;
  42. _this.autoresizeTimeout = null;
  43. _this.manualColumnWidths = [];
  44. addClass(_this.handle, 'manualColumnResizer');
  45. addClass(_this.guide, 'manualColumnResizerGuide');
  46. return _this;
  47. }
  48. /**
  49. * Check if the plugin is enabled in the handsontable settings.
  50. *
  51. * @returns {Boolean}
  52. */
  53. _createClass(ManualColumnResize, [{
  54. key: 'isEnabled',
  55. value: function isEnabled() {
  56. return this.hot.getSettings().manualColumnResize;
  57. }
  58. /**
  59. * Enable plugin for this Handsontable instance.
  60. */
  61. }, {
  62. key: 'enablePlugin',
  63. value: function enablePlugin() {
  64. var _this2 = this;
  65. if (this.enabled) {
  66. return;
  67. }
  68. this.manualColumnWidths = [];
  69. var initialColumnWidth = this.hot.getSettings().manualColumnResize;
  70. var loadedManualColumnWidths = this.loadManualColumnWidths();
  71. this.addHook('modifyColWidth', function (width, col) {
  72. return _this2.onModifyColWidth(width, col);
  73. });
  74. this.addHook('beforeStretchingColumnWidth', function (stretchedWidth, column) {
  75. return _this2.onBeforeStretchingColumnWidth(stretchedWidth, column);
  76. });
  77. this.addHook('beforeColumnResize', function (currentColumn, newSize, isDoubleClick) {
  78. return _this2.onBeforeColumnResize(currentColumn, newSize, isDoubleClick);
  79. });
  80. if (typeof loadedManualColumnWidths != 'undefined') {
  81. this.manualColumnWidths = loadedManualColumnWidths;
  82. } else if (Array.isArray(initialColumnWidth)) {
  83. this.manualColumnWidths = initialColumnWidth;
  84. } else {
  85. this.manualColumnWidths = [];
  86. }
  87. // Handsontable.hooks.register('beforeColumnResize');
  88. // Handsontable.hooks.register('afterColumnResize');
  89. this.bindEvents();
  90. _get(ManualColumnResize.prototype.__proto__ || Object.getPrototypeOf(ManualColumnResize.prototype), 'enablePlugin', this).call(this);
  91. }
  92. /**
  93. * Updates the plugin to use the latest options you have specified.
  94. */
  95. }, {
  96. key: 'updatePlugin',
  97. value: function updatePlugin() {
  98. var initialColumnWidth = this.hot.getSettings().manualColumnResize;
  99. if (Array.isArray(initialColumnWidth)) {
  100. this.manualColumnWidths = initialColumnWidth;
  101. } else if (!initialColumnWidth) {
  102. this.manualColumnWidths = [];
  103. }
  104. }
  105. /**
  106. * Disable plugin for this Handsontable instance.
  107. */
  108. }, {
  109. key: 'disablePlugin',
  110. value: function disablePlugin() {
  111. _get(ManualColumnResize.prototype.__proto__ || Object.getPrototypeOf(ManualColumnResize.prototype), 'disablePlugin', this).call(this);
  112. }
  113. /**
  114. * Save the current sizes using the persistentState plugin.
  115. */
  116. }, {
  117. key: 'saveManualColumnWidths',
  118. value: function saveManualColumnWidths() {
  119. this.hot.runHooks('persistentStateSave', 'manualColumnWidths', this.manualColumnWidths);
  120. }
  121. /**
  122. * Load the previously saved sizes using the persistentState plugin.
  123. *
  124. * @returns {Array}
  125. */
  126. }, {
  127. key: 'loadManualColumnWidths',
  128. value: function loadManualColumnWidths() {
  129. var storedState = {};
  130. this.hot.runHooks('persistentStateLoad', 'manualColumnWidths', storedState);
  131. return storedState.value;
  132. }
  133. /**
  134. * Set the resize handle position.
  135. *
  136. * @param {HTMLCellElement} TH TH HTML element.
  137. */
  138. }, {
  139. key: 'setupHandlePosition',
  140. value: function setupHandlePosition(TH) {
  141. var _this3 = this;
  142. if (!TH.parentNode) {
  143. return false;
  144. }
  145. this.currentTH = TH;
  146. var col = this.hot.view.wt.wtTable.getCoords(TH).col; // getCoords returns CellCoords
  147. var headerHeight = outerHeight(this.currentTH);
  148. if (col >= 0) {
  149. // if not col header
  150. var box = this.currentTH.getBoundingClientRect();
  151. this.currentCol = col;
  152. this.selectedCols = [];
  153. if (this.hot.selection.isSelected() && this.hot.selection.selectedHeader.cols) {
  154. var _hot$getSelectedRange = this.hot.getSelectedRange(),
  155. from = _hot$getSelectedRange.from,
  156. to = _hot$getSelectedRange.to;
  157. var start = from.col;
  158. var end = to.col;
  159. if (start >= end) {
  160. start = to.col;
  161. end = from.col;
  162. }
  163. if (this.currentCol >= start && this.currentCol <= end) {
  164. rangeEach(start, end, function (i) {
  165. return _this3.selectedCols.push(i);
  166. });
  167. } else {
  168. this.selectedCols.push(this.currentCol);
  169. }
  170. } else {
  171. this.selectedCols.push(this.currentCol);
  172. }
  173. this.startOffset = box.left - 6;
  174. this.startWidth = parseInt(box.width, 10);
  175. this.handle.style.top = box.top + 'px';
  176. this.handle.style.left = this.startOffset + this.startWidth + 'px';
  177. this.handle.style.height = headerHeight + 'px';
  178. this.hot.rootElement.appendChild(this.handle);
  179. }
  180. }
  181. /**
  182. * Refresh the resize handle position.
  183. */
  184. }, {
  185. key: 'refreshHandlePosition',
  186. value: function refreshHandlePosition() {
  187. this.handle.style.left = this.startOffset + this.currentWidth + 'px';
  188. }
  189. /**
  190. * Set the resize guide position.
  191. */
  192. }, {
  193. key: 'setupGuidePosition',
  194. value: function setupGuidePosition() {
  195. var handleHeight = parseInt(outerHeight(this.handle), 10);
  196. var handleBottomPosition = parseInt(this.handle.style.top, 10) + handleHeight;
  197. var maximumVisibleElementHeight = parseInt(this.hot.view.maximumVisibleElementHeight(0), 10);
  198. addClass(this.handle, 'active');
  199. addClass(this.guide, 'active');
  200. this.guide.style.top = handleBottomPosition + 'px';
  201. this.guide.style.left = this.handle.style.left;
  202. this.guide.style.height = maximumVisibleElementHeight - handleHeight + 'px';
  203. this.hot.rootElement.appendChild(this.guide);
  204. }
  205. /**
  206. * Refresh the resize guide position.
  207. */
  208. }, {
  209. key: 'refreshGuidePosition',
  210. value: function refreshGuidePosition() {
  211. this.guide.style.left = this.handle.style.left;
  212. }
  213. /**
  214. * Hide both the resize handle and resize guide.
  215. */
  216. }, {
  217. key: 'hideHandleAndGuide',
  218. value: function hideHandleAndGuide() {
  219. removeClass(this.handle, 'active');
  220. removeClass(this.guide, 'active');
  221. }
  222. /**
  223. * Check if provided element is considered a column header.
  224. *
  225. * @param {HTMLElement} element HTML element.
  226. * @returns {Boolean}
  227. */
  228. }, {
  229. key: 'checkIfColumnHeader',
  230. value: function checkIfColumnHeader(element) {
  231. if (element != this.hot.rootElement) {
  232. var parent = element.parentNode;
  233. if (parent.tagName === 'THEAD') {
  234. return true;
  235. }
  236. return this.checkIfColumnHeader(parent);
  237. }
  238. return false;
  239. }
  240. /**
  241. * Get the TH element from the provided element.
  242. *
  243. * @param {HTMLElement} element HTML element.
  244. * @returns {HTMLElement}
  245. */
  246. }, {
  247. key: 'getTHFromTargetElement',
  248. value: function getTHFromTargetElement(element) {
  249. if (element.tagName != 'TABLE') {
  250. if (element.tagName == 'TH') {
  251. return element;
  252. }
  253. return this.getTHFromTargetElement(element.parentNode);
  254. }
  255. return null;
  256. }
  257. /**
  258. * 'mouseover' event callback - set the handle position.
  259. *
  260. * @private
  261. * @param {MouseEvent} event
  262. */
  263. }, {
  264. key: 'onMouseOver',
  265. value: function onMouseOver(event) {
  266. if (this.checkIfColumnHeader(event.target)) {
  267. var th = this.getTHFromTargetElement(event.target);
  268. if (!th) {
  269. return;
  270. }
  271. var colspan = th.getAttribute('colspan');
  272. if (th && (colspan === null || colspan === 1)) {
  273. if (!this.pressed) {
  274. this.setupHandlePosition(th);
  275. }
  276. }
  277. }
  278. }
  279. /**
  280. * Auto-size row after doubleclick - callback.
  281. *
  282. * @private
  283. */
  284. }, {
  285. key: 'afterMouseDownTimeout',
  286. value: function afterMouseDownTimeout() {
  287. var _this4 = this;
  288. var render = function render() {
  289. _this4.hot.forceFullRender = true;
  290. _this4.hot.view.render(); // updates all
  291. _this4.hot.view.wt.wtOverlays.adjustElementsSize(true);
  292. };
  293. var resize = function resize(selectedCol, forceRender) {
  294. var hookNewSize = _this4.hot.runHooks('beforeColumnResize', selectedCol, _this4.newSize, true);
  295. if (hookNewSize !== void 0) {
  296. _this4.newSize = hookNewSize;
  297. }
  298. if (_this4.hot.getSettings().stretchH === 'all') {
  299. _this4.clearManualSize(selectedCol);
  300. } else {
  301. _this4.setManualSize(selectedCol, _this4.newSize); // double click sets by auto row size plugin
  302. }
  303. if (forceRender) {
  304. render();
  305. }
  306. _this4.saveManualColumnWidths();
  307. _this4.hot.runHooks('afterColumnResize', selectedCol, _this4.newSize, true);
  308. };
  309. if (this.dblclick >= 2) {
  310. var selectedColsLength = this.selectedCols.length;
  311. if (selectedColsLength > 1) {
  312. arrayEach(this.selectedCols, function (selectedCol) {
  313. resize(selectedCol);
  314. });
  315. render();
  316. } else {
  317. arrayEach(this.selectedCols, function (selectedCol) {
  318. resize(selectedCol, true);
  319. });
  320. }
  321. }
  322. this.dblclick = 0;
  323. this.autoresizeTimeout = null;
  324. }
  325. /**
  326. * 'mousedown' event callback.
  327. *
  328. * @private
  329. * @param {MouseEvent} e
  330. */
  331. }, {
  332. key: 'onMouseDown',
  333. value: function onMouseDown(event) {
  334. var _this5 = this;
  335. if (hasClass(event.target, 'manualColumnResizer')) {
  336. this.setupGuidePosition();
  337. this.pressed = this.hot;
  338. if (this.autoresizeTimeout === null) {
  339. this.autoresizeTimeout = setTimeout(function () {
  340. return _this5.afterMouseDownTimeout();
  341. }, 500);
  342. this.hot._registerTimeout(this.autoresizeTimeout);
  343. }
  344. this.dblclick++;
  345. this.startX = pageX(event);
  346. this.newSize = this.startWidth;
  347. }
  348. }
  349. /**
  350. * 'mousemove' event callback - refresh the handle and guide positions, cache the new column width.
  351. *
  352. * @private
  353. * @param {MouseEvent} e
  354. */
  355. }, {
  356. key: 'onMouseMove',
  357. value: function onMouseMove(event) {
  358. var _this6 = this;
  359. if (this.pressed) {
  360. this.currentWidth = this.startWidth + (pageX(event) - this.startX);
  361. arrayEach(this.selectedCols, function (selectedCol) {
  362. _this6.newSize = _this6.setManualSize(selectedCol, _this6.currentWidth);
  363. });
  364. this.refreshHandlePosition();
  365. this.refreshGuidePosition();
  366. }
  367. }
  368. /**
  369. * 'mouseup' event callback - apply the column resizing.
  370. *
  371. * @private
  372. * @param {MouseEvent} e
  373. */
  374. }, {
  375. key: 'onMouseUp',
  376. value: function onMouseUp(event) {
  377. var _this7 = this;
  378. var render = function render() {
  379. _this7.hot.forceFullRender = true;
  380. _this7.hot.view.render(); // updates all
  381. _this7.hot.view.wt.wtOverlays.adjustElementsSize(true);
  382. };
  383. var resize = function resize(selectedCol, forceRender) {
  384. _this7.hot.runHooks('beforeColumnResize', selectedCol, _this7.newSize);
  385. if (forceRender) {
  386. render();
  387. }
  388. _this7.saveManualColumnWidths();
  389. _this7.hot.runHooks('afterColumnResize', selectedCol, _this7.newSize);
  390. };
  391. if (this.pressed) {
  392. this.hideHandleAndGuide();
  393. this.pressed = false;
  394. if (this.newSize != this.startWidth) {
  395. var selectedColsLength = this.selectedCols.length;
  396. if (selectedColsLength > 1) {
  397. arrayEach(this.selectedCols, function (selectedCol) {
  398. resize(selectedCol);
  399. });
  400. render();
  401. } else {
  402. arrayEach(this.selectedCols, function (selectedCol) {
  403. resize(selectedCol, true);
  404. });
  405. }
  406. }
  407. this.setupHandlePosition(this.currentTH);
  408. }
  409. }
  410. /**
  411. * Bind the mouse events.
  412. *
  413. * @private
  414. */
  415. }, {
  416. key: 'bindEvents',
  417. value: function bindEvents() {
  418. var _this8 = this;
  419. this.eventManager.addEventListener(this.hot.rootElement, 'mouseover', function (e) {
  420. return _this8.onMouseOver(e);
  421. });
  422. this.eventManager.addEventListener(this.hot.rootElement, 'mousedown', function (e) {
  423. return _this8.onMouseDown(e);
  424. });
  425. this.eventManager.addEventListener(window, 'mousemove', function (e) {
  426. return _this8.onMouseMove(e);
  427. });
  428. this.eventManager.addEventListener(window, 'mouseup', function (e) {
  429. return _this8.onMouseUp(e);
  430. });
  431. }
  432. /**
  433. * Cache the current column width.
  434. *
  435. * @param {Number} column Column index.
  436. * @param {Number} width Column width.
  437. * @returns {Number}
  438. */
  439. }, {
  440. key: 'setManualSize',
  441. value: function setManualSize(column, width) {
  442. width = Math.max(width, 20);
  443. /**
  444. * We need to run col through modifyCol hook, in case the order of displayed columns is different than the order
  445. * in data source. For instance, this order can be modified by manualColumnMove plugin.
  446. */
  447. column = this.hot.runHooks('modifyCol', column);
  448. this.manualColumnWidths[column] = width;
  449. return width;
  450. }
  451. /**
  452. * Clear cache for the current column index.
  453. *
  454. * @param {Number} column Column index.
  455. */
  456. }, {
  457. key: 'clearManualSize',
  458. value: function clearManualSize(column) {
  459. column = this.hot.runHooks('modifyCol', column);
  460. this.manualColumnWidths[column] = void 0;
  461. }
  462. /**
  463. * Modify the provided column width, based on the plugin settings
  464. *
  465. * @private
  466. * @param {Number} width Column width.
  467. * @param {Number} column Column index.
  468. * @returns {Number}
  469. */
  470. }, {
  471. key: 'onModifyColWidth',
  472. value: function onModifyColWidth(width, column) {
  473. if (this.enabled) {
  474. column = this.hot.runHooks('modifyCol', column);
  475. if (this.hot.getSettings().manualColumnResize && this.manualColumnWidths[column]) {
  476. return this.manualColumnWidths[column];
  477. }
  478. }
  479. return width;
  480. }
  481. /**
  482. * Modify the provided column stretched width. This hook decides if specified column should be stretched or not.
  483. *
  484. * @private
  485. * @param {Number} stretchedWidth Stretched width.
  486. * @param {Number} column Column index.
  487. * @returns {Number}
  488. */
  489. }, {
  490. key: 'onBeforeStretchingColumnWidth',
  491. value: function onBeforeStretchingColumnWidth(stretchedWidth, column) {
  492. var width = this.manualColumnWidths[column];
  493. if (width === void 0) {
  494. width = stretchedWidth;
  495. }
  496. return width;
  497. }
  498. /**
  499. * `beforeColumnResize` hook callback.
  500. *
  501. * @private
  502. * @param {Number} currentColumn Index of the resized column.
  503. * @param {Number} newSize Calculated new column width.
  504. * @param {Boolean} isDoubleClick Flag that determines whether there was a double-click.
  505. */
  506. }, {
  507. key: 'onBeforeColumnResize',
  508. value: function onBeforeColumnResize() {
  509. // clear the header height cache information
  510. this.hot.view.wt.wtViewport.hasOversizedColumnHeadersMarked = {};
  511. }
  512. }]);
  513. return ManualColumnResize;
  514. }(BasePlugin);
  515. registerPlugin('manualColumnResize', ManualColumnResize);
  516. export default ManualColumnResize;