From 345e5f3e6c654afdb1ac1b9ee166f07522f0104b Mon Sep 17 00:00:00 2001 From: thatcher Date: Tue, 20 Dec 2011 07:39:02 -0500 Subject: [PATCH] completed first pass at refactor of buttons.js, next will be mousetracker so we can attack some significant anti-patterns that spill over into places like buttons.js because the MouseTracker doesnt have an idiomatic constructor. It may also be worth applying the MouseTracker as a mixin to avoid the extra .tracker property indirection. --- build.properties | 6 ++-- openseadragon.js | 80 ++++++++++++++++++++++----------------------- src/button.js | 76 +++++++++++++++++++++--------------------- src/eventhandler.js | 42 ++++++++++++++---------- src/mousetracker.js | 2 +- 5 files changed, 106 insertions(+), 100 deletions(-) diff --git a/build.properties b/build.properties index 4d736be3..a7538e53 100644 --- a/build.properties +++ b/build.properties @@ -1,12 +1,12 @@ #OpenSeadragon build.properties -#TODO: how do you auto-increment build_id's with for every commit? +#TODO: how do you auto-increment build_id's with every commit? # TRY: continuos integration -# TRY: svn-hooks +# TRY: git-hooks PROJECT: openseadragon BUILD_MAJOR: 0 BUILD_MINOR: 8 -BUILD_ID: 13 +BUILD_ID: 14 BUILD: ${PROJECT}.${BUILD_MAJOR}.${BUILD_MINOR}.${BUILD_ID} VERSION: ${BUILD_MAJOR}.${BUILD_MINOR}.${BUILD_ID} diff --git a/openseadragon.js b/openseadragon.js index 1670da99..540f5e6c 100644 --- a/openseadragon.js +++ b/openseadragon.js @@ -3,7 +3,7 @@ * (c) 2010 OpenSeadragon * (c) 2010 CodePlex Foundation * - * OpenSeadragon 0.8.13 + * OpenSeadragon 0.8.14 * ---------------------------------------------------------------------------- * * License: New BSD License (BSD) @@ -782,7 +782,7 @@ $.Utils = new $.Utils(); $.MouseTracker = function (elmt, clickTimeThreshold, clickDistThreshold) { //Start Thatcher - TODO: remove local function definitions in favor of - // - a global closre for MouseTracker so the number + // - a global closure for MouseTracker so the number // - of Viewers has less memory impact. Also use // - prototype pattern instead of Singleton pattern. //End Thatcher @@ -2767,6 +2767,8 @@ $.ButtonState = { $.Button = function( options ) { + var _this = this; + $.EventHandler.call( this ); this.tooltip = options.tooltip; @@ -2853,50 +2855,48 @@ $.Button = function( options ) { styleDown.top = ""; } - this.tracker.enterHandler = $.delegate( this, this._enterHandler ); - this.tracker.exitHandler = $.delegate( this, this._exitHandler ); - this.tracker.pressHandler = $.delegate( this, this._pressHandler ); - this.tracker.releaseHandler = $.delegate( this, this._releaseHandler ); - this.tracker.clickHandler = $.delegate( this, this._clickHandler ); + //TODO - refactor mousetracer next to avoid this extension + $.extend( this.tracker, { + enterHandler: function(tracker, position, buttonDownElmt, buttonDownAny) { + if ( buttonDownElmt ) { + inTo( _this, $.ButtonState.DOWN ); + _this.raiseEvent( "onEnter", _this ); + } else if ( !buttonDownAny ) { + inTo( _this, $.ButtonState.HOVER ); + } + }, + exitHandler: function(tracker, position, buttonDownElmt, buttonDownAny) { + outTo( _this, $.ButtonState.GROUP ); + if ( buttonDownElmt ) { + _this.raiseEvent( "onExit", _this ); + } + }, + pressHandler: function(tracker, position) { + inTo( _this, $.ButtonState.DOWN ); + this.raiseEvent( "onPress", _this ); + }, + releaseHandler: function(tracker, position, insideElmtPress, insideElmtRelease) { + if ( insideElmtPress && insideElmtRelease ) { + outTo( _this, $.ButtonState.HOVER ); + this.raiseEvent( "onRelease", _this ); + } else if ( insideElmtPress ) { + outTo( _this, $.ButtonState.GROUP ); + } else { + inTo( _this, $.ButtonState.HOVER ); + } + }, + clickHandler: function(tracker, position, quick, shift) { + if ( quick ) { + _this.raiseEvent("onClick", _this); + } + } + }); this.tracker.setTracking( true ); outTo( this, $.ButtonState.REST ); }; $.extend( $.Button.prototype, $.EventHandler.prototype, { - _enterHandler: function(tracker, position, buttonDownElmt, buttonDownAny) { - if ( buttonDownElmt ) { - inTo( this, $.ButtonState.DOWN ); - this.raiseEvent( "onEnter", this ); - } else if ( !buttonDownAny ) { - inTo( this, $.ButtonState.HOVER ); - } - }, - _exitHandler: function(tracker, position, buttonDownElmt, buttonDownAny) { - outTo( this, $.ButtonState.GROUP ); - if ( buttonDownElmt ) { - this.raiseEvent( "onExit", this ); - } - }, - _pressHandler: function(tracker, position) { - inTo( this, $.ButtonState.DOWN ); - this.raiseEvent( "onPress", this ); - }, - _releaseHandler: function(tracker, position, insideElmtPress, insideElmtRelease) { - if ( insideElmtPress && insideElmtRelease ) { - outTo( this, $.ButtonState.HOVER ); - this.raiseEvent( "onRelease", this ); - } else if ( insideElmtPress ) { - outTo( this, $.ButtonState.GROUP ); - } else { - inTo( this, $.ButtonState.HOVER ); - } - }, - _clickHandler: function(tracker, position, quick, shift) { - if ( quick ) { - this.raiseEvent("onClick", this); - } - }, notifyGroupEnter: function() { inTo( this, $.ButtonState.GROUP ); }, diff --git a/src/button.js b/src/button.js index 455ad526..3a214530 100644 --- a/src/button.js +++ b/src/button.js @@ -10,6 +10,8 @@ $.ButtonState = { $.Button = function( options ) { + var _this = this; + $.EventHandler.call( this ); this.tooltip = options.tooltip; @@ -96,50 +98,48 @@ $.Button = function( options ) { styleDown.top = ""; } - this.tracker.enterHandler = $.delegate( this, this._enterHandler ); - this.tracker.exitHandler = $.delegate( this, this._exitHandler ); - this.tracker.pressHandler = $.delegate( this, this._pressHandler ); - this.tracker.releaseHandler = $.delegate( this, this._releaseHandler ); - this.tracker.clickHandler = $.delegate( this, this._clickHandler ); + //TODO - refactor mousetracer next to avoid this extension + $.extend( this.tracker, { + enterHandler: function(tracker, position, buttonDownElmt, buttonDownAny) { + if ( buttonDownElmt ) { + inTo( _this, $.ButtonState.DOWN ); + _this.raiseEvent( "onEnter", _this ); + } else if ( !buttonDownAny ) { + inTo( _this, $.ButtonState.HOVER ); + } + }, + exitHandler: function(tracker, position, buttonDownElmt, buttonDownAny) { + outTo( _this, $.ButtonState.GROUP ); + if ( buttonDownElmt ) { + _this.raiseEvent( "onExit", _this ); + } + }, + pressHandler: function(tracker, position) { + inTo( _this, $.ButtonState.DOWN ); + this.raiseEvent( "onPress", _this ); + }, + releaseHandler: function(tracker, position, insideElmtPress, insideElmtRelease) { + if ( insideElmtPress && insideElmtRelease ) { + outTo( _this, $.ButtonState.HOVER ); + this.raiseEvent( "onRelease", _this ); + } else if ( insideElmtPress ) { + outTo( _this, $.ButtonState.GROUP ); + } else { + inTo( _this, $.ButtonState.HOVER ); + } + }, + clickHandler: function(tracker, position, quick, shift) { + if ( quick ) { + _this.raiseEvent("onClick", _this); + } + } + }); this.tracker.setTracking( true ); outTo( this, $.ButtonState.REST ); }; $.extend( $.Button.prototype, $.EventHandler.prototype, { - _enterHandler: function(tracker, position, buttonDownElmt, buttonDownAny) { - if ( buttonDownElmt ) { - inTo( this, $.ButtonState.DOWN ); - this.raiseEvent( "onEnter", this ); - } else if ( !buttonDownAny ) { - inTo( this, $.ButtonState.HOVER ); - } - }, - _exitHandler: function(tracker, position, buttonDownElmt, buttonDownAny) { - outTo( this, $.ButtonState.GROUP ); - if ( buttonDownElmt ) { - this.raiseEvent( "onExit", this ); - } - }, - _pressHandler: function(tracker, position) { - inTo( this, $.ButtonState.DOWN ); - this.raiseEvent( "onPress", this ); - }, - _releaseHandler: function(tracker, position, insideElmtPress, insideElmtRelease) { - if ( insideElmtPress && insideElmtRelease ) { - outTo( this, $.ButtonState.HOVER ); - this.raiseEvent( "onRelease", this ); - } else if ( insideElmtPress ) { - outTo( this, $.ButtonState.GROUP ); - } else { - inTo( this, $.ButtonState.HOVER ); - } - }, - _clickHandler: function(tracker, position, quick, shift) { - if ( quick ) { - this.raiseEvent("onClick", this); - } - }, notifyGroupEnter: function() { inTo( this, $.ButtonState.GROUP ); }, diff --git a/src/eventhandler.js b/src/eventhandler.js index 019e6154..7d74fd06 100644 --- a/src/eventhandler.js +++ b/src/eventhandler.js @@ -8,45 +8,51 @@ $.EventHandler.prototype = { - addHandler: function(id, handler) { + addHandler: function( id, handler ) { var events = this.events[ id ]; if( !events ){ this.events[ id ] = events = []; } - events[events.length] = handler; + events[ events.length ] = handler; }, - removeHandler: function(id, handler) { + removeHandler: function( id, handler ) { //Start Thatcher - unneccessary indirection. Also, because events were // - not actually being removed, we need to add the code // - to do the removal ourselves. TODO - var evt = this.events[ id ]; - if (!evt) return; + var events = this.events[ id ]; + if ( !events ){ + return; + } //End Thatcher }, - getHandler: function(id) { - var evt = this.events[ id ]; - if (!evt || !evt.length) return null; - evt = evt.length === 1 ? - [evt[0]] : - Array.apply( null, evt ); - return function(source, args) { - for (var i = 0, l = evt.length; i < l; i++) { - evt[i](source, args); + getHandler: function( id ) { + var events = this.events[ id ]; + if ( !events || !events.length ){ + return null; + } + events = events.length === 1 ? + [ events[ 0 ] ] : + Array.apply( null, events ); + return function( source, args ) { + var i, + l = events.length; + for ( i = 0; i < l; i++ ) { + events[ i ]( source, args ); } }; }, - raiseEvent: function(eventName, eventArgs) { + raiseEvent: function( eventName, eventArgs ) { var handler = this.getHandler( eventName ); - if (handler) { - if (!eventArgs) { + if ( handler ) { + if ( !eventArgs ) { eventArgs = new Object(); } - handler(this, eventArgs); + handler( this, eventArgs ); } } }; diff --git a/src/mousetracker.js b/src/mousetracker.js index dba27981..0392e0eb 100644 --- a/src/mousetracker.js +++ b/src/mousetracker.js @@ -18,7 +18,7 @@ $.MouseTracker = function (elmt, clickTimeThreshold, clickDistThreshold) { //Start Thatcher - TODO: remove local function definitions in favor of - // - a global closre for MouseTracker so the number + // - a global closure for MouseTracker so the number // - of Viewers has less memory impact. Also use // - prototype pattern instead of Singleton pattern. //End Thatcher