2
2
3
3
/**
4
4
* @ngdoc directive
5
- * @name ngMobile.directive:ngTap
5
+ * @name ngMobile.directive:ngClick
6
6
*
7
7
* @description
8
- * Specify custom behavior when element is tapped on a touchscreen device.
9
- * A tap is a brief, down-and-up touch without much motion.
8
+ * A more powerful replacement for the default ngClick designed to be used on touchscreen
9
+ * devices. Most mobile browsers wait about 300ms after a tap-and-release before sending
10
+ * the click event. This version handles them immediately, and then prevents the
11
+ * following click event from propagating.
12
+ *
13
+ * This directive can fall back to using an ordinary click event, and so works on desktop
14
+ * browsers as well as mobile.
15
+ *
16
+ * This directive also sets the CSS class `ng-click-active` while the element is being held
17
+ * down (by a mouse click or touch) so you can restyle the depressed element if you wish.
10
18
*
11
19
* @element ANY
12
20
* @param {expression } ngClick {@link guide/expression Expression } to evaluate
15
23
* @example
16
24
<doc:example>
17
25
<doc:source>
18
- <button ng-tap ="count = count + 1" ng-init="count=0">
26
+ <button ng-click ="count = count + 1" ng-init="count=0">
19
27
Increment
20
28
</button>
21
29
count: {{ count }}
@@ -37,6 +45,8 @@ ngMobile.directive('ngClick', ['$parse', '$timeout', '$rootElement',
37
45
var MOVE_TOLERANCE = 12 ; // 12px seems to work in most mobile browsers.
38
46
var PREVENT_DURATION = 2500 ; // 2.5 seconds maximum from preventGhostClick call to click
39
47
var CLICKBUSTER_THRESHOLD = 25 ; // 25 pixels in any dimension is the limit for busting clicks.
48
+
49
+ var ACTIVE_CLASS_NAME = 'ng-click-active' ;
40
50
var lastPreventedTime ;
41
51
var touchCoordinates ;
42
52
@@ -172,6 +182,7 @@ ngMobile.directive('ngClick', ['$parse', '$timeout', '$rootElement',
172
182
173
183
function resetState ( ) {
174
184
tapping = false ;
185
+ element . removeClass ( ACTIVE_CLASS_NAME ) ;
175
186
}
176
187
177
188
element . bind ( 'touchstart' , function ( event ) {
@@ -182,6 +193,8 @@ ngMobile.directive('ngClick', ['$parse', '$timeout', '$rootElement',
182
193
tapElement = tapElement . parentNode ;
183
194
}
184
195
196
+ element . addClass ( ACTIVE_CLASS_NAME ) ;
197
+
185
198
startTime = Date . now ( ) ;
186
199
187
200
var touches = event . touches && event . touches . length ? event . touches : [ event ] ;
@@ -224,7 +237,8 @@ ngMobile.directive('ngClick', ['$parse', '$timeout', '$rootElement',
224
237
clickHandler ( scope , { $event : event } ) ;
225
238
} ) ;
226
239
}
227
- tapping = false ;
240
+
241
+ resetState ( ) ;
228
242
} ) ;
229
243
230
244
// Hack for iOS Safari's benefit. It goes searching for onclick handlers and is liable to click
@@ -239,6 +253,15 @@ ngMobile.directive('ngClick', ['$parse', '$timeout', '$rootElement',
239
253
clickHandler ( scope , { $event : event } ) ;
240
254
} ) ;
241
255
} ) ;
256
+
257
+ element . bind ( 'mousedown' , function ( event ) {
258
+ element . addClass ( ACTIVE_CLASS_NAME ) ;
259
+ } ) ;
260
+
261
+ element . bind ( 'mousemove mouseup' , function ( event ) {
262
+ element . removeClass ( ACTIVE_CLASS_NAME ) ;
263
+ } ) ;
264
+
242
265
} ;
243
266
} ] ) ;
244
267
0 commit comments