-
Notifications
You must be signed in to change notification settings - Fork 0
/
swipeGestureRecognizer.js
103 lines (85 loc) · 3.5 KB
/
swipeGestureRecognizer.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
// swipeGestureRecognizer
(function(host/*host-object*/, gestureRecognizer/*gestureRecognizer-object*/, undefined) {
"use strict";
var DIRECTION_LEFT = 1;
var DIRECTION_RIGHT = 2;
var DIRECTION_TOP = 4;
var DIRECTION_BOTTOM = 8;
var r = function(sel, func) {
this.constructor.apply(this, arguments);
};
var proto = r.prototype = new gestureRecognizer();
proto.cons = {
DIRECTION_LEFT : DIRECTION_LEFT,
DIRECTION_RIGHT : DIRECTION_RIGHT,
DIRECTION_TOP : DIRECTION_TOP,
DIRECTION_BOTTOM : DIRECTION_BOTTOM
};
proto.checkThresholdToleranceForX = function(cachedTotalX, deltaX) {
var thresholdToleranceX = this.threshold.tolerance.x;
return (cachedTotalX < 0 && deltaX < thresholdToleranceX) ||
(cachedTotalX > 0 && deltaX > -thresholdToleranceX);
}
proto.checkThresholdToleranceForY = function(cachedTotalY, deltaY) {
var thresholdToleranceY = this.threshold.tolerance.y;
return (cachedTotalY > 0 && deltaY > -thresholdToleranceY) ||
(cachedTotalY < 0 && deltaY < thresholdToleranceY);
}
// userInteractionMoveCallback
proto.userInteractionMoveCallback = function(obj) {
var cachedElement = this.cache.element = obj.element;
var cachedTotal = this.cache.total;
var objPositionDelta = obj.position.delta;
var objPositionDeltaX = objPositionDelta.x;
var objPositionDeltaY = objPositionDelta.y;
var objIsLastAction = obj.isLastAction();
var thresholdValue = this.threshold.value;
var thresholdValueX = thresholdValue.x;
var thresholdValueY = thresholdValue.y;
if (this.checkThresholdToleranceForX(cachedTotal.x, objPositionDeltaX)) {
cachedTotal.x += objPositionDeltaX;
} else {
cachedTotal.x = objPositionDeltaX;
}
if (this.checkThresholdToleranceForY(cachedTotal.y, objPositionDeltaY)) {
cachedTotal.y += objPositionDeltaY;
} else {
cachedTotal.y = objPositionDeltaY;
}
//console.log(this.cache.total.x, recordMovesCallbackObject.isLastAction());
var _isSwipeLeft = objIsLastAction && cachedTotal.x < -thresholdValueX;
var _isSwipeRight = objIsLastAction && cachedTotal.x > thresholdValueX;
var _isSwipeTop = objIsLastAction && cachedTotal.y > thresholdValueY;
var _isSwipeBottom = objIsLastAction && cachedTotal.y < -thresholdValueY;
var callbackObject = {
isSwipeLeft : function() {
return _isSwipeLeft;
},
isSwipeRight : function() {
return _isSwipeRight;
},
isSwipeTop : function() {
return _isSwipeTop;
},
isSwipeBottom : function() {
return _isSwipeBottom;
},
swipeHorizontalCanceled : function() {
return objIsLastAction && !_isSwipeLeft && !_isSwipeRight;
},
swipeVerticalCanceled : function() {
return objIsLastAction && !_isSwipeTop && !_isSwipeBottom;
},
delta : objPositionDelta,
element: cachedElement
};
this.callback(callbackObject);
if (obj.isLastAction()) {
cachedTotal.x = 0;
cachedTotal.y = 0;
}
}
host.onSwipeGesture = function(selector, callback) {
return new r(selector, callback);
}
})(this, this.gestureRecognizer);