This repository has been archived by the owner on Dec 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
angular-swipe.js
234 lines (188 loc) · 6.43 KB
/
angular-swipe.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
(function(window, angular, undefined) {
'use strict';
/* global -ngSwipe */
var ngSwipe = angular.module('swipe', []);
ngSwipe.factory('swipe', [ function() {
var MOVE_BUFFER_RADIUS = 40;
var MAX_RATIO = 0.3;
var POINTER_EVENTS = {
'mouse': {
start: 'mousedown',
move: 'mousemove',
end: 'mouseup'
},
'touch': {
start: 'touchstart',
move: 'touchmove',
end: 'touchend',
cancel: 'touchcancel'
}
};
function getCoordinates(event) {
var originalEvent = event.originalEvent || event;
var touches = originalEvent.touches && originalEvent.touches.length ? originalEvent.touches : [originalEvent];
var e = (originalEvent.changedTouches && originalEvent.changedTouches[0]) || touches[0];
return {
x: e.clientX,
y: e.clientY
};
}
function getEvents(pointerTypes, eventType) {
var res = [];
angular.forEach(pointerTypes, function(pointerType) {
var eventName = POINTER_EVENTS[pointerType][eventType];
if (eventName) {
res.push(eventName);
}
});
return res.join(' ');
}
return {
bind: function(element, eventHandlers, pointerTypes) {
// Absolute total movement
var totalX, totalY;
// Coordinates of the start position.
var startCoords;
var lastPos;
// Whether a swipe is active.
var active = false;
// Decide where we are going
var isDecided = false;
var isVertical = true;
pointerTypes = pointerTypes || ['mouse', 'touch'];
element.on(getEvents(pointerTypes, 'start'), function(event) {
startCoords = getCoordinates(event);
active = true;
totalX = 0;
totalY = 0;
isDecided = false;
isVertical = true;
lastPos = startCoords;
eventHandlers['start'] && eventHandlers['start'](startCoords, event);
});
element.on(getEvents(pointerTypes, 'cancel'), function(event) {
active = false;
eventHandlers['cancel'] && eventHandlers['cancel'](event);
});
element.on(getEvents(pointerTypes, 'move'), function(event) {
if (! active) {
return;
}
if (! startCoords) {
return;
}
var coords = getCoordinates(event);
totalX += Math.abs(coords.x - lastPos.x);
totalY += Math.abs(coords.y - lastPos.y);
lastPos = coords;
if (totalX < MOVE_BUFFER_RADIUS && totalY < MOVE_BUFFER_RADIUS) {
return;
} else {
if (! isDecided){
var deltaX, deltaY, ratio;
deltaX = Math.abs(coords.x - startCoords.x);
deltaY = Math.abs(coords.y - startCoords.y);
ratio = deltaY / deltaX;
if (ratio < MAX_RATIO){
event.preventDefault();
isVertical = false;
} else {
isVertical = true;
}
isDecided = true;
}
}
event.isVertical = isVertical;
eventHandlers['move'] && eventHandlers['move'](coords, event);
});
element.on(getEvents(pointerTypes, 'end'), function(event) {
if (! active){
return;
}
event.isVertical = isVertical;
active = false;
eventHandlers['end'] && eventHandlers['end'](getCoordinates(event), event);
});
}
};
}]);
function makeSwipeDirective(directiveName, direction, axis, eventName) {
ngSwipe.directive(directiveName, ['$parse', 'swipe', function($parse, swipe) {
var MAX_OTHER_AXIS_DISTANCE = 75;
var MAX_RATIO = 0.3;
var MIN_DISTANCE = 30;
return function(scope, element, attr) {
var swipeHandler = $parse(attr[directiveName]);
var startCoords, valid;
function checkOverride(element, clazz) {
do {
var className = element.getAttribute('class');
if (className && className.match(clazz) !== null) {
return true;
}
element = element.parentElement;
} while (element !== null);
return false;
}
function validSwipe(coords) {
if (! startCoords || ! valid){
return false;
}
var deltaY = (coords.y - startCoords.y) * direction;
var deltaX = (coords.x - startCoords.x) * direction;
if(axis === null) { // tap
return Math.abs(deltaY) < MIN_DISTANCE &&
Math.abs(deltaX) < MIN_DISTANCE;
}
else if(axis === false) { // horizontal swipe
return Math.abs(deltaY) < MAX_OTHER_AXIS_DISTANCE &&
deltaX > 0 &&
deltaX > MIN_DISTANCE &&
Math.abs(deltaY) / deltaX < MAX_RATIO;
}
else { // vertical swipe
return Math.abs(deltaX) < MAX_OTHER_AXIS_DISTANCE &&
deltaY > 0 &&
deltaY > MIN_DISTANCE &&
Math.abs(deltaX) / deltaY < MAX_RATIO;
}
}
var pointerTypes = ['touch'];
if (!angular.isDefined(attr['ngSwipeDisableMouse'])) {
pointerTypes.push('mouse');
}
swipe.bind(element, {
'start': function(coords, event) {
if (axis && !checkOverride(event.target, 'noPreventDefault')) {
event.preventDefault();
}
startCoords = coords;
valid = !checkOverride(event.target, 'noStartDrag');
},
'cancel': function() {
valid = false;
},
'end': function(coords, event) {
if (validSwipe(coords)) {
scope.$apply(function() {
element.triggerHandler(eventName);
swipeHandler(scope, { $event: event });
});
}
}
}, pointerTypes);
};
}]);
}
// avoid conflicts with ngTouch module
try {
angular.module('ngTouch');
} catch(err) {
makeSwipeDirective('ngSwipeLeft', -1, false, 'swipeleft');
makeSwipeDirective('ngSwipeRight', 1, false, 'swiperight');
}
// left is negative x-coordinate, right is positive
makeSwipeDirective('ngSwipeUp', -1, true, 'swipeup');
makeSwipeDirective('ngSwipeDown', 1, true, 'swipedown');
makeSwipeDirective('ngTap', 1, null, 'tap');
})(window, window.angular);