This repository has been archived by the owner on Feb 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tripleclick.js
69 lines (58 loc) · 1.98 KB
/
tripleclick.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
// @author Rich Adams <rich@richadams.me>
// Implements a triple-click event. Click (or touch) three times within 1s on the element to trigger.
;(function($)
{
// Default options
var defaults = {
threshold: 1000, // ms
}
function tripleHandler(event)
{
var $elem = jQuery(this);
// Merge the defaults and any user defined settings.
settings = jQuery.extend({}, defaults, event.data);
// Get current values, or 0 if they don't yet exist.
var clicks = $elem.data("triclick_clicks") || 0;
var start = $elem.data("triclick_start") || 0;
// If first click, register start time.
if (clicks === 0) { start = event.timeStamp; }
// If we have a start time, check it's within limit
if (start != 0
&& event.timeStamp > start + settings.threshold)
{
// Tri-click failed, took too long.
clicks = 0;
start = event.timeStamp;
}
// Increment counter, and do finish action.
clicks += 1;
if (clicks === 3)
{
clicks = 0;
start = 0;
event.type = "tripleclick";
// Let jQuery handle the triggering of "tripleclick" event handlers
if (jQuery.event.handle === undefined) {
jQuery.event.dispatch.apply(this, arguments);
}
else {
// for jQuery before 1.9
jQuery.event.handle.apply(this, arguments);
}
}
// Update object data
$elem.data("triclick_clicks", clicks);
$elem.data("triclick_start", start);
}
var tripleclick = $.event.special.tripleclick =
{
setup: function(data, namespaces)
{
$(this).bind("touchstart click.triple", data, tripleHandler);
},
teardown: function(namespaces)
{
$(this).unbind("touchstart click.triple", tripleHandler);
}
};
})(jQuery);