2013-05-01 08:46:16 +04:00
|
|
|
/*
|
|
|
|
* OpenSeadragon
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 CodePlex Foundation
|
2013-05-14 07:32:09 +04:00
|
|
|
* Copyright (C) 2010-2013 OpenSeadragon contributors
|
2013-05-01 08:46:16 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* - Neither the name of CodePlex Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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,
|
2012-03-16 19:36:28 +04:00
|
|
|
onExit: null,
|
|
|
|
onFocus: null,
|
|
|
|
onBlur: null
|
2012-02-28 03:29:00 +04:00
|
|
|
|
|
|
|
}, options );
|
2012-02-01 00:59:09 +04:00
|
|
|
|
2012-04-11 01:02:24 +04:00
|
|
|
this.element = options.element || $.makeNeutralElement( "button" );
|
|
|
|
this.element.href = this.element.href || '#';
|
|
|
|
|
|
|
|
//if the user has specified the element to bind the control to explicitly
|
|
|
|
//then do not add the default control images
|
|
|
|
if( !options.element ){
|
|
|
|
this.imgRest = $.makeTransparentImage( this.srcRest );
|
|
|
|
this.imgGroup = $.makeTransparentImage( this.srcGroup );
|
|
|
|
this.imgHover = $.makeTransparentImage( this.srcHover );
|
|
|
|
this.imgDown = $.makeTransparentImage( this.srcDown );
|
|
|
|
|
|
|
|
this.element.appendChild( this.imgRest );
|
|
|
|
this.element.appendChild( this.imgGroup );
|
|
|
|
this.element.appendChild( this.imgHover );
|
|
|
|
this.element.appendChild( this.imgDown );
|
|
|
|
|
|
|
|
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 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 );
|
2012-03-16 19:36:28 +04:00
|
|
|
this.addHandler( "onFocus", this.onFocus );
|
|
|
|
this.addHandler( "onBlur", this.onBlur );
|
2011-12-15 02:40:22 +04:00
|
|
|
|
2011-12-17 03:29:16 +04:00
|
|
|
this.currentState = $.ButtonState.GROUP;
|
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
|
|
|
|
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-03-16 19:36:28 +04:00
|
|
|
focusHandler: function( tracker, position, buttonDownElement, buttonDownAny ) {
|
|
|
|
this.enterHandler( tracker, position, buttonDownElement, buttonDownAny );
|
|
|
|
_this.raiseEvent( "onFocus", _this );
|
|
|
|
},
|
|
|
|
|
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-03-16 19:36:28 +04:00
|
|
|
blurHandler: function( tracker, position, buttonDownElement, buttonDownAny ) {
|
|
|
|
this.exitHandler( tracker, position, buttonDownElement, buttonDownAny );
|
|
|
|
_this.raiseEvent( "onBlur", _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-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);
|
|
|
|
}
|
2012-03-16 19:36:28 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
keyHandler: function( tracker, key ){
|
2012-03-20 23:30:29 +04:00
|
|
|
//console.log( "%s : handling key %s!", _this.tooltip, key);
|
|
|
|
if( 13 === key ){
|
|
|
|
_this.raiseEvent( "onClick", _this );
|
|
|
|
_this.raiseEvent( "onRelease", _this );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-12-20 16:39:02 +04:00
|
|
|
}
|
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 );
|
2012-04-03 11:08:27 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
disable: function(){
|
|
|
|
this.notifyGroupExit();
|
|
|
|
this.element.disabled = true;
|
|
|
|
$.setElementOpacity( this.element, 0.2, true );
|
|
|
|
},
|
|
|
|
|
|
|
|
enable: function(){
|
|
|
|
this.element.disabled = false;
|
|
|
|
$.setElementOpacity( this.element, 1.0, true );
|
|
|
|
this.notifyGroupEnter();
|
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 ) {
|
2013-02-26 19:19:48 +04:00
|
|
|
$.requestAnimationFrame(function(){
|
2011-12-17 03:29:16 +04:00
|
|
|
updateFade( button );
|
2013-02-26 19:19:48 +04:00
|
|
|
});
|
2013-01-29 21:32:58 +04:00
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
|
|
|
|
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-03-16 19:36:28 +04:00
|
|
|
|
2012-04-11 01:02:24 +04:00
|
|
|
if( button.imgGroup ){
|
|
|
|
$.setElementOpacity( button.imgGroup, opacity, true );
|
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
if ( opacity > 0 ) {
|
|
|
|
// fade again
|
|
|
|
scheduleFade( button );
|
|
|
|
}
|
|
|
|
}
|
2013-01-29 21:32:58 +04:00
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
|
|
|
|
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 );
|
2013-01-29 21:32:58 +04:00
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
|
|
|
|
function stopFading( button ) {
|
|
|
|
button.shouldFade = false;
|
2012-04-11 01:02:24 +04:00
|
|
|
if( button.imgGroup ){
|
|
|
|
$.setElementOpacity( button.imgGroup, 1.0, true );
|
|
|
|
}
|
2013-01-29 21:32:58 +04:00
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
|
|
|
|
function inTo( button, newState ) {
|
2012-04-03 11:08:27 +04:00
|
|
|
|
|
|
|
if( button.element.disabled ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 ) {
|
2012-04-11 01:02:24 +04:00
|
|
|
if( button.imgHover ){
|
|
|
|
button.imgHover.style.visibility = "";
|
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
button.currentState = $.ButtonState.HOVER;
|
|
|
|
}
|
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( newState >= $.ButtonState.DOWN &&
|
|
|
|
button.currentState == $.ButtonState.HOVER ) {
|
2012-04-11 01:02:24 +04:00
|
|
|
if( button.imgDown ){
|
|
|
|
button.imgDown.style.visibility = "";
|
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
button.currentState = $.ButtonState.DOWN;
|
|
|
|
}
|
2013-01-29 21:32:58 +04:00
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
|
|
|
|
|
|
|
|
function outTo( button, newState ) {
|
2012-04-03 11:08:27 +04:00
|
|
|
|
|
|
|
if( button.element.disabled ){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( newState <= $.ButtonState.HOVER &&
|
|
|
|
button.currentState == $.ButtonState.DOWN ) {
|
2012-04-11 01:02:24 +04:00
|
|
|
if( button.imgDown ){
|
|
|
|
button.imgDown.style.visibility = "hidden";
|
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
button.currentState = $.ButtonState.HOVER;
|
|
|
|
}
|
|
|
|
|
2012-01-24 07:48:45 +04:00
|
|
|
if ( newState <= $.ButtonState.GROUP &&
|
|
|
|
button.currentState == $.ButtonState.HOVER ) {
|
2012-04-11 01:02:24 +04:00
|
|
|
if( button.imgHover ){
|
|
|
|
button.imgHover.style.visibility = "hidden";
|
|
|
|
}
|
2011-12-20 16:44:33 +04:00
|
|
|
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;
|
|
|
|
}
|
2013-01-29 21:32:58 +04:00
|
|
|
}
|
2011-12-17 03:29:16 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-06 07:50:25 +04:00
|
|
|
}( OpenSeadragon ));
|