Merge pull request #677 from twpayne/improve-type-checking

Improve type checking
This commit is contained in:
Tom Payne
2013-04-30 15:55:34 -07:00
11 changed files with 349 additions and 223 deletions

View File

@@ -17,22 +17,27 @@ ol.animation.bounce = function(options) {
var duration = goog.isDef(options.duration) ? options.duration : 1000;
var easing = goog.isDef(options.easing) ?
options.easing : ol.easing.upAndDown;
return function(map, frameState) {
if (frameState.time < start) {
frameState.animate = true;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else if (frameState.time < start + duration) {
var delta = easing((frameState.time - start) / duration);
var deltaResolution = resolution - frameState.view2DState.resolution;
frameState.animate = true;
frameState.view2DState.resolution += delta * deltaResolution;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else {
return false;
}
};
return (
/**
* @param {ol.Map} map Map.
* @param {?ol.FrameState} frameState Frame state.
*/
function(map, frameState) {
if (frameState.time < start) {
frameState.animate = true;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else if (frameState.time < start + duration) {
var delta = easing((frameState.time - start) / duration);
var deltaResolution = resolution - frameState.view2DState.resolution;
frameState.animate = true;
frameState.view2DState.resolution += delta * deltaResolution;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else {
return false;
}
});
};
@@ -48,24 +53,29 @@ ol.animation.pan = function(options) {
var duration = goog.isDef(options.duration) ? options.duration : 1000;
var easing = goog.isDef(options.easing) ?
options.easing : ol.easing.inAndOut;
return function(map, frameState) {
if (frameState.time < start) {
frameState.animate = true;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else if (frameState.time < start + duration) {
var delta = 1 - easing((frameState.time - start) / duration);
var deltaX = sourceX - frameState.view2DState.center[0];
var deltaY = sourceY - frameState.view2DState.center[1];
frameState.animate = true;
frameState.view2DState.center[0] += delta * deltaX;
frameState.view2DState.center[1] += delta * deltaY;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else {
return false;
}
};
return (
/**
* @param {ol.Map} map Map.
* @param {?ol.FrameState} frameState Frame state.
*/
function(map, frameState) {
if (frameState.time < start) {
frameState.animate = true;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else if (frameState.time < start + duration) {
var delta = 1 - easing((frameState.time - start) / duration);
var deltaX = sourceX - frameState.view2DState.center[0];
var deltaY = sourceY - frameState.view2DState.center[1];
frameState.animate = true;
frameState.view2DState.center[0] += delta * deltaX;
frameState.view2DState.center[1] += delta * deltaY;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else {
return false;
}
});
};
@@ -80,23 +90,28 @@ ol.animation.rotate = function(options) {
var easing = goog.isDef(options.easing) ?
options.easing : ol.easing.inAndOut;
return function(map, frameState) {
if (frameState.time < start) {
frameState.animate = true;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else if (frameState.time < start + duration) {
var delta = 1 - easing((frameState.time - start) / duration);
var deltaRotation =
sourceRotation - frameState.view2DState.rotation;
frameState.animate = true;
frameState.view2DState.rotation += delta * deltaRotation;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else {
return false;
}
};
return (
/**
* @param {ol.Map} map Map.
* @param {?ol.FrameState} frameState Frame state.
*/
function(map, frameState) {
if (frameState.time < start) {
frameState.animate = true;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else if (frameState.time < start + duration) {
var delta = 1 - easing((frameState.time - start) / duration);
var deltaRotation =
sourceRotation - frameState.view2DState.rotation;
frameState.animate = true;
frameState.view2DState.rotation += delta * deltaRotation;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else {
return false;
}
});
};
@@ -110,21 +125,26 @@ ol.animation.zoom = function(options) {
var duration = goog.isDef(options.duration) ? options.duration : 1000;
var easing = goog.isDef(options.easing) ?
options.easing : ol.easing.inAndOut;
return function(map, frameState) {
if (frameState.time < start) {
frameState.animate = true;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else if (frameState.time < start + duration) {
var delta = 1 - easing((frameState.time - start) / duration);
var deltaResolution =
sourceResolution - frameState.view2DState.resolution;
frameState.animate = true;
frameState.view2DState.resolution += delta * deltaResolution;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else {
return false;
}
};
return (
/**
* @param {ol.Map} map Map.
* @param {?ol.FrameState} frameState Frame state.
*/
function(map, frameState) {
if (frameState.time < start) {
frameState.animate = true;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else if (frameState.time < start + duration) {
var delta = 1 - easing((frameState.time - start) / duration);
var deltaResolution =
sourceResolution - frameState.view2DState.resolution;
frameState.animate = true;
frameState.view2DState.resolution += delta * deltaResolution;
frameState.viewHints[ol.ViewHint.ANIMATING] += 1;
return true;
} else {
return false;
}
});
};

View File

@@ -35,9 +35,14 @@ ol.coordinate.add = function(coordinate, delta) {
* @return {ol.CoordinateFormatType} Coordinate format.
*/
ol.coordinate.createStringXY = function(opt_precision) {
return function(coordinate) {
return ol.coordinate.toStringXY(coordinate, opt_precision);
};
return (
/**
* @param {ol.Coordinate|undefined} coordinate Coordinate.
* @return {string} String XY.
*/
function(coordinate) {
return ol.coordinate.toStringXY(coordinate, opt_precision);
});
};

View File

@@ -15,15 +15,21 @@ ol.ImageUrlFunctionType;
* @param {string} baseUrl Base URL (may have query data).
* @param {Object.<string,*>} params to encode in the url.
* @param {function(string, Object.<string,*>, ol.Extent, ol.Size,
* ol.Projection)} paramsFunction params function.
* ol.Projection): (string|undefined)} paramsFunction params function.
* @return {ol.ImageUrlFunctionType} Image URL function.
*/
ol.ImageUrlFunction.createFromParamsFunction =
function(baseUrl, params, paramsFunction) {
return function(extent, size, projection) {
return paramsFunction(
baseUrl, params, extent, size, projection);
};
return (
/**
* @param {ol.Extent} extent Extent.
* @param {ol.Size} size Size.
* @param {ol.Projection} projection Projection.
* @return {string|undefined} URL.
*/
function(extent, size, projection) {
return paramsFunction(baseUrl, params, extent, size, projection);
});
};

View File

@@ -266,10 +266,17 @@ ol.renderer.Layer.prototype.updateUsedTiles =
*/
ol.renderer.Layer.prototype.createGetTileIfLoadedFunction =
function(isLoadedFunction, tileSource, projection) {
return function(z, x, y) {
var tile = tileSource.getTile(z, x, y, projection);
return isLoadedFunction(tile) ? tile : null;
};
return (
/**
* @param {number} z Z.
* @param {number} x X.
* @param {number} y Y.
* @return {ol.Tile} Tile.
*/
function(z, x, y) {
var tile = tileSource.getTile(z, x, y, projection);
return isLoadedFunction(tile) ? tile : null;
});
};

View File

@@ -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;
}
});
};

View File

@@ -28,14 +28,20 @@ ol.RotationConstraint.none = function(rotation, delta) {
*/
ol.RotationConstraint.createSnapToN = function(n) {
var theta = 2 * Math.PI / n;
return function(rotation, delta) {
if (goog.isDef(rotation)) {
rotation = Math.floor((rotation + delta) / theta + 0.5) * theta;
return rotation;
} else {
return undefined;
}
};
return (
/**
* @param {number|undefined} rotation Rotation.
* @param {number} delta Delta.
* @return {number|undefined} Rotation.
*/
function(rotation, delta) {
if (goog.isDef(rotation)) {
rotation = Math.floor((rotation + delta) / theta + 0.5) * theta;
return rotation;
} else {
return undefined;
}
});
};
@@ -45,15 +51,21 @@ ol.RotationConstraint.createSnapToN = function(n) {
*/
ol.RotationConstraint.createSnapToZero = function(opt_tolerance) {
var tolerance = opt_tolerance || 0.1;
return function(rotation, delta) {
if (goog.isDef(rotation)) {
if (Math.abs(rotation + delta) <= tolerance) {
return 0;
} else {
return rotation + delta;
}
} else {
return undefined;
}
};
return (
/**
* @param {number|undefined} rotation Rotation.
* @param {number} delta Delta.
* @return {number|undefined} Rotation.
*/
function(rotation, delta) {
if (goog.isDef(rotation)) {
if (Math.abs(rotation + delta) <= tolerance) {
return 0;
} else {
return rotation + delta;
}
} else {
return undefined;
}
});
};

View File

@@ -88,16 +88,22 @@ ol.source.BingMaps.prototype.handleImageryMetadataResponse =
var imageUrl = resource.imageUrl
.replace('{subdomain}', subdomain)
.replace('{culture}', culture);
return function(tileCoord, projection) {
goog.asserts.assert(ol.projection.equivalent(
projection, this.getProjection()));
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return imageUrl.replace(
'{quadkey}', tileCoord.quadKey());
}
};
return (
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {ol.Projection} projection Projection.
* @return {string|undefined} Tile URL.
*/
function(tileCoord, projection) {
goog.asserts.assert(ol.projection.equivalent(
projection, this.getProjection()));
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return imageUrl.replace(
'{quadkey}', tileCoord.quadKey());
}
});
})));
var transform = ol.projection.getTransformFromProjections(

View File

@@ -60,7 +60,14 @@ ol.source.StaticImage.prototype.getImage =
* @return {ol.ImageUrlFunctionType} Function.
*/
ol.source.StaticImage.createImageFunction = function(url) {
return function(extent, size, projection) {
return url;
};
return (
/**
* @param {ol.Extent} extent Extent.
* @param {ol.Size} size Size.
* @param {ol.Projection} projection Projection.
* @return {string|undefined} URL.
*/
function(extent, size, projection) {
return url;
});
};

View File

@@ -73,27 +73,32 @@ ol.source.WMTS = function(options) {
* @return {ol.TileUrlFunctionType} Tile URL function.
*/
function createFromWMTSTemplate(template) {
return function(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
var localContext = {
'TileMatrix': tileGrid.getMatrixId(tileCoord.z),
'TileCol': tileCoord.x,
'TileRow': tileCoord.y
};
if (requestEncoding != ol.source.WMTSRequestEncoding.KVP) {
goog.object.extend(localContext, context);
}
var url = template;
for (var key in localContext) {
// FIXME: should we filter properties with hasOwnProperty?
url = url.replace('{' + key + '}', localContext[key])
.replace('%7B' + key + '%7D', localContext[key]);
}
return url;
}
};
return (
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {string|undefined} Tile URL.
*/
function(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
var localContext = {
'TileMatrix': tileGrid.getMatrixId(tileCoord.z),
'TileCol': tileCoord.x,
'TileRow': tileCoord.y
};
if (requestEncoding != ol.source.WMTSRequestEncoding.KVP) {
goog.object.extend(localContext, context);
}
var url = template;
for (var key in localContext) {
// FIXME: should we filter properties with hasOwnProperty?
url = url.replace('{' + key + '}', localContext[key])
.replace('%7B' + key + '%7D', localContext[key]);
}
return url;
}
});
}
var tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;

View File

@@ -18,15 +18,21 @@ ol.TileUrlFunctionType;
* @return {ol.TileUrlFunctionType} Tile URL function.
*/
ol.TileUrlFunction.createFromTemplate = function(template) {
return function(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return template.replace('{z}', tileCoord.z)
.replace('{x}', tileCoord.x)
.replace('{y}', tileCoord.y);
}
};
return (
/**
* @param {ol.TileCoord} tileCoord Tile Coordinate.
* @param {ol.Projection} projection Projection.
* @return {string|undefined} Tile URL.
*/
function(tileCoord, projection) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return template.replace('{z}', '' + tileCoord.z)
.replace('{x}', '' + tileCoord.x)
.replace('{y}', '' + tileCoord.y);
}
});
};
@@ -48,14 +54,21 @@ ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) {
if (tileUrlFunctions.length === 1) {
return tileUrlFunctions[0];
}
return function(tileCoord, projection) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
var index = goog.math.modulo(tileCoord.hash(), tileUrlFunctions.length);
return tileUrlFunctions[index].call(this, tileCoord, projection);
}
};
return (
/**
* @param {ol.TileCoord} tileCoord Tile Coordinate.
* @param {ol.Projection} projection Projection.
* @return {string|undefined} Tile URL.
*/
function(tileCoord, projection) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
var index =
goog.math.modulo(tileCoord.hash(), tileUrlFunctions.length);
return tileUrlFunctions[index].call(this, tileCoord, projection);
}
});
};
@@ -69,20 +82,26 @@ ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) {
ol.TileUrlFunction.createFromParamsFunction =
function(baseUrl, params, paramsFunction) {
var tmpExtent = ol.extent.createEmptyExtent();
return function(tileCoord, projection) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
var tileGrid = this.getTileGrid();
if (goog.isNull(tileGrid)) {
tileGrid = ol.tilegrid.getForProjection(projection);
}
var size = tileGrid.getTileSize(tileCoord.z);
var extent = tileGrid.getTileCoordExtent(tileCoord, tmpExtent);
return paramsFunction.call(this, baseUrl, params,
extent, size, projection);
}
};
return (
/**
* @param {ol.TileCoord} tileCoord Tile Coordinate.
* @param {ol.Projection} projection Projection.
* @return {string|undefined} Tile URL.
*/
function(tileCoord, projection) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
var tileGrid = this.getTileGrid();
if (goog.isNull(tileGrid)) {
tileGrid = ol.tilegrid.getForProjection(projection);
}
var size = tileGrid.getTileSize(tileCoord.z);
var extent = tileGrid.getTileCoordExtent(tileCoord, tmpExtent);
return paramsFunction.call(this, baseUrl, params,
extent, size, projection);
}
});
};
@@ -105,16 +124,22 @@ ol.TileUrlFunction.nullTileUrlFunction = function(tileCoord, projection) {
ol.TileUrlFunction.withTileCoordTransform =
function(transformFn, tileUrlFunction) {
var tmpTileCoord = new ol.TileCoord(0, 0, 0);
return function(tileCoord, projection) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return tileUrlFunction.call(
this,
transformFn.call(this, tileCoord, projection, tmpTileCoord),
projection);
}
};
return (
/**
* @param {ol.TileCoord} tileCoord Tile Coordinate.
* @param {ol.Projection} projection Projection.
* @return {string|undefined} Tile URL.
*/
function(tileCoord, projection) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return tileUrlFunction.call(
this,
transformFn.call(this, tileCoord, projection, tmpTileCoord),
projection);
}
});
};

View File

@@ -231,12 +231,17 @@ ol.View2D.prototype.getResolutionForValueFunction = function(opt_power) {
var maxResolution = this.maxResolution_;
var minResolution = this.minResolution_;
var max = Math.log(maxResolution / minResolution) / Math.log(power);
return function(value) {
var resolution = maxResolution / Math.pow(power, value * max);
goog.asserts.assert(resolution >= minResolution &&
resolution <= maxResolution);
return resolution;
};
return (
/**
* @param {number} value Value.
* @return {number} Resolution.
*/
function(value) {
var resolution = maxResolution / Math.pow(power, value * max);
goog.asserts.assert(resolution >= minResolution &&
resolution <= maxResolution);
return resolution;
});
};
@@ -264,11 +269,17 @@ ol.View2D.prototype.getValueForResolutionFunction = function(opt_power) {
var maxResolution = this.maxResolution_;
var minResolution = this.minResolution_;
var max = Math.log(maxResolution / minResolution) / Math.log(power);
return function(resolution) {
var value = (Math.log(maxResolution / resolution) / Math.log(power)) / max;
goog.asserts.assert(value >= 0 && value <= 1);
return value;
};
return (
/**
* @param {number} resolution Resolution.
* @return {number} Value.
*/
function(resolution) {
var value =
(Math.log(maxResolution / resolution) / Math.log(power)) / max;
goog.asserts.assert(value >= 0 && value <= 1);
return value;
});
};