This repository has been archived by the owner on Jan 30, 2019. It is now read-only.
/
swipeMultiTouch.jquery.js
94 lines (77 loc) · 3.05 KB
/
swipeMultiTouch.jquery.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
(function( $ ){
var SwipeMultiTouch = {
'thresholdSwipeMultiTouch': 700,
'default_settings': {
'eventTriggered': {
'swipe': false,
'multiTouch': false,
},
'started': 0,
'number_touches': 0
},
'elements': {}
};
$.fn.swipeMultiTouch = function( fn ){
var $target = this;
var __start = function( event ){
var $target = $(event.target);
var now = ((new Date()).getTime());
var swipeMultiTouchIdentifier = $target.data('swipeMultiTouchIdentifier');
if( typeof swipeMultiTouchIdentifier === 'undefined' ){
swipeMultiTouchIdentifier = now;
$target.data('swipeMultiTouchIdentifier', swipeMultiTouchIdentifier );
SwipeMultiTouch.elements[ swipeMultiTouchIdentifier ] = KUtils.objClone( SwipeMultiTouch.default_settings );
}
var swipeMultiTouch = SwipeMultiTouch.elements[ swipeMultiTouchIdentifier ];
if( swipeMultiTouch.number_touches == 0 )
SwipeMultiTouch.elements[ swipeMultiTouchIdentifier ].started = now;
SwipeMultiTouch.elements[ swipeMultiTouchIdentifier ].number_touches++;
$target.text( 'Dita in pressione: '+ SwipeMultiTouch.elements[ swipeMultiTouchIdentifier ].number_touches );
// Dopo la soglia devo resettare il numero di touches
setTimeout(function(){
__resetNumberTouches( swipeMultiTouchIdentifier );
}, SwipeMultiTouch.thresholdSwipeMultiTouch);
};
var __end = function( event ){
var $target = $(event.target);
var swipeMultiTouchIdentifier = $target.data('swipeMultiTouchIdentifier');
__resetNumberTouches( swipeMultiTouchIdentifier );
};
var __resetNumberTouches = function( swipeMultiTouchIdentifier ){
SwipeMultiTouch.elements[ swipeMultiTouchIdentifier ].number_touches = 0;
//$target.text( 'Dita in pressione: '+ SwipeMultiTouch.elements[ swipeMultiTouchIdentifier ].number_touches );
};
$(document).on({
'mousedown': function( event ){
__start( event );
},
'mouseup': function( event ){
__end( event );
},
'touchstart': function( event ){
__start( event );
},
'touchend': function( event ){
__end( event );
}
}, $target );
$target.swipe(function( event ){
var $target = $(event.target);
var swipeMultiTouchIdentifier = $target.data('swipeMultiTouchIdentifier');
var swipeMultiTouch = SwipeMultiTouch.elements[ swipeMultiTouchIdentifier ];
var Timestamps = {
'multiTouch': swipeMultiTouch.started,
'swipe': event.timeStamp
};
console.log( 'swipe!', Timestamps );
// Se l'intervallo dall'inizio dell'evento "multiTouch" all'evento dello "swipe" è inferiore della soglia allora l'evento "swipeMultiTouch" è avvenuto! :D
if( (swipeMultiTouch.number_touches>1) && (Timestamps.swipe - Timestamps.multiTouch) <= SwipeMultiTouch.thresholdSwipeMultiTouch ){
event.type = event.handleObj.origType = event.handleObj.type = 'swipeMultiTouch';
event.originalEvent = undefined;
event.number_touches = swipeMultiTouch.number_touches;
fn( event );
}
SwipeMultiTouch.elements[ swipeMultiTouchIdentifier ] = KUtils.objClone( SwipeMultiTouch.default_settings );
})
};
})( jQuery );