e46cad13aedf472494b49f668c81ab9d07cfd13704430c44e52c3fea25eedcaa6c6eeb3f443ade139a8c5048a66ef8f5a075ef20612b212ea3d83ff9dde184 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. import BasePlugin from './../_base';
  2. import {addClass, hasClass, removeClass, outerWidth} from './../../helpers/dom/element';
  3. import EventManager from './../../eventManager';
  4. import {pageX, pageY} from './../../helpers/dom/event';
  5. import {arrayEach} from './../../helpers/array';
  6. import {rangeEach} from './../../helpers/number';
  7. import {registerPlugin} from './../../plugins';
  8. // Developer note! Whenever you make a change in this file, make an analogous change in manualRowResize.js
  9. /**
  10. * @description
  11. * ManualRowResize Plugin.
  12. *
  13. * Has 2 UI components:
  14. * - handle - the draggable element that sets the desired height of the row.
  15. * - guide - the helper guide that shows the desired height as a horizontal guide.
  16. *
  17. * @plugin ManualRowResize
  18. */
  19. class ManualRowResize extends BasePlugin {
  20. constructor(hotInstance) {
  21. super(hotInstance);
  22. this.currentTH = null;
  23. this.currentRow = null;
  24. this.selectedRows = [];
  25. this.currentHeight = null;
  26. this.newSize = null;
  27. this.startY = null;
  28. this.startHeight = null;
  29. this.startOffset = null;
  30. this.handle = document.createElement('DIV');
  31. this.guide = document.createElement('DIV');
  32. this.eventManager = new EventManager(this);
  33. this.pressed = null;
  34. this.dblclick = 0;
  35. this.autoresizeTimeout = null;
  36. this.manualRowHeights = [];
  37. addClass(this.handle, 'manualRowResizer');
  38. addClass(this.guide, 'manualRowResizerGuide');
  39. }
  40. /**
  41. * Check if the plugin is enabled in the handsontable settings.
  42. *
  43. * @returns {Boolean}
  44. */
  45. isEnabled() {
  46. return this.hot.getSettings().manualRowResize;
  47. }
  48. /**
  49. * Enable plugin for this Handsontable instance.
  50. */
  51. enablePlugin() {
  52. if (this.enabled) {
  53. return;
  54. }
  55. this.manualRowHeights = [];
  56. let initialRowHeights = this.hot.getSettings().manualRowResize;
  57. let loadedManualRowHeights = this.loadManualRowHeights();
  58. if (typeof loadedManualRowHeights != 'undefined') {
  59. this.manualRowHeights = loadedManualRowHeights;
  60. } else if (Array.isArray(initialRowHeights)) {
  61. this.manualRowHeights = initialRowHeights;
  62. } else {
  63. this.manualRowHeights = [];
  64. }
  65. this.addHook('modifyRowHeight', (height, row) => this.onModifyRowHeight(height, row));
  66. // Handsontable.hooks.register('beforeRowResize');
  67. // Handsontable.hooks.register('afterRowResize');
  68. this.bindEvents();
  69. super.enablePlugin();
  70. }
  71. /**
  72. * Updates the plugin to use the latest options you have specified.
  73. */
  74. updatePlugin() {
  75. let initialRowHeights = this.hot.getSettings().manualRowResize;
  76. if (Array.isArray(initialRowHeights)) {
  77. this.manualRowHeights = initialRowHeights;
  78. } else if (!initialRowHeights) {
  79. this.manualRowHeights = [];
  80. }
  81. }
  82. /**
  83. * Disable plugin for this Handsontable instance.
  84. */
  85. disablePlugin() {
  86. super.disablePlugin();
  87. }
  88. /**
  89. * Save the current sizes using the persistentState plugin.
  90. */
  91. saveManualRowHeights() {
  92. this.hot.runHooks('persistentStateSave', 'manualRowHeights', this.manualRowHeights);
  93. }
  94. /**
  95. * Load the previously saved sizes using the persistentState plugin.
  96. *
  97. * @returns {Array}
  98. */
  99. loadManualRowHeights() {
  100. let storedState = {};
  101. this.hot.runHooks('persistentStateLoad', 'manualRowHeights', storedState);
  102. return storedState.value;
  103. }
  104. /**
  105. * Set the resize handle position.
  106. *
  107. * @param {HTMLCellElement} TH TH HTML element.
  108. */
  109. setupHandlePosition(TH) {
  110. this.currentTH = TH;
  111. let row = this.hot.view.wt.wtTable.getCoords(TH).row; // getCoords returns CellCoords
  112. let headerWidth = outerWidth(this.currentTH);
  113. if (row >= 0) { // if not col header
  114. let box = this.currentTH.getBoundingClientRect();
  115. this.currentRow = row;
  116. this.selectedRows = [];
  117. if (this.hot.selection.isSelected() && this.hot.selection.selectedHeader.rows) {
  118. let {from, to} = this.hot.getSelectedRange();
  119. let start = from.row;
  120. let end = to.row;
  121. if (start >= end) {
  122. start = to.row;
  123. end = from.row;
  124. }
  125. if (this.currentRow >= start && this.currentRow <= end) {
  126. rangeEach(start, end, (i) => this.selectedRows.push(i));
  127. } else {
  128. this.selectedRows.push(this.currentRow);
  129. }
  130. } else {
  131. this.selectedRows.push(this.currentRow);
  132. }
  133. this.startOffset = box.top - 6;
  134. this.startHeight = parseInt(box.height, 10);
  135. this.handle.style.left = `${box.left}px`;
  136. this.handle.style.top = `${this.startOffset + this.startHeight}px`;
  137. this.handle.style.width = `${headerWidth}px`;
  138. this.hot.rootElement.appendChild(this.handle);
  139. }
  140. }
  141. /**
  142. * Refresh the resize handle position.
  143. */
  144. refreshHandlePosition() {
  145. this.handle.style.top = `${this.startOffset + this.currentHeight}px`;
  146. }
  147. /**
  148. * Set the resize guide position.
  149. */
  150. setupGuidePosition() {
  151. let handleWidth = parseInt(outerWidth(this.handle), 10);
  152. let handleRightPosition = parseInt(this.handle.style.left, 10) + handleWidth;
  153. let maximumVisibleElementWidth = parseInt(this.hot.view.maximumVisibleElementWidth(0), 10);
  154. addClass(this.handle, 'active');
  155. addClass(this.guide, 'active');
  156. this.guide.style.top = this.handle.style.top;
  157. this.guide.style.left = `${handleRightPosition}px`;
  158. this.guide.style.width = `${maximumVisibleElementWidth - handleWidth}px`;
  159. this.hot.rootElement.appendChild(this.guide);
  160. }
  161. /**
  162. * Refresh the resize guide position.
  163. */
  164. refreshGuidePosition() {
  165. this.guide.style.top = this.handle.style.top;
  166. }
  167. /**
  168. * Hide both the resize handle and resize guide.
  169. */
  170. hideHandleAndGuide() {
  171. removeClass(this.handle, 'active');
  172. removeClass(this.guide, 'active');
  173. }
  174. /**
  175. * Check if provided element is considered as a row header.
  176. *
  177. * @param {HTMLElement} element HTML element.
  178. * @returns {Boolean}
  179. */
  180. checkIfRowHeader(element) {
  181. if (element != this.hot.rootElement) {
  182. let parent = element.parentNode;
  183. if (parent.tagName === 'TBODY') {
  184. return true;
  185. }
  186. return this.checkIfRowHeader(parent);
  187. }
  188. return false;
  189. }
  190. /**
  191. * Get the TH element from the provided element.
  192. *
  193. * @param {HTMLElement} element HTML element.
  194. * @returns {HTMLElement}
  195. */
  196. getTHFromTargetElement(element) {
  197. if (element.tagName != 'TABLE') {
  198. if (element.tagName == 'TH') {
  199. return element;
  200. }
  201. return this.getTHFromTargetElement(element.parentNode);
  202. }
  203. return null;
  204. }
  205. /**
  206. * 'mouseover' event callback - set the handle position.
  207. *
  208. * @private
  209. * @param {MouseEvent} event
  210. */
  211. onMouseOver(event) {
  212. if (this.checkIfRowHeader(event.target)) {
  213. let th = this.getTHFromTargetElement(event.target);
  214. if (th) {
  215. if (!this.pressed) {
  216. this.setupHandlePosition(th);
  217. }
  218. }
  219. }
  220. }
  221. /**
  222. * Auto-size row after doubleclick - callback.
  223. *
  224. * @private
  225. */
  226. afterMouseDownTimeout() {
  227. const render = () => {
  228. this.hot.forceFullRender = true;
  229. this.hot.view.render(); // updates all
  230. this.hot.view.wt.wtOverlays.adjustElementsSize(true);
  231. };
  232. const resize = (selectedRow, forceRender) => {
  233. let hookNewSize = this.hot.runHooks('beforeRowResize', selectedRow, this.newSize, true);
  234. if (hookNewSize !== void 0) {
  235. this.newSize = hookNewSize;
  236. }
  237. this.setManualSize(selectedRow, this.newSize); // double click sets auto row size
  238. if (forceRender) {
  239. render();
  240. }
  241. this.hot.runHooks('afterRowResize', selectedRow, this.newSize, true);
  242. };
  243. if (this.dblclick >= 2) {
  244. let selectedRowsLength = this.selectedRows.length;
  245. if (selectedRowsLength > 1) {
  246. arrayEach(this.selectedRows, (selectedRow) => {
  247. resize(selectedRow);
  248. });
  249. render();
  250. } else {
  251. arrayEach(this.selectedRows, (selectedRow) => {
  252. resize(selectedRow, true);
  253. });
  254. }
  255. }
  256. this.dblclick = 0;
  257. this.autoresizeTimeout = null;
  258. }
  259. /**
  260. * 'mousedown' event callback.
  261. *
  262. * @private
  263. * @param {MouseEvent} event
  264. */
  265. onMouseDown(event) {
  266. if (hasClass(event.target, 'manualRowResizer')) {
  267. this.setupGuidePosition();
  268. this.pressed = this.hot;
  269. if (this.autoresizeTimeout == null) {
  270. this.autoresizeTimeout = setTimeout(() => this.afterMouseDownTimeout(), 500);
  271. this.hot._registerTimeout(this.autoresizeTimeout);
  272. }
  273. this.dblclick++;
  274. this.startY = pageY(event);
  275. this.newSize = this.startHeight;
  276. }
  277. }
  278. /**
  279. * 'mousemove' event callback - refresh the handle and guide positions, cache the new row height.
  280. *
  281. * @private
  282. * @param {MouseEvent} event
  283. */
  284. onMouseMove(event) {
  285. if (this.pressed) {
  286. this.currentHeight = this.startHeight + (pageY(event) - this.startY);
  287. arrayEach(this.selectedRows, (selectedRow) => {
  288. this.newSize = this.setManualSize(selectedRow, this.currentHeight);
  289. });
  290. this.refreshHandlePosition();
  291. this.refreshGuidePosition();
  292. }
  293. }
  294. /**
  295. * 'mouseup' event callback - apply the row resizing.
  296. *
  297. * @private
  298. * @param {MouseEvent} event
  299. */
  300. onMouseUp(event) {
  301. const render = () => {
  302. this.hot.forceFullRender = true;
  303. this.hot.view.render(); // updates all
  304. this.hot.view.wt.wtOverlays.adjustElementsSize(true);
  305. };
  306. const runHooks = (selectedRow, forceRender) => {
  307. this.hot.runHooks('beforeRowResize', selectedRow, this.newSize);
  308. if (forceRender) {
  309. render();
  310. }
  311. this.saveManualRowHeights();
  312. this.hot.runHooks('afterRowResize', selectedRow, this.newSize);
  313. };
  314. if (this.pressed) {
  315. this.hideHandleAndGuide();
  316. this.pressed = false;
  317. if (this.newSize != this.startHeight) {
  318. let selectedRowsLength = this.selectedRows.length;
  319. if (selectedRowsLength > 1) {
  320. arrayEach(this.selectedRows, (selectedRow) => {
  321. runHooks(selectedRow);
  322. });
  323. render();
  324. } else {
  325. arrayEach(this.selectedRows, (selectedRow) => {
  326. runHooks(selectedRow, true);
  327. });
  328. }
  329. }
  330. this.setupHandlePosition(this.currentTH);
  331. }
  332. }
  333. /**
  334. * Bind the mouse events.
  335. *
  336. * @private
  337. */
  338. bindEvents() {
  339. this.eventManager.addEventListener(this.hot.rootElement, 'mouseover', (e) => this.onMouseOver(e));
  340. this.eventManager.addEventListener(this.hot.rootElement, 'mousedown', (e) => this.onMouseDown(e));
  341. this.eventManager.addEventListener(window, 'mousemove', (e) => this.onMouseMove(e));
  342. this.eventManager.addEventListener(window, 'mouseup', (e) => this.onMouseUp(e));
  343. }
  344. /**
  345. * Cache the current row height.
  346. *
  347. * @param {Number} row Row index.
  348. * @param {Number} height Row height.
  349. * @returns {Number}
  350. */
  351. setManualSize(row, height) {
  352. row = this.hot.runHooks('modifyRow', row);
  353. this.manualRowHeights[row] = height;
  354. return height;
  355. }
  356. /**
  357. * Modify the provided row height, based on the plugin settings.
  358. *
  359. * @private
  360. * @param {Number} height Row height.
  361. * @param {Number} row Row index.
  362. * @returns {Number}
  363. */
  364. onModifyRowHeight(height, row) {
  365. if (this.enabled) {
  366. let autoRowSizePlugin = this.hot.getPlugin('autoRowSize');
  367. let autoRowHeightResult = autoRowSizePlugin ? autoRowSizePlugin.heights[row] : null;
  368. row = this.hot.runHooks('modifyRow', row);
  369. let manualRowHeight = this.manualRowHeights[row];
  370. if (manualRowHeight !== void 0 && (manualRowHeight === autoRowHeightResult || manualRowHeight > (height || 0))) {
  371. return manualRowHeight;
  372. }
  373. }
  374. return height;
  375. }
  376. }
  377. registerPlugin('manualRowResize', ManualRowResize);
  378. export default ManualRowResize;