From bd9d72331585006f0a21c29062190168c9f756f4 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 30 Apr 2013 21:32:05 +0200 Subject: [PATCH] Improve type checking in ol.ResolutionConstraint --- src/ol/resolutionconstraint.js | 98 +++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 38 deletions(-) diff --git a/src/ol/resolutionconstraint.js b/src/ol/resolutionconstraint.js index 89a237e748..051da23ee3 100644 --- a/src/ol/resolutionconstraint.js +++ b/src/ol/resolutionconstraint.js @@ -20,14 +20,21 @@ ol.ResolutionConstraintType; ol.ResolutionConstraint.createContinuous = function(power, maxResolution, opt_minResolution) { var minResolution = opt_minResolution || 0; - return function(resolution, delta, direction) { - if (goog.isDef(resolution)) { - resolution /= Math.pow(power, delta); - return goog.math.clamp(resolution, minResolution, maxResolution); - } else { - return undefined; - } - }; + return ( + /** + * @param {number|undefined} resolution Resolution. + * @param {number} delta Delta. + * @param {number} direction Direction. + * @return {number|undefined} Resolution. + */ + function(resolution, delta, direction) { + if (goog.isDef(resolution)) { + resolution /= Math.pow(power, delta); + return goog.math.clamp(resolution, minResolution, maxResolution); + } else { + return undefined; + } + }); }; @@ -37,15 +44,23 @@ ol.ResolutionConstraint.createContinuous = */ ol.ResolutionConstraint.createSnapToResolutions = function(resolutions) { - return function(resolution, delta, direction) { - if (goog.isDef(resolution)) { - var z = ol.array.linearFindNearest(resolutions, resolution, direction); - z = goog.math.clamp(z + delta, 0, resolutions.length - 1); - return resolutions[z]; - } else { - return undefined; - } - }; + return ( + /** + * @param {number|undefined} resolution Resolution. + * @param {number} delta Delta. + * @param {number} direction Direction. + * @return {number|undefined} Resolution. + */ + function(resolution, delta, direction) { + if (goog.isDef(resolution)) { + var z = + ol.array.linearFindNearest(resolutions, resolution, direction); + z = goog.math.clamp(z + delta, 0, resolutions.length - 1); + return resolutions[z]; + } else { + return undefined; + } + }); }; @@ -57,25 +72,32 @@ ol.ResolutionConstraint.createSnapToResolutions = */ ol.ResolutionConstraint.createSnapToPower = function(power, maxResolution, opt_maxLevel) { - return function(resolution, delta, direction) { - if (goog.isDef(resolution)) { - var offset; - if (direction > 0) { - offset = 0; - } else if (direction < 0) { - offset = 1; - } else { - offset = 0.5; - } - var oldLevel = Math.floor( - Math.log(maxResolution / resolution) / Math.log(power) + offset); - var newLevel = Math.max(oldLevel + delta, 0); - if (goog.isDef(opt_maxLevel)) { - newLevel = Math.min(newLevel, opt_maxLevel); - } - return maxResolution / Math.pow(power, newLevel); - } else { - return undefined; - } - }; + return ( + /** + * @param {number|undefined} resolution Resolution. + * @param {number} delta Delta. + * @param {number} direction Direction. + * @return {number|undefined} Resolution. + */ + function(resolution, delta, direction) { + if (goog.isDef(resolution)) { + var offset; + if (direction > 0) { + offset = 0; + } else if (direction < 0) { + offset = 1; + } else { + offset = 0.5; + } + var oldLevel = Math.floor( + Math.log(maxResolution / resolution) / Math.log(power) + offset); + var newLevel = Math.max(oldLevel + delta, 0); + if (goog.isDef(opt_maxLevel)) { + newLevel = Math.min(newLevel, opt_maxLevel); + } + return maxResolution / Math.pow(power, newLevel); + } else { + return undefined; + } + }); };