View / make the constrainResolution function private

Other classes should not need to worry about constraining the resolution
or not, as the View will eventually do this on its own.
This commit is contained in:
Olivier Guyot
2019-01-10 17:38:37 +01:00
parent 3c1e3779e2
commit e6c4b2ffd1
4 changed files with 27 additions and 40 deletions

View File

@@ -345,13 +345,12 @@ class View extends BaseObject {
if (options.resolution !== undefined) {
this.setResolution(options.resolution);
} else if (options.zoom !== undefined) {
this.setResolution(this.constrainResolution(
this.maxResolution_, options.zoom - this.minZoom_));
if (this.resolutions_) { // in case map zoom is out of min/max zoom range
this.setResolution(clamp(
Number(this.getResolution() || properties[ViewProperty.RESOLUTION]),
const resolution = this.getResolutionForZoom(options.zoom);
this.setResolution(clamp(resolution,
this.minResolution_, this.maxResolution_));
} else {
this.setZoom(options.zoom);
}
}

View File

@@ -4,7 +4,7 @@
import {disable} from '../rotationconstraint.js';
import ViewHint from '../ViewHint.js';
import {shiftKeyOnly, mouseOnly} from '../events/condition.js';
import {rotate, rotateWithoutConstraints, zoom, zoomWithoutConstraints} from './Interaction.js';
import {rotate, rotateWithoutConstraints, zoom} from './Interaction.js';
import PointerInteraction from './Pointer.js';
@@ -95,7 +95,7 @@ class DragRotateAndZoom extends PointerInteraction {
this.lastAngle_ = theta;
if (this.lastMagnitude_ !== undefined) {
const resolution = this.lastMagnitude_ * (view.getResolution() / magnitude);
zoomWithoutConstraints(view, resolution);
zoom(view, resolution);
}
if (this.lastMagnitude_ !== undefined) {
this.lastScaleDelta_ = this.lastMagnitude_ / magnitude;

View File

@@ -177,8 +177,25 @@ export function rotateWithoutConstraints(view, rotation, opt_anchor, opt_duratio
* assumed.
*/
export function zoom(view, resolution, opt_anchor, opt_duration, opt_direction) {
resolution = view.constrainResolution(resolution, 0, opt_direction);
zoomWithoutConstraints(view, resolution, opt_anchor, opt_duration);
if (resolution) {
const currentResolution = view.getResolution();
const currentCenter = view.getCenter();
if (currentResolution !== undefined && currentCenter &&
resolution !== currentResolution && opt_duration) {
view.animate({
resolution: resolution,
anchor: opt_anchor,
duration: opt_duration,
easing: easeOut
});
} else {
if (opt_anchor) {
const center = view.calculateCenterZoom(resolution, opt_anchor);
view.setCenter(center);
}
view.setResolution(resolution);
}
}
}
@@ -234,33 +251,4 @@ export function zoomByDelta(view, delta, opt_anchor, opt_duration) {
}
}
/**
* @param {import("../View.js").default} view View.
* @param {number|undefined} resolution Resolution to go to.
* @param {import("../coordinate.js").Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration.
*/
export function zoomWithoutConstraints(view, resolution, opt_anchor, opt_duration) {
if (resolution) {
const currentResolution = view.getResolution();
const currentCenter = view.getCenter();
if (currentResolution !== undefined && currentCenter &&
resolution !== currentResolution && opt_duration) {
view.animate({
resolution: resolution,
anchor: opt_anchor,
duration: opt_duration,
easing: easeOut
});
} else {
if (opt_anchor) {
const center = view.calculateCenterZoom(resolution, opt_anchor);
view.setCenter(center);
}
view.setResolution(resolution);
}
}
}
export default Interaction;

View File

@@ -3,7 +3,7 @@
*/
import ViewHint from '../ViewHint.js';
import {FALSE} from '../functions.js';
import {zoom, zoomWithoutConstraints} from './Interaction.js';
import {zoom} from './Interaction.js';
import PointerInteraction, {centroid as centroidFromPointers} from './Pointer.js';
@@ -116,7 +116,7 @@ class PinchZoom extends PointerInteraction {
// scale, bypass the resolution constraint
map.render();
zoomWithoutConstraints(view, newResolution, this.anchor_);
zoom(view, newResolution, this.anchor_);
}
/**