forked from mlyknown/vue-gesture
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue2-gesture.js
377 lines (342 loc) · 14.6 KB
/
vue2-gesture.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* MIT License
Copyright (c) 2017 Allan Karlson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* MIT License
Copyright (c) 2016 biubiubiu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
// tap — fires when the element is tapped.
// doubleTap — this pair of events can be used to detect both single and double taps on the same element (if you don’t need double tap detection, use tap instead).
// longTap — fires when an element is tapped and the finger is held down for more than 750ms.
// swipe, swipeLeft, swipeRight, swipeUp, swipeDown — fires when an element is swiped (optionally in the given direction)
// touchstart touchmove touchend - These equivalent to touch the primary event
;(function(){
if(vueGesture && vueGesture.config && vueGesture.config.id === "[email protected]") return;
var vueGesture = {};
vueGesture.gloabal = {
id: "[email protected]",
domUuid : 1,
InternalKeyName : "vue2GestureInternalKey"
}
vueGesture.domCaches = {};
vueGesture.util={
getType:function(o){
var _t;
return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();
},
deepClone:function(source){
var self=this;
var destination=self.getType(source);
destination=destination==='array'?[]:(destination==='object'?{}:source);
for (var p in source) {
if (self.getType(source[p]) === "array" || self.getType(source[p]) === "object") {
destination[p] = self.getType(source[p]) === "array" ? [] : {};
destination[p]=arguments.callee.call(self, source[p]);
} else {
destination[p] = source[p];
}
}
return destination;
},
isFunction: function(f){
return (f && Object.prototype.toString.call(f)==="[object Function]") ? true : false;
}
};
vueGesture.config = {
maxSingleTapTimeInterval : 300, // ms
maxSingleTapPageDistanceSquared : 25, // within 5px we consider it as a single tap
minLongtapTimeInterval : 700,
maxDoubleTapTimeInterval: 300,
maxDoubleTapPageDistanceSquared: 64, //8px
gestureEventsToClick:['tap', 'longtap', 'touchstart']
};
vueGesture.Statics = {
initEvents : function(self){
var _self = this;
var b = vueGesture.Statics.isInDomCaches(self);
if(b) return;
var domCache = vueGesture.Statics.getDomCache(self);
domCache.listenTouchEvents.touchstart = function(e) {
if (_self.isPC()) {return}
if(_self.isPrimaryTouch(e)) return;
_self.touchstartHandler(self, e);
}
domCache.listenTouchEvents.touchmove = function(e) {
if (_self.isPC()) {return}
if(_self.isPrimaryTouch(e)) return;
_self.touchmoveHandler(self, e);
}
domCache.listenTouchEvents.touchend = function(e) {
if(e.type != "touchend") return;
if (_self.isPC()) {return}
if(_self.isPrimaryTouch(e)) return;
_self.touchendHandler(self, e);
}
domCache.listenTouchEvents.click = function(e){
//todo
// if(_self.isPrimaryTouch(e)) return;
_self.clickHandler(self, e);
}
self.addEventListener('touchstart',domCache.listenTouchEvents.touchstart,false);
self.addEventListener('touchmove',domCache.listenTouchEvents.touchmove,false);
self.addEventListener('touchend',domCache.listenTouchEvents.touchend,false);
self.addEventListener('click',domCache.listenTouchEvents.click,false);
},
invokeHandler : function(e, o, touch, gestureName){
var _self = this;
if (vueGesture.judgements[gestureName](touch)) {
_self.executeFn(e, o);
}
},
clickHandler: function(self , e){
var _self = this;
var domCache = vueGesture.Statics.getDomCache(self);
var touch = domCache.touch;
var o = domCache.gestureEvents['click'];
if (o) {
_self.executeFn(e ,o);
}
if (_self.isPC()) {
vueGesture.config.gestureEventsToClick.forEach(function(elem){
var _o = domCache.gestureEvents[elem];
if (_o){
_self.executeFn(e ,_o);
}
});
}
},
touchstartHandler : function(self, e) {
var _self = this;
var domCache = vueGesture.Statics.getDomCache(self);
var touch = domCache.touch;
var o = domCache.gestureEvents['touchstart'];
if (o) {
_self.executeFn(e, o);
}
touch.touchstartTime = e.timeStamp;
touch.touchstartCoord.pageX = e.touches[0].pageX;
touch.touchstartCoord.pageY = e.touches[0].pageY;
},
touchmoveHandler : function(self ,e){
var _self = this;
var domCache = vueGesture.Statics.getDomCache(self);
var touch = domCache.touch;
var o = domCache.gestureEvents['touchmove'];
if (o) {
_self.executeFn(e , o);
}
},
touchendHandler : function(self, e) {
var _self = this;
var domCache = vueGesture.Statics.getDomCache(self);
var touch = domCache.touch;
touch.touchendTime = e.timeStamp;
touch.touchendCoord.pageX = e.changedTouches[0].pageX;
touch.touchendCoord.pageY = e.changedTouches[0].pageY;
//is match condition;
for (var o in domCache.gestureEvents){
_self.invokeHandler(e, domCache.gestureEvents[o], touch, o);
}
touch.lastTouchstartTime = touch.touchstartTime;
touch.lastTouchendTime = touch.touchendTime;
touch.lastTouchstartCoord = vueGesture.util.deepClone(touch.touchstartCoord);
touch.lastTouchendCoord = vueGesture.util.deepClone(touch.touchendCoord);
},
getDomCache : function(self) {
return vueGesture.domCaches[self[vueGesture.gloabal.InternalKeyName]] ||
(vueGesture.domCaches[self[vueGesture.gloabal.InternalKeyName] = vueGesture.gloabal.domUuid++] = this.initDomCache());
},
isInDomCaches : function(self){
return vueGesture.domCaches[self[vueGesture.gloabal.InternalKeyName]] ? true : false;
},
unbindDirective : function(self) {
var domCache = vueGesture.Statics.getDomCache(self);
vueGesture.Statics.removeDirectiveEventListeners(self, domCache);
//todo Memory recovery
vueGesture.domCaches[self.el[vueGesture.gloabal.InternalKeyName]] = null;
delete vueGesture.domCaches[self.el[vueGesture.gloabal.InternalKeyName]];
},
initDomCache : function(){
return {
touch : {
touchstartTime : 0,
touchendTime : 0,
touchstartCoord : {},
touchendCoord : {},
lastTouchendTime : 0,
lastTouchstartTime: 0,
lastTouchstartCoord : {},
lastTouchendCoord: {},
get timeInterval () {
return this.touchendTime - this.touchstartTime;
},
get pageXDistance () {
return this.touchendCoord.pageX - this.touchstartCoord.pageX;
},
get pageYDistance () {
return this.touchendCoord.pageY - this.touchstartCoord.pageY;
},
get lastTimeInterval () {
return this.lastTouchendTime - this.lastTouchstartTime;
},
get lastPageXDistance () {
return this.touchendCoord.pageX - this.touchstartCoord.pageX;
},
get lastPageYDistance () {
return this.touchendCoord.pageY - this.touchstartCoord.pageY;
},
get deltaTime () {
return this.touchendTime - this.lastTouchstartTime;
}
},
gestureEvents: {},
listenTouchEvents: {}
}
},
getEventNameByArg : function(arg){
var str = (0 === arg.indexOf(':') ? arg.substr(1):arg);
str = str.toLowerCase();
if (typeof vueGesture.judgements[str] != "function") {
return false;
}
return str;
},
isPrimaryTouch : function(event){
// ensure swiping with one touch and not pinching
return (event.touches.length > 1 || event.scale && event.scale !== 1);
},
isPC :function() {
var uaInfo = navigator.userAgent;
var agents = ["Android", "iPhone", "Windows Phone", "iPad", "iPod"];
var flag = true;
for (var i = 0; i < agents.length; i++) {
if (uaInfo.indexOf(agents[i]) > 0) { flag = false; break; }
}
return flag;
},
removeDirectiveEventListeners : function(self, domCache){
self.removeEventListener('touchstart', domCache.listenTouchEvents.touchstart);
self.removeEventListener('touchmove', domCache.listenTouchEvents.touchmove);
self.removeEventListener('touchend', domCache.listenTouchEvents.touchend);
self.removeEventListener('click', domCache.listenTouchEvents.click);
},
executeFn: function(e ,o){
o.fn(e);
if(typeof o.modifiers != "undefined")
{
if(o.modifiers.stop)
e.stopPropagation();
if(o.modifiers.prevent)
e.preventDefault();
}
}
};
vueGesture.judgements = {
'tap': function(touch){
return (touch.timeInterval < vueGesture.config.maxSingleTapTimeInterval) && (touch.pageXDistance * touch.pageXDistance + touch.pageYDistance * touch.pageYDistance) < vueGesture.config.maxSingleTapPageDistanceSquared;
},
'longtap': function(touch){
return (touch.timeInterval > vueGesture.config.minLongtapTimeInterval) && (touch.pageXDistance * touch.pageXDistance + touch.pageYDistance * touch.pageYDistance) < vueGesture.config.maxSingleTapPageDistanceSquared;
},
'doubletap': function(touch){
return touch.deltaTime < vueGesture.config.maxDoubleTapTimeInterval &&
(touch.lastPageXDistance * touch.lastPageXDistance + touch.lastPageYDistance * touch.lastPageYDistance) < vueGesture.config.maxDoubleTapPageDistanceSquared &&
(touch.timeInterval < vueGesture.config.maxSingleTapTimeInterval) && (touch.pageXDistance * touch.pageXDistance + touch.pageYDistance * touch.pageYDistance) < vueGesture.config.maxSingleTapPageDistanceSquared &&
(touch.lastTimeInterval < vueGesture.config.maxSingleTapTimeInterval) && (touch.lastPageXDistance * touch.lastPageXDistance + touch.lastPageYDistance * touch.lastPageYDistance) < vueGesture.config.maxSingleTapPageDistanceSquared;
},
'swipe': function(touch){
return (touch.pageXDistance * touch.pageXDistance + touch.pageYDistance * touch.pageYDistance) > vueGesture.config.maxSingleTapPageDistanceSquared;
},
'swipeleft': function(touch){
if(!this['swipe'](touch)) return false;
return touch.pageXDistance < 0 && Math.abs(touch.pageXDistance) > Math.abs(touch.pageYDistance);
},
'swiperight': function(touch){
if(!this['swipe'](touch)) return false;
return touch.pageXDistance > 0 && Math.abs(touch.pageXDistance) > Math.abs(touch.pageYDistance);
},
'swipeup': function(touch){
if(!this['swipe'](touch)) return false;
return touch.pageYDistance < 0 && Math.abs(touch.pageYDistance) > Math.abs(touch.pageXDistance);
},
'swipedown': function(touch){
if(!this['swipe'](touch)) return false;
return touch.pageYDistance > 0 && Math.abs(touch.pageYDistance) > Math.abs(touch.pageXDistance);
},
//not calculate
'touchstart': function(){return false},
'touchmove': function(){return false},
'touchend': function(){return true},
'click': function(){return false;}
};
Vue.component('vue2-gesture', {
template: '<span><slot></slot></span>',
props: {
type: {type: String, default: function () { return null }},
call: {type: Function },
types: {type: Array, default : function (){return []}}
},
mounted: function () {
var self = this;
vueGesture.Statics.initEvents(this.$el);
if(typeof this.call !== 'function') {
console.log(this.call);
return console.error('The expression of directive "v-gesture" must be a function!');
}
var all_types = [];
if(this.type != null)
{
all_types.push(this.type);
}
for(var i in this.types)
{
all_types.push(this.types[i])
}
for(var i in all_types)
{
var eventName = vueGesture.Statics.getEventNameByArg(all_types[i]);
var domCache = vueGesture.Statics.getDomCache(this.$el);
if(!eventName) {
console.error("self.arg not correct argument;");
return;
}
domCache.gestureEvents[eventName] = {};
domCache.gestureEvents[eventName].fn = this.call;
}
},
data: function () {
return {};
}
});
})()