-
Notifications
You must be signed in to change notification settings - Fork 11
/
tap.js
104 lines (90 loc) · 3.07 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
/*
*
* Find more about this plugin by visiting
* http://alxgbsn.co.uk/
*
* Copyright (c) 2013 Alex Gibson, http://alxgbsn.co.uk/
* Released under MIT license
*
*/
/*global window: false, document: false */
(function (window, document) {
'use strict';
function Tap(el) {
this.element = typeof el === 'object' ? el : document.getElementById(el);
this.moved = false; //flags if the finger has moved
this.startX = 0; //starting x coordinate
this.startY = 0; //starting y coordinate
this.hasTouchEventOccured = false; //flag touch event
this.element.addEventListener('touchstart', this, false);
this.element.addEventListener('mousedown', this, false);
}
//start
Tap.prototype.start = function (e) {
if (e.type === 'touchstart') {
this.hasTouchEventOccured = true;
}
this.moved = false;
this.startX = e.type === 'touchstart' ? e.touches[0].clientX : e.clientX;
this.startY = e.type === 'touchstart' ? e.touches[0].clientY : e.clientY;
this.element.addEventListener('touchmove', this, false);
this.element.addEventListener('touchend', this, false);
this.element.addEventListener('touchcancel', this, false);
this.element.addEventListener('mousemove', this, false);
this.element.addEventListener('mouseup', this, false);
};
//move
Tap.prototype.move = function (e) {
var x = e.type === 'touchmove' ? e.touches[0].clientX : e.clientX,
y = e.type === 'touchmove' ? e.touches[0].clientY : e.clientY;
//if finger moves more than 10px flag to cancel
if (Math.abs(x - this.startX) > 10 || Math.abs(y - this.startY) > 10) {
this.moved = true;
}
};
//end
Tap.prototype.end = function (e) {
var evt;
if (this.hasTouchEventOccured && e.type === 'mouseup') {
e.preventDefault();
e.stopPropagation();
this.hasTouchEventOccured = false;
return;
}
if (!this.moved) {
evt = document.createEvent('Event');
evt.initEvent('tap', true, true);
e.target.dispatchEvent(evt);
}
this.element.removeEventListener('touchmove', this, false);
this.element.removeEventListener('touchend', this, false);
this.element.removeEventListener('touchcancel', this, false);
this.element.removeEventListener('mousemove', this, false);
this.element.removeEventListener('mouseup', this, false);
};
//touchcancel
Tap.prototype.cancel = function (e) {
//reset state
this.moved = false;
this.startX = 0;
this.startY = 0;
this.element.removeEventListener('touchmove', this, false);
this.element.removeEventListener('touchend', this, false);
this.element.removeEventListener('touchcancel', this, false);
this.element.removeEventListener('mousemove', this, false);
this.element.removeEventListener('mouseup', this, false);
};
Tap.prototype.handleEvent = function (e) {
switch (e.type) {
case 'touchstart': this.start(e); break;
case 'touchmove': this.move(e); break;
case 'touchend': this.end(e); break;
case 'touchcancel': this.cancel(e); break;
case 'mousedown': this.start(e); break;
case 'mousemove': this.move(e); break;
case 'mouseup': this.end(e); break;
}
};
//public function
window.Tap = Tap;
}(window, document));