Merge pull request #7828 from bjornharrtell/refactor-constraints

Refactor constraints static methods to module functions
This commit is contained in:
Tim Schaub
2018-02-14 23:01:22 -07:00
committed by GitHub
9 changed files with 46 additions and 54 deletions

View File

@@ -3,10 +3,10 @@
*/
import {DEFAULT_TILE_SIZE} from './tilegrid/common.js';
import {inherits, getUid, nullFunction} from './index.js';
import CenterConstraint from './CenterConstraint.js';
import {createExtent, none as centerNone} from './centerconstraint.js';
import BaseObject from './Object.js';
import ResolutionConstraint from './ResolutionConstraint.js';
import RotationConstraint from './RotationConstraint.js';
import {createSnapToResolutions, createSnapToPower} from './resolutionconstraint.js';
import {createSnapToZero, createSnapToN, none as rotationNone, disable} from './rotationconstraint.js';
import ViewHint from './ViewHint.js';
import ViewProperty from './ViewProperty.js';
import {linearFindNearest} from './array.js';
@@ -1092,9 +1092,9 @@ View.prototype.setZoom = function(zoom) {
*/
export function createCenterConstraint(options) {
if (options.extent !== undefined) {
return CenterConstraint.createExtent(options.extent);
return createExtent(options.extent);
} else {
return CenterConstraint.none;
return centerNone;
}
}
@@ -1128,7 +1128,7 @@ export function createResolutionConstraint(options) {
maxResolution = resolutions[minZoom];
minResolution = resolutions[maxZoom] !== undefined ?
resolutions[maxZoom] : resolutions[resolutions.length - 1];
resolutionConstraint = ResolutionConstraint.createSnapToResolutions(
resolutionConstraint = createSnapToResolutions(
resolutions);
} else {
// calculate the default min and max resolution
@@ -1173,7 +1173,7 @@ export function createResolutionConstraint(options) {
Math.log(maxResolution / minResolution) / Math.log(zoomFactor));
minResolution = maxResolution / Math.pow(zoomFactor, maxZoom - minZoom);
resolutionConstraint = ResolutionConstraint.createSnapToPower(
resolutionConstraint = createSnapToPower(
zoomFactor, maxResolution, maxZoom - minZoom);
}
return {constraint: resolutionConstraint, maxResolution: maxResolution,
@@ -1191,16 +1191,16 @@ export function createRotationConstraint(options) {
if (enableRotation) {
const constrainRotation = options.constrainRotation;
if (constrainRotation === undefined || constrainRotation === true) {
return RotationConstraint.createSnapToZero();
return createSnapToZero();
} else if (constrainRotation === false) {
return RotationConstraint.none;
return rotationNone;
} else if (typeof constrainRotation === 'number') {
return RotationConstraint.createSnapToN(constrainRotation);
return createSnapToN(constrainRotation);
} else {
return RotationConstraint.none;
return rotationNone;
}
} else {
return RotationConstraint.disable;
return disable;
}
}

View File

@@ -1,15 +1,14 @@
/**
* @module ol/CenterConstraint
* @module ol/centerconstraint
*/
import {clamp} from './math.js';
const CenterConstraint = {};
/**
* @param {ol.Extent} extent Extent.
* @return {ol.CenterConstraintType} The constraint.
*/
CenterConstraint.createExtent = function(extent) {
export function createExtent(extent) {
return (
/**
* @param {ol.Coordinate|undefined} center Center.
@@ -26,14 +25,13 @@ CenterConstraint.createExtent = function(extent) {
}
}
);
};
}
/**
* @param {ol.Coordinate|undefined} center Center.
* @return {ol.Coordinate|undefined} Center.
*/
CenterConstraint.none = function(center) {
export function none(center) {
return center;
};
export default CenterConstraint;
}

View File

@@ -2,7 +2,7 @@
* @module ol/interaction/DragRotate
*/
import {inherits} from '../index.js';
import RotationConstraint from '../RotationConstraint.js';
import {disable} from '../rotationconstraint.js';
import ViewHint from '../ViewHint.js';
import {altShiftKeysOnly, mouseOnly, mouseActionButton} from '../events/condition.js';
import {FALSE} from '../functions.js';
@@ -65,7 +65,7 @@ function handleDragEvent(mapBrowserEvent) {
const map = mapBrowserEvent.map;
const view = map.getView();
if (view.getConstraints().rotation === RotationConstraint.disable) {
if (view.getConstraints().rotation === disable) {
return;
}
const size = map.getSize();

View File

@@ -2,7 +2,7 @@
* @module ol/interaction/DragRotateAndZoom
*/
import {inherits} from '../index.js';
import RotationConstraint from '../RotationConstraint.js';
import {disable} from '../rotationconstraint.js';
import ViewHint from '../ViewHint.js';
import {shiftKeyOnly, mouseOnly} from '../events/condition.js';
import Interaction from '../interaction/Interaction.js';
@@ -85,7 +85,7 @@ function handleDragEvent(mapBrowserEvent) {
const theta = Math.atan2(deltaY, deltaX);
const magnitude = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
const view = map.getView();
if (view.getConstraints().rotation !== RotationConstraint.disable && this.lastAngle_ !== undefined) {
if (view.getConstraints().rotation !== disable && this.lastAngle_ !== undefined) {
const angleDelta = theta - this.lastAngle_;
Interaction.rotateWithoutConstraints(
view, view.getRotation() - angleDelta);

View File

@@ -6,7 +6,7 @@ import ViewHint from '../ViewHint.js';
import {FALSE} from '../functions.js';
import Interaction from '../interaction/Interaction.js';
import PointerInteraction from '../interaction/Pointer.js';
import RotationConstraint from '../RotationConstraint.js';
import {disable} from '../rotationconstraint.js';
/**
* @classdesc
@@ -97,7 +97,7 @@ function handleDragEvent(mapBrowserEvent) {
const map = mapBrowserEvent.map;
const view = map.getView();
if (view.getConstraints().rotation === RotationConstraint.disable) {
if (view.getConstraints().rotation === disable) {
return;
}

View File

@@ -1,16 +1,14 @@
/**
* @module ol/ResolutionConstraint
* @module ol/resolutionconstraint
*/
import {linearFindNearest} from './array.js';
import {clamp} from './math.js';
const ResolutionConstraint = {};
/**
* @param {Array.<number>} resolutions Resolutions.
* @return {ol.ResolutionConstraintType} Zoom function.
*/
ResolutionConstraint.createSnapToResolutions = function(resolutions) {
export function createSnapToResolutions(resolutions) {
return (
/**
* @param {number|undefined} resolution Resolution.
@@ -34,7 +32,7 @@ ResolutionConstraint.createSnapToResolutions = function(resolutions) {
}
}
);
};
}
/**
@@ -43,7 +41,7 @@ ResolutionConstraint.createSnapToResolutions = function(resolutions) {
* @param {number=} opt_maxLevel Maximum level.
* @return {ol.ResolutionConstraintType} Zoom function.
*/
ResolutionConstraint.createSnapToPower = function(power, maxResolution, opt_maxLevel) {
export function createSnapToPower(power, maxResolution, opt_maxLevel) {
return (
/**
* @param {number|undefined} resolution Resolution.
@@ -65,5 +63,4 @@ ResolutionConstraint.createSnapToPower = function(power, maxResolution, opt_maxL
return undefined;
}
});
};
export default ResolutionConstraint;
}

View File

@@ -1,22 +1,20 @@
/**
* @module ol/RotationConstraint
* @module ol/rotationconstraint
*/
import {toRadians} from './math.js';
const RotationConstraint = {};
/**
* @param {number|undefined} rotation Rotation.
* @param {number} delta Delta.
* @return {number|undefined} Rotation.
*/
RotationConstraint.disable = function(rotation, delta) {
export function disable(rotation, delta) {
if (rotation !== undefined) {
return 0;
} else {
return undefined;
}
};
}
/**
@@ -24,20 +22,20 @@ RotationConstraint.disable = function(rotation, delta) {
* @param {number} delta Delta.
* @return {number|undefined} Rotation.
*/
RotationConstraint.none = function(rotation, delta) {
export function none(rotation, delta) {
if (rotation !== undefined) {
return rotation + delta;
} else {
return undefined;
}
};
}
/**
* @param {number} n N.
* @return {ol.RotationConstraintType} Rotation constraint.
*/
RotationConstraint.createSnapToN = function(n) {
export function createSnapToN(n) {
const theta = 2 * Math.PI / n;
return (
/**
@@ -53,14 +51,14 @@ RotationConstraint.createSnapToN = function(n) {
return undefined;
}
});
};
}
/**
* @param {number=} opt_tolerance Tolerance.
* @return {ol.RotationConstraintType} Rotation constraint.
*/
RotationConstraint.createSnapToZero = function(opt_tolerance) {
export function createSnapToZero(opt_tolerance) {
const tolerance = opt_tolerance || toRadians(5);
return (
/**
@@ -79,5 +77,4 @@ RotationConstraint.createSnapToZero = function(opt_tolerance) {
return undefined;
}
});
};
export default RotationConstraint;
}