-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (48 loc) · 1.26 KB
/
index.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
var events = require('events');
module.exports = Hold;
/**
* Hold Constructor
* @param {DOM} el
* @param {Function} fn
*/
function Hold(el, fn){
if (!(this instanceof Hold)) return new Hold(el, fn);
this.el = el;
this.fn = fn || function() {};
this.events = events(this.el, this);
this.events.bind('mousedown', 'ontouchstart');
this.events.bind('mouseup', 'ontouchend');
this.events.bind('touchstart');
this.events.bind('touchend');
}
// consider making 'touchleave' its own module.
Hold.prototype.bindLeave = function(){
this.events.bind('mouseout', 'ontouchend');
this.events.bind('touchmove');
};
Hold.prototype.unbindLeave = function(){
this.events.unbind('mouseleave', 'ontouchend');
this.events.unbind('touchmove');
}
Hold.prototype.ontouchmove = function(e){
var touch = e.touches[0];
var element = document.elementFromPoint(touch.pageX, touch.pageY);
if (element !== this.el) {
this.ontouchend();
}
};
Hold.prototype.ontouchstart = function(e){
e.preventDefault();
this.bindLeave();
var self = this;
this.timeout = setTimeout(function(){
self.fn(e);
}, 800);
};
Hold.prototype.ontouchend = function(e){
clearTimeout(this.timeout);
this.unbindLeave();
};
Hold.prototype.unbind = function(){
this.events.unbind();
};