From 55c1aca351374d8e95ad87eef107a47c3d403bd3 Mon Sep 17 00:00:00 2001 From: thatcher Date: Fri, 8 Mar 2013 23:09:20 -0500 Subject: [PATCH] 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. --- src/eventhandler.js | 108 ++++++++++++++++++++++++++++---------------- src/navigator.js | 8 ++-- src/viewport.js | 4 +- 3 files changed, 76 insertions(+), 44 deletions(-) diff --git a/src/eventhandler.js b/src/eventhandler.js index b4f9f677..b67fcbd7 100644 --- a/src/eventhandler.js +++ b/src/eventhandler.js @@ -2,9 +2,7 @@ /** * For use by classes which want to support custom, non-browser events. - * TODO: This is an aweful name! This thing represents an "event source", - * not an "event handler". PLEASE change the to EventSource. Also please - * change 'addHandler', 'removeHandler' and 'raiseEvent' to 'bind', + * TODO: Consider mapping 'addHandler', 'removeHandler' and 'raiseEvent' to 'bind', * 'unbind', and 'trigger' respectively. Finally add a method 'one' which * automatically unbinds a listener after the first triggered event that * 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 - * @param {String} eventName - Name of event to register. - * @param {Function} handler - Function to call when event is triggered. + * @param {String} eventName - Name of event to trigger. */ - raiseEvent: function( eventName, eventArgs ) { + raiseEvent: function( eventName ) { //uncomment if you want to get a log og all events //$.console.log( eventName ); - var handler = this.getHandler( eventName ); + var handler = getHandler( this, eventName ), + eventArg, + allArgs, + i; if ( handler ) { - if ( !eventArgs ) { - eventArgs = {}; + if( arguments.length > 1 ){ + 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 )); diff --git a/src/navigator.js b/src/navigator.js index 04d17a4d..e34070a8 100644 --- a/src/navigator.js +++ b/src/navigator.js @@ -238,7 +238,7 @@ $.extend( $.Navigator.prototype, $.EventHandler.prototype, $.Viewer.prototype, { * @inner * @function */ -function onCanvasClick( tracker, position, quick, shift ) { +function onCanvasClick( tracker, position, quick, shift, originalEvent ) { this.displayRegion.focus(); } @@ -248,7 +248,7 @@ function onCanvasClick( tracker, position, quick, shift ) { * @inner * @function */ -function onCanvasDrag( tracker, position, delta, shift ) { +function onCanvasDrag( tracker, position, delta, shift, originalEvent ) { if ( this.viewer.viewport ) { if( !this.panHorizontal ){ delta.x = 0; @@ -270,7 +270,7 @@ function onCanvasDrag( tracker, position, delta, shift ) { * @inner * @function */ -function onCanvasRelease( tracker, position, insideElementPress, insideElementRelease ) { +function onCanvasRelease( tracker, position, insideElementPress, insideElementRelease, originalEvent ) { if ( insideElementPress && this.viewer.viewport ) { this.viewer.viewport.applyConstraints(); } @@ -282,7 +282,7 @@ function onCanvasRelease( tracker, position, insideElementPress, insideElementRe * @inner * @function */ -function onCanvasScroll( tracker, position, scroll, shift ) { +function onCanvasScroll( tracker, position, scroll, shift, originalEvent ) { var factor; if ( this.viewer.viewport ) { factor = Math.pow( this.zoomPerScroll, scroll ); diff --git a/src/viewport.js b/src/viewport.js index f92db393..d349ec85 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -78,7 +78,7 @@ $.Viewport.prototype = { */ resetContentSize: function( contentSize ){ if( this.viewer ){ - if( false === this.viewer.raiseEvent( 'reset', { contentSize: contentSize } ) ){ + if( this.viewer.raiseEvent( 'reset', { contentSize: contentSize } ) ){ //cancel event return this; } @@ -133,7 +133,7 @@ $.Viewport.prototype = { */ goHome: function( immediately ) { if( this.viewer ){ - if( false === this.viewer.raiseEvent( 'home', { immediately: immediately } ) ){ + if( this.viewer.raiseEvent( 'home', { immediately: immediately } ) ){ //cancel event return this; }