utils.js 4.7 KB

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