2011-12-06 07:50:25 +04:00
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
(function( $ ){
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
|
|
|
* An enumeration of button states including, REST, GROUP, HOVER, and DOWN
|
|
|
|
* @static
|
|
|
|
*/
|
2011-12-06 07:50:25 +04:00
|
|
|
$.ButtonState = {
|
|
|
|
REST: 0,
|
|
|
|
GROUP: 1,
|
|
|
|
HOVER: 2,
|
|
|
|
DOWN: 3
|
|
|
|
};
|
|
|
|
|
2012-01-25 23:14:02 +04:00
|
|
|
/**
|
|
|
|
* Manages events, hover states for individual buttons, tool-tips, as well
|
|
|
|
* as fading the bottons out when the user has not interacted with them
|
|
|
|
* for a specified period.
|
|
|
|
* @class
|
2012-02-01 00:59:09 +04:00
|
|
|
* @extends OpenSeadragon.EventHandler
|
2012-01-25 23:14:02 +04:00
|
|
|
* @param {Object} options
|
2012-02-01 00:59:09 +04:00
|
|
|
* @param {String} options.tooltip Provides context help for the button we the
|
|
|
|
* user hovers over it.
|
2012-01-25 23:14:02 +04:00
|
|
|
* @param {String} options.srcRest URL of image to use in 'rest' state
|
|
|
|
* @param {String} options.srcGroup URL of image to use in 'up' state
|
|
|
|
* @param {String} options.srcHover URL of image to use in 'hover' state
|
|
|
|
* @param {String} options.srcDown URL of image to use in 'domn' state
|
2012-02-01 00:59:09 +04:00
|
|
|
* @param {Element} [options.element] Element to use as a container for the
|
|
|
|
* button.
|
|
|
|
* @property {String} tooltip Provides context help for the button we the
|
|
|
|
* user hovers over it.
|
|
|
|
* @property {String} srcRest URL of image to use in 'rest' state
|
|
|
|
* @property {String} srcGroup URL of image to use in 'up' state
|
|
|
|
* @property {String} srcHover URL of image to use in 'hover' state
|
|
|
|
* @property {String} srcDown URL of image to use in 'domn' state
|
2012-02-28 03:29:00 +04:00
|
|
|
* @property {Object} config Configurable settings for this button. DEPRECATED.
|
2012-02-01 00:59:09 +04:00
|
|
|
* @property {Element} [element] Element to use as a container for the
|
|
|
|
* button.
|
|
|
|
* @property {Number} fadeDelay How long to wait before fading
|
|
|
|
* @property {Number} fadeLength How long should it take to fade the button.
|
|
|
|
* @property {Number} fadeBeginTime When the button last began to fade.
|
|
|
|
* @property {Boolean} shouldFade Whether this button should fade after user
|
|
|
|
* stops interacting with the viewport.
|
|
|
|
this.fadeDelay = 0; // begin fading immediately
|
|
|
|
this.fadeLength = 2000; // fade over a period of 2 seconds
|
|
|
|
this.fadeBeginTime = null;
|
|
|
|
this.shouldFade = false;
|
2012-01-25 23:14:02 +04:00
|
|
|
*/
|
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 );
|
|
|
|
|
2012-02-28 03:29:00 +04:00
|
|
|
$.extend( true, this, {
|
|
|
|
|
|
|
|
tooltip: null,
|
|
|
|
srcRest: null,
|
|
|
|
srcGroup: null,
|
|
|
|
srcHover: null,
|
|
|
|
srcDown: null,
|
|
|
|
clickTimeThreshold: $.DEFAULT_SETTINGS.clickTimeThreshold,
|
|
|
|
clickDistThreshold: $.DEFAULT_SETTINGS.clickDistThreshold,
|
|
|
|
// begin fading immediately
|
|
|
|
fadeDelay: 0,
|
|
|
|
// fade over a period of 2 seconds
|
|
|
|
fadeLength: 2000,
|
|
|
|
onPress: null,
|
|
|
|
onRelease: null,
|
|
|
|
onClick: null,
|
|
|
|
onEnter: null,
|
|
|
|
onExit: null
|
|
|
|
|
|
|
|
}, options );
|
2012-02-01 00:59:09 +04:00
|
|
|
|
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-02-28 03:29:00 +04:00
|
|
|
this.element = options.element || $.makeNeutralElement( "a" );
|
|
|
|
this.element.href = '#';
|
2011-12-15 03:22:02 +04:00
|
|
|
|
2012-02-28 03:29:00 +04:00
|
|
|
this.addHandler( "onPress", this.onPress );
|
|
|
|
this.addHandler( "onRelease", this.onRelease );
|
|
|
|
this.addHandler( "onClick", this.onClick );
|
|
|
|
this.addHandler( "onEnter", this.onEnter );
|
|
|
|
this.addHandler( "onExit", this.onExit );
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
this.currentState = $.ButtonState.GROUP;
|
2012-02-28 03:29:00 +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.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-02-28 03:29:00 +04:00
|
|
|
this.imgGroup.style.position =
|
|
|
|
this.imgHover.style.position =
|
|
|
|
this.imgDown.style.position =
|
|
|
|
"absolute";
|
|
|
|
|
|
|
|
this.imgGroup.style.top =
|
|
|
|
this.imgHover.style.top =
|
|
|
|
this.imgDown.style.top =
|
|
|
|
"0px";
|
|
|
|
|
|
|
|
this.imgGroup.style.left =
|
|
|
|
this.imgHover.style.left =
|
|
|
|
this.imgDown.style.left =
|
|
|
|
"0px";
|
|
|
|
|
|
|
|
this.imgHover.style.visibility =
|
|
|
|
this.imgDown.style.visibility =
|
|
|
|
"hidden";
|
|
|
|
|
|
|
|
if ( $.Browser.vendor == $.BROWSERS.FIREFOX && $.Browser.version < 3 ){
|
|
|
|
this.imgGroup.style.top =
|
|
|
|
this.imgHover.style.top =
|
|
|
|
this.imgDown.style.top =
|
|
|
|
"";
|
2011-12-15 02:40:22 +04:00
|
|
|
}
|
|
|
|
|
2012-02-28 03:29:00 +04:00
|
|
|
this.tracker = new $.MouseTracker({
|
|
|
|
|
|
|
|
element: this.element,
|
|
|
|
clickTimeThreshold: this.clickTimeThreshold,
|
|
|
|
clickDistThreshold: this.clickDistThreshold,
|
2012-02-28 17:07:56 +04:00
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
enterHandler: function( tracker, position, buttonDownElement, buttonDownAny ) {
|
|
|
|
if ( buttonDownElement ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
inTo( _this, $.ButtonState.DOWN );
|
|
|
|
_this.raiseEvent( "onEnter", _this );
|
|
|
|
} else if ( !buttonDownAny ) {
|
|
|
|
inTo( _this, $.ButtonState.HOVER );
|
|
|
|
}
|
|
|
|
},
|
2012-02-28 17:07:56 +04:00
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
exitHandler: function( tracker, position, buttonDownElement, buttonDownAny ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
outTo( _this, $.ButtonState.GROUP );
|
2012-02-01 06:01:37 +04:00
|
|
|
if ( buttonDownElement ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
_this.raiseEvent( "onExit", _this );
|
|
|
|
}
|
|
|
|
},
|
2012-02-28 17:07:56 +04:00
|
|
|
|
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-02-28 17:07:56 +04:00
|
|
|
|
2012-02-01 06:01:37 +04:00
|
|
|
releaseHandler: function( tracker, position, insideElementPress, insideElementRelease ) {
|
|
|
|
if ( insideElementPress && insideElementRelease ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
outTo( _this, $.ButtonState.HOVER );
|
2011-12-20 16:44:33 +04:00
|
|
|
_this.raiseEvent( "onRelease", _this );
|
2012-02-01 06:01:37 +04:00
|
|
|
} else if ( insideElementPress ) {
|
2011-12-20 16:39:02 +04:00
|
|
|
outTo( _this, $.ButtonState.GROUP );
|
|
|
|
} else {
|
|
|
|
inTo( _this, $.ButtonState.HOVER );
|
|
|
|
}
|
|
|
|
},
|
2012-02-28 17:07:56 +04:00
|
|
|
|
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
|
|
|
|
2012-02-28 03:29:00 +04:00
|
|
|
}).setTracking( true );
|
|
|
|
|
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
|
|
|
$.extend( $.Button.prototype, $.EventHandler.prototype, {
|
2012-01-24 07:48:45 +04:00
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* TODO: Determine what this function is intended to do and if it's actually
|
|
|
|
* useful as an API point.
|
|
|
|
* @function
|
|
|
|
* @name OpenSeadragon.Button.prototype.notifyGroupEnter
|
|
|
|
*/
|
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
|
|
|
|
2012-02-01 00:59:09 +04:00
|
|
|
/**
|
|
|
|
* TODO: Determine what this function is intended to do and if it's actually
|
|
|
|
* useful as an API point.
|
|
|
|
* @function
|
|
|
|
* @name OpenSeadragon.Button.prototype.notifyGroupExit
|
|
|
|
*/
|
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();
|
2012-02-28 17:07:56 +04:00
|
|
|
deltaTime = currentTime - button.fadeBeginTime;
|
|
|
|
opacity = 1.0 - deltaTime / button.fadeLength;
|
2011-12-17 03:29:16 +04:00
|
|
|
opacity = Math.min( 1.0, opacity );
|
|
|
|
opacity = Math.max( 0.0, opacity );
|
2012-02-28 17:07:56 +04:00
|
|
|
|
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;
|
2012-02-28 17:07:56 +04:00
|
|
|
button.fadeBeginTime = +new Date() + 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-02-28 17:07:56 +04:00
|
|
|
if ( newState <= $.ButtonState.REST &&
|
2012-01-24 07:48:45 +04:00
|
|
|
button.currentState == $.ButtonState.GROUP ) {
|
2012-02-28 17:07:56 +04:00
|
|
|
beginFading( button );
|
2011-12-17 03:29:16 +04:00
|
|
|
button.currentState = $.ButtonState.REST;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
}( OpenSeadragon ));
|