2011-12-06 07:50:25 +04:00
|
|
|
|
|
|
|
(function( $ ){
|
|
|
|
|
|
|
|
$.ButtonState = {
|
|
|
|
REST: 0,
|
|
|
|
GROUP: 1,
|
|
|
|
HOVER: 2,
|
|
|
|
DOWN: 3
|
|
|
|
};
|
|
|
|
|
2011-12-15 02:40:22 +04:00
|
|
|
$.Button = function( options ) {
|
|
|
|
|
2011-12-20 16:39:02 +04:00
|
|
|
var _this = this;
|
|
|
|
|
2011-12-15 03:22:02 +04:00
|
|
|
$.EventHandler.call( this );
|
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
this.tooltip = options.tooltip;
|
|
|
|
this.srcRest = options.srcRest;
|
|
|
|
this.srcGroup = options.srcGroup;
|
|
|
|
this.srcHover = options.srcHover;
|
|
|
|
this.srcDown = options.srcDown;
|
2011-12-17 02:56:38 +04:00
|
|
|
//TODO: make button elements accessible by making them a-tags
|
|
|
|
// maybe even consider basing them on the element and adding
|
|
|
|
// methods jquery-style.
|
2012-01-24 07:48:45 +04:00
|
|
|
this.element = options.element || $.makeNeutralElement( "a" );
|
|
|
|
this.element.href = '#';
|
2011-12-15 02:40:22 +04:00
|
|
|
this.config = options.config;
|
2011-12-15 03:22:02 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( options.onPress ){
|
|
|
|
this.addHandler( "onPress", options.onPress );
|
2011-12-15 03:22:02 +04:00
|
|
|
}
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( options.onRelease ){
|
|
|
|
this.addHandler( "onRelease", options.onRelease );
|
2011-12-15 03:22:02 +04:00
|
|
|
}
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( options.onClick ){
|
|
|
|
this.addHandler( "onClick", options.onClick );
|
2011-12-15 03:22:02 +04:00
|
|
|
}
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( options.onEnter ){
|
|
|
|
this.addHandler( "onEnter", options.onEnter );
|
2011-12-15 03:22:02 +04:00
|
|
|
}
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( options.onExit ){
|
|
|
|
this.addHandler( "onExit", options.onExit );
|
2011-12-15 03:22:02 +04:00
|
|
|
}
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
this.currentState = $.ButtonState.GROUP;
|
2012-01-24 07:48:45 +04:00
|
|
|
this.tracker = new $.MouseTracker(
|
2011-12-17 02:56:38 +04:00
|
|
|
this.element,
|
2011-12-15 02:40:22 +04:00
|
|
|
this.config.clickTimeThreshold,
|
|
|
|
this.config.clickDistThreshold
|
|
|
|
);
|
2012-01-18 03:30:41 +04:00
|
|
|
this.imgRest = $.makeTransparentImage( this.srcRest );
|
|
|
|
this.imgGroup = $.makeTransparentImage( this.srcGroup );
|
|
|
|
this.imgHover = $.makeTransparentImage( this.srcHover );
|
|
|
|
this.imgDown = $.makeTransparentImage( this.srcDown );
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
this.fadeDelay = 0; // begin fading immediately
|
|
|
|
this.fadeLength = 2000; // fade over a period of 2 seconds
|
|
|
|
this.fadeBeginTime = null;
|
|
|
|
this.shouldFade = false;
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
this.element.style.display = "inline-block";
|
2011-12-17 02:56:38 +04:00
|
|
|
this.element.style.position = "relative";
|
2011-12-17 03:29:16 +04:00
|
|
|
this.element.title = this.tooltip;
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
this.element.appendChild( this.imgRest );
|
|
|
|
this.element.appendChild( this.imgGroup );
|
|
|
|
this.element.appendChild( this.imgHover );
|
|
|
|
this.element.appendChild( this.imgDown );
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
var styleRest = this.imgRest.style,
|
|
|
|
styleGroup = this.imgGroup.style,
|
|
|
|
styleHover = this.imgHover.style,
|
|
|
|
styleDown = this.imgDown.style;
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
styleGroup.position =
|
|
|
|
styleHover.position =
|
|
|
|
styleDown.position =
|
|
|
|
"absolute";
|
|
|
|
|
|
|
|
styleGroup.top =
|
|
|
|
styleHover.top =
|
|
|
|
styleDown.top =
|
|
|
|
"0px";
|
|
|
|
|
|
|
|
styleGroup.left =
|
|
|
|
styleHover.left =
|
|
|
|
styleDown.left =
|
|
|
|
"0px";
|
|
|
|
|
|
|
|
styleHover.visibility =
|
|
|
|
styleDown.visibility =
|
|
|
|
"hidden";
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2012-01-18 03:30:41 +04:00
|
|
|
if ( $.Browser.vendor == $.BROWSERS.FIREFOX
|
|
|
|
&& $.Browser.version < 3 ){
|
2011-12-17 03:29:16 +04:00
|
|
|
|
|
|
|
styleGroup.top =
|
|
|
|
styleHover.top =
|
|
|
|
styleDown.top = "";
|
2011-12-15 02:40:22 +04:00
|
|
|
}
|
|
|
|
|
2011-12-20 16:39:02 +04:00
|
|
|
//TODO - refactor mousetracer next to avoid this extension
|
|
|
|
$.extend( this.tracker, {
|
2012-01-24 07:48:45 +04:00
|
|
|
enterHandler: function( tracker, position, buttonDownElmt, buttonDownAny ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
if ( buttonDownElmt ) {
|
|
|
|
inTo( _this, $.ButtonState.DOWN );
|
|
|
|
_this.raiseEvent( "onEnter", _this );
|
|
|
|
} else if ( !buttonDownAny ) {
|
|
|
|
inTo( _this, $.ButtonState.HOVER );
|
|
|
|
}
|
|
|
|
},
|
2012-01-24 07:48:45 +04:00
|
|
|
exitHandler: function( tracker, position, buttonDownElmt, buttonDownAny ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
outTo( _this, $.ButtonState.GROUP );
|
|
|
|
if ( buttonDownElmt ) {
|
|
|
|
_this.raiseEvent( "onExit", _this );
|
|
|
|
}
|
|
|
|
},
|
2012-01-24 07:48:45 +04:00
|
|
|
pressHandler: function( tracker, position ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
inTo( _this, $.ButtonState.DOWN );
|
2011-12-20 16:44:33 +04:00
|
|
|
_this.raiseEvent( "onPress", _this );
|
2011-12-20 16:39:02 +04:00
|
|
|
},
|
2012-01-24 07:48:45 +04:00
|
|
|
releaseHandler: function( tracker, position, insideElmtPress, insideElmtRelease ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
if ( insideElmtPress && insideElmtRelease ) {
|
|
|
|
outTo( _this, $.ButtonState.HOVER );
|
2011-12-20 16:44:33 +04:00
|
|
|
_this.raiseEvent( "onRelease", _this );
|
2011-12-20 16:39:02 +04:00
|
|
|
} else if ( insideElmtPress ) {
|
|
|
|
outTo( _this, $.ButtonState.GROUP );
|
|
|
|
} else {
|
|
|
|
inTo( _this, $.ButtonState.HOVER );
|
|
|
|
}
|
|
|
|
},
|
2012-01-24 07:48:45 +04:00
|
|
|
clickHandler: function( tracker, position, quick, shift ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
if ( quick ) {
|
|
|
|
_this.raiseEvent("onClick", _this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
this.tracker.setTracking( true );
|
|
|
|
outTo( this, $.ButtonState.REST );
|
2011-12-06 07:50:25 +04:00
|
|
|
};
|
|
|
|
|
2011-12-15 03:22:02 +04:00
|
|
|
$.extend( $.Button.prototype, $.EventHandler.prototype, {
|
2012-01-24 07:48:45 +04:00
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
notifyGroupEnter: function() {
|
2011-12-17 03:29:16 +04:00
|
|
|
inTo( this, $.ButtonState.GROUP );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
2012-01-24 07:48:45 +04:00
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
notifyGroupExit: function() {
|
2011-12-17 03:29:16 +04:00
|
|
|
outTo( this, $.ButtonState.REST );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
2012-01-24 07:48:45 +04:00
|
|
|
|
2011-12-15 03:22:02 +04:00
|
|
|
});
|
2011-12-06 07:50:25 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
|
|
|
|
function scheduleFade( button ) {
|
|
|
|
window.setTimeout(function(){
|
|
|
|
updateFade( button );
|
|
|
|
}, 20 );
|
|
|
|
};
|
|
|
|
|
|
|
|
function updateFade( button ) {
|
|
|
|
var currentTime,
|
|
|
|
deltaTime,
|
|
|
|
opacity;
|
|
|
|
|
|
|
|
if ( button.shouldFade ) {
|
|
|
|
currentTime = +new Date();
|
|
|
|
deltaTime = currentTime - this.fadeBeginTime;
|
|
|
|
opacity = 1.0 - deltaTime / this.fadeLength;
|
|
|
|
opacity = Math.min( 1.0, opacity );
|
|
|
|
opacity = Math.max( 0.0, opacity );
|
|
|
|
|
2012-01-18 03:30:41 +04:00
|
|
|
$.setElementOpacity( button.imgGroup, opacity, true );
|
2011-12-17 03:29:16 +04:00
|
|
|
if ( opacity > 0 ) {
|
|
|
|
// fade again
|
|
|
|
scheduleFade( button );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function beginFading( button ) {
|
|
|
|
button.shouldFade = true;
|
|
|
|
button.fadeBeginTime = new Date().getTime() + button.fadeDelay;
|
2012-01-24 07:48:45 +04:00
|
|
|
window.setTimeout( function(){
|
2011-12-17 03:29:16 +04:00
|
|
|
scheduleFade( button );
|
|
|
|
}, button.fadeDelay );
|
|
|
|
};
|
|
|
|
|
|
|
|
function stopFading( button ) {
|
|
|
|
button.shouldFade = false;
|
2012-01-18 03:30:41 +04:00
|
|
|
$.setElementOpacity( button.imgGroup, 1.0, true );
|
2011-12-17 03:29:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
function inTo( button, newState ) {
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( newState >= $.ButtonState.GROUP &&
|
|
|
|
button.currentState == $.ButtonState.REST ) {
|
2011-12-17 03:29:16 +04:00
|
|
|
stopFading( button );
|
|
|
|
button.currentState = $.ButtonState.GROUP;
|
|
|
|
}
|
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( newState >= $.ButtonState.HOVER &&
|
|
|
|
button.currentState == $.ButtonState.GROUP ) {
|
2011-12-17 03:29:16 +04:00
|
|
|
button.imgHover.style.visibility = "";
|
|
|
|
button.currentState = $.ButtonState.HOVER;
|
|
|
|
}
|
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( newState >= $.ButtonState.DOWN &&
|
|
|
|
button.currentState == $.ButtonState.HOVER ) {
|
2011-12-17 03:29:16 +04:00
|
|
|
button.imgDown.style.visibility = "";
|
|
|
|
button.currentState = $.ButtonState.DOWN;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function outTo( button, newState ) {
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( newState <= $.ButtonState.HOVER &&
|
|
|
|
button.currentState == $.ButtonState.DOWN ) {
|
2011-12-17 03:29:16 +04:00
|
|
|
button.imgDown.style.visibility = "hidden";
|
|
|
|
button.currentState = $.ButtonState.HOVER;
|
|
|
|
}
|
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( newState <= $.ButtonState.GROUP &&
|
|
|
|
button.currentState == $.ButtonState.HOVER ) {
|
2011-12-20 16:44:33 +04:00
|
|
|
button.imgHover.style.visibility = "hidden";
|
|
|
|
button.currentState = $.ButtonState.GROUP;
|
2011-12-17 03:29:16 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( button.newState <= $.ButtonState.REST &&
|
|
|
|
button.currentState == $.ButtonState.GROUP ) {
|
2011-12-17 03:29:16 +04:00
|
|
|
button.beginFading();
|
|
|
|
button.currentState = $.ButtonState.REST;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
}( OpenSeadragon ));
|