Interaction: Pass move and stop methods to the hooks.

This commit is contained in:
Scott González 2012-01-27 20:57:17 -05:00
parent f5c2e29e16
commit 17b0324c28

View File

@ -17,11 +17,17 @@ var interaction; // = $.ui.interaction
$.widget( "ui.interaction", { $.widget( "ui.interaction", {
version: "@VERSION", version: "@VERSION",
_create: function() { _create: function() {
// force the context so we can pass these methods to the hooks
this._interactionMove = $.proxy( this, "_interactionMove" );
this._interactionStop = $.proxy( this, "_interactionStop" );
// initialize all hooks for this widget
for ( var hook in interaction.hooks ) { for ( var hook in interaction.hooks ) {
interaction.hooks[ hook ].setup( this, this._startProxy( hook ) ); interaction.hooks[ hook ].setup( this, this._startProxy( hook ) );
} }
}, },
// a pass through to _interactionStart() which tracks the hook that was used
_startProxy: function( hook ) { _startProxy: function( hook ) {
var that = this; var that = this;
return function( event, target, pointerPosition ) { return function( event, target, pointerPosition ) {
@ -37,16 +43,20 @@ $.widget( "ui.interaction", {
return false; return false;
} }
// check if the event occurred on a valid target
if ( false === this._isValidTarget( $( target ) ) ) { if ( false === this._isValidTarget( $( target ) ) ) {
return false; return false;
} }
// check if the widget wants the event to start an interaction
started = ( this._start( event, pointerPosition ) !== false ); started = ( this._start( event, pointerPosition ) !== false );
if ( started ) { if ( started ) {
interaction.started = true; interaction.started = true;
interaction.hooks[ hook ].handle( this ); interaction.hooks[ hook ].handle( this,
this._interactionMove, this._interactionStop );
} }
// let the hook know if the interaction was started
return started; return started;
}, },
@ -88,17 +98,17 @@ interaction.hooks.mouse = {
}); });
}, },
handle: function( widget ) { handle: function( widget, move, stop ) {
function mousemove( event ) { function mousemove( event ) {
event.preventDefault(); event.preventDefault();
widget._interactionMove( event, { move( event, {
x: event.pageX, x: event.pageX,
y: event.pageY y: event.pageY
}); });
} }
function mouseup( event ) { function mouseup( event ) {
widget._interactionStop( event, { stop( event, {
x: event.pageX, x: event.pageX,
y: event.pageY y: event.pageY
}); });
@ -153,8 +163,8 @@ var touchHook = interaction.hooks.touch = {
}); });
}, },
handle: function( widget ) { handle: function( widget, move, stop ) {
function touchmove( event ) { function moveHandler( event ) {
// TODO: test non-Apple WebKits to see if they allow // TODO: test non-Apple WebKits to see if they allow
// zooming/scrolling if we don't preventDefault() // zooming/scrolling if we don't preventDefault()
var touch = getTouch( event ); var touch = getTouch( event );
@ -163,31 +173,31 @@ var touchHook = interaction.hooks.touch = {
} }
event.preventDefault(); event.preventDefault();
widget._interactionMove( event, { move( event, {
x: touch.pageX, x: touch.pageX,
y: touch.pageY y: touch.pageY
}); });
} }
function touchend( event ) { function stopHandler( event ) {
var touch = getTouch( event ); var touch = getTouch( event );
if ( !touch ) { if ( !touch ) {
return; return;
} }
widget._interactionStop( event, { stop( event, {
x: touch.pageX, x: touch.pageX,
y: touch.pageY y: touch.pageY
}); });
touchHook.id = null; touchHook.id = null;
widget.document widget.document
.unbind( "touchmove", touchmove ) .unbind( "touchmove", moveHandler )
.unbind( "touchend", touchend ); .unbind( "touchend", stopHandler );
} }
widget._bind( widget.document, { widget._bind( widget.document, {
"touchmove": touchmove, "touchmove": moveHandler,
"touchend": touchend "touchend": stopHandler
}); });
} }
}; };
@ -228,8 +238,8 @@ var pointerHook = interaction.hooks.msPointer = {
}); });
}, },
handle: function( widget ) { handle: function( widget, move, stop ) {
function move( _event ) { function moveHandler( _event ) {
var event = _event.originalEvent, var event = _event.originalEvent,
pageX = event.pageX, pageX = event.pageX,
pageY = event.pageY; pageY = event.pageY;
@ -249,34 +259,34 @@ var pointerHook = interaction.hooks.msPointer = {
pointerHook.x = pageX; pointerHook.x = pageX;
pointerHook.y = pageY; pointerHook.y = pageY;
widget._interactionMove( event, { move( event, {
x: pageX, x: pageX,
y: pageY y: pageY
}); });
} }
function stop( _event ) { function stopHandler( _event ) {
var event = _event.originalEvent; var event = _event.originalEvent;
if ( event.pointerId !== pointerHook.id ) { if ( event.pointerId !== pointerHook.id ) {
return; return;
} }
widget._interactionStop( event, { stop( event, {
x: event.pageX, x: event.pageX,
y: event.pageY y: event.pageY
}); });
pointerHook.id = pointerHook.x = pointerHook.y = undefined; pointerHook.id = pointerHook.x = pointerHook.y = undefined;
widget.document widget.document
.unbind( "MSPointerMove", move ) .unbind( "MSPointerMove", moveHandler )
.unbind( "MSPointerUp", stop ) .unbind( "MSPointerUp", stopHandler )
.unbind( "MSPointerCancel", stop ); .unbind( "MSPointerCancel", stopHandler );
} }
widget._bind( widget.document, { widget._bind( widget.document, {
"MSPointerMove": move, "MSPointerMove": moveHandler,
"MSPointerUp": stop, "MSPointerUp": stopHandler,
"MSPointerCancel": stop "MSPointerCancel": stopHandler
}); });
} }
}; };