More type checking for View2D

This commit is contained in:
Éric Lemoine
2013-07-04 09:13:31 +02:00
parent 552853a3b8
commit e6efa741d2
2 changed files with 20 additions and 18 deletions
+11 -9
View File
@@ -59,21 +59,22 @@ ol.View2D = function(opt_options) {
values[ol.View2DProperty.PROJECTION] = ol.proj.createProjection( values[ol.View2DProperty.PROJECTION] = ol.proj.createProjection(
options.projection, 'EPSG:3857'); options.projection, 'EPSG:3857');
var parts = ol.View2D.createResolutionConstraint_(options); var resolutionConstraintInfo = ol.View2D.createResolutionConstraint_(
options);
/** /**
* @private * @private
* @type {number} * @type {number}
*/ */
this.maxResolution_ = parts[1]; this.maxResolution_ = resolutionConstraintInfo.maxResolution;
/** /**
* @private * @private
* @type {number} * @type {number}
*/ */
this.minResolution_ = parts[2]; this.minResolution_ = resolutionConstraintInfo.minResolution;
var resolutionConstraint = parts[0]; var resolutionConstraint = resolutionConstraintInfo.constraint;
var rotationConstraint = ol.View2D.createRotationConstraint_(options); var rotationConstraint = ol.View2D.createRotationConstraint_(options);
/** /**
@@ -87,7 +88,7 @@ ol.View2D = function(opt_options) {
values[ol.View2DProperty.RESOLUTION] = options.resolution; values[ol.View2DProperty.RESOLUTION] = options.resolution;
} else if (goog.isDef(options.zoom)) { } else if (goog.isDef(options.zoom)) {
values[ol.View2DProperty.RESOLUTION] = resolutionConstraint( values[ol.View2DProperty.RESOLUTION] = resolutionConstraint(
this.maxResolution_, options.zoom); this.maxResolution_, options.zoom, 0);
} }
values[ol.View2DProperty.ROTATION] = values[ol.View2DProperty.ROTATION] =
goog.isDef(options.rotation) ? options.rotation : 0; goog.isDef(options.rotation) ? options.rotation : 0;
@@ -403,7 +404,7 @@ goog.exportProperty(
* @param {number} zoom Zoom level. * @param {number} zoom Zoom level.
*/ */
ol.View2D.prototype.setZoom = function(zoom) { ol.View2D.prototype.setZoom = function(zoom) {
var resolution = this.constrainResolution(this.maxResolution_, zoom); var resolution = this.constrainResolution(this.maxResolution_, zoom, 0);
this.setResolution(resolution); this.setResolution(resolution);
}; };
@@ -411,8 +412,8 @@ ol.View2D.prototype.setZoom = function(zoom) {
/** /**
* @private * @private
* @param {ol.View2DOptions} options View2D options. * @param {ol.View2DOptions} options View2D options.
* @return {Array} Array of three elements: the resolution constraint, * @return {{constraint: ol.ResolutionConstraintType, maxResolution: number,
* maxResolution, and minResolution. * minResolution: number}}
*/ */
ol.View2D.createResolutionConstraint_ = function(options) { ol.View2D.createResolutionConstraint_ = function(options) {
var resolutionConstraint; var resolutionConstraint;
@@ -450,7 +451,8 @@ ol.View2D.createResolutionConstraint_ = function(options) {
resolutionConstraint = ol.ResolutionConstraint.createSnapToPower( resolutionConstraint = ol.ResolutionConstraint.createSnapToPower(
zoomFactor, maxResolution, maxZoom); zoomFactor, maxResolution, maxZoom);
} }
return [resolutionConstraint, maxResolution, minResolution]; return {constraint: resolutionConstraint, maxResolution: maxResolution,
minResolution: minResolution};
}; };
+9 -9
View File
@@ -8,7 +8,7 @@ describe('ol.View2D', function() {
describe('with no options', function() { describe('with no options', function() {
it('gives a correct resolution constraint function', function() { it('gives a correct resolution constraint function', function() {
var options = {}; var options = {};
var fn = ol.View2D.createResolutionConstraint_(options)[0]; var fn = ol.View2D.createResolutionConstraint_(options).constraint;
expect(fn(156543.03392804097, 0, 0)) expect(fn(156543.03392804097, 0, 0))
.to.roughlyEqual(156543.03392804097, 1e-9); .to.roughlyEqual(156543.03392804097, 1e-9);
expect(fn(78271.51696402048, 0, 0)) expect(fn(78271.51696402048, 0, 0))
@@ -24,12 +24,12 @@ describe('ol.View2D', function() {
maxZoom: 3, maxZoom: 3,
zoomFactor: 3 zoomFactor: 3
}; };
var parts = ol.View2D.createResolutionConstraint_(options); var info = ol.View2D.createResolutionConstraint_(options);
var maxResolution = parts[1]; var maxResolution = info.maxResolution;
expect(maxResolution).to.eql(81); expect(maxResolution).to.eql(81);
var minResolution = parts[2]; var minResolution = info.minResolution;
expect(minResolution).to.eql(3); expect(minResolution).to.eql(3);
var fn = parts[0]; var fn = info.constraint;
expect(fn(82, 0, 0)).to.eql(81); expect(fn(82, 0, 0)).to.eql(81);
expect(fn(81, 0, 0)).to.eql(81); expect(fn(81, 0, 0)).to.eql(81);
expect(fn(27, 0, 0)).to.eql(27); expect(fn(27, 0, 0)).to.eql(27);
@@ -44,12 +44,12 @@ describe('ol.View2D', function() {
var options = { var options = {
resolutions: [97, 76, 65, 54, 0.45] resolutions: [97, 76, 65, 54, 0.45]
}; };
var parts = ol.View2D.createResolutionConstraint_(options); var info = ol.View2D.createResolutionConstraint_(options);
var maxResolution = parts[1]; var maxResolution = info.maxResolution;
expect(maxResolution).to.eql(97); expect(maxResolution).to.eql(97);
var minResolution = parts[2]; var minResolution = info.minResolution;
expect(minResolution).to.eql(0.45); expect(minResolution).to.eql(0.45);
var fn = parts[0]; var fn = info.constraint;
expect(fn(97, 0, 0)).to.eql(97); expect(fn(97, 0, 0)).to.eql(97);
expect(fn(76, 0, 0)).to.eql(76); expect(fn(76, 0, 0)).to.eql(76);
expect(fn(65, 0, 0)).to.eql(65); expect(fn(65, 0, 0)).to.eql(65);