-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjquery-updownbutton.js
130 lines (105 loc) · 3.44 KB
/
jquery-updownbutton.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
124
125
126
127
128
129
130
(function( $ ){
function _isInBounds(touch, $elem) {
var offset = $elem.offset(),
width = $elem.outerWidth(),
height = $elem.outerHeight(),
left = offset.left,
right = left + width,
top = offset.top,
bottom = top + height,
touchX = touch.pageX,
touchY = touch.pageY;
return (touchX > left && touchX < right && touchY > top && touchY < bottom);
}
var BUTTON_DOWN = true;
var BUTTON_UP = false;
function _setButtonState($button, down) {
if (down) {
$button.find("img.up").removeClass("active");
$button.find("img.down").addClass("active");
} else {
$button.find("img.down").removeClass("active");
$button.find("img.up").addClass("active");
}
}
var methods = {
init : function( props ) {
var options = $.extend({
}, props || {});
return this.each(function() {
var $doc = $(document);
var $button = $(this);
var isTouched = false;
var hasSentClick = false;
$button.on("click", function(e) {
//trace("click sent")
hasSentClick = true;
return false;
});
$button.on("mousedown touchstart", function(e) {
//trace("mousedown: " + e.type + " " + e.target.nodeName + " " + e.target.getAttribute("class") + " " + e.target.getAttribute("src"));
e.preventDefault();
hasSentClick = false;
_setButtonState($button, BUTTON_DOWN);
isTouched = true;
$button.on("mouseover.rem", function(e) {
_setButtonState($button, BUTTON_DOWN);
isTouched = true;
});
$button.on("mouseout.rem touchleave.rem", function(e) {
_setButtonState($button, BUTTON_UP);
isTouched = false;
});
$doc.on("mousemove.rem touchmove.rem", function(e) {
e = (e.originalEvent.touches) ? e.originalEvent.touches[0] : e;
// check if the finger is still inside
isTouched = _isInBounds(e, $button);
if (isTouched) {
_setButtonState($button, BUTTON_DOWN);
} else {
_setButtonState($button, BUTTON_UP);
}
});
$doc.on("mouseup.rem", function(e) {
//trace("mouseup.rem: " + e.type + " " + e.target.nodeName + " " + e.target.getAttribute("class") + " " + e.target.getAttribute("src"));
_setButtonState($button, BUTTON_UP);
$button.off(".rem");
$doc.off(".rem");
setTimeout( function() {
if (isTouched && !hasSentClick) {
trace("delayed click");
$button.click();
}
}, 10);
return false;
});
$doc.on("touchend.rem", function(e) {
//trace("touchend.rem: " + e.type);
_setButtonState($button, BUTTON_UP);
$button.off(".rem");
$doc.off(".rem");
// Generate event if mouse or touch is inside the component
if (isTouched && e.type != "mouseup") {
$button.click();
// guarantee that just one event will be sent, even if a browser handles both mouseup and touchend
isTouched = false;
}
});
});
});
},
destroy : function( ) {
return this.each(function(){
});
}
};
$.fn.updownButton = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.updownButton' );
}
};
})( jQuery );