MSPointer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. var __extends = (this && this.__extends) || (function () {
  12. var extendStatics = function (d, b) {
  13. extendStatics = Object.setPrototypeOf ||
  14. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  15. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  16. return extendStatics(d, b);
  17. };
  18. return function (d, b) {
  19. extendStatics(d, b);
  20. function __() { this.constructor = d; }
  21. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  22. };
  23. })();
  24. import H from './Globals.js';
  25. var charts = H.charts, doc = H.doc, noop = H.noop, win = H.win;
  26. import Pointer from './Pointer.js';
  27. import U from './Utilities.js';
  28. var addEvent = U.addEvent, css = U.css, objectEach = U.objectEach, removeEvent = U.removeEvent;
  29. /* globals MSPointerEvent, PointerEvent */
  30. // The touches object keeps track of the points being touched at all times
  31. var touches = {};
  32. var hasPointerEvent = !!win.PointerEvent;
  33. /* eslint-disable valid-jsdoc */
  34. /** @private */
  35. function getWebkitTouches() {
  36. var fake = [];
  37. fake.item = function (i) {
  38. return this[i];
  39. };
  40. objectEach(touches, function (touch) {
  41. fake.push({
  42. pageX: touch.pageX,
  43. pageY: touch.pageY,
  44. target: touch.target
  45. });
  46. });
  47. return fake;
  48. }
  49. /** @private */
  50. function translateMSPointer(e, method, wktype, func) {
  51. var p;
  52. if ((e.pointerType === 'touch' ||
  53. e.pointerType === e.MSPOINTER_TYPE_TOUCH) && charts[H.hoverChartIndex]) {
  54. func(e);
  55. p = charts[H.hoverChartIndex].pointer;
  56. p[method]({
  57. type: wktype,
  58. target: e.currentTarget,
  59. preventDefault: noop,
  60. touches: getWebkitTouches()
  61. });
  62. }
  63. }
  64. /** @private */
  65. var MSPointer = /** @class */ (function (_super) {
  66. __extends(MSPointer, _super);
  67. function MSPointer() {
  68. return _super !== null && _super.apply(this, arguments) || this;
  69. }
  70. /* *
  71. *
  72. * Functions
  73. *
  74. * */
  75. /**
  76. * Add or remove the MS Pointer specific events
  77. *
  78. * @private
  79. * @function Highcharts.Pointer#batchMSEvents
  80. *
  81. * @param {Function} fn
  82. *
  83. * @return {void}
  84. */
  85. MSPointer.prototype.batchMSEvents = function (fn) {
  86. fn(this.chart.container, hasPointerEvent ? 'pointerdown' : 'MSPointerDown', this.onContainerPointerDown);
  87. fn(this.chart.container, hasPointerEvent ? 'pointermove' : 'MSPointerMove', this.onContainerPointerMove);
  88. fn(doc, hasPointerEvent ? 'pointerup' : 'MSPointerUp', this.onDocumentPointerUp);
  89. };
  90. // Destroy MS events also
  91. MSPointer.prototype.destroy = function () {
  92. this.batchMSEvents(removeEvent);
  93. _super.prototype.destroy.call(this);
  94. };
  95. // Disable default IE actions for pinch and such on chart element
  96. MSPointer.prototype.init = function (chart, options) {
  97. _super.prototype.init.call(this, chart, options);
  98. if (this.hasZoom) { // #4014
  99. css(chart.container, {
  100. '-ms-touch-action': 'none',
  101. 'touch-action': 'none'
  102. });
  103. }
  104. };
  105. /**
  106. * @private
  107. * @function Highcharts.Pointer#onContainerPointerDown
  108. *
  109. * @param {Highcharts.PointerEventObject} e
  110. *
  111. * @return {void}
  112. */
  113. MSPointer.prototype.onContainerPointerDown = function (e) {
  114. translateMSPointer(e, 'onContainerTouchStart', 'touchstart', function (e) {
  115. touches[e.pointerId] = {
  116. pageX: e.pageX,
  117. pageY: e.pageY,
  118. target: e.currentTarget
  119. };
  120. });
  121. };
  122. /**
  123. * @private
  124. * @function Highcharts.Pointer#onContainerPointerMove
  125. *
  126. * @param {Highcharts.PointerEventObject} e
  127. *
  128. * @return {void}
  129. */
  130. MSPointer.prototype.onContainerPointerMove = function (e) {
  131. translateMSPointer(e, 'onContainerTouchMove', 'touchmove', function (e) {
  132. touches[e.pointerId] = ({ pageX: e.pageX, pageY: e.pageY });
  133. if (!touches[e.pointerId].target) {
  134. touches[e.pointerId].target = e.currentTarget;
  135. }
  136. });
  137. };
  138. /**
  139. * @private
  140. * @function Highcharts.Pointer#onDocumentPointerUp
  141. *
  142. * @param {Highcharts.PointerEventObject} e
  143. *
  144. * @return {void}
  145. */
  146. MSPointer.prototype.onDocumentPointerUp = function (e) {
  147. translateMSPointer(e, 'onDocumentTouchEnd', 'touchend', function (e) {
  148. delete touches[e.pointerId];
  149. });
  150. };
  151. // Add IE specific touch events to chart
  152. MSPointer.prototype.setDOMEvents = function () {
  153. _super.prototype.setDOMEvents.call(this);
  154. if (this.hasZoom || this.followTouchMove) {
  155. this.batchMSEvents(addEvent);
  156. }
  157. };
  158. return MSPointer;
  159. }(Pointer));
  160. export default MSPointer;