44* Dual licensed under the MIT or GPL Version 2 licenses.
55* http://jquery.org/license
66*/
7+
8+ // This plugin is an experient for abstracting away the touch and mouse
9+ // events so that developers don't have to worry about which method of input
10+ // the device their document is loaded on supports.
11+ //
12+ // The idea here is to allow the developer to register listeners for the
13+ // basic mouse events, such as mousedown, mousemove, mouseup, and click,
14+ // and the plugin will take care of registering the correct listeners
15+ // behind the scenes to invoke the listener at the fastest possible time
16+ // for that device, while still retaining the order of event firing in
17+ // the traditional mouse environment, should multiple handlers be registered
18+ // on the same element for different events.
19+ //
20+ // The current version simply adds mBind and mUnbind to the $.fn space,
21+ // but we're considering other methods for making this easier. One alternative
22+ // would be to allow users to use virtual mouse event names, such as
23+ // "vmousedown", "vmouseup", etc, to trigger the traditional jQuery special/custom
24+ // event api, which would then trigger this same code.
25+
726( function ( $ , window , document , undefined ) {
827
928var nextSequencerID = 0 ;
1029
30+ var eventToSequencerName = {
31+ touchstart : "mmousedown" ,
32+ touchmove : "mmousemove" ,
33+ touchend : "mmouseup" ,
34+ mousedown : "mmousedown" ,
35+ mousemove : "mmousemove" ,
36+ mouseup : "mmouseup" ,
37+ mouseover : "mmouseover" ,
38+ mouseout : "mmouseout" ,
39+ click : "mclick"
40+ } ;
41+
42+ var downEvents = "touchstart mousedown" ;
43+ var upEvents = "touchend mouseup" ;
44+ var moveEvents = "touchmove mousemove" ;
45+
1146function MouseEventSequencer ( )
1247{
1348 this . seqID = "MSE-" + nextSequencerID ++ ;
1449 this . handlerDict = { } ;
1550 this . mode = null ;
51+ this . downFunc = null ;
1652}
1753
1854$ . extend ( MouseEventSequencer . prototype , {
1955 bind : function ( ele , eventType , handler )
2056 {
21- var $ele = $ ( ele ) ,
22- hArray = handlerDict [ eventType ] ;
23- if ( ! hArray )
24- hArray = handlerDict [ eventType ] = [ ] ;
57+ var self = this ,
58+ seqName = eventToSequencerName [ eventType ] ,
59+ $ele = $ ( ele ) ,
60+ hArray = handlerDict [ seqName ] ;
61+
62+ if ( ! hArray ) {
63+ hArray = handlerDict [ seqName ] = [ ] ;
64+ }
2565 hArray . push ( handler ) ;
2666
27- // We always register for touchstart and mousedown because
28- // we may need to synthesize some events when dealing with
29- // touch devices.
67+ if ( ! this . downFunc ) {
68+ // We always register for touchstart and mousedown because
69+ // we may need to synthesize some events when dealing with
70+ // touch devices.
3071
31- $ele . bind ( "touchstart." + this . seqID + " mousedown." + this . seqID , function ( e , d ) { self . handleDown ( e , d ) ; } ) ;
72+ this . downFunc = function ( e , d ) { self . handleDown ( e , d ) ; }
73+ $ele . bind ( "touchstart." + this . seqID + " mousedown." + this . seqID , this . downFunc ) ;
74+ }
3275 } ,
3376
3477 unbind : function ( ele , eventType , handler )
@@ -50,8 +93,10 @@ $.extend(MouseEventSequencer.prototype, {
5093
5194 handleDown : function ( event , data )
5295 {
53- this . mode = isTouchEvent ( event . type ) ? "touch" : "mouse" ;
54- this . trigger ( "mmouseover" ) ;
96+ this . mode = this . getModeFromType ( event . type ) ;
97+ if ( this . mode == "touch" ) {
98+ this . trigger ( "mmouseover" ) ;
99+ }
55100 this . trigger ( "mmousedown" ) ;
56101 } ,
57102
@@ -79,6 +124,11 @@ $.extend(MouseEventSequencer.prototype, {
79124 {
80125 if ( this . mode != "touch" )
81126 this . trigger ( "mmouseout" ) ;
127+ } ,
128+
129+ getModeFromType : function ( eventType )
130+ {
131+ return isTouchEvent ( eventType ) ? "touch" : "mouse" ;
82132 }
83133} ) ;
84134
@@ -94,6 +144,7 @@ function isTouchEvent(eventType)
94144 return eventType . search ( / ^ t o u c h ( s t a r t | e n d | m o v e | c a n c e l ) $ / ) != - 1 ;
95145}
96146
147+
97148$ . extend ( $ . fn , {
98149 mBind : function ( eventType , handler )
99150 {
0 commit comments