-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRactive-events-swipe.js
101 lines (90 loc) · 3 KB
/
Ractive-events-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
(function (global, factory) {
'use strict';
// Common JS (i.e. browserify) environment
if (typeof module !== 'undefined' && module.exports && typeof require === 'function') {
factory(require('Ractive'));
}
// AMD?
else if (typeof define === 'function' && define.amd) {
define(['Ractive'], factory);
}
// browser global
else if (global.Ractive) {
factory(global.Ractive);
} else {
throw new Error('Could not find Ractive! It must be loaded before the Ractive-events-swap plugin');
}
}(typeof window !== 'undefined' ? window : this, function (Ractive) {
'use strict';
var makeSwipeDefinition = function (direction){
return function (node, fire) {
var chinna = function (event) {
var sx, sy, ex, ey, currentTarget;
event.preventDefault();
if (event.touches.length !== 1) { // no.of.fingers != 1
cancel();
return;
}
var touch = event.touches[0];
sx = touch.pageX;
sy = touch.pageY;
currentTarget = this;
function move(event) {
event.preventDefault();
if (event.touches.length !== 1) {
console.log('mutiple fingers')
cancel();
}
var touch = event.touches[0];
ex = touch.pageX;
ey = touch.pageY;
};
function end(event) {
event.preventDefault();
var dx = ex - sx;
var dy = ey - sy;
var ax = Math.abs(dx);
var ay = Math.abs(dy);
if (Math.max(ax, ay) > 20) {
var swipeDirection = ax > ay ? (dx < 0 ? 'swipeleft' : 'swiperight') : (dy < 0 ? 'swipeup' : 'swipedown')
if (swipeDirection == direction) {
fire({
node: currentTarget,
original: event,
direction: swipeDirection,
});
}
}
cancel();
}
function cancel() {
node.removeEventListener('touchend', end, false);
window.removeEventListener('touchend', end, false);
node.removeEventListener('touchmove', move, false);
window.removeEventListener('touchmove', move, false);
node.removeEventListener('touchcancel', cancel, false);
window.removeEventListener('touchcancel', cancel, false);
};
node.addEventListener('touchmove', move, false);
window.addEventListener('touchmove', move, false);
node.addEventListener('touchend', end, false);
window.addEventListener('touchend', end, false);
node.addEventListener('touchcancel', cancel, false);
window.addEventListener('touchcancel', cancel, false);
}
node.addEventListener('touchstart', chinna, false);
window.addEventListener('touchstart', chinna, false);
return {
teardown: function () {
node.removeEventListener('touchstart', chinna, false);
window.removeEventListener('touchstart', chinna, false);
}
};
}
}
var events = Ractive.events;
events.swipeleft = makeSwipeDefinition('swipeleft');
events.swiperight = makeSwipeDefinition('swiperight');
events.swipeup = makeSwipeDefinition('swipeup');
events.swipedown = makeSwipeDefinition('swipedown');
}));