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.

This commit is contained in:
thatcher 2011-12-20 07:39:02 -05:00
parent 381763c19e
commit 345e5f3e6c
5 changed files with 106 additions and 100 deletions

View File

@ -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}

View File

@ -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 );
},

View File

@ -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 );
},

View File

@ -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 );
}
}
};

View File

@ -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