-
Notifications
You must be signed in to change notification settings - Fork 1
/
fast_click.js
101 lines (78 loc) · 2.11 KB
/
fast_click.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
/**
* @author Joe Gaudet - joe@learndot.com
* @copyright ©2012 Matygo Educational Incorporated operating as Learndot
* @license Licensed under MIT license (see LICENSE.md)
*/
app.directive('fastClick', function ($parse, Modernizr) {
'use strict';
return {
restrict: 'A',
link: function (scope, element, attrs) {
/**
* Parsed function from the directive
* @type {*}
*/
var fn = $parse(attrs.fastClick),
/**
* Track the start points
*/
startX,
startY,
/**
* Whether or not we have for some reason
* cancelled the event.
*/
canceled,
/**
* Our click function
*/
clickFunction = function (event) {
if (!canceled) {
scope.$apply(function () {
fn(scope, {$event: event});
});
}
};
/**
* If we are actually on a touch device lets
* setup our fast clicks
*/
if (Modernizr.touch) {
element.on('touchstart', function (event) {
event.stopPropagation();
var touches = event.originalEvent.touches;
startX = touches[0].clientX;
startY = touches[0].clientY;
canceled = false;
});
element.on('touchend', function (event) {
event.stopPropagation();
clickFunction();
});
element.on('touchmove', function (event) {
var touches = event.originalEvent.touches;
// handles the case where we've swiped on a button
if (Math.abs(touches[0].clientX - startX) > 10 ||
Math.abs(touches[0].clientY - startY) > 10) {
canceled = true;
}
});
}
/**
* If we are not on a touch enabled device lets bind
* the action to click
*/
if (!Modernizr.touch) {
element.on('click', function (event) {
clickFunction(event);
});
}
}
};
});
app.provider('Modernizr', function () {
'use strict';
this.$get = function () {
return Modernizr || {};
};
});