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