2011-11-23 17:03:09 +00:00
|
|
|
(function( $, undefined ) {
|
|
|
|
|
|
|
|
var interaction; // = $.ui.interaction
|
|
|
|
|
|
|
|
$.widget( "ui.interaction", {
|
|
|
|
version: "@VERSION",
|
|
|
|
_create: function() {
|
|
|
|
for ( var hook in interaction.hooks ) {
|
|
|
|
interaction.hooks[ hook ].setup( this, this._startProxy( hook ) );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_startProxy: function( hook ) {
|
|
|
|
var that = this;
|
2012-01-09 02:48:53 +00:00
|
|
|
return function( event, position ) {
|
|
|
|
that._interactionStart( event, position, hook );
|
2011-11-23 17:03:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-01-09 02:48:53 +00:00
|
|
|
_interactionStart: function( event, position, hook ) {
|
2012-01-12 03:04:41 +00:00
|
|
|
// only one interaction can happen at a time
|
|
|
|
if ( interaction.started ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-09 02:48:53 +00:00
|
|
|
if ( false !== this._start( event, position ) ) {
|
2011-11-23 17:03:09 +00:00
|
|
|
interaction.started = true;
|
|
|
|
interaction.hooks[ hook ].handle( this );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-01-09 02:48:53 +00:00
|
|
|
_interactionMove: function( event, position ) {
|
|
|
|
this._move( event, position );
|
2011-11-23 17:03:09 +00:00
|
|
|
},
|
|
|
|
|
2012-01-09 02:48:53 +00:00
|
|
|
_interactionStop: function( event, position ) {
|
|
|
|
this._stop( event, position );
|
2011-11-23 17:03:09 +00:00
|
|
|
interaction.started = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
interaction = $.ui.interaction;
|
|
|
|
$.extend( interaction, {
|
|
|
|
started: false,
|
|
|
|
hooks: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
interaction.hooks.mouse = {
|
|
|
|
setup: function( widget, start ) {
|
|
|
|
widget._bind({
|
|
|
|
"mousedown": function( event ) {
|
|
|
|
if ( event.which === 1 ) {
|
|
|
|
event.preventDefault();
|
2012-01-09 02:48:53 +00:00
|
|
|
start( event, {
|
|
|
|
left: event.pageX,
|
|
|
|
top: event.pageY
|
|
|
|
});
|
2011-11-23 17:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handle: function( widget ) {
|
2012-01-08 01:49:49 +00:00
|
|
|
function mousemove( event ) {
|
|
|
|
event.preventDefault();
|
2012-01-09 02:48:53 +00:00
|
|
|
widget._interactionMove( event, {
|
|
|
|
left: event.pageX,
|
|
|
|
top: event.pageY
|
|
|
|
});
|
2012-01-08 01:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mouseup( event ) {
|
2012-01-09 02:48:53 +00:00
|
|
|
widget._interactionStop( event, {
|
|
|
|
left: event.pageX,
|
|
|
|
top: event.pageY
|
|
|
|
});
|
2012-01-08 01:49:49 +00:00
|
|
|
widget.document
|
|
|
|
.unbind( "mousemove", mousemove )
|
|
|
|
.unbind( "mouseup", mouseup );
|
|
|
|
}
|
|
|
|
|
2011-11-23 17:03:09 +00:00
|
|
|
widget._bind( widget.document, {
|
2012-01-08 01:49:49 +00:00
|
|
|
"mousemove": mousemove,
|
|
|
|
"mouseup": mouseup
|
2011-11-23 17:03:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-09 03:01:29 +00:00
|
|
|
// WebKit doesn't support TouchList.identifiedTouch()
|
|
|
|
function getTouch( event ) {
|
|
|
|
var touch,
|
|
|
|
touches = event.originalEvent.changedTouches,
|
|
|
|
i = 0, length = touches.length;
|
|
|
|
|
|
|
|
for ( ; i < length; i++ ) {
|
|
|
|
if ( touches[ i ].identifier === touchHook.id ) {
|
|
|
|
return touches[ i ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var touchHook = interaction.hooks.touch = {
|
|
|
|
setup: function( widget, start ) {
|
|
|
|
widget._bind({
|
|
|
|
"touchstart": function( event ) {
|
|
|
|
var touch;
|
|
|
|
|
|
|
|
if ( touchHook.id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
touch = event.originalEvent.changedTouches.item( 0 );
|
|
|
|
touchHook.id = touch.identifier;
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
start( event, {
|
|
|
|
left: touch.pageX,
|
|
|
|
top: touch.pageY
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handle: function( widget ) {
|
|
|
|
function touchmove( event ) {
|
2012-01-09 03:17:19 +00:00
|
|
|
// TODO: test non-Apple WebKits to see if they allow
|
|
|
|
// zooming/scrolling if we don't preventDefault()
|
2012-01-09 03:01:29 +00:00
|
|
|
var touch = getTouch( event );
|
|
|
|
if ( !touch ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
widget._interactionMove( event, {
|
|
|
|
left: touch.pageX,
|
|
|
|
top: touch.pageY
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function touchend( event ) {
|
|
|
|
var touch = getTouch( event );
|
|
|
|
if ( !touch ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
widget._interactionStop( event, {
|
|
|
|
left: touch.pageX,
|
|
|
|
top: touch.pageY
|
|
|
|
});
|
|
|
|
touchHook.id = null;
|
|
|
|
widget.document
|
|
|
|
.unbind( "touchmove", touchmove )
|
|
|
|
.unbind( "touchend", touchend );
|
|
|
|
}
|
|
|
|
|
|
|
|
widget._bind( widget.document, {
|
|
|
|
"touchmove": touchmove,
|
|
|
|
"touchend": touchend
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-12 03:04:41 +00:00
|
|
|
// TODO: test mouse
|
2012-01-12 03:47:34 +00:00
|
|
|
// TODO: how can we detect a "right click" with a pen?
|
2012-01-09 03:17:19 +00:00
|
|
|
var pointerHook = interaction.hooks.msPointer = {
|
|
|
|
setup: function( widget, start ) {
|
|
|
|
widget._bind({
|
2012-01-12 03:04:41 +00:00
|
|
|
"MSPointerDown": function( _event ) {
|
|
|
|
var event = _event.originalEvent;
|
|
|
|
|
2012-01-09 03:17:19 +00:00
|
|
|
if ( pointerHook.id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-12 03:04:41 +00:00
|
|
|
// track which pointer is performing the interaction
|
|
|
|
pointerHook.id = event.pointerId;
|
|
|
|
// prevent panning/zooming
|
|
|
|
event.preventManipulation();
|
|
|
|
// prevent promoting pointer events to mouse events
|
|
|
|
event.preventMouseEvent();
|
|
|
|
|
2012-01-09 03:17:19 +00:00
|
|
|
start( event, {
|
|
|
|
left: event.pageX,
|
|
|
|
top: event.pageY
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handle: function( widget ) {
|
2012-01-12 03:04:41 +00:00
|
|
|
function move( _event ) {
|
2012-01-12 03:31:39 +00:00
|
|
|
var event = _event.originalEvent,
|
|
|
|
pageX = event.pageX,
|
|
|
|
pageY = event.pageY;
|
2012-01-12 03:04:41 +00:00
|
|
|
|
|
|
|
// always prevent manipulation to avoid panning/zooming
|
|
|
|
event.preventManipulation();
|
2012-01-09 03:17:19 +00:00
|
|
|
|
2012-01-12 03:04:41 +00:00
|
|
|
if ( event.pointerId !== pointerHook.id ) {
|
2012-01-09 03:17:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-12 03:31:39 +00:00
|
|
|
// MS streams events constantly, even if there is no movement
|
|
|
|
// so we optimize by ignoring repeat events
|
|
|
|
if ( pointerHook.x === pageX && pointerHook.y === pageY ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pointerHook.x = pageX;
|
|
|
|
pointerHook.y = pageY;
|
2012-01-09 03:17:19 +00:00
|
|
|
widget._interactionMove( event, {
|
2012-01-12 03:31:39 +00:00
|
|
|
left: pageX,
|
|
|
|
top: pageY
|
2012-01-09 03:17:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-01-12 03:04:41 +00:00
|
|
|
function stop( _event ) {
|
|
|
|
var event = _event.originalEvent;
|
|
|
|
|
|
|
|
if ( event.pointerId !== pointerHook.id ) {
|
2012-01-09 03:17:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
widget._interactionStop( event, {
|
|
|
|
left: event.pageX,
|
|
|
|
top: event.pageY
|
|
|
|
});
|
2012-01-12 03:34:46 +00:00
|
|
|
pointerHook.id = pointerHook.x = pointerHook.y = undefined;
|
2012-01-09 03:17:19 +00:00
|
|
|
widget.document
|
2012-01-12 03:04:41 +00:00
|
|
|
.unbind( "MSPointerMove", move )
|
|
|
|
.unbind( "MSPointerUp", stop )
|
|
|
|
.unbind( "MSPointerCancel", stop );
|
2012-01-09 03:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
widget._bind( widget.document, {
|
2012-01-12 03:04:41 +00:00
|
|
|
"MSPointerMove": move,
|
|
|
|
"MSPointerUp": stop,
|
|
|
|
"MSPointerCancel": stop
|
2012-01-09 03:17:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-23 17:03:09 +00:00
|
|
|
})( jQuery );
|