Skip to content

Commit 3227745

Browse files
committed
Interaction: Update interactions to be completely based on pointer events
1 parent 223a247 commit 3227745

File tree

1 file changed

+25
-173
lines changed

1 file changed

+25
-173
lines changed

ui/jquery.ui.interaction.js

Lines changed: 25 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
(function( $, undefined ) {
1414

15-
var interaction, touchHook, pointerHook;
15+
var interaction, pointerHook;
1616

1717
$.widget( "ui.interaction", {
1818
version: "@VERSION",
@@ -90,18 +90,26 @@ $.extend( interaction, {
9090
hooks: {}
9191
});
9292

93-
interaction.hooks.mouse = {
93+
pointerHook = interaction.hooks.pointer = {
9494
setup: function( widget, start ) {
9595
widget._on( widget.widget(), {
96-
"mousedown": function( event ) {
97-
// only react to the primary button
98-
if ( event.which === 1 ) {
96+
"pointerdown": function( event ) {
97+
event = event.originalEvent;
98+
if ( pointerHook.id ) {
99+
return;
100+
}
101+
102+
// only react to the primary button or touch
103+
if ( event.button === 0 ) {
99104
var started = start( event, event.target, {
100105
x: event.pageX,
101106
y: event.pageY
102107
});
103108

104109
if ( started ) {
110+
// track pointer which is performing the interaction
111+
pointerHook.id = event.pointerId;
112+
105113
// prevent selection
106114
event.preventDefault();
107115
}
@@ -111,174 +119,18 @@ interaction.hooks.mouse = {
111119
},
112120

113121
handle: function( widget, move, stop ) {
114-
function mousemove( event ) {
115-
event.preventDefault();
122+
function pointermove( event ) {
123+
event = event.originalEvent;
116124
move( event, {
117125
x: event.pageX,
118126
y: event.pageY
119127
});
120128
}
121129

122-
function mouseup( event ) {
123-
stop( event, {
124-
x: event.pageX,
125-
y: event.pageY
126-
});
127-
widget.document
128-
.unbind( "mousemove", mousemove )
129-
.unbind( "mouseup", mouseup );
130-
}
131-
132-
widget._on( widget.document, {
133-
"mousemove": mousemove,
134-
"mouseup": mouseup
135-
});
136-
}
137-
};
138-
139-
// WebKit doesn't support TouchList.identifiedTouch()
140-
function getTouch( event ) {
141-
var touches = event.originalEvent.changedTouches,
142-
i = 0, length = touches.length;
143-
144-
for ( ; i < length; i++ ) {
145-
if ( touches[ i ].identifier === touchHook.id ) {
146-
return touches[ i ];
147-
}
148-
}
149-
}
150-
151-
touchHook = interaction.hooks.touch = {
152-
setup: function( widget, start ) {
153-
widget._on( widget.widget(), {
154-
"touchstart": function( event ) {
155-
var touch, started;
156-
157-
if ( touchHook.id ) {
158-
return;
159-
}
160-
161-
touch = event.originalEvent.changedTouches.item( 0 );
162-
started = start( event, touch.target, {
163-
x: touch.pageX,
164-
y: touch.pageY
165-
});
166-
167-
if ( started ) {
168-
// track which finger is performing the interaction
169-
touchHook.id = touch.identifier;
170-
// prevent panning/zooming
171-
event.preventDefault();
172-
}
173-
}
174-
});
175-
},
176-
177-
handle: function( widget, move, stop ) {
178-
function moveHandler( event ) {
179-
// TODO: test non-Apple WebKits to see if they allow
180-
// zooming/scrolling if we don't preventDefault()
181-
var touch = getTouch( event );
182-
if ( !touch ) {
183-
return;
184-
}
185-
186-
event.preventDefault();
187-
move( event, {
188-
x: touch.pageX,
189-
y: touch.pageY
190-
});
191-
}
192-
193-
function stopHandler( event ) {
194-
var touch = getTouch( event );
195-
if ( !touch ) {
196-
return;
197-
}
198-
199-
stop( event, {
200-
x: touch.pageX,
201-
y: touch.pageY
202-
});
203-
touchHook.id = null;
204-
widget.document
205-
.unbind( "touchmove", moveHandler )
206-
.unbind( "touchend", stopHandler );
207-
}
208-
209-
widget._on( widget.document, {
210-
"touchmove": moveHandler,
211-
"touchend": stopHandler
212-
});
213-
}
214-
};
215-
216-
pointerHook = interaction.hooks.msPointer = {
217-
setup: function( widget, start ) {
218-
widget._on( widget.widget(), {
219-
"MSPointerDown": function( _event ) {
220-
var started,
221-
event = _event.originalEvent;
222-
223-
if ( pointerHook.id ) {
224-
return;
225-
}
226-
227-
// TODO: how can we detect a "right click" with a pen?
228-
// TODO: get full details about which and button from MS
229-
// touch and pen = 1
230-
// primary mouse button = 2
231-
if ( event.which > 2 ) {
232-
return;
233-
}
234-
235-
started = start( event, event.target, {
236-
x: event.pageX,
237-
y: event.pageY
238-
});
239-
240-
if ( started ) {
241-
// track which pointer is performing the interaction
242-
pointerHook.id = event.pointerId;
243-
// prevent panning/zooming
244-
event.preventManipulation();
245-
// prevent promoting pointer events to mouse events
246-
event.preventMouseEvent();
247-
}
248-
}
249-
});
250-
},
251-
252-
handle: function( widget, move, stop ) {
253-
function moveHandler( _event ) {
254-
var event = _event.originalEvent,
255-
pageX = event.pageX,
256-
pageY = event.pageY;
257-
258-
// always prevent manipulation to avoid panning/zooming
259-
event.preventManipulation();
260-
261-
if ( event.pointerId !== pointerHook.id ) {
262-
return;
263-
}
264-
265-
// MS streams events constantly, even if there is no movement
266-
// so we optimize by ignoring repeat events
267-
if ( pointerHook.x === pageX && pointerHook.y === pageY ) {
268-
return;
269-
}
270-
271-
pointerHook.x = pageX;
272-
pointerHook.y = pageY;
273-
move( event, {
274-
x: pageX,
275-
y: pageY
276-
});
277-
}
278-
279-
function stopHandler( _event ) {
280-
var event = _event.originalEvent;
130+
function pointerup( event ) {
131+
event = event.originalEvent;
281132

133+
// Only stop if original pointer stops
282134
if ( event.pointerId !== pointerHook.id ) {
283135
return;
284136
}
@@ -287,17 +139,17 @@ pointerHook = interaction.hooks.msPointer = {
287139
x: event.pageX,
288140
y: event.pageY
289141
});
290-
pointerHook.id = pointerHook.x = pointerHook.y = undefined;
142+
143+
pointerHook.id = null;
144+
291145
widget.document
292-
.unbind( "MSPointerMove", moveHandler )
293-
.unbind( "MSPointerUp", stopHandler )
294-
.unbind( "MSPointerCancel", stopHandler );
146+
.unbind( "pointermove", pointermove )
147+
.unbind( "pointerup", pointerup );
295148
}
296149

297150
widget._on( widget.document, {
298-
"MSPointerMove": moveHandler,
299-
"MSPointerUp": stopHandler,
300-
"MSPointerCancel": stopHandler
151+
"pointermove": pointermove,
152+
"pointerup": pointerup
301153
});
302154
}
303155
};

0 commit comments

Comments
 (0)