event.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import Eventful from './Eventful.js';
  2. import env from './env.js';
  3. import { isCanvasEl, transformCoordWithViewport } from './dom.js';
  4. var MOUSE_EVENT_REG = /^(?:mouse|pointer|contextmenu|drag|drop)|click/;
  5. var _calcOut = [];
  6. var firefoxNotSupportOffsetXY = env.browser.firefox
  7. && +env.browser.version.split('.')[0] < 39;
  8. export function clientToLocal(el, e, out, calculate) {
  9. out = out || {};
  10. if (calculate) {
  11. calculateZrXY(el, e, out);
  12. }
  13. else if (firefoxNotSupportOffsetXY
  14. && e.layerX != null
  15. && e.layerX !== e.offsetX) {
  16. out.zrX = e.layerX;
  17. out.zrY = e.layerY;
  18. }
  19. else if (e.offsetX != null) {
  20. out.zrX = e.offsetX;
  21. out.zrY = e.offsetY;
  22. }
  23. else {
  24. calculateZrXY(el, e, out);
  25. }
  26. return out;
  27. }
  28. function calculateZrXY(el, e, out) {
  29. if (env.domSupported && el.getBoundingClientRect) {
  30. var ex = e.clientX;
  31. var ey = e.clientY;
  32. if (isCanvasEl(el)) {
  33. var box = el.getBoundingClientRect();
  34. out.zrX = ex - box.left;
  35. out.zrY = ey - box.top;
  36. return;
  37. }
  38. else {
  39. if (transformCoordWithViewport(_calcOut, el, ex, ey)) {
  40. out.zrX = _calcOut[0];
  41. out.zrY = _calcOut[1];
  42. return;
  43. }
  44. }
  45. }
  46. out.zrX = out.zrY = 0;
  47. }
  48. export function getNativeEvent(e) {
  49. return e
  50. || window.event;
  51. }
  52. export function normalizeEvent(el, e, calculate) {
  53. e = getNativeEvent(e);
  54. if (e.zrX != null) {
  55. return e;
  56. }
  57. var eventType = e.type;
  58. var isTouch = eventType && eventType.indexOf('touch') >= 0;
  59. if (!isTouch) {
  60. clientToLocal(el, e, e, calculate);
  61. var wheelDelta = getWheelDeltaMayPolyfill(e);
  62. e.zrDelta = wheelDelta ? wheelDelta / 120 : -(e.detail || 0) / 3;
  63. }
  64. else {
  65. var touch = eventType !== 'touchend'
  66. ? e.targetTouches[0]
  67. : e.changedTouches[0];
  68. touch && clientToLocal(el, touch, e, calculate);
  69. }
  70. var button = e.button;
  71. if (e.which == null && button !== undefined && MOUSE_EVENT_REG.test(e.type)) {
  72. e.which = (button & 1 ? 1 : (button & 2 ? 3 : (button & 4 ? 2 : 0)));
  73. }
  74. return e;
  75. }
  76. function getWheelDeltaMayPolyfill(e) {
  77. var rawWheelDelta = e.wheelDelta;
  78. if (rawWheelDelta) {
  79. return rawWheelDelta;
  80. }
  81. var deltaX = e.deltaX;
  82. var deltaY = e.deltaY;
  83. if (deltaX == null || deltaY == null) {
  84. return rawWheelDelta;
  85. }
  86. var delta = deltaY !== 0 ? Math.abs(deltaY) : Math.abs(deltaX);
  87. var sign = deltaY > 0 ? -1
  88. : deltaY < 0 ? 1
  89. : deltaX > 0 ? -1
  90. : 1;
  91. return 3 * delta * sign;
  92. }
  93. export function addEventListener(el, name, handler, opt) {
  94. el.addEventListener(name, handler, opt);
  95. }
  96. export function removeEventListener(el, name, handler, opt) {
  97. el.removeEventListener(name, handler, opt);
  98. }
  99. export var stop = function (e) {
  100. e.preventDefault();
  101. e.stopPropagation();
  102. e.cancelBubble = true;
  103. };
  104. export function isMiddleOrRightButtonOnMouseUpDown(e) {
  105. return e.which === 2 || e.which === 3;
  106. }
  107. export { Eventful as Dispatcher };