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-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.
|
|
|
|
this.element = options.element || $.Utils.makeNeutralElement("span");
|
2011-12-15 02:40:22 +04:00
|
|
|
this.config = options.config;
|
2011-12-15 03:22:02 +04:00
|
|
|
|
|
|
|
if ( options.onPress != undefined ){
|
|
|
|
this.addHandler("onPress", options.onPress );
|
|
|
|
}
|
|
|
|
if ( options.onRelease != undefined ){
|
|
|
|
this.addHandler("onRelease", options.onRelease );
|
|
|
|
}
|
|
|
|
if ( options.onClick != undefined ){
|
|
|
|
this.addHandler("onClick", options.onClick );
|
|
|
|
}
|
|
|
|
if ( options.onEnter != undefined ){
|
|
|
|
this.addHandler("onEnter", options.onEnter );
|
|
|
|
}
|
|
|
|
if ( options.onExit != undefined ){
|
|
|
|
this.addHandler("onExit", options.onExit );
|
|
|
|
}
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
this.currentState = $.ButtonState.GROUP;
|
|
|
|
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
|
|
|
|
);
|
2011-12-17 03:29:16 +04:00
|
|
|
this.imgRest = $.Utils.makeTransparentImage( this.srcRest );
|
|
|
|
this.imgGroup = $.Utils.makeTransparentImage( this.srcGroup );
|
|
|
|
this.imgHover = $.Utils.makeTransparentImage( this.srcHover );
|
|
|
|
this.imgDown = $.Utils.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
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
var styleRest = this.imgRest.style;
|
|
|
|
var styleGroup = this.imgGroup.style;
|
|
|
|
var styleHover = this.imgHover.style;
|
|
|
|
var 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
|
|
|
|
2011-12-15 03:22:02 +04:00
|
|
|
if ( $.Utils.getBrowser() == $.Browser.FIREFOX
|
|
|
|
&& $.Utils.getBrowserVersion() < 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-17 03:29:16 +04:00
|
|
|
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 );
|
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, {
|
2011-12-06 07:50:25 +04:00
|
|
|
_enterHandler: function(tracker, position, buttonDownElmt, buttonDownAny) {
|
2011-12-17 03:29:16 +04:00
|
|
|
if ( buttonDownElmt ) {
|
|
|
|
inTo( this, $.ButtonState.DOWN );
|
|
|
|
this.raiseEvent( "onEnter", this );
|
|
|
|
} else if ( !buttonDownAny ) {
|
|
|
|
inTo( this, $.ButtonState.HOVER );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_exitHandler: function(tracker, position, buttonDownElmt, buttonDownAny) {
|
2011-12-17 03:29:16 +04:00
|
|
|
outTo( this, $.ButtonState.GROUP );
|
|
|
|
if ( buttonDownElmt ) {
|
|
|
|
this.raiseEvent( "onExit", this );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_pressHandler: function(tracker, position) {
|
2011-12-17 03:29:16 +04:00
|
|
|
inTo( this, $.ButtonState.DOWN );
|
|
|
|
this.raiseEvent( "onPress", this );
|
2011-12-06 07:50:25 +04:00
|
|
|
},
|
|
|
|
_releaseHandler: function(tracker, position, insideElmtPress, insideElmtRelease) {
|
2011-12-17 03:29:16 +04:00
|
|
|
if ( insideElmtPress && insideElmtRelease ) {
|
|
|
|
outTo( this, $.ButtonState.HOVER );
|
|
|
|
this.raiseEvent( "onRelease", this );
|
|
|
|
} else if ( insideElmtPress ) {
|
|
|
|
outTo( this, $.ButtonState.GROUP );
|
2011-12-06 07:50:25 +04:00
|
|
|
} else {
|
2011-12-17 03:29:16 +04:00
|
|
|
inTo( this, $.ButtonState.HOVER );
|
2011-12-06 07:50:25 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_clickHandler: function(tracker, position, quick, shift) {
|
2011-12-17 03:29:16 +04:00
|
|
|
if ( quick ) {
|
|
|
|
this.raiseEvent("onClick", this);
|
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
|
|
|
},
|
|
|
|
notifyGroupExit: function() {
|
2011-12-17 03:29:16 +04:00
|
|
|
outTo( this, $.ButtonState.REST );
|
2011-12-06 07:50:25 +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 );
|
|
|
|
|
|
|
|
$.Utils.setElementOpacity( button.imgGroup, opacity, true );
|
|
|
|
if ( opacity > 0 ) {
|
|
|
|
// fade again
|
|
|
|
scheduleFade( button );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function beginFading( button ) {
|
|
|
|
button.shouldFade = true;
|
|
|
|
button.fadeBeginTime = new Date().getTime() + button.fadeDelay;
|
|
|
|
window.setTimeout(function(){
|
|
|
|
scheduleFade( button );
|
|
|
|
}, button.fadeDelay );
|
|
|
|
};
|
|
|
|
|
|
|
|
function stopFading( button ) {
|
|
|
|
button.shouldFade = false;
|
|
|
|
$.Utils.setElementOpacity( button.imgGroup, 1.0, true );
|
|
|
|
};
|
|
|
|
|
|
|
|
function inTo( button, newState ) {
|
|
|
|
if ( newState >= $.ButtonState.GROUP && button.currentState == $.ButtonState.REST ) {
|
|
|
|
stopFading( button );
|
|
|
|
button.currentState = $.ButtonState.GROUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( newState >= $.ButtonState.HOVER && button.currentState == $.ButtonState.GROUP ) {
|
|
|
|
button.imgHover.style.visibility = "";
|
|
|
|
button.currentState = $.ButtonState.HOVER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( newState >= $.ButtonState.DOWN && button.currentState == $.ButtonState.HOVER ) {
|
|
|
|
button.imgDown.style.visibility = "";
|
|
|
|
button.currentState = $.ButtonState.DOWN;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function outTo( button, newState ) {
|
|
|
|
if ( newState <= $.ButtonState.HOVER && button.currentState == $.ButtonState.DOWN ) {
|
|
|
|
button.imgDown.style.visibility = "hidden";
|
|
|
|
button.currentState = $.ButtonState.HOVER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( newState <= $.ButtonState.GROUP && button.currentState == $.ButtonState.HOVER ) {
|
|
|
|
this.imgHover.style.visibility = "hidden";
|
|
|
|
this.currentState = $.ButtonState.GROUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( button.newState <= $.ButtonState.REST && button.currentState == $.ButtonState.GROUP ) {
|
|
|
|
button.beginFading();
|
|
|
|
button.currentState = $.ButtonState.REST;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
}( OpenSeadragon ));
|