utils.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { isObject } from './../../helpers/object';
  2. import { isDefined } from './../../helpers/mixed';
  3. import { CellCoords } from './../../3rdparty/walkontable/src';
  4. export var DIRECTIONS = {
  5. horizontal: 'horizontal',
  6. vertical: 'vertical'
  7. };
  8. /**
  9. * Get deltas array.
  10. *
  11. * @param {CellCoords} start
  12. * @param {CellCoords} end
  13. * @param {Array} data
  14. * @param {String} direction
  15. * @returns {Array}
  16. */
  17. export function getDeltas(start, end, data, direction) {
  18. var rowsLength = data.length;
  19. var columnsLength = data ? data[0].length : 0;
  20. var deltas = [];
  21. var diffRow = end.row - start.row;
  22. var diffCol = end.col - start.col;
  23. if (['down', 'up'].indexOf(direction) !== -1) {
  24. var arr = [];
  25. for (var col = 0; col <= diffCol; col++) {
  26. var startValue = parseInt(data[0][col], 10);
  27. var endValue = parseInt(data[rowsLength - 1][col], 10);
  28. var delta = (direction === 'down' ? endValue - startValue : startValue - endValue) / (rowsLength - 1) || 0;
  29. arr.push(delta);
  30. }
  31. deltas.push(arr);
  32. }
  33. if (['right', 'left'].indexOf(direction) !== -1) {
  34. for (var row = 0; row <= diffRow; row++) {
  35. var _startValue = parseInt(data[row][0], 10);
  36. var _endValue = parseInt(data[row][columnsLength - 1], 10);
  37. var _delta = (direction === 'right' ? _endValue - _startValue : _startValue - _endValue) / (columnsLength - 1) || 0;
  38. deltas.push([_delta]);
  39. }
  40. }
  41. return deltas;
  42. }
  43. /**
  44. * Get direction between positions and cords of selections difference (drag area)
  45. *
  46. * @param {Array} startSelection
  47. * @param {Array} endSelection
  48. * @returns {{direction: String, start: CellCoords, end: CellCoords}}
  49. */
  50. export function getDragDirectionAndRange(startSelection, endSelection) {
  51. var startOfDragCoords = void 0,
  52. endOfDragCoords = void 0,
  53. directionOfDrag = void 0;
  54. if (endSelection[0] === startSelection[0] && endSelection[1] < startSelection[1]) {
  55. directionOfDrag = 'left';
  56. startOfDragCoords = new CellCoords(endSelection[0], endSelection[1]);
  57. endOfDragCoords = new CellCoords(endSelection[2], startSelection[1] - 1);
  58. } else if (endSelection[0] === startSelection[0] && endSelection[3] > startSelection[3]) {
  59. directionOfDrag = 'right';
  60. startOfDragCoords = new CellCoords(endSelection[0], startSelection[3] + 1);
  61. endOfDragCoords = new CellCoords(endSelection[2], endSelection[3]);
  62. } else if (endSelection[0] < startSelection[0] && endSelection[1] === startSelection[1]) {
  63. directionOfDrag = 'up';
  64. startOfDragCoords = new CellCoords(endSelection[0], endSelection[1]);
  65. endOfDragCoords = new CellCoords(startSelection[0] - 1, endSelection[3]);
  66. } else if (endSelection[2] > startSelection[2] && endSelection[1] === startSelection[1]) {
  67. directionOfDrag = 'down';
  68. startOfDragCoords = new CellCoords(startSelection[2] + 1, endSelection[1]);
  69. endOfDragCoords = new CellCoords(endSelection[2], endSelection[3]);
  70. }
  71. return {
  72. directionOfDrag: directionOfDrag,
  73. startOfDragCoords: startOfDragCoords,
  74. endOfDragCoords: endOfDragCoords
  75. };
  76. }
  77. /**
  78. * Get mapped FillHandle setting containing information about
  79. * allowed FillHandle directions and if allowed is automatic insertion of rows on drag
  80. *
  81. * @param {Boolean|Object} fillHandle property of Handsontable settings
  82. * @returns {{directions: Array, autoInsertRow: Boolean}} object allowing access to information
  83. * about FillHandle in more useful way
  84. */
  85. export function getMappedFillHandleSetting(fillHandle) {
  86. var mappedSettings = {};
  87. if (fillHandle === true) {
  88. mappedSettings.directions = Object.keys(DIRECTIONS);
  89. mappedSettings.autoInsertRow = true;
  90. } else if (isObject(fillHandle)) {
  91. if (isDefined(fillHandle.autoInsertRow)) {
  92. // autoInsertRow for horizontal direction will be always false
  93. if (fillHandle.direction === DIRECTIONS.horizontal) {
  94. mappedSettings.autoInsertRow = false;
  95. } else {
  96. mappedSettings.autoInsertRow = fillHandle.autoInsertRow;
  97. }
  98. } else {
  99. mappedSettings.autoInsertRow = false;
  100. }
  101. if (isDefined(fillHandle.direction)) {
  102. mappedSettings.directions = [fillHandle.direction];
  103. } else {
  104. mappedSettings.directions = Object.keys(DIRECTIONS);
  105. }
  106. } else if (typeof fillHandle === 'string') {
  107. mappedSettings.directions = [fillHandle];
  108. mappedSettings.autoInsertRow = true;
  109. } else {
  110. mappedSettings.directions = [];
  111. mappedSettings.autoInsertRow = false;
  112. }
  113. return mappedSettings;
  114. }