|
| 1 | +(function( $ ) { |
| 2 | +var eventName, lastTouch, |
| 3 | + POINTER_TYPE_UNAVAILABLE = "", |
| 4 | + POINTER_TYPE_TOUCH = "touch", |
| 5 | + POINTER_TYPE_PEN = "pen", |
| 6 | + POINTER_TYPE_MOUSE = "mouse", |
| 7 | + eventMap = (function() { |
| 8 | + if ( "MSPointerEvent" in window ) { |
| 9 | + return { |
| 10 | + pointerdown: "MSPointerDown", |
| 11 | + pointerup: "MSPointerUp", |
| 12 | + pointermove: "MSPointerMove MSPointerHover", |
| 13 | + pointercancel: "MSPointerCancel", |
| 14 | + pointerover: "MSPointerOver", |
| 15 | + pointerout: "MSPointerOut" |
| 16 | + }; |
| 17 | + } |
| 18 | + |
| 19 | + return { |
| 20 | + pointerdown: "touchstart mousedown", |
| 21 | + pointerup: "touchend mouseup", |
| 22 | + pointermove: "touchmove mousemove", |
| 23 | + pointercancel: "touchcancel", |
| 24 | + pointerover: "mouseover", |
| 25 | + pointerout: "mouseout" |
| 26 | + }; |
| 27 | + })(); |
| 28 | + |
| 29 | +function processEvent( event, pointerType ) { |
| 30 | + var propLength, touch, |
| 31 | + i = 0, |
| 32 | + orig = event, |
| 33 | + mouseProps = $.event.mouseHooks.props; |
| 34 | + |
| 35 | + event = $.Event( pointerType, { |
| 36 | + bubbles: true, |
| 37 | + cancelable: true, |
| 38 | + pointerId: 0, |
| 39 | + width: 0, |
| 40 | + height: 0, |
| 41 | + pressure: 0, |
| 42 | + tiltX: 0, |
| 43 | + tiltY: 0, |
| 44 | + pointerType: "", |
| 45 | + isPrimary: false |
| 46 | + }); |
| 47 | + |
| 48 | + if ( orig.type.indexOf("mouse") !== -1 ) { |
| 49 | + for ( propLength = mouseProps.length; i < propLength; i++ ) { |
| 50 | + event[ mouseProps[ i ] ] = orig[ mouseProps[ i ] ]; |
| 51 | + } |
| 52 | + |
| 53 | + event.pointerId = 1; |
| 54 | + event.pointerType = POINTER_TYPE_MOUSE; |
| 55 | + event.isPrimary = true; |
| 56 | + event.button = event.button !== undefined ? event.button : -1, |
| 57 | + event.buttons = event.buttons !== undefined ? event.buttons : 0, |
| 58 | + event.pressure = event.buttons > 0 ? 0.5 : 0; |
| 59 | + } else if ( orig.type.indexOf("touch") !== -1 ) { |
| 60 | + touch = orig.originalEvent.changedTouches[ 0 ]; |
| 61 | + event.pageX = touch.pageX; |
| 62 | + event.pageY = touch.pageY; |
| 63 | + event.screenX = touch.screenX; |
| 64 | + event.screenY = touch.screenY; |
| 65 | + event.clientX = touch.clientX; |
| 66 | + event.clientY = touch.clientY; |
| 67 | + event.pointerId = touch.identifier; |
| 68 | + event.pointerType = POINTER_TYPE_TOUCH; |
| 69 | + event.button = 0; |
| 70 | + event.buttons = 1; |
| 71 | + event.pressure = 0.5; |
| 72 | + event.originalEvent = orig; |
| 73 | + // TODO: Properly determine primary pointer |
| 74 | + event.isPrimary = true; |
| 75 | + } else if ( orig.type.indexOf("Pointer") !== -1 ) { |
| 76 | + event.pointerId = orig.originalEvent.pointerId; |
| 77 | + event.pointerType = processMSPointerType( orig.originalEvent.pointerType ); |
| 78 | + event.button = orig.originalEvent.button; |
| 79 | + event.buttons = event.pointerType === POINTER_TYPE_TOUCH ? 1 : orig.originalEvent.buttons; |
| 80 | + event.width = orig.originalEvent.width; |
| 81 | + event.height = orig.originalEvent.height; |
| 82 | + event.pressure = orig.originalEvent.pressure; |
| 83 | + event.tiltX = orig.originalEvent.tiltX; |
| 84 | + event.tiltY = orig.originalEvent.tiltY; |
| 85 | + event.pageX = orig.originalEvent.pageX; |
| 86 | + event.pageY = orig.originalEvent.pageY; |
| 87 | + event.screenX = orig.originalEvent.screenX; |
| 88 | + event.screenY = orig.originalEvent.screenY; |
| 89 | + event.clientX = orig.originalEvent.clientX; |
| 90 | + event.clientY = orig.originalEvent.clientY; |
| 91 | + event.isPrimary = orig.originalEvent.isPrimary; |
| 92 | + } |
| 93 | + |
| 94 | + return event; |
| 95 | +} |
| 96 | + |
| 97 | +function processMSPointerType( type ) { |
| 98 | + if ( type === 2 ) { |
| 99 | + return POINTER_TYPE_TOUCH; |
| 100 | + } else if ( type === 3 ) { |
| 101 | + return POINTER_TYPE_PEN; |
| 102 | + } else if ( type === 4 ) { |
| 103 | + return POINTER_TYPE_MOUSE; |
| 104 | + } else { |
| 105 | + return POINTER_TYPE_UNAVAILABLE; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function createSpecialEvent( eventName ) { |
| 110 | + $.event.special[ eventName ] = { |
| 111 | + setup: function() { |
| 112 | + $( this ).on( eventMap[ eventName ], $.event.special[ eventName ].handler ); |
| 113 | + }, |
| 114 | + teardown: function() { |
| 115 | + $( this ).off( eventMap[ eventName ], $.event.special[ eventName ].handler ); |
| 116 | + }, |
| 117 | + handler: function( event ) { |
| 118 | + if ( !lastTouch || event.type.indexOf("touch") !== -1 || |
| 119 | + ( event.type.indexOf("mouse") !== -1 && event.timestamp - lastTouch.timestamp > 1000 ) ) { |
| 120 | + if ( event.type.indexOf("touch") !== -1 ) { |
| 121 | + lastTouch = event; |
| 122 | + } |
| 123 | + if ( eventName === "pointerdown" && event.type.indexOf("touch") !== -1 ) { |
| 124 | + $( event.target ).trigger( processEvent( event, "pointerover" ) ); |
| 125 | + } |
| 126 | + $( event.target ).trigger( processEvent( event, eventName ) ); |
| 127 | + if ( eventName === "pointerup" && event.type.indexOf("touch") !== -1 ) { |
| 128 | + $( event.target ).trigger( processEvent( event, "pointerout" ) ); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +for ( eventName in eventMap ) { |
| 136 | + createSpecialEvent( eventName ); |
| 137 | +} |
| 138 | +})( jQuery ); |
0 commit comments