View / better names for getValid* and applyParameters_ methods

This commit is contained in:
Olivier Guyot
2019-02-15 13:43:50 +01:00
parent c2af03f152
commit 405e206717
8 changed files with 43 additions and 46 deletions
+14 -17
View File
@@ -622,7 +622,7 @@ class View extends BaseObject {
} }
this.targetRotation_ = rotation; this.targetRotation_ = rotation;
} }
this.applyParameters_(true); this.applyTargetState_(true);
more = true; more = true;
if (!animation.complete) { if (!animation.complete) {
break; break;
@@ -1070,7 +1070,7 @@ class View extends BaseObject {
[size[0] - padding[1] - padding[3], size[1] - padding[0] - padding[2]]); [size[0] - padding[1] - padding[3], size[1] - padding[0] - padding[2]]);
resolution = isNaN(resolution) ? minResolution : resolution = isNaN(resolution) ? minResolution :
Math.max(resolution, minResolution); Math.max(resolution, minResolution);
resolution = this.getValidResolution(resolution, nearest ? 0 : 1); resolution = this.getConstrainedResolution(resolution, nearest ? 0 : 1);
// calculate center // calculate center
sinAngle = -sinAngle; // go back to original rotation sinAngle = -sinAngle; // go back to original rotation
@@ -1086,14 +1086,14 @@ class View extends BaseObject {
if (options.duration !== undefined) { if (options.duration !== undefined) {
this.animate({ this.animate({
resolution: resolution, resolution: resolution,
center: this.getValidCenter(center, resolution), center: this.getConstrainedCenter(center, resolution),
duration: options.duration, duration: options.duration,
easing: options.easing easing: options.easing
}, callback); }, callback);
} else { } else {
this.targetResolution_ = resolution; this.targetResolution_ = resolution;
this.targetCenter_ = center; this.targetCenter_ = center;
this.applyParameters_(false, true); this.applyTargetState_(false, true);
animationCallback(callback, true); animationCallback(callback, true);
} }
} }
@@ -1159,7 +1159,7 @@ class View extends BaseObject {
} }
this.targetResolution_ *= ratio; this.targetResolution_ *= ratio;
this.applyParameters_(); this.applyTargetState_();
} }
/** /**
@@ -1188,7 +1188,7 @@ class View extends BaseObject {
this.targetCenter_ = this.calculateCenterRotate(newRotation, opt_anchor); this.targetCenter_ = this.calculateCenterRotate(newRotation, opt_anchor);
} }
this.targetRotation_ += delta; this.targetRotation_ += delta;
this.applyParameters_(); this.applyTargetState_();
} }
/** /**
@@ -1199,7 +1199,7 @@ class View extends BaseObject {
*/ */
setCenter(center) { setCenter(center) {
this.targetCenter_ = center; this.targetCenter_ = center;
this.applyParameters_(); this.applyTargetState_();
} }
/** /**
@@ -1221,7 +1221,7 @@ class View extends BaseObject {
*/ */
setResolution(resolution) { setResolution(resolution) {
this.targetResolution_ = resolution; this.targetResolution_ = resolution;
this.applyParameters_(); this.applyTargetState_();
} }
/** /**
@@ -1232,7 +1232,7 @@ class View extends BaseObject {
*/ */
setRotation(rotation) { setRotation(rotation) {
this.targetRotation_ = rotation; this.targetRotation_ = rotation;
this.applyParameters_(); this.applyTargetState_();
} }
/** /**
@@ -1252,7 +1252,7 @@ class View extends BaseObject {
* @param {boolean=} opt_forceMoving Apply constraints as if the view is moving. * @param {boolean=} opt_forceMoving Apply constraints as if the view is moving.
* @private * @private
*/ */
applyParameters_(opt_doNotCancelAnims, opt_forceMoving) { applyTargetState_(opt_doNotCancelAnims, opt_forceMoving) {
const isMoving = this.getAnimating() || this.getInteracting() || opt_forceMoving; const isMoving = this.getAnimating() || this.getInteracting() || opt_forceMoving;
// compute rotation // compute rotation
@@ -1338,9 +1338,8 @@ class View extends BaseObject {
* @param {number=} opt_targetResolution Target resolution. If not supplied, the current one will be used. * @param {number=} opt_targetResolution Target resolution. If not supplied, the current one will be used.
* This is useful to guess a valid center position at a different zoom level. * This is useful to guess a valid center position at a different zoom level.
* @return {import("./coordinate.js").Coordinate|undefined} Valid center position. * @return {import("./coordinate.js").Coordinate|undefined} Valid center position.
* @api
*/ */
getValidCenter(targetCenter, opt_targetResolution) { getConstrainedCenter(targetCenter, opt_targetResolution) {
const size = this.getSizeFromViewport_(this.getRotation()); const size = this.getSizeFromViewport_(this.getRotation());
return this.constraints_.center(targetCenter, opt_targetResolution || this.getResolution(), size); return this.constraints_.center(targetCenter, opt_targetResolution || this.getResolution(), size);
} }
@@ -1352,11 +1351,10 @@ class View extends BaseObject {
* the available value respectively lower or greater than the target one. Leaving `0` will simply choose * the available value respectively lower or greater than the target one. Leaving `0` will simply choose
* the nearest available value. * the nearest available value.
* @return {number|undefined} Valid zoom level. * @return {number|undefined} Valid zoom level.
* @api
*/ */
getValidZoomLevel(targetZoom, opt_direction) { getConstrainedZoom(targetZoom, opt_direction) {
const targetRes = this.getResolutionForZoom(targetZoom); const targetRes = this.getResolutionForZoom(targetZoom);
return this.getZoomForResolution(this.getValidResolution(targetRes)); return this.getZoomForResolution(this.getConstrainedResolution(targetRes));
} }
/** /**
@@ -1366,9 +1364,8 @@ class View extends BaseObject {
* the available value respectively lower or greater than the target one. Leaving `0` will simply choose * the available value respectively lower or greater than the target one. Leaving `0` will simply choose
* the nearest available value. * the nearest available value.
* @return {number|undefined} Valid resolution. * @return {number|undefined} Valid resolution.
* @api
*/ */
getValidResolution(targetResolution, opt_direction) { getConstrainedResolution(targetResolution, opt_direction) {
const direction = opt_direction || 0; const direction = opt_direction || 0;
const size = this.getSizeFromViewport_(this.getRotation()); const size = this.getSizeFromViewport_(this.getRotation());
+1 -1
View File
@@ -116,7 +116,7 @@ class Zoom extends Control {
} }
const currentZoom = view.getZoom(); const currentZoom = view.getZoom();
if (currentZoom !== undefined) { if (currentZoom !== undefined) {
const newZoom = view.getValidZoomLevel(currentZoom + delta); const newZoom = view.getConstrainedZoom(currentZoom + delta);
if (this.duration_ > 0) { if (this.duration_ > 0) {
if (view.getAnimating()) { if (view.getAnimating()) {
view.cancelAnimations(); view.cancelAnimations();
+1 -1
View File
@@ -217,7 +217,7 @@ class ZoomSlider extends Control {
event.offsetY - this.thumbSize_[1] / 2); event.offsetY - this.thumbSize_[1] / 2);
const resolution = this.getResolutionForPosition_(relativePosition); const resolution = this.getResolutionForPosition_(relativePosition);
const zoom = view.getValidZoomLevel(view.getZoomForResolution(resolution)); const zoom = view.getConstrainedZoom(view.getZoomForResolution(resolution));
view.animate({ view.animate({
zoom: zoom, zoom: zoom,
+1 -1
View File
@@ -116,7 +116,7 @@ class DragPan extends PointerInteraction {
centerpx[1] - distance * Math.sin(angle) centerpx[1] - distance * Math.sin(angle)
]); ]);
view.animate({ view.animate({
center: view.getValidCenter(dest), center: view.getConstrainedCenter(dest),
duration: 500, duration: 500,
easing: easeOut easing: easeOut
}); });
+2 -2
View File
@@ -80,8 +80,8 @@ function onBoxEnd() {
extent = mapExtent; extent = mapExtent;
} }
const resolution = view.getValidResolution(view.getResolutionForExtent(extent, size)); const resolution = view.getConstrainedResolution(view.getResolutionForExtent(extent, size));
const center = view.getValidCenter(getCenter(extent), resolution); const center = view.getConstrainedCenter(getCenter(extent), resolution);
view.animate({ view.animate({
resolution: resolution, resolution: resolution,
+2 -2
View File
@@ -114,7 +114,7 @@ export function pan(view, delta, opt_duration) {
view.animate({ view.animate({
duration: opt_duration !== undefined ? opt_duration : 250, duration: opt_duration !== undefined ? opt_duration : 250,
easing: linear, easing: linear,
center: view.getValidCenter(center) center: view.getConstrainedCenter(center)
}); });
} }
} }
@@ -132,7 +132,7 @@ export function zoomByDelta(view, delta, opt_anchor, opt_duration) {
return; return;
} }
const newZoom = view.getValidZoomLevel(currentZoom + delta); const newZoom = view.getConstrainedZoom(currentZoom + delta);
const newResolution = view.getResolutionForZoom(newZoom); const newResolution = view.getResolutionForZoom(newZoom);
if (view.getAnimating()) { if (view.getAnimating()) {
+1 -1
View File
@@ -103,7 +103,7 @@ describe('ol.interaction.DragZoom', function() {
setTimeout(function() { setTimeout(function() {
const view = map.getView(); const view = map.getView();
const resolution = view.getResolution(); const resolution = view.getResolution();
expect(resolution).to.eql(view.getValidResolution(0.5)); expect(resolution).to.eql(view.getConstrainedResolution(0.5));
done(); done();
}, 50); }, 50);
}, 50); }, 50);
+21 -21
View File
@@ -1499,14 +1499,14 @@ describe('ol.View', function() {
}); });
}); });
describe('#getValidZoomLevel()', function() { describe('#getConstrainedZoom()', function() {
let view; let view;
it('works correctly without constraint', function() { it('works correctly without constraint', function() {
view = new View({ view = new View({
zoom: 0 zoom: 0
}); });
expect(view.getValidZoomLevel(3)).to.be(3); expect(view.getConstrainedZoom(3)).to.be(3);
}); });
it('works correctly with resolution constraints', function() { it('works correctly with resolution constraints', function() {
view = new View({ view = new View({
@@ -1514,28 +1514,28 @@ describe('ol.View', function() {
minZoom: 4, minZoom: 4,
maxZoom: 8 maxZoom: 8
}); });
expect(view.getValidZoomLevel(3)).to.be(4); expect(view.getConstrainedZoom(3)).to.be(4);
expect(view.getValidZoomLevel(10)).to.be(8); expect(view.getConstrainedZoom(10)).to.be(8);
}); });
it('works correctly with a specific resolution set', function() { it('works correctly with a specific resolution set', function() {
view = new View({ view = new View({
zoom: 0, zoom: 0,
resolutions: [512, 256, 128, 64, 32, 16, 8] resolutions: [512, 256, 128, 64, 32, 16, 8]
}); });
expect(view.getValidZoomLevel(0)).to.be(0); expect(view.getConstrainedZoom(0)).to.be(0);
expect(view.getValidZoomLevel(4)).to.be(4); expect(view.getConstrainedZoom(4)).to.be(4);
expect(view.getValidZoomLevel(8)).to.be(6); expect(view.getConstrainedZoom(8)).to.be(6);
}); });
}); });
describe('#getValidResolution()', function() { describe('#getConstrainedResolution()', function() {
let view; let view;
const defaultMaxRes = 156543.03392804097; const defaultMaxRes = 156543.03392804097;
it('works correctly by snapping to power of 2', function() { it('works correctly by snapping to power of 2', function() {
view = new View(); view = new View();
expect(view.getValidResolution(1000000)).to.be(defaultMaxRes); expect(view.getConstrainedResolution(1000000)).to.be(defaultMaxRes);
expect(view.getValidResolution(defaultMaxRes / 8)).to.be(defaultMaxRes / 8); expect(view.getConstrainedResolution(defaultMaxRes / 8)).to.be(defaultMaxRes / 8);
}); });
it('works correctly by snapping to a custom zoom factor', function() { it('works correctly by snapping to a custom zoom factor', function() {
view = new View({ view = new View({
@@ -1544,11 +1544,11 @@ describe('ol.View', function() {
maxZoom: 4, maxZoom: 4,
constrainResolution: true constrainResolution: true
}); });
expect(view.getValidResolution(90, 1)).to.be(100); expect(view.getConstrainedResolution(90, 1)).to.be(100);
expect(view.getValidResolution(90, -1)).to.be(20); expect(view.getConstrainedResolution(90, -1)).to.be(20);
expect(view.getValidResolution(20)).to.be(20); expect(view.getConstrainedResolution(20)).to.be(20);
expect(view.getValidResolution(5)).to.be(4); expect(view.getConstrainedResolution(5)).to.be(4);
expect(view.getValidResolution(1)).to.be(4); expect(view.getConstrainedResolution(1)).to.be(4);
}); });
it('works correctly with a specific resolution set', function() { it('works correctly with a specific resolution set', function() {
view = new View({ view = new View({
@@ -1556,12 +1556,12 @@ describe('ol.View', function() {
resolutions: [512, 256, 128, 64, 32, 16, 8], resolutions: [512, 256, 128, 64, 32, 16, 8],
constrainResolution: true constrainResolution: true
}); });
expect(view.getValidResolution(1000, 1)).to.be(512); expect(view.getConstrainedResolution(1000, 1)).to.be(512);
expect(view.getValidResolution(260, 1)).to.be(512); expect(view.getConstrainedResolution(260, 1)).to.be(512);
expect(view.getValidResolution(260)).to.be(256); expect(view.getConstrainedResolution(260)).to.be(256);
expect(view.getValidResolution(30)).to.be(32); expect(view.getConstrainedResolution(30)).to.be(32);
expect(view.getValidResolution(30, -1)).to.be(16); expect(view.getConstrainedResolution(30, -1)).to.be(16);
expect(view.getValidResolution(4, -1)).to.be(8); expect(view.getConstrainedResolution(4, -1)).to.be(8);
}); });
}); });