UNPKG

5.34 kBJavaScriptView Raw
1/**
2 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
3 * @version v18.1.2
4 * @link http://www.ag-grid.com/
5 * @license MIT
6 */
7"use strict";
8Object.defineProperty(exports, "__esModule", { value: true });
9var eventService_1 = require("../eventService");
10var utils_1 = require("../utils");
11var TouchListener = (function () {
12 function TouchListener(eElement, preventMouseClick) {
13 if (preventMouseClick === void 0) { preventMouseClick = false; }
14 var _this = this;
15 this.destroyFuncs = [];
16 this.touching = false;
17 this.eventService = new eventService_1.EventService();
18 this.eElement = eElement;
19 this.preventMouseClick = preventMouseClick;
20 var startListener = this.onTouchStart.bind(this);
21 var moveListener = this.onTouchMove.bind(this);
22 var endListener = this.onTouchEnd.bind(this);
23 this.eElement.addEventListener("touchstart", startListener, { passive: true });
24 this.eElement.addEventListener("touchmove", moveListener, { passive: true });
25 // we set passive=false, as we want to prevent default on this event
26 this.eElement.addEventListener("touchend", endListener, { passive: false });
27 this.destroyFuncs.push(function () {
28 _this.eElement.addEventListener("touchstart", startListener, { passive: true });
29 _this.eElement.addEventListener("touchmove", moveListener, { passive: true });
30 _this.eElement.addEventListener("touchend", endListener, { passive: false });
31 });
32 }
33 TouchListener.prototype.getActiveTouch = function (touchList) {
34 for (var i = 0; i < touchList.length; i++) {
35 var matches = touchList[i].identifier === this.touchStart.identifier;
36 if (matches) {
37 return touchList[i];
38 }
39 }
40 return null;
41 };
42 TouchListener.prototype.addEventListener = function (eventType, listener) {
43 this.eventService.addEventListener(eventType, listener);
44 };
45 TouchListener.prototype.removeEventListener = function (eventType, listener) {
46 this.eventService.removeEventListener(eventType, listener);
47 };
48 TouchListener.prototype.onTouchStart = function (touchEvent) {
49 var _this = this;
50 // only looking at one touch point at any time
51 if (this.touching) {
52 return;
53 }
54 this.touchStart = touchEvent.touches[0];
55 this.touching = true;
56 this.moved = false;
57 var touchStartCopy = this.touchStart;
58 setTimeout(function () {
59 var touchesMatch = _this.touchStart === touchStartCopy;
60 if (_this.touching && touchesMatch && !_this.moved) {
61 _this.moved = true;
62 var event_1 = {
63 type: TouchListener.EVENT_LONG_TAP,
64 touchStart: _this.touchStart,
65 touchEvent: touchEvent
66 };
67 _this.eventService.dispatchEvent(event_1);
68 }
69 }, 500);
70 };
71 TouchListener.prototype.onTouchMove = function (touchEvent) {
72 if (!this.touching) {
73 return;
74 }
75 var touch = this.getActiveTouch(touchEvent.touches);
76 if (!touch) {
77 return;
78 }
79 var eventIsFarAway = !utils_1.Utils.areEventsNear(touch, this.touchStart, 4);
80 if (eventIsFarAway) {
81 this.moved = true;
82 }
83 };
84 TouchListener.prototype.onTouchEnd = function (touchEvent) {
85 if (!this.touching) {
86 return;
87 }
88 if (!this.moved) {
89 var event_2 = {
90 type: TouchListener.EVENT_TAP,
91 touchStart: this.touchStart
92 };
93 this.eventService.dispatchEvent(event_2);
94 this.checkForDoubleTap();
95 // stops the tap from also been processed as a mouse click
96 if (this.preventMouseClick) {
97 touchEvent.preventDefault();
98 }
99 }
100 this.touching = false;
101 };
102 TouchListener.prototype.checkForDoubleTap = function () {
103 var now = new Date().getTime();
104 if (this.lastTapTime > 0) {
105 // if previous tap, see if duration is short enough to be considered double tap
106 var interval = now - this.lastTapTime;
107 if (interval > TouchListener.DOUBLE_TAP_MILLIS) {
108 // dispatch double tap event
109 var event_3 = {
110 type: TouchListener.EVENT_DOUBLE_TAP,
111 touchStart: this.touchStart
112 };
113 this.eventService.dispatchEvent(event_3);
114 // this stops a tripple tap ending up as two double taps
115 this.lastTapTime = null;
116 }
117 else {
118 this.lastTapTime = now;
119 }
120 }
121 else {
122 this.lastTapTime = now;
123 }
124 };
125 TouchListener.prototype.destroy = function () {
126 this.destroyFuncs.forEach(function (func) { return func(); });
127 };
128 TouchListener.EVENT_TAP = "tap";
129 TouchListener.EVENT_DOUBLE_TAP = "doubleTap";
130 TouchListener.EVENT_LONG_TAP = "longTap";
131 TouchListener.DOUBLE_TAP_MILLIS = 500;
132 return TouchListener;
133}());
134exports.TouchListener = TouchListener;