-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gestures.js
339 lines (300 loc) · 7.05 KB
/
Gestures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
Copyright 2021 Mircerlancerous - https://github.com/mircerlancerous/Gestures
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var Gestures = new function(){
var Types = {
All: 0,
SwipeLeft: 1,
SwipeRight: 2,
SwipeUp: 3,
SwipeDown: 4,
Down: 5,
Up: 6,
Tap: 7, Click: 7,
Hold: 8, Context: 8, LongClick: 8, //long hold then release or mouse right click
Holding: 9, //long hold - fires before release
Move: 10, Live: 10 //returns live positions
};
function TypeInvalid(type){
try{
let num = parseInt(type);
if(isNaN(num) || num < 0 || num > 10){
return true;
}
}
catch(e){
return true;
}
return false;
}
function enableElementListeners(elm){
elm.addEventListener("contextmenu", contextMenu, false);
elm.addEventListener("pointerup", pointerUp, false);
elm.addEventListener("pointerleave", pointerUp, false);
elm.addEventListener("pointercancel", pointerUp, false);
elm.addEventListener("pointerdown", pointerDown, false);
elm.addEventListener("pointermove", pointerMove, false);
}
function removeElementListeners(elm){
if(!elm){
return;
}
elm.removeEventListener("contextmenu", contextMenu, false);
elm.removeEventListener("pointerup", pointerUp, false);
elm.removeEventListener("pointerleave", pointerUp, false);
elm.removeEventListener("pointercancel", pointerUp, false);
elm.removeEventListener("pointerdown", pointerDown, false);
elm.removeEventListener("pointermove", pointerMove, false);
}
function addElement(elm, callback, type){
let obj = {
type: type,
callback: callback,
};
let isNew = false;
let data = findElementData(elm);
if(data == null){
isNew = true;
data = {
elm: elm,
types: [obj]
};
elmList.push(data);
}
else{
data.types.push(obj);
}
return isNew;
}
function findElementData(elm){
for(let i=0; i<elmList.length; i++){
//if this element no longer exists, auto clean-up
if(!elmList[i].elm){
elmList.splice(i,1);
i--;
continue;
}
//this is the element we're looking for
if(elm == elmList[i].elm){
return elmList[i];
}
}
return null; //should only get here when adding a new element
}
function doCallback(elm, obj){
let i, data = findElementData(elm);
for(i=0; i<data.types.length; i++){
if(data.types[i].type == Types.All || data.types[i].type == obj.type){
obj.elm = elm;
data.types[i].callback(obj);
break;
}
}
}
function reset(){
LastMove.x = 0;
LastMove.y = 0;
LastMove.count = 0;
Down.time = null;
clearTimeout(Down.timer);
}
function onDownTimeout(){
let reference = Down;
if(LastMove.count > 0){
reference = LastMove;
}
let obj = {
type: Types.Holding,
x: reference.x,
y: reference.y
};
doCallback(Down.elm, obj);
}
var elmList = [];
var Down = {
elm: null,
x: 0,
y: 0,
time: null,
timer: null
};
var LastMove = {
x: 0,
y: 0,
count: 0 //number of moves in this pointer session
};
var Config = {
MaxTapMoves: 5, //max number of move events to still count as a tap or hold
MinSwipeLength: 30, //px
MinLongTouchTime: 500 //ms
};
/***********************************************************************/
return {
Types: Types,
//type is optional
addListener: function(elm, callback, type){
if(TypeInvalid(type)){
type = Types.All;
}
let newElement = addElement(elm, callback, type);
if(newElement){
enableElementListeners(elm);
}
},
removeListener: function(elm, type){
for(let i=0; i<elmList.length; i++){
//if no element is passed, delete all
if(!elm || elm == elmList[i].elm){
if(typeof(type) === 'undefined'){
//completely remove the listeners for the element
removeElementListeners(elmList[i].elm);
elmList.splice(i,1);
}
else{
//remove just the callback for the type
// -- todo --
}
}
}
}
};
/***********************************************************************/
function stopDefaults(e){
e.preventDefault();
e.stopPropagation();
}
function contextMenu(e){
e.preventDefault();
}
function pointerUp(e){
stopDefaults(e);
if(Down.time == null){
return;
}
if(e.pageX == 0 && e.pageY == 0){
//handle pointer cancel
reset();
return;
}
let obj = {
type: Types.Up,
x: e.pageX,
y: e.pageY
};
doCallback(this, obj);
let leave = false;
//if the pointer has left the element
if(e.type == "pointerleave" || e.type == "pointercancel"){
leave = true;
}
let wasSwipe = false;
//check first if this was a swipe - must be faster than hold
if(Date.now() - Down.time < Config.MinLongTouchTime){
let changeX = obj.x - Down.x;
let changeY = obj.y - Down.y;
let negativeX = false, negativeY = false;
if(changeX < 0){
changeX *= -1;
negativeX = true;
}
if(changeY < 0){
changeY *= -1;
negativeY = true;
}
if(changeX >= changeY){
if(leave || changeX >= Config.MinSwipeLength){
//if left
if(negativeX){
obj.type = Types.SwipeLeft;
}
//if right
else{
obj.type = Types.SwipeRight;
}
doCallback(this, obj);
wasSwipe = true;
}
}
else{
if(leave || changeY >= Config.MinSwipeLength){
//if up
if(negativeY){
obj.type = Types.SwipeUp;
}
//if down
else{
obj.type = Types.SwipeDown;
}
doCallback(this, obj);
wasSwipe = true;
}
}
}
//check if this was a tap or hold
if(!wasSwipe && !leave && LastMove.count <= Config.MaxTapMoves){
if(Date.now() - Down.time > Config.MinLongTouchTime){
obj.type = Types.LongClick;
}
else{
obj.type = Types.Tap;
}
doCallback(this, obj);
}
reset();
}
function pointerDown(e){
stopDefaults(e);
//don't support multitouch
if(Down.time != null){
return;
}
Down.time = Date.now();
Down.x = e.pageX;
Down.y = e.pageY;
Down.elm = this;
//start the timer for the 'holding' event
Down.timer = setTimeout(onDownTimeout, Config.MinLongTouchTime);
LastMove.x = Down.x;
LastMove.y = Down.y;
let obj = {
type: Types.Down,
x: e.pageX,
y: e.pageY
};
doCallback(this, obj)
//Check if this is a right-click
if(e.button == 2){
//Fire a hold event
obj.type = Types.LongClick;
doCallback(this, obj)
reset();
}
}
function pointerMove(e){
stopDefaults(e);
if(Down.time == null){
return;
}
if(LastMove.x == e.pageX && LastMove.y == e.pageY){
return;
}
LastMove.x = e.pageX;
LastMove.y = e.pageY;
LastMove.count++;
let obj = {
type: Types.Move,
x: e.pageX,
y: e.pageY
};
doCallback(this, obj)
}
};