-
Notifications
You must be signed in to change notification settings - Fork 2
/
swipe_event.js
178 lines (148 loc) · 4.5 KB
/
swipe_event.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
;(function($) {
$.browser.supportTouch = (function() {
return ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;
})();
if (!$.browser.supportTouch) {
return;
}
var doc = document;
var boolScrolling = false;
var scrollStartPos = [];
window.addEventListener('touchstart', function(e) {
scrollStartPos = [doc.body.scrollLeft, doc.body.scrollTop];
}, false);
window.addEventListener('touchmove', function(e) {
if (doc.body.scrollLeft !== scrollStartPos[0]
|| doc.body.scrollTop !== scrollStartPos[1]) {
boolScrolling = true;
}
}, false);
window.addEventListener('scroll', function(e) {
boolScrolling = false;
scrollStartPos = [];
}, false);
var defaultOption = {
direction: 'horizontal'
};
var proxy = function(fn, context) {
return function() {
fn.apply(context, arguments);
};
};
var getAngle = function(pos1, pos2) {
var x = pos2[0] - pos1[0];
var y = pos2[1] - pos1[1];
return Math.atan2(y, x) * 180 / Math.PI;
};
var getDirectionFromAngle = function(angle) {
var directions = {
up: angle < -45 && angle > -135,
down: angle > 45 && angle < 135,
right: angle >= -45 && angle <= 45,
left: angle >= 135 || angle <= -135
};
var direction, key;
for (key in directions) {
if (directions.hasOwnProperty(key) && directions[key]) {
direction = key;
break;
}
}
return direction;
};
var Swipe = function(node, options) {
this.start = null;
this.node = node;
this.options = options;
this.valid = false;
node[0].addEventListener('touchstart', proxy(this.handleTouchStart, this), false);
node[0].addEventListener('touchmove', proxy(this.handleTouchMove, this), false);
node[0].addEventListener('touchcancel', proxy(this.handleTouchEnd, this), false);
node[0].addEventListener('touchend', proxy(this.handleTouchEnd, this), false);
};
Swipe.prototype = {
handleTouchStart: function(e) {
if (boolScrolling) {
return;
}
this.offset = 0;
this.end = null;
this.start = [e.touches[0].pageX, e.touches[0].pageY];
this.valid = true;
this.startMove = true;
this.savePrevDirection = '';
document.documentElement.scrollTop = 0;
var params = {
originEvent: e
};
if (this.options.start) {
this.options.start.call(this.node, params);
}
this.node.trigger('swipe::start', params);
},
handleTouchMove: function(e) {
if (!this.valid || boolScrolling) {
return;
}
var options = this.options;
var point = e.touches[0];
var angle = getAngle(this.start, [point.pageX, point.pageY]);
var direction = getDirectionFromAngle(angle);
var offset = (options.direction === 'horizontal') ? point.pageX - this.start[0] : point.pageY - this.start[1];
var invalidDirection = (options.direction === 'horizontal' && (/up|down/.test(direction)))
|| (options.direction === 'vertical' && (/left|right/.test(direction)));
if ( invalidDirection && this.startMove ) {
this.valid = false;
return;
}
e.preventDefault();
this.offset = offset;
this.valid = true;
this.startMove = false;
if (invalidDirection) {
direction = this.savePrevDirection;
}
var params = {
originEvent: e,
point: point,
offset: this.offset,
direction: direction,
angle: angle
};
if (this.options.move) {
this.options.move.call(this.node, params);
}
this.node.trigger('swipe::move', params);
this.end = this.offset;
this.savePrevDirection = direction;
},
handleTouchEnd: function(e) {
if (boolScrolling) {
return;
}
if (!this.valid || !this.end) {
this.end = null;
this.start = null;
return;
}
e.preventDefault();
var params = {
originEvent: e,
offset: this.end
};
if (this.options.end) {
this.options.end.call(this.node, params);
}
this.node.trigger('swipe::end', params);
this.end = null;
this.start = null;
this.valid = false;
}
};
$.fn.swipeEvent = function(options) {
return $.each(this, function() {
var node = $(this);
node.data('swipe', new Swipe(node, $.extend({}, defaultOption, options || {})));
});
};
})(jQuery);