1212 */
1313( function ( $ ) {
1414
15+ var iPhone = navigator . userAgent . indexOf ( 'iPhone' ) != - 1 ;
16+
1517$ . widget ( "ui.mouse" , {
1618 options : {
1719 cancel : ':input,option' ,
1820 distance : 1 ,
1921 delay : 0
2022 } ,
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+
2144 _mouseInit : function ( ) {
2245 var self = this ;
2346
47+ iPhone && ( this . _eventNames = {
48+ start : 'touchstart' ,
49+ drag : 'touchmove' ,
50+ stop : 'touchend'
51+ } ) ;
52+
2453 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 ) ;
2756 } )
2857 . bind ( 'click.' + this . widgetName , function ( event ) {
2958 if ( self . _preventClickEvent ) {
@@ -54,7 +83,7 @@ $.widget("ui.mouse", {
5483 this . _mouseDownEvent = event ;
5584
5685 var self = this ,
57- btnIsLeft = ( event . which == 1 ) ,
86+ btnIsLeft = ( event . which == 1 || iPhone ) ,
5887 elIsCancel = ( typeof this . options . cancel == "string" ? $ ( event . target ) . parents ( ) . add ( event . target ) . filter ( this . options . cancel ) . length : false ) ;
5988 if ( ! btnIsLeft || elIsCancel || ! this . _mouseCapture ( event ) ) {
6089 return true ;
@@ -77,19 +106,19 @@ $.widget("ui.mouse", {
77106
78107 // these delegates are required to keep context
79108 this . _mouseMoveDelegate = function ( event ) {
80- return self . _mouseMove ( event ) ;
109+ return self . _iPhoneEvent ( event ) && self . _mouseMove ( event ) ;
81110 } ;
82111 this . _mouseUpDelegate = function ( event ) {
83- return self . _mouseUp ( event ) ;
112+ return self . _iPhoneEvent ( event ) && self . _mouseUp ( event ) ;
84113 } ;
85114 $ ( 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 ) ;
88117
89118 // preventDefault() is used to prevent the selection of text here -
90119 // however, in Safari, this causes select boxes not to be selectable
91120 // anymore, so this fix is needed
92- ( $ . browser . safari || event . preventDefault ( ) ) ;
121+ ( ( $ . browser . safari && ! iPhone ) || event . preventDefault ( ) ) ;
93122
94123 event . originalEvent . mouseHandled = true ;
95124 return true ;
@@ -117,8 +146,8 @@ $.widget("ui.mouse", {
117146
118147 _mouseUp : function ( event ) {
119148 $ ( 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 ) ;
122151
123152 if ( this . _mouseStarted ) {
124153 this . _mouseStarted = false ;
0 commit comments