-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtap.js
123 lines (96 loc) · 3.39 KB
/
tap.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
(function( window ) {
var Tap = {};
var utils = {};
utils.attachEvent = function( element, eventName, callback ) {
return element.addEventListener( eventName, callback, false );
};
utils.fireFakeEvent = function( e, eventName ) {
return e.target.dispatchEvent( utils.createEvent( eventName ) );
};
utils.createEvent = function( name ) {
var evnt = window.document.createEvent( 'HTMLEvents' );
evnt.initEvent( name, true, true );
evnt.eventName = name;
return evnt;
};
utils.getRealEvent = function( e ) {
return e.originalEvent && e.originalEvent.touches && e.originalEvent.touches.length ? e.originalEvent.touches[ 0 ] : e;
};
var eventMatrix = [{
// Touchable devices
test: ( 'propertyIsEnumerable' in window || 'hasOwnProperty' in document ) && ( window.propertyIsEnumerable( 'ontouchstart' ) || document.hasOwnProperty( 'ontouchstart') ),
events: {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
}
}, {
// IE10
test: window.navigator.msPointerEnabled,
events: {
start: 'MSPointerDown',
move: 'MSPointerMove',
end: 'MSPointerUp'
}
}, {
// Modern device agnostic web
test: window.navigator.pointerEnabled,
events: {
start: 'pointerdown',
move: 'pointermove',
end: 'pointerup'
}
}];
Tap.options = {
eventName: 'tap',
fingerMaxOffset: 11
};
var attachDeviceEvent, init, handlers, deviceEvents,
coords = {};
attachDeviceEvent = function( eventName ) {
return utils.attachEvent( document.body, deviceEvents[ eventName ], handlers[ eventName ] );
};
handlers = {
start: function( e ) {
e = utils.getRealEvent( e );
coords.start = [ e.pageX, e.pageY ];
coords.offset = [ 0, 0 ];
},
move: function( e ) {
if (!coords['start'] && !coords['move']) return false;
e = utils.getRealEvent( e );
coords.move = [ e.pageX, e.pageY ];
coords.offset = [
Math.abs( coords.move[ 0 ] - coords.start[ 0 ] ),
Math.abs( coords.move[ 1 ] - coords.start[ 1 ] )
];
},
end: function( e ) {
e = utils.getRealEvent( e );
if ( coords.offset[ 0 ] < Tap.options.fingerMaxOffset && coords.offset[ 1 ] < Tap.options.fingerMaxOffset && !utils.fireFakeEvent( e, Tap.options.eventName ) ) {
e.preventDefault();
}
coords = {};
},
click: function( e ) {
if ( !utils.fireFakeEvent( e, Tap.options.eventName ) ) {
return e.preventDefault();
}
}
};
init = function() {
var i = eventMatrix.length;
while ( i-- ) {
if ( eventMatrix[ i ].test ) {
deviceEvents = eventMatrix[ i ].events;
attachDeviceEvent( 'start' );
attachDeviceEvent( 'move' );
attachDeviceEvent( 'end' );
return false;
}
}
return utils.attachEvent( document.body, 'click', handlers[ 'click' ] );
};
utils.attachEvent( window, 'load', init );
window.Tap = Tap;
})( window );