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-15 17:57:39 +00:00
|
|
|
return function( event, target, pointerPosition ) {
|
|
|
|
return that._interactionStart( event, target, pointerPosition, hook );
|
2011-11-23 17:03:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
_interactionStart: function( event, target, pointerPosition, hook ) {
|
2012-01-14 21:14:39 +00:00
|
|
|
var started;
|
|
|
|
|
2012-01-12 03:04:41 +00:00
|
|
|
// only one interaction can happen at a time
|
|
|
|
if ( interaction.started ) {
|
2012-01-14 21:14:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( false === this._isValidTarget( $( target ) ) ) {
|
|
|
|
return false;
|
2012-01-12 03:04:41 +00:00
|
|
|
}
|
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
started = this._start( event, pointerPosition );
|
2012-01-14 21:14:39 +00:00
|
|
|
if ( started ) {
|
2011-11-23 17:03:09 +00:00
|
|
|
interaction.started = true;
|
|
|
|
interaction.hooks[ hook ].handle( this );
|
|
|
|
}
|
2012-01-14 21:14:39 +00:00
|
|
|
|
|
|
|
return started;
|
2011-11-23 17:03:09 +00:00
|
|
|
},
|
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
_interactionMove: function( event, pointerPosition ) {
|
|
|
|
this._move( event, pointerPosition );
|
2011-11-23 17:03:09 +00:00
|
|
|
},
|
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
_interactionStop: function( event, pointerPosition ) {
|
|
|
|
this._stop( event, pointerPosition );
|
2011-11-23 17:03:09 +00:00
|
|
|
interaction.started = false;
|
2012-01-14 21:14:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_isValidTarget: function( target ) {
|
|
|
|
return true;
|
2011-11-23 17:03:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
interaction = $.ui.interaction;
|
|
|
|
$.extend( interaction, {
|
|
|
|
started: false,
|
|
|
|
hooks: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
interaction.hooks.mouse = {
|
|
|
|
setup: function( widget, start ) {
|
|
|
|
widget._bind({
|
|
|
|
"mousedown": function( event ) {
|
|
|
|
if ( event.which === 1 ) {
|
2012-01-14 21:14:39 +00:00
|
|
|
var started = start( event, event.target, {
|
2012-01-15 17:57:39 +00:00
|
|
|
x: event.pageX,
|
|
|
|
y: event.pageY
|
2012-01-09 02:48:53 +00:00
|
|
|
});
|
2012-01-14 21:14:39 +00:00
|
|
|
|
|
|
|
if ( started ) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
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, {
|
2012-01-15 17:57:39 +00:00
|
|
|
x: event.pageX,
|
|
|
|
y: event.pageY
|
2012-01-09 02:48:53 +00:00
|
|
|
});
|
2012-01-08 01:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mouseup( event ) {
|
2012-01-09 02:48:53 +00:00
|
|
|
widget._interactionStop( event, {
|
2012-01-15 17:57:39 +00:00
|
|
|
x: event.pageX,
|
|
|
|
y: event.pageY
|
2012-01-09 02:48:53 +00:00
|
|
|
});
|
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 ) {
|
2012-01-14 21:14:39 +00:00
|
|
|
var touch, started;
|
2012-01-09 03:01:29 +00:00
|
|
|
|
|
|
|
if ( touchHook.id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
touch = event.originalEvent.changedTouches.item( 0 );
|
2012-01-14 21:14:39 +00:00
|
|
|
started = start( event, touch.target, {
|
2012-01-15 17:57:39 +00:00
|
|
|
x: touch.pageX,
|
|
|
|
y: touch.pageY
|
2012-01-09 03:01:29 +00:00
|
|
|
});
|
2012-01-14 21:14:39 +00:00
|
|
|
|
|
|
|
if ( started ) {
|
|
|
|
// track which finger is performing the interaction
|
|
|
|
touchHook.id = touch.identifier;
|
|
|
|
// prevent panning/zooming
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2012-01-09 03:01:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
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, {
|
2012-01-15 17:57:39 +00:00
|
|
|
x: touch.pageX,
|
|
|
|
y: touch.pageY
|
2012-01-09 03:01:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function touchend( event ) {
|
|
|
|
var touch = getTouch( event );
|
|
|
|
if ( !touch ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
widget._interactionStop( event, {
|
2012-01-15 17:57:39 +00:00
|
|
|
x: touch.pageX,
|
|
|
|
y: touch.pageY
|
2012-01-09 03:01:29 +00:00
|
|
|
});
|
|
|
|
touchHook.id = null;
|
|
|
|
widget.document
|
|
|
|
.unbind( "touchmove", touchmove )
|
|
|
|
.unbind( "touchend", touchend );
|
|
|
|
}
|
|
|
|
|
|
|
|
widget._bind( widget.document, {
|
|
|
|
"touchmove": touchmove,
|
|
|
|
"touchend": touchend
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 ) {
|
2012-01-14 21:14:39 +00:00
|
|
|
var started,
|
|
|
|
event = _event.originalEvent;
|
2012-01-12 03:04:41 +00:00
|
|
|
|
2012-01-09 03:17:19 +00:00
|
|
|
if ( pointerHook.id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-13 02:25:19 +00:00
|
|
|
// TODO: how can we detect a "right click" with a pen?
|
|
|
|
// TODO: get full details about which and button from MS
|
|
|
|
// touch and pen = 1
|
|
|
|
// primary mouse button = 2
|
|
|
|
if ( event.which > 2 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:14:39 +00:00
|
|
|
started = start( event, event.target, {
|
2012-01-15 17:57:39 +00:00
|
|
|
x: event.pageX,
|
|
|
|
y: event.pageY
|
2012-01-09 03:17:19 +00:00
|
|
|
});
|
2012-01-14 21:14:39 +00:00
|
|
|
|
|
|
|
if ( started ) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
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-15 17:57:39 +00:00
|
|
|
x: pageX,
|
|
|
|
y: 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, {
|
2012-01-15 17:57:39 +00:00
|
|
|
x: event.pageX,
|
|
|
|
y: event.pageY
|
2012-01-09 03:17:19 +00:00
|
|
|
});
|
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 );
|