second pass at internal use of events through raiseEvent via eventhandler interfaces. Gonna add some tests to see if its addressing the basic use cases.

This commit is contained in:
thatcher 2013-03-08 23:09:20 -05:00
parent 3d2fb54ea3
commit 55c1aca351
3 changed files with 76 additions and 44 deletions

View File

@ -2,9 +2,7 @@
/** /**
* For use by classes which want to support custom, non-browser events. * For use by classes which want to support custom, non-browser events.
* TODO: This is an aweful name! This thing represents an "event source", * TODO: Consider mapping 'addHandler', 'removeHandler' and 'raiseEvent' to 'bind',
* not an "event handler". PLEASE change the to EventSource. Also please
* change 'addHandler', 'removeHandler' and 'raiseEvent' to 'bind',
* 'unbind', and 'trigger' respectively. Finally add a method 'one' which * 'unbind', and 'trigger' respectively. Finally add a method 'one' which
* automatically unbinds a listener after the first triggered event that * automatically unbinds a listener after the first triggered event that
* matches. * matches.
@ -66,53 +64,87 @@ $.EventHandler.prototype = {
}, },
/**
* Retrive the list of all handlers registered for a given event.
* @function
* @param {String} eventName - Name of event to get handlers for.
*/
getHandler: function( eventName ) {
var events = this.events[ eventName ];
if ( !events || !events.length ){
return null;
}
events = events.length === 1 ?
[ events[ 0 ] ] :
Array.apply( null, events );
return function( source, args ) {
var i,
length = events.length,
stop;
for ( i = 0; i < length; i++ ) {
if( $.isFunction( events[ i ] ) {
stop = events[ i ].apply( source, args );
if( stop === false ){
return stop;
}
}
}
};
},
/** /**
* Trigger an event, optionally passing additional information. * Trigger an event, optionally passing additional information.
* @function * @function
* @param {String} eventName - Name of event to register. * @param {String} eventName - Name of event to trigger.
* @param {Function} handler - Function to call when event is triggered.
*/ */
raiseEvent: function( eventName, eventArgs ) { raiseEvent: function( eventName ) {
//uncomment if you want to get a log og all events //uncomment if you want to get a log og all events
//$.console.log( eventName ); //$.console.log( eventName );
var handler = this.getHandler( eventName ); var handler = getHandler( this, eventName ),
eventArg,
allArgs,
i;
if ( handler ) { if ( handler ) {
if ( !eventArgs ) { if( arguments.length > 1 ){
eventArgs = {}; eventArg = arguments[ 1 ];
} else {
eventArg = {};
}
allArgs = [ eventArg ];
if( arguments.length > 2 ){
for( i = 0; i < arguments.length; i++ ){
allArgs[ i ] = arguments[ i + 2 ];
}
} }
return handler( this, eventArgs ); return handler( this, allArgs );
} }
} }
}; };
/**
* Retrive the list of all handlers registered for a given event.
* @private
* @inner
* @param {String} eventName - Name of event to get handlers for.
*/
function getHandler( handler, eventName ) {
var events = handler.events[ eventName ];
if ( !events || !events.length ){
return null;
}
return function( source, allArgs ) {
var i,
length = events.length,
stopPropagation,
preventDefault,
returnValue,
eventArg = arguments;
//Dress up the primary 'event' argument to provide the usual
//stopPropagation and preventDefault functions.
if( allArgs && allArgs.length ){
allArgs[0].stopProgagation = function(){
stopPropagation = true;
};
allArgs[0].preventDefault = function(){
preventDefault = true;
};
} else {
allArgs = [];
}
for ( i = 0; i < length; i++ ) {
if( $.isFunction( events[ i ] ) ) {
returnValue = events[ i ].apply( source, allArgs );
//stopping propagation isnt dom related, only implying that the chaing
//of registered event handlers for this event will be inturrupted.
if( stopPropagation === true || length === i + 1 ){
if( preventDefault || false === returnValue ){
//the implementing class that invoked raiseEvent may choose
//to use the return value of false to prevent the default
//behavior that would normally occur after the event was invoked
return false;
}
}
}
}
};
}
}( OpenSeadragon )); }( OpenSeadragon ));

View File

@ -238,7 +238,7 @@ $.extend( $.Navigator.prototype, $.EventHandler.prototype, $.Viewer.prototype, {
* @inner * @inner
* @function * @function
*/ */
function onCanvasClick( tracker, position, quick, shift ) { function onCanvasClick( tracker, position, quick, shift, originalEvent ) {
this.displayRegion.focus(); this.displayRegion.focus();
} }
@ -248,7 +248,7 @@ function onCanvasClick( tracker, position, quick, shift ) {
* @inner * @inner
* @function * @function
*/ */
function onCanvasDrag( tracker, position, delta, shift ) { function onCanvasDrag( tracker, position, delta, shift, originalEvent ) {
if ( this.viewer.viewport ) { if ( this.viewer.viewport ) {
if( !this.panHorizontal ){ if( !this.panHorizontal ){
delta.x = 0; delta.x = 0;
@ -270,7 +270,7 @@ function onCanvasDrag( tracker, position, delta, shift ) {
* @inner * @inner
* @function * @function
*/ */
function onCanvasRelease( tracker, position, insideElementPress, insideElementRelease ) { function onCanvasRelease( tracker, position, insideElementPress, insideElementRelease, originalEvent ) {
if ( insideElementPress && this.viewer.viewport ) { if ( insideElementPress && this.viewer.viewport ) {
this.viewer.viewport.applyConstraints(); this.viewer.viewport.applyConstraints();
} }
@ -282,7 +282,7 @@ function onCanvasRelease( tracker, position, insideElementPress, insideElementRe
* @inner * @inner
* @function * @function
*/ */
function onCanvasScroll( tracker, position, scroll, shift ) { function onCanvasScroll( tracker, position, scroll, shift, originalEvent ) {
var factor; var factor;
if ( this.viewer.viewport ) { if ( this.viewer.viewport ) {
factor = Math.pow( this.zoomPerScroll, scroll ); factor = Math.pow( this.zoomPerScroll, scroll );

View File

@ -78,7 +78,7 @@ $.Viewport.prototype = {
*/ */
resetContentSize: function( contentSize ){ resetContentSize: function( contentSize ){
if( this.viewer ){ if( this.viewer ){
if( false === this.viewer.raiseEvent( 'reset', { contentSize: contentSize } ) ){ if( this.viewer.raiseEvent( 'reset', { contentSize: contentSize } ) ){
//cancel event //cancel event
return this; return this;
} }
@ -133,7 +133,7 @@ $.Viewport.prototype = {
*/ */
goHome: function( immediately ) { goHome: function( immediately ) {
if( this.viewer ){ if( this.viewer ){
if( false === this.viewer.raiseEvent( 'home', { immediately: immediately } ) ){ if( this.viewer.raiseEvent( 'home', { immediately: immediately } ) ){
//cancel event //cancel event
return this; return this;
} }