manualRowResize.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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';
  7. import { addClass, hasClass, removeClass, outerWidth } 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. * ManualRowResize Plugin.
  17. *
  18. * Has 2 UI components:
  19. * - handle - the draggable element that sets the desired height of the row.
  20. * - guide - the helper guide that shows the desired height as a horizontal guide.
  21. *
  22. * @plugin ManualRowResize
  23. */
  24. var ManualRowResize = function (_BasePlugin) {
  25. _inherits(ManualRowResize, _BasePlugin);
  26. function ManualRowResize(hotInstance) {
  27. _classCallCheck(this, ManualRowResize);
  28. var _this = _possibleConstructorReturn(this, (ManualRowResize.__proto__ || Object.getPrototypeOf(ManualRowResize)).call(this, hotInstance));
  29. _this.currentTH = null;
  30. _this.currentRow = null;
  31. _this.selectedRows = [];
  32. _this.currentHeight = null;
  33. _this.newSize = null;
  34. _this.startY = null;
  35. _this.startHeight = 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.manualRowHeights = [];
  44. addClass(_this.handle, 'manualRowResizer');
  45. addClass(_this.guide, 'manualRowResizerGuide');
  46. return _this;
  47. }
  48. /**
  49. * Check if the plugin is enabled in the handsontable settings.
  50. *
  51. * @returns {Boolean}
  52. */
  53. _createClass(ManualRowResize, [{
  54. key: 'isEnabled',
  55. value: function isEnabled() {
  56. return this.hot.getSettings().manualRowResize;
  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.manualRowHeights = [];
  69. var initialRowHeights = this.hot.getSettings().manualRowResize;
  70. var loadedManualRowHeights = this.loadManualRowHeights();
  71. if (typeof loadedManualRowHeights != 'undefined') {
  72. this.manualRowHeights = loadedManualRowHeights;
  73. } else if (Array.isArray(initialRowHeights)) {
  74. this.manualRowHeights = initialRowHeights;
  75. } else {
  76. this.manualRowHeights = [];
  77. }
  78. this.addHook('modifyRowHeight', function (height, row) {
  79. return _this2.onModifyRowHeight(height, row);
  80. });
  81. // Handsontable.hooks.register('beforeRowResize');
  82. // Handsontable.hooks.register('afterRowResize');
  83. this.bindEvents();
  84. _get(ManualRowResize.prototype.__proto__ || Object.getPrototypeOf(ManualRowResize.prototype), 'enablePlugin', this).call(this);
  85. }
  86. /**
  87. * Updates the plugin to use the latest options you have specified.
  88. */
  89. }, {
  90. key: 'updatePlugin',
  91. value: function updatePlugin() {
  92. var initialRowHeights = this.hot.getSettings().manualRowResize;
  93. if (Array.isArray(initialRowHeights)) {
  94. this.manualRowHeights = initialRowHeights;
  95. } else if (!initialRowHeights) {
  96. this.manualRowHeights = [];
  97. }
  98. }
  99. /**
  100. * Disable plugin for this Handsontable instance.
  101. */
  102. }, {
  103. key: 'disablePlugin',
  104. value: function disablePlugin() {
  105. _get(ManualRowResize.prototype.__proto__ || Object.getPrototypeOf(ManualRowResize.prototype), 'disablePlugin', this).call(this);
  106. }
  107. /**
  108. * Save the current sizes using the persistentState plugin.
  109. */
  110. }, {
  111. key: 'saveManualRowHeights',
  112. value: function saveManualRowHeights() {
  113. this.hot.runHooks('persistentStateSave', 'manualRowHeights', this.manualRowHeights);
  114. }
  115. /**
  116. * Load the previously saved sizes using the persistentState plugin.
  117. *
  118. * @returns {Array}
  119. */
  120. }, {
  121. key: 'loadManualRowHeights',
  122. value: function loadManualRowHeights() {
  123. var storedState = {};
  124. this.hot.runHooks('persistentStateLoad', 'manualRowHeights', storedState);
  125. return storedState.value;
  126. }
  127. /**
  128. * Set the resize handle position.
  129. *
  130. * @param {HTMLCellElement} TH TH HTML element.
  131. */
  132. }, {
  133. key: 'setupHandlePosition',
  134. value: function setupHandlePosition(TH) {
  135. var _this3 = this;
  136. this.currentTH = TH;
  137. var row = this.hot.view.wt.wtTable.getCoords(TH).row; // getCoords returns CellCoords
  138. var headerWidth = outerWidth(this.currentTH);
  139. if (row >= 0) {
  140. // if not col header
  141. var box = this.currentTH.getBoundingClientRect();
  142. this.currentRow = row;
  143. this.selectedRows = [];
  144. if (this.hot.selection.isSelected() && this.hot.selection.selectedHeader.rows) {
  145. var _hot$getSelectedRange = this.hot.getSelectedRange(),
  146. from = _hot$getSelectedRange.from,
  147. to = _hot$getSelectedRange.to;
  148. var start = from.row;
  149. var end = to.row;
  150. if (start >= end) {
  151. start = to.row;
  152. end = from.row;
  153. }
  154. if (this.currentRow >= start && this.currentRow <= end) {
  155. rangeEach(start, end, function (i) {
  156. return _this3.selectedRows.push(i);
  157. });
  158. } else {
  159. this.selectedRows.push(this.currentRow);
  160. }
  161. } else {
  162. this.selectedRows.push(this.currentRow);
  163. }
  164. this.startOffset = box.top - 6;
  165. this.startHeight = parseInt(box.height, 10);
  166. this.handle.style.left = box.left + 'px';
  167. this.handle.style.top = this.startOffset + this.startHeight + 'px';
  168. this.handle.style.width = headerWidth + 'px';
  169. this.hot.rootElement.appendChild(this.handle);
  170. }
  171. }
  172. /**
  173. * Refresh the resize handle position.
  174. */
  175. }, {
  176. key: 'refreshHandlePosition',
  177. value: function refreshHandlePosition() {
  178. this.handle.style.top = this.startOffset + this.currentHeight + 'px';
  179. }
  180. /**
  181. * Set the resize guide position.
  182. */
  183. }, {
  184. key: 'setupGuidePosition',
  185. value: function setupGuidePosition() {
  186. var handleWidth = parseInt(outerWidth(this.handle), 10);
  187. var handleRightPosition = parseInt(this.handle.style.left, 10) + handleWidth;
  188. var maximumVisibleElementWidth = parseInt(this.hot.view.maximumVisibleElementWidth(0), 10);
  189. addClass(this.handle, 'active');
  190. addClass(this.guide, 'active');
  191. this.guide.style.top = this.handle.style.top;
  192. this.guide.style.left = handleRightPosition + 'px';
  193. this.guide.style.width = maximumVisibleElementWidth - handleWidth + 'px';
  194. this.hot.rootElement.appendChild(this.guide);
  195. }
  196. /**
  197. * Refresh the resize guide position.
  198. */
  199. }, {
  200. key: 'refreshGuidePosition',
  201. value: function refreshGuidePosition() {
  202. this.guide.style.top = this.handle.style.top;
  203. }
  204. /**
  205. * Hide both the resize handle and resize guide.
  206. */
  207. }, {
  208. key: 'hideHandleAndGuide',
  209. value: function hideHandleAndGuide() {
  210. removeClass(this.handle, 'active');
  211. removeClass(this.guide, 'active');
  212. }
  213. /**
  214. * Check if provided element is considered as a row header.
  215. *
  216. * @param {HTMLElement} element HTML element.
  217. * @returns {Boolean}
  218. */
  219. }, {
  220. key: 'checkIfRowHeader',
  221. value: function checkIfRowHeader(element) {
  222. if (element != this.hot.rootElement) {
  223. var parent = element.parentNode;
  224. if (parent.tagName === 'TBODY') {
  225. return true;
  226. }
  227. return this.checkIfRowHeader(parent);
  228. }
  229. return false;
  230. }
  231. /**
  232. * Get the TH element from the provided element.
  233. *
  234. * @param {HTMLElement} element HTML element.
  235. * @returns {HTMLElement}
  236. */
  237. }, {
  238. key: 'getTHFromTargetElement',
  239. value: function getTHFromTargetElement(element) {
  240. if (element.tagName != 'TABLE') {
  241. if (element.tagName == 'TH') {
  242. return element;
  243. }
  244. return this.getTHFromTargetElement(element.parentNode);
  245. }
  246. return null;
  247. }
  248. /**
  249. * 'mouseover' event callback - set the handle position.
  250. *
  251. * @private
  252. * @param {MouseEvent} event
  253. */
  254. }, {
  255. key: 'onMouseOver',
  256. value: function onMouseOver(event) {
  257. if (this.checkIfRowHeader(event.target)) {
  258. var th = this.getTHFromTargetElement(event.target);
  259. if (th) {
  260. if (!this.pressed) {
  261. this.setupHandlePosition(th);
  262. }
  263. }
  264. }
  265. }
  266. /**
  267. * Auto-size row after doubleclick - callback.
  268. *
  269. * @private
  270. */
  271. }, {
  272. key: 'afterMouseDownTimeout',
  273. value: function afterMouseDownTimeout() {
  274. var _this4 = this;
  275. var render = function render() {
  276. _this4.hot.forceFullRender = true;
  277. _this4.hot.view.render(); // updates all
  278. _this4.hot.view.wt.wtOverlays.adjustElementsSize(true);
  279. };
  280. var resize = function resize(selectedRow, forceRender) {
  281. var hookNewSize = _this4.hot.runHooks('beforeRowResize', selectedRow, _this4.newSize, true);
  282. if (hookNewSize !== void 0) {
  283. _this4.newSize = hookNewSize;
  284. }
  285. _this4.setManualSize(selectedRow, _this4.newSize); // double click sets auto row size
  286. if (forceRender) {
  287. render();
  288. }
  289. _this4.hot.runHooks('afterRowResize', selectedRow, _this4.newSize, true);
  290. };
  291. if (this.dblclick >= 2) {
  292. var selectedRowsLength = this.selectedRows.length;
  293. if (selectedRowsLength > 1) {
  294. arrayEach(this.selectedRows, function (selectedRow) {
  295. resize(selectedRow);
  296. });
  297. render();
  298. } else {
  299. arrayEach(this.selectedRows, function (selectedRow) {
  300. resize(selectedRow, true);
  301. });
  302. }
  303. }
  304. this.dblclick = 0;
  305. this.autoresizeTimeout = null;
  306. }
  307. /**
  308. * 'mousedown' event callback.
  309. *
  310. * @private
  311. * @param {MouseEvent} event
  312. */
  313. }, {
  314. key: 'onMouseDown',
  315. value: function onMouseDown(event) {
  316. var _this5 = this;
  317. if (hasClass(event.target, 'manualRowResizer')) {
  318. this.setupGuidePosition();
  319. this.pressed = this.hot;
  320. if (this.autoresizeTimeout == null) {
  321. this.autoresizeTimeout = setTimeout(function () {
  322. return _this5.afterMouseDownTimeout();
  323. }, 500);
  324. this.hot._registerTimeout(this.autoresizeTimeout);
  325. }
  326. this.dblclick++;
  327. this.startY = pageY(event);
  328. this.newSize = this.startHeight;
  329. }
  330. }
  331. /**
  332. * 'mousemove' event callback - refresh the handle and guide positions, cache the new row height.
  333. *
  334. * @private
  335. * @param {MouseEvent} event
  336. */
  337. }, {
  338. key: 'onMouseMove',
  339. value: function onMouseMove(event) {
  340. var _this6 = this;
  341. if (this.pressed) {
  342. this.currentHeight = this.startHeight + (pageY(event) - this.startY);
  343. arrayEach(this.selectedRows, function (selectedRow) {
  344. _this6.newSize = _this6.setManualSize(selectedRow, _this6.currentHeight);
  345. });
  346. this.refreshHandlePosition();
  347. this.refreshGuidePosition();
  348. }
  349. }
  350. /**
  351. * 'mouseup' event callback - apply the row resizing.
  352. *
  353. * @private
  354. * @param {MouseEvent} event
  355. */
  356. }, {
  357. key: 'onMouseUp',
  358. value: function onMouseUp(event) {
  359. var _this7 = this;
  360. var render = function render() {
  361. _this7.hot.forceFullRender = true;
  362. _this7.hot.view.render(); // updates all
  363. _this7.hot.view.wt.wtOverlays.adjustElementsSize(true);
  364. };
  365. var runHooks = function runHooks(selectedRow, forceRender) {
  366. _this7.hot.runHooks('beforeRowResize', selectedRow, _this7.newSize);
  367. if (forceRender) {
  368. render();
  369. }
  370. _this7.saveManualRowHeights();
  371. _this7.hot.runHooks('afterRowResize', selectedRow, _this7.newSize);
  372. };
  373. if (this.pressed) {
  374. this.hideHandleAndGuide();
  375. this.pressed = false;
  376. if (this.newSize != this.startHeight) {
  377. var selectedRowsLength = this.selectedRows.length;
  378. if (selectedRowsLength > 1) {
  379. arrayEach(this.selectedRows, function (selectedRow) {
  380. runHooks(selectedRow);
  381. });
  382. render();
  383. } else {
  384. arrayEach(this.selectedRows, function (selectedRow) {
  385. runHooks(selectedRow, true);
  386. });
  387. }
  388. }
  389. this.setupHandlePosition(this.currentTH);
  390. }
  391. }
  392. /**
  393. * Bind the mouse events.
  394. *
  395. * @private
  396. */
  397. }, {
  398. key: 'bindEvents',
  399. value: function bindEvents() {
  400. var _this8 = this;
  401. this.eventManager.addEventListener(this.hot.rootElement, 'mouseover', function (e) {
  402. return _this8.onMouseOver(e);
  403. });
  404. this.eventManager.addEventListener(this.hot.rootElement, 'mousedown', function (e) {
  405. return _this8.onMouseDown(e);
  406. });
  407. this.eventManager.addEventListener(window, 'mousemove', function (e) {
  408. return _this8.onMouseMove(e);
  409. });
  410. this.eventManager.addEventListener(window, 'mouseup', function (e) {
  411. return _this8.onMouseUp(e);
  412. });
  413. }
  414. /**
  415. * Cache the current row height.
  416. *
  417. * @param {Number} row Row index.
  418. * @param {Number} height Row height.
  419. * @returns {Number}
  420. */
  421. }, {
  422. key: 'setManualSize',
  423. value: function setManualSize(row, height) {
  424. row = this.hot.runHooks('modifyRow', row);
  425. this.manualRowHeights[row] = height;
  426. return height;
  427. }
  428. /**
  429. * Modify the provided row height, based on the plugin settings.
  430. *
  431. * @private
  432. * @param {Number} height Row height.
  433. * @param {Number} row Row index.
  434. * @returns {Number}
  435. */
  436. }, {
  437. key: 'onModifyRowHeight',
  438. value: function onModifyRowHeight(height, row) {
  439. if (this.enabled) {
  440. var autoRowSizePlugin = this.hot.getPlugin('autoRowSize');
  441. var autoRowHeightResult = autoRowSizePlugin ? autoRowSizePlugin.heights[row] : null;
  442. row = this.hot.runHooks('modifyRow', row);
  443. var manualRowHeight = this.manualRowHeights[row];
  444. if (manualRowHeight !== void 0 && (manualRowHeight === autoRowHeightResult || manualRowHeight > (height || 0))) {
  445. return manualRowHeight;
  446. }
  447. }
  448. return height;
  449. }
  450. }]);
  451. return ManualRowResize;
  452. }(BasePlugin);
  453. registerPlugin('manualRowResize', ManualRowResize);
  454. export default ManualRowResize;