e590fc0a42bba7b58d89b7af20b478551e5545b5f2e0cea733a8ff9ef8e6de4baa85a90575d9f140507840152db6cba77d2c1df64437a1b927d53fd0f0055a 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { closestDown, hasClass, isChildOf, getParent } from './../../../helpers/dom/element';
  2. import { partial } from './../../../helpers/function';
  3. import { isMobileBrowser } from './../../../helpers/browser';
  4. import EventManager from './../../../eventManager';
  5. /**
  6. *
  7. */
  8. function Event(instance) {
  9. var that = this;
  10. var eventManager = new EventManager(instance);
  11. this.instance = instance;
  12. var dblClickOrigin = [null, null];
  13. this.dblClickTimeout = [null, null];
  14. var onMouseDown = function onMouseDown(event) {
  15. var activeElement = document.activeElement;
  16. var getParentNode = partial(getParent, event.realTarget);
  17. var realTarget = event.realTarget;
  18. // ignore focusable element from mouse down processing (https://github.com/handsontable/handsontable/issues/3555)
  19. if (realTarget === activeElement || getParentNode(0) === activeElement || getParentNode(1) === activeElement) {
  20. return;
  21. }
  22. var cell = that.parentCell(realTarget);
  23. if (hasClass(realTarget, 'corner')) {
  24. that.instance.getSetting('onCellCornerMouseDown', event, realTarget);
  25. } else if (cell.TD) {
  26. if (that.instance.hasSetting('onCellMouseDown')) {
  27. that.instance.getSetting('onCellMouseDown', event, cell.coords, cell.TD, that.instance);
  28. }
  29. }
  30. if (event.button !== 2) {
  31. // if not right mouse button
  32. if (cell.TD) {
  33. dblClickOrigin[0] = cell.TD;
  34. clearTimeout(that.dblClickTimeout[0]);
  35. that.dblClickTimeout[0] = setTimeout(function () {
  36. dblClickOrigin[0] = null;
  37. }, 1000);
  38. }
  39. }
  40. };
  41. var onTouchMove = function onTouchMove(event) {
  42. that.instance.touchMoving = true;
  43. };
  44. var longTouchTimeout;
  45. var onTouchStart = function onTouchStart(event) {
  46. var container = this;
  47. eventManager.addEventListener(this, 'touchmove', onTouchMove);
  48. // Prevent cell selection when scrolling with touch event - not the best solution performance-wise
  49. that.checkIfTouchMove = setTimeout(function () {
  50. if (that.instance.touchMoving === true) {
  51. that.instance.touchMoving = void 0;
  52. eventManager.removeEventListener('touchmove', onTouchMove, false);
  53. }
  54. onMouseDown(event);
  55. }, 30);
  56. };
  57. var onMouseOver = function onMouseOver(event) {
  58. var table, td, mainWOT;
  59. if (that.instance.hasSetting('onCellMouseOver')) {
  60. table = that.instance.wtTable.TABLE;
  61. td = closestDown(event.realTarget, ['TD', 'TH'], table);
  62. mainWOT = that.instance.cloneSource || that.instance;
  63. if (td && td !== mainWOT.lastMouseOver && isChildOf(td, table)) {
  64. mainWOT.lastMouseOver = td;
  65. that.instance.getSetting('onCellMouseOver', event, that.instance.wtTable.getCoords(td), td, that.instance);
  66. }
  67. }
  68. };
  69. var onMouseOut = function onMouseOut(event) {
  70. var table = void 0;
  71. var lastTD = void 0;
  72. var nextTD = void 0;
  73. if (that.instance.hasSetting('onCellMouseOut')) {
  74. table = that.instance.wtTable.TABLE;
  75. lastTD = closestDown(event.realTarget, ['TD', 'TH'], table);
  76. nextTD = closestDown(event.relatedTarget, ['TD', 'TH'], table);
  77. if (lastTD && lastTD !== nextTD && isChildOf(lastTD, table)) {
  78. that.instance.getSetting('onCellMouseOut', event, that.instance.wtTable.getCoords(lastTD), lastTD, that.instance);
  79. }
  80. }
  81. };
  82. var onMouseUp = function onMouseUp(event) {
  83. if (event.button !== 2) {
  84. // if not right mouse button
  85. var cell = that.parentCell(event.realTarget);
  86. if (cell.TD === dblClickOrigin[0] && cell.TD === dblClickOrigin[1]) {
  87. if (hasClass(event.realTarget, 'corner')) {
  88. that.instance.getSetting('onCellCornerDblClick', event, cell.coords, cell.TD, that.instance);
  89. } else {
  90. that.instance.getSetting('onCellDblClick', event, cell.coords, cell.TD, that.instance);
  91. }
  92. dblClickOrigin[0] = null;
  93. dblClickOrigin[1] = null;
  94. } else if (cell.TD === dblClickOrigin[0]) {
  95. that.instance.getSetting('onCellMouseUp', event, cell.coords, cell.TD, that.instance);
  96. dblClickOrigin[1] = cell.TD;
  97. clearTimeout(that.dblClickTimeout[1]);
  98. that.dblClickTimeout[1] = setTimeout(function () {
  99. dblClickOrigin[1] = null;
  100. }, 500);
  101. } else if (cell.TD && that.instance.hasSetting('onCellMouseUp')) {
  102. that.instance.getSetting('onCellMouseUp', event, cell.coords, cell.TD, that.instance);
  103. }
  104. }
  105. };
  106. var onTouchEnd = function onTouchEnd(event) {
  107. clearTimeout(longTouchTimeout);
  108. // that.instance.longTouch == void 0;
  109. event.preventDefault();
  110. onMouseUp(event);
  111. // eventManager.removeEventListener(that.instance.wtTable.holder, "mouseup", onMouseUp);
  112. };
  113. eventManager.addEventListener(this.instance.wtTable.holder, 'mousedown', onMouseDown);
  114. eventManager.addEventListener(this.instance.wtTable.TABLE, 'mouseover', onMouseOver);
  115. eventManager.addEventListener(this.instance.wtTable.TABLE, 'mouseout', onMouseOut);
  116. eventManager.addEventListener(this.instance.wtTable.holder, 'mouseup', onMouseUp);
  117. // check if full HOT instance, or detached WOT AND run on mobile device
  118. if (this.instance.wtTable.holder.parentNode.parentNode && isMobileBrowser() && !that.instance.wtTable.isWorkingOnClone()) {
  119. var classSelector = '.' + this.instance.wtTable.holder.parentNode.className.split(' ').join('.');
  120. eventManager.addEventListener(this.instance.wtTable.holder, 'touchstart', function (event) {
  121. that.instance.touchApplied = true;
  122. if (isChildOf(event.target, classSelector)) {
  123. onTouchStart.call(event.target, event);
  124. }
  125. });
  126. eventManager.addEventListener(this.instance.wtTable.holder, 'touchend', function (event) {
  127. that.instance.touchApplied = false;
  128. if (isChildOf(event.target, classSelector)) {
  129. onTouchEnd.call(event.target, event);
  130. }
  131. });
  132. if (!that.instance.momentumScrolling) {
  133. that.instance.momentumScrolling = {};
  134. }
  135. eventManager.addEventListener(this.instance.wtTable.holder, 'scroll', function (event) {
  136. clearTimeout(that.instance.momentumScrolling._timeout);
  137. if (!that.instance.momentumScrolling.ongoing) {
  138. that.instance.getSetting('onBeforeTouchScroll');
  139. }
  140. that.instance.momentumScrolling.ongoing = true;
  141. that.instance.momentumScrolling._timeout = setTimeout(function () {
  142. if (!that.instance.touchApplied) {
  143. that.instance.momentumScrolling.ongoing = false;
  144. that.instance.getSetting('onAfterMomentumScroll');
  145. }
  146. }, 200);
  147. });
  148. }
  149. eventManager.addEventListener(window, 'resize', function () {
  150. if (that.instance.getSetting('stretchH') !== 'none') {
  151. that.instance.draw();
  152. }
  153. });
  154. this.destroy = function () {
  155. clearTimeout(this.dblClickTimeout[0]);
  156. clearTimeout(this.dblClickTimeout[1]);
  157. eventManager.destroy();
  158. };
  159. }
  160. Event.prototype.parentCell = function (elem) {
  161. var cell = {};
  162. var TABLE = this.instance.wtTable.TABLE;
  163. var TD = closestDown(elem, ['TD', 'TH'], TABLE);
  164. if (TD) {
  165. cell.coords = this.instance.wtTable.getCoords(TD);
  166. cell.TD = TD;
  167. } else if (hasClass(elem, 'wtBorder') && hasClass(elem, 'current')) {
  168. cell.coords = this.instance.selections.current.cellRange.highlight; // selections.current is current selected cell
  169. cell.TD = this.instance.wtTable.getCell(cell.coords);
  170. } else if (hasClass(elem, 'wtBorder') && hasClass(elem, 'area')) {
  171. if (this.instance.selections.area.cellRange) {
  172. cell.coords = this.instance.selections.area.cellRange.to; // selections.area is area selected cells
  173. cell.TD = this.instance.wtTable.getCell(cell.coords);
  174. }
  175. }
  176. return cell;
  177. };
  178. export default Event;