MouseTracker Improvements

* Exposed secondary pointer button (middle, right, etc.) events from MouseTracker and through viewer (#479)
* MouseTracker - Improved IE 8 compatibility
This commit is contained in:
Mark Salsbery 2015-01-10 14:49:02 -08:00
parent 8ff70dd54f
commit 30b5ec04c6
3 changed files with 392 additions and 105 deletions

View File

@ -3,6 +3,9 @@ OPENSEADRAGON CHANGELOG
1.2.1: (in progress)
* Exposed secondary pointer button (middle, right, etc.) events from MouseTracker and through viewer (#479)
* MouseTracker - Improved IE 8 compatibility
1.2.0:
* New combined IIIF TileSource for 1.0 through 2.0 (#441)

View File

@ -72,8 +72,12 @@
* An optional handler for pointer exit.
* @param {OpenSeadragon.EventHandler} [options.pressHandler=null]
* An optional handler for pointer press.
* @param {OpenSeadragon.EventHandler} [options.auxPressHandler=null]
* An optional handler for pointer non-primary button press.
* @param {OpenSeadragon.EventHandler} [options.releaseHandler=null]
* An optional handler for pointer release.
* @param {OpenSeadragon.EventHandler} [options.auxReleaseHandler=null]
* An optional handler for pointer non-primary button release.
* @param {OpenSeadragon.EventHandler} [options.moveHandler=null]
* An optional handler for pointer move.
* @param {OpenSeadragon.EventHandler} [options.scrollHandler=null]
@ -150,7 +154,9 @@
this.enterHandler = options.enterHandler || null;
this.exitHandler = options.exitHandler || null;
this.pressHandler = options.pressHandler || null;
this.auxPressHandler = options.auxPressHandler || null;
this.releaseHandler = options.releaseHandler || null;
this.auxReleaseHandler = options.auxReleaseHandler || null;
this.moveHandler = options.moveHandler || null;
this.scrollHandler = options.scrollHandler || null;
this.clickHandler = options.clickHandler || null;
@ -183,6 +189,8 @@
DOMMouseScroll: function ( event ) { onMouseWheel( _this, event ); },
MozMousePixelScroll: function ( event ) { onMouseWheel( _this, event ); },
mouseenter: function ( event ) { onMouseEnter( _this, event ); }, // Used on IE8 only
mouseleave: function ( event ) { onMouseLeave( _this, event ); }, // Used on IE8 only
mouseover: function ( event ) { onMouseOver( _this, event ); },
mouseout: function ( event ) { onMouseOut( _this, event ); },
mousedown: function ( event ) { onMouseDown( _this, event ); },
@ -390,6 +398,34 @@
*/
pressHandler: function () { },
/**
* Implement or assign implementation to these handlers during or after
* calling the constructor.
* @function
* @param {Object} event
* @param {OpenSeadragon.MouseTracker} event.eventSource
* A reference to the tracker instance.
* @param {String} event.pointerType
* "mouse", "touch", "pen", etc.
* @param {OpenSeadragon.Point} event.position
* The position of the event relative to the tracked element.
* @param {Number} event.button
* Button which caused the event.
* -1: none, 0: primary/left, 1: aux/middle, 2: secondary/right, 3: X1/back, 4: X2/forward, 5: pen eraser.
* @param {Number} event.buttons
* Current buttons pressed.
* Combination of bit flags 0: none, 1: primary (or touch contact), 2: secondary, 4: aux (often middle), 8: X1 (often back), 16: X2 (often forward), 32: pen eraser.
* @param {Boolean} event.isTouchEvent
* True if the original event is a touch event, otherwise false. <span style="color:red;">Deprecated. Use pointerType and/or originalEvent instead.</span>
* @param {Object} event.originalEvent
* The original event object.
* @param {Boolean} event.preventDefaultAction
* Set to true to prevent the tracker subscriber from performing its default action (subscriber implementation dependent). Default: false.
* @param {Object} event.userData
* Arbitrary user-defined object.
*/
auxPressHandler: function () { },
/**
* Implement or assign implementation to these handlers during or after
* calling the constructor.
@ -420,6 +456,34 @@
*/
releaseHandler: function () { },
/**
* Implement or assign implementation to these handlers during or after
* calling the constructor.
* @function
* @param {Object} event
* @param {OpenSeadragon.MouseTracker} event.eventSource
* A reference to the tracker instance.
* @param {String} event.pointerType
* "mouse", "touch", "pen", etc.
* @param {OpenSeadragon.Point} event.position
* The position of the event relative to the tracked element.
* @param {Number} event.button
* Button which caused the event.
* -1: none, 0: primary/left, 1: aux/middle, 2: secondary/right, 3: X1/back, 4: X2/forward, 5: pen eraser.
* @param {Number} event.buttons
* Current buttons pressed.
* Combination of bit flags 0: none, 1: primary (or touch contact), 2: secondary, 4: aux (often middle), 8: X1 (often back), 16: X2 (often forward), 32: pen eraser.
* @param {Boolean} event.isTouchEvent
* True if the original event is a touch event, otherwise false. <span style="color:red;">Deprecated. Use pointerType and/or originalEvent instead.</span>
* @param {Object} event.originalEvent
* The original event object.
* @param {Boolean} event.preventDefaultAction
* Set to true to prevent the tracker subscriber from performing its default action (subscriber implementation dependent). Default: false.
* @param {Object} event.userData
* Arbitrary user-defined object.
*/
auxReleaseHandler: function () { },
/**
* Implement or assign implementation to these handlers during or after
* calling the constructor.
@ -838,8 +902,14 @@
$.MouseTracker.haveMouseEnter = false;
} else {
// Legacy W3C mouse events
$.MouseTracker.subscribeEvents.push( "mouseover", "mouseout", "mousedown", "mouseup", "mousemove" );
if ( $.Browser.vendor === $.BROWSERS.IE && $.Browser.version < 9 ) {
$.MouseTracker.subscribeEvents.push( "mouseenter", "mouseleave" );
$.MouseTracker.haveMouseEnter = true;
} else {
$.MouseTracker.subscribeEvents.push( "mouseover", "mouseout" );
$.MouseTracker.haveMouseEnter = false;
}
$.MouseTracker.subscribeEvents.push( "mousedown", "mouseup", "mousemove" );
if ( 'ontouchstart' in window ) {
// iOS, Android, and other W3c Touch Event implementations (see http://www.w3.org/TR/2011/WD-touch-events-20110505)
$.MouseTracker.subscribeEvents.push( "touchstart", "touchend", "touchmove", "touchcancel" );
@ -1181,14 +1251,17 @@
* @inner
*/
function capturePointer( tracker, pointerType ) {
var delegate = THIS[ tracker.hash ],
pointsList = tracker.getActivePointersListByType( pointerType ),
eventParams = getCaptureEventParams( tracker, pointerType );
var pointsList = tracker.getActivePointersListByType( pointerType ),
eventParams;
pointsList.captureCount++;
if ( pointsList.captureCount === 1 ) {
// We emulate mouse capture by hanging listeners on the window object.
if ( $.Browser.vendor === $.BROWSERS.IE && $.Browser.version < 9 ) {
tracker.element.setCapture( true );
} else {
eventParams = getCaptureEventParams( tracker, pointerType );
// We emulate mouse capture by hanging listeners on the document object.
// (Note we listen on the capture phase so the captured handlers will get called first)
$.addEvent(
$.MouseTracker.captureElement,
@ -1204,6 +1277,7 @@
);
}
}
}
/**
@ -1212,14 +1286,17 @@
* @inner
*/
function releasePointer( tracker, pointerType ) {
var delegate = THIS[ tracker.hash ],
pointsList = tracker.getActivePointersListByType( pointerType ),
eventParams = getCaptureEventParams( tracker, pointerType );
var pointsList = tracker.getActivePointersListByType( pointerType ),
eventParams;
pointsList.captureCount--;
if ( pointsList.captureCount === 0 ) {
// We emulate mouse capture by hanging listeners on the window object.
if ( $.Browser.vendor === $.BROWSERS.IE && $.Browser.version < 9 ) {
tracker.element.releaseCapture();
} else {
eventParams = getCaptureEventParams( tracker, pointerType );
// We emulate mouse capture by hanging listeners on the document object.
// (Note we listen on the capture phase so the captured handlers will get called first)
$.removeEvent(
$.MouseTracker.captureElement,
@ -1235,6 +1312,7 @@
);
}
}
}
/**
@ -1506,20 +1584,40 @@
}
/**
* Only used on IE 8
*
* @private
* @inner
*/
function onMouseEnter( tracker, event ) {
event = $.getEvent( event );
handleMouseEnter( tracker, event );
}
/**
* @private
* @inner
*/
function onMouseOver( tracker, event ) {
var gPoint;
event = $.getEvent( event );
if ( this === event.relatedTarget || isParentChild( event.currentTarget, event.relatedTarget ) ) {
return;
}
gPoint = {
handleMouseEnter( tracker, event );
}
/**
* @private
* @inner
*/
function handleMouseEnter( tracker, event ) {
var gPoint = {
id: $.MouseTracker.mousePointerId,
type: 'mouse',
isPrimary: true,
@ -1531,20 +1629,40 @@
}
/**
* Only used on IE 8
*
* @private
* @inner
*/
function onMouseLeave( tracker, event ) {
event = $.getEvent( event );
handleMouseExit( tracker, event );
}
/**
* @private
* @inner
*/
function onMouseOut( tracker, event ) {
var gPoint;
event = $.getEvent( event );
if ( this === event.relatedTarget || isParentChild( event.currentTarget, event.relatedTarget ) ) {
return;
}
gPoint = {
handleMouseExit( tracker, event );
}
/**
* @private
* @inner
*/
function handleMouseExit( tracker, event ) {
var gPoint = {
id: $.MouseTracker.mousePointerId,
type: 'mouse',
isPrimary: true,
@ -1556,6 +1674,31 @@
}
/**
* Returns a W3C DOM level 3 standard button value given an event.button property:
* -1 == none, 0 == primary/left, 1 == middle, 2 == secondary/right, 3 == X1/back, 4 == X2/forward, 5 == eraser (pen)
* @private
* @inner
*/
function getStandardizedButton( button ) {
if ( $.Browser.vendor === $.BROWSERS.IE && $.Browser.version < 9 ) {
// On IE 8, 0 == none, 1 == left, 2 == right, 3 == left and right, 4 == middle, 5 == left and middle, 6 == right and middle, 7 == all three
// TODO: Support chorded (multiple) button presses on IE 8?
if ( button === 1 ) {
return 0;
} else if ( button === 2 ) {
return 2;
} else if ( button === 4 ) {
return 1;
} else {
return -1;
}
} else {
return button;
}
}
/**
* @private
* @inner
@ -1573,7 +1716,7 @@
currentTime: $.now()
};
if ( updatePointersDown( tracker, event, [ gPoint ], event.button ) ) {
if ( updatePointersDown( tracker, event, [ gPoint ], getStandardizedButton( event.button ) ) ) {
$.stopEvent( event );
capturePointer( tracker, 'mouse' );
}
@ -1622,11 +1765,12 @@
currentTime: $.now()
};
if ( updatePointersUp( tracker, event, [ gPoint ], event.button ) ) {
if ( updatePointersUp( tracker, event, [ gPoint ], getStandardizedButton( event.button ) ) ) {
releasePointer( tracker, 'mouse' );
}
}
/**
* @private
* @inner
@ -2286,7 +2430,7 @@
* @param {Array.<OpenSeadragon.MouseTracker.GesturePoint>} gPoints
* Gesture points associated with the event.
* @param {Number} buttonChanged
* The button involved in the event: -1: none, 0: primary, 1: aux, 2: secondary, 3: X1, 4: X2, 5: pen eraser.
* The button involved in the event: -1: none, 0: primary/left, 1: aux/middle, 2: secondary/right, 3: X1/back, 4: X2/forward, 5: pen eraser.
* Note on chorded button presses (a button pressed when another button is already pressed): In the W3C Pointer Events model,
* only one pointerdown/pointerup event combo is fired. Chorded button state changes instead fire pointermove events.
*
@ -2303,6 +2447,27 @@
if ( typeof event.buttons !== 'undefined' ) {
pointsList.buttons = event.buttons;
} else {
if ( $.Browser.vendor === $.BROWSERS.IE && $.Browser.version < 9 ) {
if ( buttonChanged === 0 ) {
// Primary
pointsList.buttons += 1;
} else if ( buttonChanged === 1 ) {
// Aux
pointsList.buttons += 4;
} else if ( buttonChanged === 2 ) {
// Secondary
pointsList.buttons += 2;
} else if ( buttonChanged === 3 ) {
// X1 (Back)
pointsList.buttons += 8;
} else if ( buttonChanged === 4 ) {
// X2 (Forward)
pointsList.buttons += 16;
} else if ( buttonChanged === 5 ) {
// Pen Eraser
pointsList.buttons += 32;
}
} else {
if ( buttonChanged === 0 ) {
// Primary
@ -2324,10 +2489,30 @@
pointsList.buttons |= 32;
}
}
}
// Only capture and track primary button, pen, and touch contacts
//if ( buttonChanged !== 0 ) {
if ( buttonChanged !== 0 && buttonChanged !== 1 ) { //TODO Remove this IE8 compatibility and use the commented line above
if ( buttonChanged !== 0 ) {
// Aux Press
if ( tracker.auxPressHandler ) {
propagate = tracker.auxPressHandler(
{
eventSource: tracker,
pointerType: gPoints[ 0 ].type,
position: getPointRelativeToAbsolute( gPoints[ 0 ].currentPos, tracker.element ),
button: buttonChanged,
buttons: pointsList.buttons,
isTouchEvent: gPoints[ 0 ].type === 'touch',
originalEvent: event,
preventDefaultAction: false,
userData: tracker.userData
}
);
if ( propagate === false ) {
$.cancelEvent( event );
}
}
return false;
}
@ -2407,7 +2592,7 @@
* @param {Array.<OpenSeadragon.MouseTracker.GesturePoint>} gPoints
* Gesture points associated with the event.
* @param {Number} buttonChanged
* The button involved in the event: -1: none, 0: primary, 1: aux, 2: secondary, 3: X1, 4: X2, 5: pen eraser.
* The button involved in the event: -1: none, 0: primary/left, 1: aux/middle, 2: secondary/right, 3: X1/back, 4: X2/forward, 5: pen eraser.
* Note on chorded button presses (a button pressed when another button is already pressed): In the W3C Pointer Events model,
* only one pointerdown/pointerup event combo is fired. Chorded button state changes instead fire pointermove events.
*
@ -2430,6 +2615,27 @@
if ( typeof event.buttons !== 'undefined' ) {
pointsList.buttons = event.buttons;
} else {
if ( $.Browser.vendor === $.BROWSERS.IE && $.Browser.version < 9 ) {
if ( buttonChanged === 0 ) {
// Primary
pointsList.buttons -= 1;
} else if ( buttonChanged === 1 ) {
// Aux
pointsList.buttons -= 4;
} else if ( buttonChanged === 2 ) {
// Secondary
pointsList.buttons -= 2;
} else if ( buttonChanged === 3 ) {
// X1 (Back)
pointsList.buttons -= 8;
} else if ( buttonChanged === 4 ) {
// X2 (Forward)
pointsList.buttons -= 16;
} else if ( buttonChanged === 5 ) {
// Pen Eraser
pointsList.buttons -= 32;
}
} else {
if ( buttonChanged === 0 ) {
// Primary
@ -2451,10 +2657,30 @@
pointsList.buttons ^= ~32;
}
}
}
// Only capture and track primary button, pen, and touch contacts
//if ( buttonChanged !== 0 ) {
if ( buttonChanged !== 0 && buttonChanged !== 1 ) { //TODO Remove this IE8 compatibility and use the commented line above
if ( buttonChanged !== 0 ) {
// Aux Release
if ( tracker.auxReleaseHandler ) {
propagate = tracker.auxReleaseHandler(
{
eventSource: tracker,
pointerType: gPoints[ 0 ].type,
position: getPointRelativeToAbsolute( gPoints[ 0 ].currentPos, tracker.element ),
button: buttonChanged,
buttons: pointsList.buttons,
isTouchEvent: gPoints[ 0 ].type === 'touch',
originalEvent: event,
preventDefaultAction: false,
userData: tracker.userData
}
);
if ( propagate === false ) {
$.cancelEvent( event );
}
}
return false;
}

View File

@ -390,6 +390,8 @@ $.Viewer = function( options ) {
dragHandler: $.delegate( this, onCanvasDrag ),
dragEndHandler: $.delegate( this, onCanvasDragEnd ),
releaseHandler: $.delegate( this, onCanvasRelease ),
auxPressHandler: $.delegate( this, onCanvasAuxPress ),
auxReleaseHandler: $.delegate( this, onCanvasAuxRelease ),
scrollHandler: $.delegate( this, onCanvasScroll ),
pinchHandler: $.delegate( this, onCanvasPinch )
}).setTracking( this.mouseNavEnabled ? true : false ); // default state
@ -2422,7 +2424,7 @@ function onCanvasDragEnd( event ) {
function onCanvasRelease( event ) {
/**
* Raised when the mouse button is released or touch ends on the {@link OpenSeadragon.Viewer#canvas} element.
* Raised when the primary mouse button is released or touch ends on the {@link OpenSeadragon.Viewer#canvas} element.
*
* @event canvas-release
* @memberof OpenSeadragon.Viewer
@ -2444,6 +2446,62 @@ function onCanvasRelease( event ) {
});
}
function onCanvasAuxPress( event ) {
/**
* Raised when any non-primary pointer button is pressed on the {@link OpenSeadragon.Viewer#canvas} element.
*
* @event canvas-aux-press
* @memberof OpenSeadragon.Viewer
* @type {object}
* @property {OpenSeadragon.Viewer} eventSource - A reference to the Viewer which raised this event.
* @property {OpenSeadragon.MouseTracker} tracker - A reference to the MouseTracker which originated this event.
* @property {OpenSeadragon.Point} position - The position of the event relative to the tracked element.
* @property {String} pointerType - "mouse", "touch", "pen", etc.
* @property {Number} button - Button which caused the event.
* -1: none, 0: primary/left, 1: aux/middle, 2: secondary/right, 3: X1/back, 4: X2/forward, 5: pen eraser.
* @property {Number} buttons - Current buttons pressed.
* Combination of bit flags 0: none, 1: primary (or touch contact), 2: secondary, 4: aux (often middle), 8: X1 (often back), 16: X2 (often forward), 32: pen eraser.
* @property {Object} originalEvent - The original DOM event.
* @property {?Object} userData - Arbitrary subscriber-defined object.
*/
this.raiseEvent( 'canvas-aux-press', {
tracker: event.eventSource,
position: event.position,
pointerType: event.pointerType,
button: event.button,
buttons: event.buttons,
originalEvent: event.originalEvent
});
}
function onCanvasAuxRelease( event ) {
/**
* Raised when any non-primary pointer button is released on the {@link OpenSeadragon.Viewer#canvas} element.
*
* @event canvas-aux-release
* @memberof OpenSeadragon.Viewer
* @type {object}
* @property {OpenSeadragon.Viewer} eventSource - A reference to the Viewer which raised this event.
* @property {OpenSeadragon.MouseTracker} tracker - A reference to the MouseTracker which originated this event.
* @property {OpenSeadragon.Point} position - The position of the event relative to the tracked element.
* @property {String} pointerType - "mouse", "touch", "pen", etc.
* @property {Number} button - Button which caused the event.
* -1: none, 0: primary/left, 1: aux/middle, 2: secondary/right, 3: X1/back, 4: X2/forward, 5: pen eraser.
* @property {Number} buttons - Current buttons pressed.
* Combination of bit flags 0: none, 1: primary (or touch contact), 2: secondary, 4: aux (often middle), 8: X1 (often back), 16: X2 (often forward), 32: pen eraser.
* @property {Object} originalEvent - The original DOM event.
* @property {?Object} userData - Arbitrary subscriber-defined object.
*/
this.raiseEvent( 'canvas-aux-release', {
tracker: event.eventSource,
position: event.position,
pointerType: event.pointerType,
button: event.button,
buttons: event.buttons,
originalEvent: event.originalEvent
});
}
function onCanvasPinch( event ) {
var gestureSettings,
centerPt,
@ -2593,7 +2651,7 @@ function onContainerRelease( event ) {
}
}
/**
* Raised when the mouse button is released or touch ends on the {@link OpenSeadragon.Viewer#container} element.
* Raised when the primary mouse button is released or touch ends on the {@link OpenSeadragon.Viewer#container} element.
*
* @event container-release
* @memberof OpenSeadragon.Viewer