a767c57959311283162936851a79491668cadef1e889b6e531911f30889b4d11161cb0d70cfb57e8da5da1ce007371d989beb5ec1e7523475d514227923480 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. import BasePlugin from './../_base.js';
  2. import Hooks from './../../pluginHooks';
  3. import {arrayEach} from './../../helpers/array';
  4. import {addClass, removeClass, offset} from './../../helpers/dom/element';
  5. import {rangeEach} from './../../helpers/number';
  6. import EventManager from './../../eventManager';
  7. import {registerPlugin} from './../../plugins';
  8. import RowsMapper from './rowsMapper';
  9. import BacklightUI from './ui/backlight';
  10. import GuidelineUI from './ui/guideline';
  11. import {CellCoords} from './../../3rdparty/walkontable/src';
  12. import './manualRowMove.css';
  13. Hooks.getSingleton().register('beforeRowMove');
  14. Hooks.getSingleton().register('afterRowMove');
  15. Hooks.getSingleton().register('unmodifyRow');
  16. const privatePool = new WeakMap();
  17. const CSS_PLUGIN = 'ht__manualRowMove';
  18. const CSS_SHOW_UI = 'show-ui';
  19. const CSS_ON_MOVING = 'on-moving--rows';
  20. const CSS_AFTER_SELECTION = 'after-selection--rows';
  21. /**
  22. * @plugin ManualRowMove
  23. *
  24. * @description
  25. * This plugin allows to change rows order.
  26. *
  27. * API:
  28. * - moveRow - move single row to the new position.
  29. * - moveRows - move many rows (as an array of indexes) to the new position.
  30. *
  31. * If you want apply visual changes, you have to call manually the render() method on the instance of handsontable.
  32. *
  33. * UI components:
  34. * - backlight - highlight of selected rows.
  35. * - guideline - line which shows where rows has been moved.
  36. *
  37. * @class ManualRowMove
  38. * @plugin ManualRowMove
  39. */
  40. class ManualRowMove extends BasePlugin {
  41. constructor(hotInstance) {
  42. super(hotInstance);
  43. /**
  44. * Set up WeakMap of plugin to sharing private parameters;
  45. */
  46. privatePool.set(this, {
  47. rowsToMove: [],
  48. pressed: void 0,
  49. disallowMoving: void 0,
  50. target: {
  51. eventPageY: void 0,
  52. coords: void 0,
  53. TD: void 0,
  54. row: void 0
  55. }
  56. });
  57. /**
  58. * List of last removed row indexes.
  59. *
  60. * @type {Array}
  61. */
  62. this.removedRows = [];
  63. /**
  64. * Object containing visual row indexes mapped to data source indexes.
  65. *
  66. * @type {RowsMapper}
  67. */
  68. this.rowsMapper = new RowsMapper(this);
  69. /**
  70. * Event Manager object.
  71. *
  72. * @type {Object}
  73. */
  74. this.eventManager = new EventManager(this);
  75. /**
  76. * Backlight UI object.
  77. *
  78. * @type {Object}
  79. */
  80. this.backlight = new BacklightUI(hotInstance);
  81. /**
  82. * Guideline UI object.
  83. *
  84. * @type {Object}
  85. */
  86. this.guideline = new GuidelineUI(hotInstance);
  87. }
  88. /**
  89. * Check if plugin is enabled.
  90. *
  91. * @returns {Boolean}
  92. */
  93. isEnabled() {
  94. return !!this.hot.getSettings().manualRowMove;
  95. }
  96. /**
  97. * Enable the plugin.
  98. */
  99. enablePlugin() {
  100. if (this.enabled) {
  101. return;
  102. }
  103. this.addHook('beforeOnCellMouseDown', (event, coords, TD, blockCalculations) => this.onBeforeOnCellMouseDown(event, coords, TD, blockCalculations));
  104. this.addHook('beforeOnCellMouseOver', (event, coords, TD, blockCalculations) => this.onBeforeOnCellMouseOver(event, coords, TD, blockCalculations));
  105. this.addHook('afterScrollHorizontally', () => this.onAfterScrollHorizontally());
  106. this.addHook('modifyRow', (row, source) => this.onModifyRow(row, source));
  107. this.addHook('beforeRemoveRow', (index, amount) => this.onBeforeRemoveRow(index, amount));
  108. this.addHook('afterRemoveRow', (index, amount) => this.onAfterRemoveRow(index, amount));
  109. this.addHook('afterCreateRow', (index, amount) => this.onAfterCreateRow(index, amount));
  110. this.addHook('afterLoadData', (firstTime) => this.onAfterLoadData(firstTime));
  111. this.addHook('beforeColumnSort', (column, order) => this.onBeforeColumnSort(column, order));
  112. this.addHook('unmodifyRow', (row) => this.onUnmodifyRow(row));
  113. this.registerEvents();
  114. // TODO: move adding plugin classname to BasePlugin.
  115. addClass(this.hot.rootElement, CSS_PLUGIN);
  116. super.enablePlugin();
  117. }
  118. /**
  119. * Updates the plugin to use the latest options you have specified.
  120. */
  121. updatePlugin() {
  122. this.disablePlugin();
  123. this.enablePlugin();
  124. this.onAfterPluginsInitialized();
  125. super.updatePlugin();
  126. }
  127. /**
  128. * Disable plugin for this Handsontable instance.
  129. */
  130. disablePlugin() {
  131. let pluginSettings = this.hot.getSettings().manualRowMove;
  132. if (Array.isArray(pluginSettings)) {
  133. this.rowsMapper.clearMap();
  134. }
  135. removeClass(this.hot.rootElement, CSS_PLUGIN);
  136. this.unregisterEvents();
  137. this.backlight.destroy();
  138. this.guideline.destroy();
  139. super.disablePlugin();
  140. }
  141. /**
  142. * Move a single row.
  143. *
  144. * @param {Number} row Visual row index to be moved.
  145. * @param {Number} target Visual row index being a target for the moved row.
  146. */
  147. moveRow(row, target) {
  148. this.moveRows([row], target);
  149. }
  150. /**
  151. * Move multiple rows.
  152. *
  153. * @param {Array} rows Array of visual row indexes to be moved.
  154. * @param {Number} target Visual row index being a target for the moved rows.
  155. */
  156. moveRows(rows, target) {
  157. let priv = privatePool.get(this);
  158. let beforeMoveHook = this.hot.runHooks('beforeRowMove', rows, target);
  159. priv.disallowMoving = beforeMoveHook === false;
  160. if (!priv.disallowMoving) {
  161. // first we need to rewrite an visual indexes to logical for save reference after move
  162. arrayEach(rows, (row, index, array) => {
  163. array[index] = this.rowsMapper.getValueByIndex(row);
  164. });
  165. // next, when we have got an logical indexes, we can move rows
  166. arrayEach(rows, (row, index) => {
  167. let actualPosition = this.rowsMapper.getIndexByValue(row);
  168. if (actualPosition !== target) {
  169. this.rowsMapper.moveRow(actualPosition, target + index);
  170. }
  171. });
  172. // after moving we have to clear rowsMapper from null entries
  173. this.rowsMapper.clearNull();
  174. }
  175. this.hot.runHooks('afterRowMove', rows, target);
  176. }
  177. /**
  178. * Correct the cell selection after the move action. Fired only when action was made with a mouse.
  179. * That means that changing the row order using the API won't correct the selection.
  180. *
  181. * @private
  182. * @param {Number} startRow Visual row index for the start of the selection.
  183. * @param {Number} endRow Visual row index for the end of the selection.
  184. */
  185. changeSelection(startRow, endRow) {
  186. let selection = this.hot.selection;
  187. let lastColIndex = this.hot.countCols() - 1;
  188. selection.setRangeStartOnly(new CellCoords(startRow, 0));
  189. selection.setRangeEnd(new CellCoords(endRow, lastColIndex), false);
  190. }
  191. /**
  192. * Get the sum of the heights of rows in the provided range.
  193. *
  194. * @private
  195. * @param {Number} from Visual row index.
  196. * @param {Number} to Visual row index.
  197. * @returns {Number}
  198. */
  199. getRowsHeight(from, to) {
  200. let height = 0;
  201. for (let i = from; i < to; i++) {
  202. let rowHeight = this.hot.view.wt.wtTable.getRowHeight(i) || 23;
  203. height += rowHeight;
  204. }
  205. return height;
  206. }
  207. /**
  208. * Load initial settings when persistent state is saved or when plugin was initialized as an array.
  209. *
  210. * @private
  211. */
  212. initialSettings() {
  213. let pluginSettings = this.hot.getSettings().manualRowMove;
  214. if (Array.isArray(pluginSettings)) {
  215. this.moveRows(pluginSettings, 0);
  216. } else if (pluginSettings !== void 0) {
  217. let persistentState = this.persistentStateLoad();
  218. if (persistentState.length) {
  219. this.moveRows(persistentState, 0);
  220. }
  221. }
  222. }
  223. /**
  224. * Check if the provided row is in the fixedRowsTop section.
  225. *
  226. * @private
  227. * @param {Number} row Visual row index to check.
  228. * @returns {Boolean}
  229. */
  230. isFixedRowTop(row) {
  231. return row < this.hot.getSettings().fixedRowsTop;
  232. }
  233. /**
  234. * Check if the provided row is in the fixedRowsBottom section.
  235. *
  236. * @private
  237. * @param {Number} row Visual row index to check.
  238. * @returns {Boolean}
  239. */
  240. isFixedRowBottom(row) {
  241. return row > this.hot.getSettings().fixedRowsBottom;
  242. }
  243. /**
  244. * Save the manual row positions to the persistent state.
  245. *
  246. * @private
  247. */
  248. persistentStateSave() {
  249. this.hot.runHooks('persistentStateSave', 'manualRowMove', this.rowsMapper._arrayMap);
  250. }
  251. /**
  252. * Load the manual row positions from the persistent state.
  253. *
  254. * @private
  255. * @returns {Array} Stored state.
  256. */
  257. persistentStateLoad() {
  258. let storedState = {};
  259. this.hot.runHooks('persistentStateLoad', 'manualRowMove', storedState);
  260. return storedState.value ? storedState.value : [];
  261. }
  262. /**
  263. * Prepare array of indexes based on actual selection.
  264. *
  265. * @private
  266. * @returns {Array}
  267. */
  268. prepareRowsToMoving() {
  269. let selection = this.hot.getSelectedRange();
  270. let selectedRows = [];
  271. if (!selection) {
  272. return selectedRows;
  273. }
  274. let {from, to} = selection;
  275. let start = Math.min(from.row, to.row);
  276. let end = Math.max(from.row, to.row);
  277. rangeEach(start, end, (i) => {
  278. selectedRows.push(i);
  279. });
  280. return selectedRows;
  281. }
  282. /**
  283. * Update the UI visual position.
  284. *
  285. * @private
  286. */
  287. refreshPositions() {
  288. let priv = privatePool.get(this);
  289. let coords = priv.target.coords;
  290. let firstVisible = this.hot.view.wt.wtTable.getFirstVisibleRow();
  291. let lastVisible = this.hot.view.wt.wtTable.getLastVisibleRow();
  292. let fixedRows = this.hot.getSettings().fixedRowsTop;
  293. let countRows = this.hot.countRows();
  294. if (coords.row < fixedRows && firstVisible > 0) {
  295. this.hot.scrollViewportTo(firstVisible - 1);
  296. }
  297. if (coords.row >= lastVisible && lastVisible < countRows) {
  298. this.hot.scrollViewportTo(lastVisible + 1, undefined, true);
  299. }
  300. let wtTable = this.hot.view.wt.wtTable;
  301. let TD = priv.target.TD;
  302. let rootElementOffset = offset(this.hot.rootElement);
  303. let tdOffsetTop = this.hot.view.THEAD.offsetHeight + this.getRowsHeight(0, coords.row);
  304. let mouseOffsetTop = priv.target.eventPageY - rootElementOffset.top + wtTable.holder.scrollTop;
  305. let hiderHeight = wtTable.hider.offsetHeight;
  306. let tbodyOffsetTop = wtTable.TBODY.offsetTop;
  307. let backlightElemMarginTop = this.backlight.getOffset().top;
  308. let backlightElemHeight = this.backlight.getSize().height;
  309. if ((rootElementOffset.top + wtTable.holder.offsetHeight) < priv.target.eventPageY) {
  310. priv.target.coords.row++;
  311. }
  312. if (this.isFixedRowTop(coords.row)) {
  313. tdOffsetTop += wtTable.holder.scrollTop;
  314. }
  315. // todo: fixedRowsBottom
  316. // if (this.isFixedRowBottom(coords.row)) {
  317. //
  318. // }
  319. if (coords.row < 0) {
  320. // if hover on colHeader
  321. priv.target.row = firstVisible > 0 ? firstVisible - 1 : firstVisible;
  322. } else if ((TD.offsetHeight / 2) + tdOffsetTop <= mouseOffsetTop) {
  323. // if hover on lower part of TD
  324. priv.target.row = coords.row + 1;
  325. // unfortunately first row is bigger than rest
  326. tdOffsetTop += coords.row === 0 ? TD.offsetHeight - 1 : TD.offsetHeight;
  327. } else {
  328. // elsewhere on table
  329. priv.target.row = coords.row;
  330. }
  331. let backlightTop = mouseOffsetTop;
  332. let guidelineTop = tdOffsetTop;
  333. if (mouseOffsetTop + backlightElemHeight + backlightElemMarginTop >= hiderHeight) {
  334. // prevent display backlight below table
  335. backlightTop = hiderHeight - backlightElemHeight - backlightElemMarginTop;
  336. } else if (mouseOffsetTop + backlightElemMarginTop < tbodyOffsetTop) {
  337. // prevent display above below table
  338. backlightTop = tbodyOffsetTop + Math.abs(backlightElemMarginTop);
  339. }
  340. if (tdOffsetTop >= hiderHeight - 1) {
  341. // prevent display guideline below table
  342. guidelineTop = hiderHeight - 1;
  343. }
  344. let topOverlayHeight = 0;
  345. if (this.hot.view.wt.wtOverlays.topOverlay) {
  346. topOverlayHeight = this.hot.view.wt.wtOverlays.topOverlay.clone.wtTable.TABLE.offsetHeight;
  347. }
  348. if (coords.row >= fixedRows && (guidelineTop - wtTable.holder.scrollTop) < topOverlayHeight) {
  349. this.hot.scrollViewportTo(coords.row);
  350. }
  351. this.backlight.setPosition(backlightTop);
  352. this.guideline.setPosition(guidelineTop);
  353. }
  354. /**
  355. * This method checks arrayMap from rowsMapper and updates the rowsMapper if it's necessary.
  356. *
  357. * @private
  358. */
  359. updateRowsMapper() {
  360. let countRows = this.hot.countSourceRows();
  361. let rowsMapperLen = this.rowsMapper._arrayMap.length;
  362. if (rowsMapperLen === 0) {
  363. this.rowsMapper.createMap(countRows || this.hot.getSettings().startRows);
  364. } else if (rowsMapperLen < countRows) {
  365. let diff = countRows - rowsMapperLen;
  366. this.rowsMapper.insertItems(rowsMapperLen, diff);
  367. } else if (rowsMapperLen > countRows) {
  368. let maxIndex = countRows - 1;
  369. let rowsToRemove = [];
  370. arrayEach(this.rowsMapper._arrayMap, (value, index, array) => {
  371. if (value > maxIndex) {
  372. rowsToRemove.push(index);
  373. }
  374. });
  375. this.rowsMapper.removeItems(rowsToRemove);
  376. }
  377. }
  378. /**
  379. * Bind the events used by the plugin.
  380. *
  381. * @private
  382. */
  383. registerEvents() {
  384. this.eventManager.addEventListener(document.documentElement, 'mousemove', (event) => this.onMouseMove(event));
  385. this.eventManager.addEventListener(document.documentElement, 'mouseup', () => this.onMouseUp());
  386. }
  387. /**
  388. * Unbind the events used by the plugin.
  389. *
  390. * @private
  391. */
  392. unregisterEvents() {
  393. this.eventManager.clear();
  394. }
  395. /**
  396. * `beforeColumnSort` hook callback. If user uses the sorting, manual row moving is disabled.
  397. *
  398. * @private
  399. * @param {Number} column Column index where soring is present
  400. * @param {*} order State of sorting. ASC/DESC/None
  401. */
  402. onBeforeColumnSort(column, order) {
  403. let priv = privatePool.get(this);
  404. priv.disallowMoving = order !== void 0;
  405. }
  406. /**
  407. * Change the behavior of selection / dragging.
  408. *
  409. * @private
  410. * @param {MouseEvent} event
  411. * @param {CellCoords} coords
  412. * @param {HTMLElement} TD
  413. * @param {Object} blockCalculations
  414. */
  415. onBeforeOnCellMouseDown(event, coords, TD, blockCalculations) {
  416. let wtTable = this.hot.view.wt.wtTable;
  417. let isHeaderSelection = this.hot.selection.selectedHeader.rows;
  418. let selection = this.hot.getSelectedRange();
  419. let priv = privatePool.get(this);
  420. if (!selection || !isHeaderSelection || priv.pressed || event.button !== 0) {
  421. priv.pressed = false;
  422. priv.rowsToMove.length = 0;
  423. removeClass(this.hot.rootElement, [CSS_ON_MOVING, CSS_SHOW_UI]);
  424. return;
  425. }
  426. let guidelineIsNotReady = this.guideline.isBuilt() && !this.guideline.isAppended();
  427. let backlightIsNotReady = this.backlight.isBuilt() && !this.backlight.isAppended();
  428. if (guidelineIsNotReady && backlightIsNotReady) {
  429. this.guideline.appendTo(wtTable.hider);
  430. this.backlight.appendTo(wtTable.hider);
  431. }
  432. let {from, to} = selection;
  433. let start = Math.min(from.row, to.row);
  434. let end = Math.max(from.row, to.row);
  435. if (coords.col < 0 && (coords.row >= start && coords.row <= end)) {
  436. blockCalculations.row = true;
  437. priv.pressed = true;
  438. priv.target.eventPageY = event.pageY;
  439. priv.target.coords = coords;
  440. priv.target.TD = TD;
  441. priv.rowsToMove = this.prepareRowsToMoving();
  442. let leftPos = wtTable.holder.scrollLeft + wtTable.getColumnWidth(-1);
  443. this.backlight.setPosition(null, leftPos);
  444. this.backlight.setSize(wtTable.hider.offsetWidth - leftPos, this.getRowsHeight(start, end + 1));
  445. this.backlight.setOffset((this.getRowsHeight(start, coords.row) + event.layerY) * -1, null);
  446. addClass(this.hot.rootElement, CSS_ON_MOVING);
  447. this.refreshPositions();
  448. } else {
  449. removeClass(this.hot.rootElement, CSS_AFTER_SELECTION);
  450. priv.pressed = false;
  451. priv.rowsToMove.length = 0;
  452. }
  453. }
  454. /**
  455. * 'mouseMove' event callback. Fired when pointer move on document.documentElement.
  456. *
  457. * @private
  458. * @param {MouseEvent} event `mousemove` event properties.
  459. */
  460. onMouseMove(event) {
  461. let priv = privatePool.get(this);
  462. if (!priv.pressed) {
  463. return;
  464. }
  465. // callback for browser which doesn't supports CSS pointer-event: none
  466. if (event.realTarget === this.backlight.element) {
  467. let height = this.backlight.getSize().height;
  468. this.backlight.setSize(null, 0);
  469. setTimeout(function() {
  470. this.backlight.setPosition(null, height);
  471. });
  472. }
  473. priv.target.eventPageY = event.pageY;
  474. this.refreshPositions();
  475. }
  476. /**
  477. * 'beforeOnCellMouseOver' hook callback. Fired when pointer was over cell.
  478. *
  479. * @private
  480. * @param {MouseEvent} event `mouseover` event properties.
  481. * @param {CellCoords} coords Cell coordinates where was fired event.
  482. * @param {HTMLElement} TD Cell represented as HTMLElement.
  483. * @param {Object} blockCalculations Object which contains information about blockCalculation for row, column or cells.
  484. */
  485. onBeforeOnCellMouseOver(event, coords, TD, blockCalculations) {
  486. let selectedRange = this.hot.getSelectedRange();
  487. let priv = privatePool.get(this);
  488. if (!selectedRange || !priv.pressed) {
  489. return;
  490. }
  491. if (priv.rowsToMove.indexOf(coords.row) > -1) {
  492. removeClass(this.hot.rootElement, CSS_SHOW_UI);
  493. } else {
  494. addClass(this.hot.rootElement, CSS_SHOW_UI);
  495. }
  496. blockCalculations.row = true;
  497. blockCalculations.column = true;
  498. blockCalculations.cell = true;
  499. priv.target.coords = coords;
  500. priv.target.TD = TD;
  501. }
  502. /**
  503. * `onMouseUp` hook callback.
  504. *
  505. * @private
  506. */
  507. onMouseUp() {
  508. let priv = privatePool.get(this);
  509. let target = priv.target.row;
  510. let rowsLen = priv.rowsToMove.length;
  511. priv.pressed = false;
  512. priv.backlightHeight = 0;
  513. removeClass(this.hot.rootElement, [CSS_ON_MOVING, CSS_SHOW_UI, CSS_AFTER_SELECTION]);
  514. if (this.hot.selection.selectedHeader.rows) {
  515. addClass(this.hot.rootElement, CSS_AFTER_SELECTION);
  516. }
  517. if (rowsLen < 1 || target === void 0 || priv.rowsToMove.indexOf(target) > -1 ||
  518. (priv.rowsToMove[rowsLen - 1] === target - 1)) {
  519. return;
  520. }
  521. this.moveRows(priv.rowsToMove, target);
  522. this.persistentStateSave();
  523. this.hot.render();
  524. if (!priv.disallowMoving) {
  525. let selectionStart = this.rowsMapper.getIndexByValue(priv.rowsToMove[0]);
  526. let selectionEnd = this.rowsMapper.getIndexByValue(priv.rowsToMove[rowsLen - 1]);
  527. this.changeSelection(selectionStart, selectionEnd);
  528. }
  529. priv.rowsToMove.length = 0;
  530. }
  531. /**
  532. * `afterScrollHorizontally` hook callback. Fired the table was scrolled horizontally.
  533. *
  534. * @private
  535. */
  536. onAfterScrollHorizontally() {
  537. let wtTable = this.hot.view.wt.wtTable;
  538. let headerWidth = wtTable.getColumnWidth(-1);
  539. let scrollLeft = wtTable.holder.scrollLeft;
  540. let posLeft = headerWidth + scrollLeft;
  541. this.backlight.setPosition(null, posLeft);
  542. this.backlight.setSize(wtTable.hider.offsetWidth - posLeft);
  543. }
  544. /**
  545. * `afterCreateRow` hook callback.
  546. *
  547. * @private
  548. * @param {Number} index Index of the created row.
  549. * @param {Number} amount Amount of created rows.
  550. */
  551. onAfterCreateRow(index, amount) {
  552. this.rowsMapper.shiftItems(index, amount);
  553. }
  554. /**
  555. * On before remove row listener.
  556. *
  557. * @private
  558. * @param {Number} index Row index.
  559. * @param {Number} amount Defines how many rows removed.
  560. */
  561. onBeforeRemoveRow(index, amount) {
  562. this.removedRows.length = 0;
  563. if (index !== false) {
  564. // Collect physical row index.
  565. rangeEach(index, index + amount - 1, (removedIndex) => {
  566. this.removedRows.push(this.hot.runHooks('modifyRow', removedIndex, this.pluginName));
  567. });
  568. }
  569. }
  570. /**
  571. * `afterRemoveRow` hook callback.
  572. *
  573. * @private
  574. * @param {Number} index Index of the removed row.
  575. * @param {Number} amount Amount of removed rows.
  576. */
  577. onAfterRemoveRow(index, amount) {
  578. this.rowsMapper.unshiftItems(this.removedRows);
  579. }
  580. /**
  581. * `afterLoadData` hook callback.
  582. *
  583. * @private
  584. * @param {Boolean} firstTime True if that was loading data during the initialization.
  585. */
  586. onAfterLoadData(firstTime) {
  587. this.updateRowsMapper();
  588. }
  589. /**
  590. * 'modifyRow' hook callback.
  591. *
  592. * @private
  593. * @param {Number} row Visual Row index.
  594. * @returns {Number} Modified row index.
  595. */
  596. onModifyRow(row, source) {
  597. if (source !== this.pluginName) {
  598. let rowInMapper = this.rowsMapper.getValueByIndex(row);
  599. row = rowInMapper === null ? row : rowInMapper;
  600. }
  601. return row;
  602. }
  603. /**
  604. * 'unmodifyRow' hook callback.
  605. *
  606. * @private
  607. * @param {Number} row Visual row index.
  608. * @returns {Number} Logical row index.
  609. */
  610. onUnmodifyRow(row) {
  611. let indexInMapper = this.rowsMapper.getIndexByValue(row);
  612. return indexInMapper === null ? row : indexInMapper;
  613. }
  614. /**
  615. * `afterPluginsInitialized` hook callback.
  616. *
  617. * @private
  618. */
  619. onAfterPluginsInitialized() {
  620. this.updateRowsMapper();
  621. this.initialSettings();
  622. this.backlight.build();
  623. this.guideline.build();
  624. }
  625. /**
  626. * Destroy plugin instance.
  627. */
  628. destroy() {
  629. this.backlight.destroy();
  630. this.guideline.destroy();
  631. super.destroy();
  632. }
  633. }
  634. registerPlugin('ManualRowMove', ManualRowMove);
  635. export default ManualRowMove;