autofill.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 Hooks from './../../pluginHooks';
  8. import { offset, outerHeight, outerWidth } from './../../helpers/dom/element';
  9. import EventManager from './../../eventManager';
  10. import { registerPlugin } from './../../plugins';
  11. import { CellCoords } from './../../3rdparty/walkontable/src';
  12. import { getDeltas, getDragDirectionAndRange, DIRECTIONS, getMappedFillHandleSetting } from './utils';
  13. Hooks.getSingleton().register('modifyAutofillRange');
  14. Hooks.getSingleton().register('beforeAutofill');
  15. var INSERT_ROW_ALTER_ACTION_NAME = 'insert_row';
  16. var INTERVAL_FOR_ADDING_ROW = 200;
  17. /**
  18. * This plugin provides "drag-down" and "copy-down" functionalities, both operated
  19. * using the small square in the right bottom of the cell selection.
  20. *
  21. * "Drag-down" expands the value of the selected cells to the neighbouring
  22. * cells when you drag the small square in the corner.
  23. *
  24. * "Copy-down" copies the value of the selection to all empty cells
  25. * below when you double click the small square.
  26. *
  27. * @class Autofill
  28. * @plugin Autofill
  29. */
  30. var Autofill = function (_BasePlugin) {
  31. _inherits(Autofill, _BasePlugin);
  32. function Autofill(hotInstance) {
  33. _classCallCheck(this, Autofill);
  34. /**
  35. * Event manager
  36. *
  37. * @type {EventManager}
  38. */
  39. var _this = _possibleConstructorReturn(this, (Autofill.__proto__ || Object.getPrototypeOf(Autofill)).call(this, hotInstance));
  40. _this.eventManager = new EventManager(_this);
  41. /**
  42. * Specifies if adding new row started.
  43. *
  44. * @type {Boolean}
  45. */
  46. _this.addingStarted = false;
  47. /**
  48. * Specifies if there was mouse down on the cell corner.
  49. *
  50. * @type {Boolean}
  51. */
  52. _this.mouseDownOnCellCorner = false;
  53. /**
  54. * Specifies if mouse was dragged outside Handsontable.
  55. *
  56. * @type {Boolean}
  57. */
  58. _this.mouseDragOutside = false;
  59. /**
  60. * Specifies how many cell levels were dragged using the handle.
  61. *
  62. * @type {Boolean}
  63. */
  64. _this.handleDraggedCells = 0;
  65. /**
  66. * Specifies allowed directions of drag.
  67. *
  68. * @type {Array}
  69. */
  70. _this.directions = [];
  71. /**
  72. * Specifies if can insert new rows if needed.
  73. *
  74. * @type {Boolean}
  75. */
  76. _this.autoInsertRow = false;
  77. return _this;
  78. }
  79. /**
  80. * Check if the plugin is enabled in the Handsontable settings.
  81. *
  82. * @returns {Boolean}
  83. */
  84. _createClass(Autofill, [{
  85. key: 'isEnabled',
  86. value: function isEnabled() {
  87. return this.hot.getSettings().fillHandle;
  88. }
  89. /**
  90. * Enable plugin for this Handsontable instance.
  91. */
  92. }, {
  93. key: 'enablePlugin',
  94. value: function enablePlugin() {
  95. var _this2 = this;
  96. if (this.enabled) {
  97. return;
  98. }
  99. this.mapSettings();
  100. this.registerEvents();
  101. this.addHook('afterOnCellCornerMouseDown', function (event) {
  102. return _this2.onAfterCellCornerMouseDown(event);
  103. });
  104. this.addHook('afterOnCellCornerDblClick', function (event) {
  105. return _this2.onCellCornerDblClick(event);
  106. });
  107. this.addHook('beforeOnCellMouseOver', function (event, coords, TD) {
  108. return _this2.onBeforeCellMouseOver(coords);
  109. });
  110. _get(Autofill.prototype.__proto__ || Object.getPrototypeOf(Autofill.prototype), 'enablePlugin', this).call(this);
  111. }
  112. /**
  113. * Update plugin for this Handsontable instance.
  114. */
  115. }, {
  116. key: 'updatePlugin',
  117. value: function updatePlugin() {
  118. this.disablePlugin();
  119. this.enablePlugin();
  120. _get(Autofill.prototype.__proto__ || Object.getPrototypeOf(Autofill.prototype), 'updatePlugin', this).call(this);
  121. }
  122. /**
  123. * Disable plugin for this Handsontable instance.
  124. */
  125. }, {
  126. key: 'disablePlugin',
  127. value: function disablePlugin() {
  128. this.clearMappedSettings();
  129. _get(Autofill.prototype.__proto__ || Object.getPrototypeOf(Autofill.prototype), 'disablePlugin', this).call(this);
  130. }
  131. /**
  132. * Get selection data
  133. *
  134. * @private
  135. * @returns {Array} Array with the data.
  136. */
  137. }, {
  138. key: 'getSelectionData',
  139. value: function getSelectionData() {
  140. var selRange = {
  141. from: this.hot.getSelectedRange().from,
  142. to: this.hot.getSelectedRange().to
  143. };
  144. return this.hot.getData(selRange.from.row, selRange.from.col, selRange.to.row, selRange.to.col);
  145. }
  146. /**
  147. * Try to apply fill values to the area in fill border, omitting the selection border.
  148. *
  149. * @private
  150. * @returns {Boolean} reports if fill was applied.
  151. */
  152. }, {
  153. key: 'fillIn',
  154. value: function fillIn() {
  155. if (this.hot.view.wt.selections.fill.isEmpty()) {
  156. return false;
  157. }
  158. var cornersOfSelectionAndDragAreas = this.hot.view.wt.selections.fill.getCorners();
  159. this.resetSelectionOfDraggedArea();
  160. var cornersOfSelectedCells = this.getCornersOfSelectedCells();
  161. var _getDragDirectionAndR = getDragDirectionAndRange(cornersOfSelectedCells, cornersOfSelectionAndDragAreas),
  162. directionOfDrag = _getDragDirectionAndR.directionOfDrag,
  163. startOfDragCoords = _getDragDirectionAndR.startOfDragCoords,
  164. endOfDragCoords = _getDragDirectionAndR.endOfDragCoords;
  165. this.hot.runHooks('modifyAutofillRange', cornersOfSelectedCells, cornersOfSelectionAndDragAreas);
  166. if (startOfDragCoords && startOfDragCoords.row > -1 && startOfDragCoords.col > -1) {
  167. var selectionData = this.getSelectionData();
  168. var deltas = getDeltas(startOfDragCoords, endOfDragCoords, selectionData, directionOfDrag);
  169. this.hot.runHooks('beforeAutofill', startOfDragCoords, endOfDragCoords, selectionData);
  170. this.hot.populateFromArray(startOfDragCoords.row, startOfDragCoords.col, selectionData, endOfDragCoords.row, endOfDragCoords.col, this.pluginName + '.fill', null, directionOfDrag, deltas);
  171. this.setSelection(cornersOfSelectionAndDragAreas);
  172. } else {
  173. // reset to avoid some range bug
  174. this.hot.selection.refreshBorders();
  175. }
  176. return true;
  177. }
  178. /**
  179. * Reduce the selection area if the handle was dragged outside of the table or on headers.
  180. *
  181. * @private
  182. * @param {CellCoords} coords indexes of selection corners.
  183. * @returns {CellCoords}
  184. */
  185. }, {
  186. key: 'reduceSelectionAreaIfNeeded',
  187. value: function reduceSelectionAreaIfNeeded(coords) {
  188. if (coords.row < 0) {
  189. coords.row = 0;
  190. }
  191. if (coords.col < 0) {
  192. coords.col = 0;
  193. }
  194. return coords;
  195. }
  196. /**
  197. * Get the coordinates of the drag & drop borders.
  198. *
  199. * @private
  200. * @param {CellCoords} coordsOfSelection `CellCoords` coord object.
  201. * @returns {Array}
  202. */
  203. }, {
  204. key: 'getCoordsOfDragAndDropBorders',
  205. value: function getCoordsOfDragAndDropBorders(coordsOfSelection) {
  206. var topLeftCorner = this.hot.getSelectedRange().getTopLeftCorner();
  207. var bottomRightCorner = this.hot.getSelectedRange().getBottomRightCorner();
  208. var coords = void 0;
  209. if (this.directions.includes(DIRECTIONS.vertical) && (bottomRightCorner.row < coordsOfSelection.row || topLeftCorner.row > coordsOfSelection.row)) {
  210. coords = new CellCoords(coordsOfSelection.row, bottomRightCorner.col);
  211. } else if (this.directions.includes(DIRECTIONS.horizontal)) {
  212. coords = new CellCoords(bottomRightCorner.row, coordsOfSelection.col);
  213. } else {
  214. // wrong direction
  215. return;
  216. }
  217. return this.reduceSelectionAreaIfNeeded(coords);
  218. }
  219. /**
  220. * Show the fill border.
  221. *
  222. * @private
  223. * @param {CellCoords} coordsOfSelection `CellCoords` coord object.
  224. */
  225. }, {
  226. key: 'showBorder',
  227. value: function showBorder(coordsOfSelection) {
  228. var coordsOfDragAndDropBorders = this.getCoordsOfDragAndDropBorders(coordsOfSelection);
  229. if (coordsOfDragAndDropBorders) {
  230. this.redrawBorders(coordsOfDragAndDropBorders);
  231. }
  232. }
  233. /**
  234. * Add new row
  235. *
  236. * @private
  237. */
  238. }, {
  239. key: 'addRow',
  240. value: function addRow() {
  241. var _this3 = this;
  242. this.hot._registerTimeout(setTimeout(function () {
  243. _this3.hot.alter(INSERT_ROW_ALTER_ACTION_NAME, void 0, 1, _this3.pluginName + '.fill');
  244. _this3.addingStarted = false;
  245. }, INTERVAL_FOR_ADDING_ROW));
  246. }
  247. /**
  248. * Add new rows if they are needed to continue auto-filling values.
  249. *
  250. * @private
  251. */
  252. }, {
  253. key: 'addNewRowIfNeeded',
  254. value: function addNewRowIfNeeded() {
  255. if (this.hot.view.wt.selections.fill.cellRange && this.addingStarted === false && this.autoInsertRow) {
  256. var cornersOfSelectedCells = this.hot.getSelected();
  257. var cornersOfSelectedDragArea = this.hot.view.wt.selections.fill.getCorners();
  258. var nrOfTableRows = this.hot.countRows();
  259. if (cornersOfSelectedCells[2] < nrOfTableRows - 1 && cornersOfSelectedDragArea[2] === nrOfTableRows - 1) {
  260. this.addingStarted = true;
  261. this.addRow();
  262. }
  263. }
  264. }
  265. /**
  266. * Get corners of selected cells.
  267. *
  268. * @private
  269. * @returns {Array}
  270. */
  271. }, {
  272. key: 'getCornersOfSelectedCells',
  273. value: function getCornersOfSelectedCells() {
  274. if (this.hot.selection.isMultiple()) {
  275. return this.hot.view.wt.selections.area.getCorners();
  276. }
  277. return this.hot.view.wt.selections.current.getCorners();
  278. }
  279. /**
  280. * Get index of last adjacent filled in row
  281. *
  282. * @private
  283. * @param {Array} cornersOfSelectedCells indexes of selection corners.
  284. * @returns {Number} gives number greater than or equal to zero when selection adjacent can be applied.
  285. * or -1 when selection adjacent can't be applied
  286. */
  287. }, {
  288. key: 'getIndexOfLastAdjacentFilledInRow',
  289. value: function getIndexOfLastAdjacentFilledInRow(cornersOfSelectedCells) {
  290. var data = this.hot.getData();
  291. var nrOfTableRows = this.hot.countRows();
  292. var lastFilledInRowIndex = void 0;
  293. for (var rowIndex = cornersOfSelectedCells[2] + 1; rowIndex < nrOfTableRows; rowIndex++) {
  294. for (var columnIndex = cornersOfSelectedCells[1]; columnIndex <= cornersOfSelectedCells[3]; columnIndex++) {
  295. var dataInCell = data[rowIndex][columnIndex];
  296. if (dataInCell) {
  297. return -1;
  298. }
  299. }
  300. var dataInNextLeftCell = data[rowIndex][cornersOfSelectedCells[1] - 1];
  301. var dataInNextRightCell = data[rowIndex][cornersOfSelectedCells[3] + 1];
  302. if (!!dataInNextLeftCell || !!dataInNextRightCell) {
  303. lastFilledInRowIndex = rowIndex;
  304. }
  305. }
  306. return lastFilledInRowIndex;
  307. }
  308. /**
  309. * Add a selection from the start area to the specific row index.
  310. *
  311. * @private
  312. * @param {Array} selectStartArea selection area from which we start to create more comprehensive selection.
  313. * @param {Number} rowIndex
  314. */
  315. }, {
  316. key: 'addSelectionFromStartAreaToSpecificRowIndex',
  317. value: function addSelectionFromStartAreaToSpecificRowIndex(selectStartArea, rowIndex) {
  318. this.hot.view.wt.selections.fill.clear();
  319. this.hot.view.wt.selections.fill.add(new CellCoords(selectStartArea[0], selectStartArea[1]));
  320. this.hot.view.wt.selections.fill.add(new CellCoords(rowIndex, selectStartArea[3]));
  321. }
  322. /**
  323. * Set selection based on passed corners.
  324. *
  325. * @private
  326. * @param {Array} cornersOfArea
  327. */
  328. }, {
  329. key: 'setSelection',
  330. value: function setSelection(cornersOfArea) {
  331. this.hot.selection.setRangeStart(new CellCoords(cornersOfArea[0], cornersOfArea[1]));
  332. this.hot.selection.setRangeEnd(new CellCoords(cornersOfArea[2], cornersOfArea[3]));
  333. }
  334. /**
  335. * Try to select cells down to the last row in the left column and then returns if selection was applied.
  336. *
  337. * @private
  338. * @returns {Boolean}
  339. */
  340. }, {
  341. key: 'selectAdjacent',
  342. value: function selectAdjacent() {
  343. var cornersOfSelectedCells = this.getCornersOfSelectedCells();
  344. var lastFilledInRowIndex = this.getIndexOfLastAdjacentFilledInRow(cornersOfSelectedCells);
  345. if (lastFilledInRowIndex === -1) {
  346. return false;
  347. }
  348. this.addSelectionFromStartAreaToSpecificRowIndex(cornersOfSelectedCells, lastFilledInRowIndex);
  349. return true;
  350. }
  351. /**
  352. * Reset selection of dragged area.
  353. *
  354. * @private
  355. */
  356. }, {
  357. key: 'resetSelectionOfDraggedArea',
  358. value: function resetSelectionOfDraggedArea() {
  359. this.handleDraggedCells = 0;
  360. this.hot.view.wt.selections.fill.clear();
  361. }
  362. /**
  363. * Redraw borders.
  364. *
  365. * @private
  366. * @param {CellCoords} coords `CellCoords` coord object.
  367. */
  368. }, {
  369. key: 'redrawBorders',
  370. value: function redrawBorders(coords) {
  371. this.hot.view.wt.selections.fill.clear();
  372. this.hot.view.wt.selections.fill.add(this.hot.getSelectedRange().from);
  373. this.hot.view.wt.selections.fill.add(this.hot.getSelectedRange().to);
  374. this.hot.view.wt.selections.fill.add(coords);
  375. this.hot.view.render();
  376. }
  377. /**
  378. * Get if mouse was dragged outside.
  379. *
  380. * @private
  381. * @param {MouseEvent} event `mousemove` event properties.
  382. * @returns {Boolean}
  383. */
  384. }, {
  385. key: 'getIfMouseWasDraggedOutside',
  386. value: function getIfMouseWasDraggedOutside(event) {
  387. var tableBottom = offset(this.hot.table).top - (window.pageYOffset || document.documentElement.scrollTop) + outerHeight(this.hot.table);
  388. var tableRight = offset(this.hot.table).left - (window.pageXOffset || document.documentElement.scrollLeft) + outerWidth(this.hot.table);
  389. return event.clientY > tableBottom && event.clientX <= tableRight;
  390. }
  391. /**
  392. * Bind the events used by the plugin.
  393. *
  394. * @private
  395. */
  396. }, {
  397. key: 'registerEvents',
  398. value: function registerEvents() {
  399. var _this4 = this;
  400. this.eventManager.addEventListener(document.documentElement, 'mouseup', function () {
  401. return _this4.onMouseUp();
  402. });
  403. this.eventManager.addEventListener(document.documentElement, 'mousemove', function (event) {
  404. return _this4.onMouseMove(event);
  405. });
  406. }
  407. /**
  408. * On cell corner double click callback.
  409. *
  410. * @private
  411. */
  412. }, {
  413. key: 'onCellCornerDblClick',
  414. value: function onCellCornerDblClick() {
  415. var selectionApplied = this.selectAdjacent();
  416. if (selectionApplied) {
  417. this.fillIn();
  418. }
  419. }
  420. /**
  421. * On after cell corner mouse down listener.
  422. *
  423. * @private
  424. */
  425. }, {
  426. key: 'onAfterCellCornerMouseDown',
  427. value: function onAfterCellCornerMouseDown() {
  428. this.handleDraggedCells = 1;
  429. this.mouseDownOnCellCorner = true;
  430. }
  431. /**
  432. * On before cell mouse over listener.
  433. *
  434. * @private
  435. * @param {CellCoords} coords `CellCoords` coord object.
  436. */
  437. }, {
  438. key: 'onBeforeCellMouseOver',
  439. value: function onBeforeCellMouseOver(coords) {
  440. if (this.mouseDownOnCellCorner && !this.hot.view.isMouseDown() && this.handleDraggedCells) {
  441. this.handleDraggedCells++;
  442. this.showBorder(coords);
  443. this.addNewRowIfNeeded();
  444. }
  445. }
  446. /**
  447. * On mouse up listener.
  448. *
  449. * @private
  450. */
  451. }, {
  452. key: 'onMouseUp',
  453. value: function onMouseUp() {
  454. if (this.handleDraggedCells) {
  455. if (this.handleDraggedCells > 1) {
  456. this.fillIn();
  457. }
  458. this.handleDraggedCells = 0;
  459. this.mouseDownOnCellCorner = false;
  460. }
  461. }
  462. /**
  463. * On mouse move listener.
  464. *
  465. * @private
  466. * @param {MouseEvent} event `mousemove` event properties.
  467. */
  468. }, {
  469. key: 'onMouseMove',
  470. value: function onMouseMove(event) {
  471. var mouseWasDraggedOutside = this.getIfMouseWasDraggedOutside(event);
  472. if (this.addingStarted === false && this.handleDraggedCells > 0 && mouseWasDraggedOutside) {
  473. this.mouseDragOutside = true;
  474. this.addingStarted = true;
  475. } else {
  476. this.mouseDragOutside = false;
  477. }
  478. if (this.mouseDragOutside && this.autoInsertRow) {
  479. this.addRow();
  480. }
  481. }
  482. /**
  483. * Clear mapped settings.
  484. *
  485. * @private
  486. */
  487. }, {
  488. key: 'clearMappedSettings',
  489. value: function clearMappedSettings() {
  490. this.directions.length = 0;
  491. this.autoInsertRow = false;
  492. }
  493. /**
  494. * Map settings.
  495. *
  496. * @private
  497. */
  498. }, {
  499. key: 'mapSettings',
  500. value: function mapSettings() {
  501. var mappedSettings = getMappedFillHandleSetting(this.hot.getSettings().fillHandle);
  502. this.directions = mappedSettings.directions;
  503. this.autoInsertRow = mappedSettings.autoInsertRow;
  504. }
  505. /**
  506. * Destroy plugin instance.
  507. */
  508. }, {
  509. key: 'destroy',
  510. value: function destroy() {
  511. _get(Autofill.prototype.__proto__ || Object.getPrototypeOf(Autofill.prototype), 'destroy', this).call(this);
  512. }
  513. }]);
  514. return Autofill;
  515. }(BasePlugin);
  516. registerPlugin('autofill', Autofill);
  517. export default Autofill;