First attempt at removing duplicated code

I've created _applyBoundaryConstraints() and _fitBounds() to remove
duplicated code.
This commit is contained in:
Henri Astre 2014-06-20 13:44:59 -07:00
parent 48aded3824
commit e67f6b4003

View File

@ -319,41 +319,36 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
return this.zoomSpring.target.value; return this.zoomSpring.target.value;
} }
}, },
/** /**
* @function * @function
* @return {OpenSeadragon.Viewport} Chainable. * @param {OpenSeadragon.Rect} bounds
* @fires OpenSeadragon.Viewer.event:constrain * @param {Boolean} immediately
*/ * @return {OpenSeadragon.Rect} constrained bounds.
applyConstraints: function( immediately ) { */
var actualZoom = this.getZoom(), _applyBoundaryConstraints: function( bounds, immediately ) {
constrainedZoom = Math.max( var horizontalThreshold,
Math.min( actualZoom, this.getMaxZoom() ),
this.getMinZoom()
),
bounds,
horizontalThreshold,
verticalThreshold, verticalThreshold,
left, left,
right, right,
top, top,
bottom, bottom,
dx = 0, dx = 0,
dy = 0; dy = 0,
newBounds = new $.Rect(
bounds.x,
bounds.y,
bounds.width,
bounds.height
);
horizontalThreshold = this.visibilityRatio * newBounds.width;
verticalThreshold = this.visibilityRatio * newBounds.height;
if ( actualZoom != constrainedZoom ) { left = newBounds.x + newBounds.width;
this.zoomTo( constrainedZoom, this.zoomPoint, immediately ); right = 1 - newBounds.x;
} top = newBounds.y + newBounds.height;
bottom = this.contentAspectY - newBounds.y;
bounds = this.getBounds();
horizontalThreshold = this.visibilityRatio * bounds.width;
verticalThreshold = this.visibilityRatio * bounds.height;
left = bounds.x + bounds.width;
right = 1 - bounds.x;
top = bounds.y + bounds.height;
bottom = this.contentAspectY - bounds.y;
if ( this.wrapHorizontal ) { if ( this.wrapHorizontal ) {
//do nothing //do nothing
@ -382,15 +377,43 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
} }
if ( dx || dy || immediately ) { if ( dx || dy || immediately ) {
bounds.x += dx; newBounds.x += dx;
bounds.y += dy; newBounds.y += dy;
if( bounds.width > 1 ){ if( newBounds.width > 1 ){
bounds.x = 0.5 - bounds.width/2; newBounds.x = 0.5 - newBounds.width/2;
} }
if( bounds.height > this.contentAspectY ){ if( newBounds.height > this.contentAspectY ){
bounds.y = this.contentAspectY/2 - bounds.height/2; newBounds.y = this.contentAspectY/2 - newBounds.height/2;
} }
this.fitBounds( bounds, immediately ); }
return newBounds;
},
/**
* @function
* @return {OpenSeadragon.Viewport} Chainable.
* @fires OpenSeadragon.Viewer.event:constrain
*/
applyConstraints: function( immediately ) {
var actualZoom = this.getZoom(),
constrainedZoom = Math.max(
Math.min( actualZoom, this.getMaxZoom() ),
this.getMinZoom()
),
bounds,
constrainedBounds;
if ( actualZoom != constrainedZoom ) {
this.zoomTo( constrainedZoom, this.zoomPoint, immediately );
}
bounds = this.getBounds();
constrainedBounds = this._applyBoundaryConstraints( bounds, immediately );
if ( bounds.x !== constrainedBounds.x || bounds.y !== constrainedBounds.y || immediately ){
this.fitBounds( constrainedBounds, immediately );
} }
if( this.viewer ){ if( this.viewer ){
@ -419,66 +442,18 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
ensureVisible: function( immediately ) { ensureVisible: function( immediately ) {
return this.applyConstraints( immediately ); return this.applyConstraints( immediately );
}, },
/**
* @function
* @param {OpenSeadragon.Rect} bounds
* @param {Boolean} immediately
* @return {OpenSeadragon.Viewport} Chainable.
*/
fitBounds: function( bounds, immediately ) {
var aspect = this.getAspectRatio(),
center = bounds.getCenter(),
newBounds = new $.Rect(
bounds.x,
bounds.y,
bounds.width,
bounds.height
),
oldBounds,
oldZoom,
newZoom,
referencePoint;
if ( newBounds.getAspectRatio() >= aspect ) {
newBounds.height = bounds.width / aspect;
newBounds.y = center.y - newBounds.height / 2;
} else {
newBounds.width = bounds.height * aspect;
newBounds.x = center.x - newBounds.width / 2;
}
this.panTo( this.getCenter( true ), true );
this.zoomTo( this.getZoom( true ), null, true );
oldBounds = this.getBounds();
oldZoom = this.getZoom();
newZoom = 1.0 / newBounds.width;
if ( newZoom == oldZoom || newBounds.width == oldBounds.width ) {
return this.panTo( center, immediately );
}
referencePoint = oldBounds.getTopLeft().times(
this.containerSize.x / oldBounds.width
).minus(
newBounds.getTopLeft().times(
this.containerSize.x / newBounds.width
)
).divide(
this.containerSize.x / oldBounds.width -
this.containerSize.x / newBounds.width
);
return this.zoomTo( newZoom, referencePoint, immediately );
},
/** /**
* @function * @function
* @param {OpenSeadragon.Rect} bounds * @param {OpenSeadragon.Rect} bounds
* @param {Boolean} immediately * @param {Object} options (immediately=null, constraints=false)
* @return {OpenSeadragon.Viewport} Chainable. * @return {OpenSeadragon.Viewport} Chainable.
*/ */
fitBoundsWithConstraints: function( bounds, immediately ) { _fitBounds: function( bounds, options ) {
var newOptions = options || {};
var immediately = newOptions.immediately || null;
var constraints = newOptions.constraints || false;
var aspect = this.getAspectRatio(), var aspect = this.getAspectRatio(),
center = bounds.getCenter(), center = bounds.getCenter(),
newBounds = new $.Rect( newBounds = new $.Rect(
@ -491,15 +466,8 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
oldZoom, oldZoom,
newZoom, newZoom,
referencePoint, referencePoint,
horizontalThreshold, newBoundsAspectRatio,
verticalThreshold, newConstrainedZoom;
left,
right,
top,
bottom,
dx = 0,
dy = 0,
newBoundsAspectRatio;
if ( newBounds.getAspectRatio() >= aspect ) { if ( newBounds.getAspectRatio() >= aspect ) {
newBounds.height = bounds.width / aspect; newBounds.height = bounds.width / aspect;
@ -509,7 +477,9 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
newBounds.x = center.x - newBounds.width / 2; newBounds.x = center.x - newBounds.width / 2;
} }
newBoundsAspectRatio = newBounds.getAspectRatio(); if ( constraints ) {
newBoundsAspectRatio = newBounds.getAspectRatio();
}
this.panTo( this.getCenter( true ), true ); this.panTo( this.getCenter( true ), true );
this.zoomTo( this.getZoom( true ), null, true ); this.zoomTo( this.getZoom( true ), null, true );
@ -518,66 +488,41 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
oldZoom = this.getZoom(); oldZoom = this.getZoom();
newZoom = 1.0 / newBounds.width; newZoom = 1.0 / newBounds.width;
var newConstrainedZoom = Math.max( if ( constraints ) {
Math.min(newZoom, this.getMaxZoom() ), newConstrainedZoom = Math.max(
this.getMinZoom() Math.min(newZoom, this.getMaxZoom() ),
); this.getMinZoom()
);
if (newZoom !== newConstrainedZoom) {
newZoom = newConstrainedZoom; if (newZoom !== newConstrainedZoom) {
newBounds.width = 1.0 / newZoom; newZoom = newConstrainedZoom;
newBounds.x = center.x - newBounds.width / 2; newBounds.width = 1.0 / newZoom;
newBounds.height = newBounds.width / newBoundsAspectRatio; newBounds.x = center.x - newBounds.width / 2;
newBounds.y = center.y - newBounds.height / 2; newBounds.height = newBounds.width / newBoundsAspectRatio;
} newBounds.y = center.y - newBounds.height / 2;
horizontalThreshold = this.visibilityRatio * newBounds.width;
verticalThreshold = this.visibilityRatio * newBounds.height;
left = newBounds.x + newBounds.width;
right = 1 - newBounds.x;
top = newBounds.y + newBounds.height;
bottom = this.contentAspectY - newBounds.y;
if ( this.wrapHorizontal ) {
//do nothing
} else {
if ( left < horizontalThreshold ) {
dx = horizontalThreshold - left;
} }
if ( right < horizontalThreshold ) {
dx = dx ? newBounds = this._applyBoundaryConstraints( newBounds, immediately );
( dx + right - horizontalThreshold ) / 2 :
( right - horizontalThreshold ); if( this.viewer ){
} /**
} * Raised when the viewport constraints are applied (see {@link OpenSeadragon.Viewport#applyConstraints}).
*
if ( this.wrapVertical ) { * @event constrain
//do nothing * @memberof OpenSeadragon.Viewer
} else { * @type {object}
if ( top < verticalThreshold ) { * @property {OpenSeadragon.Viewer} eventSource - A reference to the Viewer which raised this event.
dy = ( verticalThreshold - top ); * @property {Boolean} immediately
} * @property {?Object} userData - Arbitrary subscriber-defined object.
if ( bottom < verticalThreshold ) { */
dy = dy ? this.viewer.raiseEvent( 'constrain', {
( dy + bottom - verticalThreshold ) / 2 : immediately: immediately
( bottom - verticalThreshold ); });
}
}
if ( dx || dy ) {
newBounds.x += dx;
newBounds.y += dy;
if( newBounds.width > 1 ){
newBounds.x = 0.5 - newBounds.width/2;
}
if( newBounds.height > this.contentAspectY ){
newBounds.y = this.contentAspectY/2 - newBounds.height/2;
} }
} }
if ( newZoom == oldZoom || newBounds.width == oldBounds.width ) { if ( newZoom == oldZoom || newBounds.width == oldBounds.width ) {
return this.panTo( newBounds.getCenter(), immediately ); return this.panTo( constraints ? newBounds.getCenter() : center, immediately );
} }
referencePoint = oldBounds.getTopLeft().times( referencePoint = oldBounds.getTopLeft().times(
@ -592,6 +537,32 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
); );
return this.zoomTo( newZoom, referencePoint, immediately ); return this.zoomTo( newZoom, referencePoint, immediately );
},
/**
* @function
* @param {OpenSeadragon.Rect} bounds
* @param {Boolean} immediately
* @return {OpenSeadragon.Viewport} Chainable.
*/
fitBounds: function( bounds, immediately ) {
return this._fitBounds( bounds, {
immediately: immediately,
constraints: false
} );
},
/**
* @function
* @param {OpenSeadragon.Rect} bounds
* @param {Boolean} immediately
* @return {OpenSeadragon.Viewport} Chainable.
*/
fitBoundsWithConstraints: function( bounds, immediately ) {
return this._fitBounds( bounds, {
immediately: immediately,
constraints: true
} );
}, },
/** /**