Improve type checking in ol.ResolutionConstraint

This commit is contained in:
Tom Payne
2013-04-30 21:32:05 +02:00
parent 2690410cbe
commit bd9d723315
+60 -38
View File
@@ -20,14 +20,21 @@ ol.ResolutionConstraintType;
ol.ResolutionConstraint.createContinuous = ol.ResolutionConstraint.createContinuous =
function(power, maxResolution, opt_minResolution) { function(power, maxResolution, opt_minResolution) {
var minResolution = opt_minResolution || 0; var minResolution = opt_minResolution || 0;
return function(resolution, delta, direction) { return (
if (goog.isDef(resolution)) { /**
resolution /= Math.pow(power, delta); * @param {number|undefined} resolution Resolution.
return goog.math.clamp(resolution, minResolution, maxResolution); * @param {number} delta Delta.
} else { * @param {number} direction Direction.
return undefined; * @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 = ol.ResolutionConstraint.createSnapToResolutions =
function(resolutions) { function(resolutions) {
return function(resolution, delta, direction) { return (
if (goog.isDef(resolution)) { /**
var z = ol.array.linearFindNearest(resolutions, resolution, direction); * @param {number|undefined} resolution Resolution.
z = goog.math.clamp(z + delta, 0, resolutions.length - 1); * @param {number} delta Delta.
return resolutions[z]; * @param {number} direction Direction.
} else { * @return {number|undefined} Resolution.
return undefined; */
} 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 = ol.ResolutionConstraint.createSnapToPower =
function(power, maxResolution, opt_maxLevel) { function(power, maxResolution, opt_maxLevel) {
return function(resolution, delta, direction) { return (
if (goog.isDef(resolution)) { /**
var offset; * @param {number|undefined} resolution Resolution.
if (direction > 0) { * @param {number} delta Delta.
offset = 0; * @param {number} direction Direction.
} else if (direction < 0) { * @return {number|undefined} Resolution.
offset = 1; */
} else { function(resolution, delta, direction) {
offset = 0.5; if (goog.isDef(resolution)) {
} var offset;
var oldLevel = Math.floor( if (direction > 0) {
Math.log(maxResolution / resolution) / Math.log(power) + offset); offset = 0;
var newLevel = Math.max(oldLevel + delta, 0); } else if (direction < 0) {
if (goog.isDef(opt_maxLevel)) { offset = 1;
newLevel = Math.min(newLevel, opt_maxLevel); } else {
} offset = 0.5;
return maxResolution / Math.pow(power, newLevel); }
} else { var oldLevel = Math.floor(
return undefined; 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;
}
});
}; };