12
12
*/
13
13
( function ( $ ) {
14
14
15
+ var iPhone = navigator . userAgent . indexOf ( 'iPhone' ) != - 1 ;
16
+
15
17
$ . widget ( "ui.mouse" , {
16
18
options : {
17
19
cancel : ':input,option' ,
18
20
distance : 1 ,
19
21
delay : 0
20
22
} ,
23
+
24
+ _eventNames : {
25
+ start : 'mousedown' ,
26
+ drag : 'mousemove' ,
27
+ stop : 'mouseup'
28
+ } ,
29
+
30
+ _iPhoneEvent : function ( event ) {
31
+
32
+ if ( event . type == 'touchend' && this . _mouseStarted )
33
+ event = this . _prevEvent ;
34
+
35
+ var t = event . originalEvent . touches ;
36
+ return ! iPhone || ( t . length == 1 ? ( this . _prevEvent = $ . extend ( event , {
37
+ target : t [ 0 ] . target ,
38
+ pageX : t [ 0 ] . pageX ,
39
+ pageY : t [ 0 ] . pageY
40
+ } ) ) : false ) ;
41
+
42
+ } ,
43
+
21
44
_mouseInit : function ( ) {
22
45
var self = this ;
23
46
47
+ iPhone && ( this . _eventNames = {
48
+ start : 'touchstart' ,
49
+ drag : 'touchmove' ,
50
+ stop : 'touchend'
51
+ } ) ;
52
+
24
53
this . element
25
- . bind ( 'mousedown .'+ this . widgetName , function ( event ) {
26
- return self . _mouseDown ( event ) ;
54
+ . bind ( this . _eventNames . start + ' .'+ this . widgetName , function ( event ) {
55
+ return self . _iPhoneEvent ( event ) && self . _mouseDown ( event ) ;
27
56
} )
28
57
. bind ( 'click.' + this . widgetName , function ( event ) {
29
58
if ( self . _preventClickEvent ) {
@@ -54,7 +83,7 @@ $.widget("ui.mouse", {
54
83
this . _mouseDownEvent = event ;
55
84
56
85
var self = this ,
57
- btnIsLeft = ( event . which == 1 ) ,
86
+ btnIsLeft = ( event . which == 1 || iPhone ) ,
58
87
elIsCancel = ( typeof this . options . cancel == "string" ? $ ( event . target ) . parents ( ) . add ( event . target ) . filter ( this . options . cancel ) . length : false ) ;
59
88
if ( ! btnIsLeft || elIsCancel || ! this . _mouseCapture ( event ) ) {
60
89
return true ;
@@ -77,19 +106,19 @@ $.widget("ui.mouse", {
77
106
78
107
// these delegates are required to keep context
79
108
this . _mouseMoveDelegate = function ( event ) {
80
- return self . _mouseMove ( event ) ;
109
+ return self . _iPhoneEvent ( event ) && self . _mouseMove ( event ) ;
81
110
} ;
82
111
this . _mouseUpDelegate = function ( event ) {
83
- return self . _mouseUp ( event ) ;
112
+ return self . _iPhoneEvent ( event ) && self . _mouseUp ( event ) ;
84
113
} ;
85
114
$ ( document )
86
- . bind ( 'mousemove .'+ this . widgetName , this . _mouseMoveDelegate )
87
- . bind ( 'mouseup .'+ this . widgetName , this . _mouseUpDelegate ) ;
115
+ . bind ( this . _eventNames . drag + ' .'+ this . widgetName , this . _mouseMoveDelegate )
116
+ . bind ( this . _eventNames . stop + ' .'+ this . widgetName , this . _mouseUpDelegate ) ;
88
117
89
118
// preventDefault() is used to prevent the selection of text here -
90
119
// however, in Safari, this causes select boxes not to be selectable
91
120
// anymore, so this fix is needed
92
- ( $ . browser . safari || event . preventDefault ( ) ) ;
121
+ ( ( $ . browser . safari && ! iPhone ) || event . preventDefault ( ) ) ;
93
122
94
123
event . originalEvent . mouseHandled = true ;
95
124
return true ;
@@ -117,8 +146,8 @@ $.widget("ui.mouse", {
117
146
118
147
_mouseUp : function ( event ) {
119
148
$ ( document )
120
- . unbind ( 'mousemove .'+ this . widgetName , this . _mouseMoveDelegate )
121
- . unbind ( 'mouseup .'+ this . widgetName , this . _mouseUpDelegate ) ;
149
+ . unbind ( this . _eventNames . drag + ' .'+ this . widgetName , this . _mouseMoveDelegate )
150
+ . unbind ( this . _eventNames . stop + ' .'+ this . widgetName , this . _mouseUpDelegate ) ;
122
151
123
152
if ( this . _mouseStarted ) {
124
153
this . _mouseStarted = false ;
0 commit comments