mouseEventHandler.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { isRightClick as isRightClickEvent, isLeftClick as isLeftClickEvent } from './../helpers/dom/event';
  2. import { CellCoords } from './../3rdparty/walkontable/src';
  3. /**
  4. * MouseDown handler.
  5. *
  6. * @param {Object} options
  7. * @param {Boolean} options.isShiftKey The flag which indicates if the shift key is pressed.
  8. * @param {Boolean} options.isLeftClick The flag which indicates if the left mouse button is pressed.
  9. * @param {Boolean} options.isRightClick The flag which indicates if the right mouse button is pressed.
  10. * @param {CellRange} options.coords The CellCoords object with defined visual coordinates.
  11. * @param {Selection} options.selection The Selection class instance.
  12. * @param {Object} options.controller An object with keys `row`, `column`, `cell` which indicate what
  13. * operation will be performed in later selection stages.
  14. */
  15. export function mouseDown({ isShiftKey, isLeftClick, isRightClick, coords, selection, controller }) {
  16. const currentSelection = selection.isSelected() ? selection.getSelectedRange().current() : null;
  17. const selectedCorner = selection.isSelectedByCorner();
  18. const selectedRow = selection.isSelectedByRowHeader();
  19. if (isShiftKey && currentSelection) {
  20. if (coords.row >= 0 && coords.col >= 0 && !controller.cells) {
  21. selection.setRangeEnd(coords);
  22. } else if ((selectedCorner || selectedRow) && coords.row >= 0 && coords.col >= 0 && !controller.cells) {
  23. selection.setRangeEnd(new CellCoords(coords.row, coords.col));
  24. } else if (selectedCorner && coords.row < 0 && !controller.column) {
  25. selection.setRangeEnd(new CellCoords(currentSelection.to.row, coords.col));
  26. } else if (selectedRow && coords.col < 0 && !controller.row) {
  27. selection.setRangeEnd(new CellCoords(coords.row, currentSelection.to.col));
  28. } else if (((!selectedCorner && !selectedRow && coords.col < 0) ||
  29. (selectedCorner && coords.col < 0)) && !controller.row) {
  30. selection.selectRows(currentSelection.from.row, coords.row);
  31. } else if (((!selectedCorner && !selectedRow && coords.row < 0) ||
  32. (selectedRow && coords.row < 0)) && !controller.column) {
  33. selection.selectColumns(currentSelection.from.col, coords.col);
  34. }
  35. } else {
  36. const newCoord = new CellCoords(coords.row, coords.col);
  37. if (newCoord.row < 0) {
  38. newCoord.row = 0;
  39. }
  40. if (newCoord.col < 0) {
  41. newCoord.col = 0;
  42. }
  43. const allowRightClickSelection = !selection.inInSelection(newCoord);
  44. const performSelection = isLeftClick || (isRightClick && allowRightClickSelection);
  45. // clicked row header and when some column was selected
  46. if (coords.row < 0 && coords.col >= 0 && !controller.column) {
  47. if (performSelection) {
  48. selection.selectColumns(coords.col);
  49. }
  50. // clicked column header and when some row was selected
  51. } else if (coords.col < 0 && coords.row >= 0 && !controller.row) {
  52. if (performSelection) {
  53. selection.selectRows(coords.row);
  54. }
  55. } else if (coords.col >= 0 && coords.row >= 0 && !controller.cells) {
  56. if (performSelection) {
  57. selection.setRangeStart(coords);
  58. }
  59. } else if (coords.col < 0 && coords.row < 0) {
  60. selection.setRangeStart(coords);
  61. }
  62. }
  63. }
  64. /**
  65. * MouseOver handler.
  66. *
  67. * @param {Object} options
  68. * @param {Boolean} options.isLeftClick
  69. * @param {CellRange} options.coords The CellCoords object with defined visual coordinates.
  70. * @param {Selection} options.selection The Selection class instance.
  71. * @param {Object} options.controller An object with keys `row`, `column`, `cell` which indicate what
  72. * operation will be performed in later selection stages.
  73. */
  74. export function mouseOver({ isLeftClick, coords, selection, controller }) {
  75. if (!isLeftClick) {
  76. return;
  77. }
  78. const selectedRow = selection.isSelectedByRowHeader();
  79. const selectedColumn = selection.isSelectedByColumnHeader();
  80. const countCols = selection.tableProps.countCols();
  81. const countRows = selection.tableProps.countRows();
  82. if (selectedColumn && !controller.column) {
  83. selection.setRangeEnd(new CellCoords(countRows - 1, coords.col));
  84. } else if (selectedRow && !controller.row) {
  85. selection.setRangeEnd(new CellCoords(coords.row, countCols - 1));
  86. } else if (!controller.cell) {
  87. selection.setRangeEnd(coords);
  88. }
  89. }
  90. const handlers = new Map([
  91. ['mousedown', mouseDown],
  92. ['mouseover', mouseOver],
  93. ['touchstart', mouseDown],
  94. ]);
  95. /**
  96. * Mouse handler for selection functionality.
  97. *
  98. * @param {Event} event An native event to handle.
  99. * @param {Object} options
  100. * @param {CellRange} options.coords The CellCoords object with defined visual coordinates.
  101. * @param {Selection} options.selection The Selection class instance.
  102. * @param {Object} options.controller An object with keys `row`, `column`, `cell` which indicate what
  103. * operation will be performed in later selection stages.
  104. */
  105. export function handleMouseEvent(event, { coords, selection, controller }) {
  106. handlers.get(event.type)({
  107. coords,
  108. selection,
  109. controller,
  110. isShiftKey: event.shiftKey,
  111. isLeftClick: isLeftClickEvent(event) || event.type === 'touchstart',
  112. isRightClick: isRightClickEvent(event),
  113. });
  114. }