173f16dcb7ccf299f155b293b9aff4211e8b92c2038eeda39787de12f7eb502aa141e144e2a681171ce6a3e5f62b61c21436124e11902adc4299fdce7149ef 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {isObject} from './../../helpers/object';
  2. import {isDefined} from './../../helpers/mixed';
  3. import {CellCoords} from './../../3rdparty/walkontable/src';
  4. export const 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. const rowsLength = data.length;
  19. const columnsLength = data ? data[0].length : 0;
  20. const deltas = [];
  21. const diffRow = end.row - start.row;
  22. const diffCol = end.col - start.col;
  23. if (['down', 'up'].indexOf(direction) !== -1) {
  24. const arr = [];
  25. for (let col = 0; col <= diffCol; col++) {
  26. let startValue = parseInt(data[0][col], 10);
  27. let endValue = parseInt(data[rowsLength - 1][col], 10);
  28. let 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 (let row = 0; row <= diffRow; row++) {
  35. let startValue = parseInt(data[row][0], 10);
  36. let endValue = parseInt(data[row][columnsLength - 1], 10);
  37. let 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. let startOfDragCoords,
  52. endOfDragCoords,
  53. directionOfDrag;
  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] &&
  67. endSelection[1] === startSelection[1]) {
  68. directionOfDrag = 'down';
  69. startOfDragCoords = new CellCoords(startSelection[2] + 1, endSelection[1]);
  70. endOfDragCoords = new CellCoords(endSelection[2], endSelection[3]);
  71. }
  72. return {
  73. directionOfDrag,
  74. startOfDragCoords,
  75. endOfDragCoords
  76. };
  77. }
  78. /**
  79. * Get mapped FillHandle setting containing information about
  80. * allowed FillHandle directions and if allowed is automatic insertion of rows on drag
  81. *
  82. * @param {Boolean|Object} fillHandle property of Handsontable settings
  83. * @returns {{directions: Array, autoInsertRow: Boolean}} object allowing access to information
  84. * about FillHandle in more useful way
  85. */
  86. export function getMappedFillHandleSetting(fillHandle) {
  87. const mappedSettings = {};
  88. if (fillHandle === true) {
  89. mappedSettings.directions = Object.keys(DIRECTIONS);
  90. mappedSettings.autoInsertRow = true;
  91. } else if (isObject(fillHandle)) {
  92. if (isDefined(fillHandle.autoInsertRow)) {
  93. // autoInsertRow for horizontal direction will be always false
  94. if (fillHandle.direction === DIRECTIONS.horizontal) {
  95. mappedSettings.autoInsertRow = false;
  96. } else {
  97. mappedSettings.autoInsertRow = fillHandle.autoInsertRow;
  98. }
  99. } else {
  100. mappedSettings.autoInsertRow = false;
  101. }
  102. if (isDefined(fillHandle.direction)) {
  103. mappedSettings.directions = [fillHandle.direction];
  104. } else {
  105. mappedSettings.directions = Object.keys(DIRECTIONS);
  106. }
  107. } else if (typeof fillHandle === 'string') {
  108. mappedSettings.directions = [fillHandle];
  109. mappedSettings.autoInsertRow = true;
  110. } else {
  111. mappedSettings.directions = [];
  112. mappedSettings.autoInsertRow = false;
  113. }
  114. return mappedSettings;
  115. }