Move ScaleLineUnits to ScaleLine control module

This commit is contained in:
Tim Schaub
2018-04-23 10:35:00 -06:00
parent 311ab0eae1
commit b4badd0c53
2 changed files with 35 additions and 39 deletions
+35 -23
View File
@@ -5,17 +5,29 @@ import {inherits} from '../index.js';
import {getChangeEventType} from '../Object.js'; import {getChangeEventType} from '../Object.js';
import {assert} from '../asserts.js'; import {assert} from '../asserts.js';
import Control from '../control/Control.js'; import Control from '../control/Control.js';
import ScaleLineUnits from '../control/ScaleLineUnits.js';
import {CLASS_UNSELECTABLE} from '../css.js'; import {CLASS_UNSELECTABLE} from '../css.js';
import {listen} from '../events.js'; import {listen} from '../events.js';
import {getPointResolution, METERS_PER_UNIT} from '../proj.js'; import {getPointResolution, METERS_PER_UNIT} from '../proj.js';
import Units from '../proj/Units.js'; import ProjUnits from '../proj/Units.js';
/** /**
* @type {string} * @type {string}
*/ */
const UNITS = 'units'; const UNITS_PROP = 'units';
/**
* Units for the scale line. Supported values are `'degrees'`, `'imperial'`,
* `'nautical'`, `'metric'`, `'us'`.
* @enum {string}
*/
export const Units = {
DEGREES: 'degrees',
IMPERIAL: 'imperial',
NAUTICAL: 'nautical',
METRIC: 'metric',
US: 'us'
};
/** /**
@@ -33,7 +45,7 @@ const LEADING_DIGITS = [1, 2, 5];
* should be re-rendered. This is called in a `requestAnimationFrame` callback. * should be re-rendered. This is called in a `requestAnimationFrame` callback.
* @property {Element|string} [target] Specify a target if you want the control * @property {Element|string} [target] Specify a target if you want the control
* to be rendered outside of the map's viewport. * to be rendered outside of the map's viewport.
* @property {module:ol/control/ScaleLineUnits~ScaleLineUnits|string} [units='metric'] Units. * @property {module:ol/control/ScaleLine~Units|string} [units='metric'] Units.
*/ */
@@ -110,11 +122,11 @@ const ScaleLine = function(opt_options) {
}); });
listen( listen(
this, getChangeEventType(UNITS), this, getChangeEventType(UNITS_PROP),
this.handleUnitsChanged_, this); this.handleUnitsChanged_, this);
this.setUnits(/** @type {module:ol/control/ScaleLineUnits~ScaleLineUnits} */ (options.units) || this.setUnits(/** @type {module:ol/control/ScaleLine~Units} */ (options.units) ||
ScaleLineUnits.METRIC); Units.METRIC);
}; };
@@ -123,13 +135,13 @@ inherits(ScaleLine, Control);
/** /**
* Return the units to use in the scale line. * Return the units to use in the scale line.
* @return {module:ol/control/ScaleLineUnits~ScaleLineUnits|undefined} The units * @return {module:ol/control/ScaleLine~Units|undefined} The units
* to use in the scale line. * to use in the scale line.
* @observable * @observable
* @api * @api
*/ */
ScaleLine.prototype.getUnits = function() { ScaleLine.prototype.getUnits = function() {
return /** @type {module:ol/control/ScaleLineUnits~ScaleLineUnits|undefined} */ (this.get(UNITS)); return /** @type {module:ol/control/ScaleLine~Units|undefined} */ (this.get(UNITS_PROP));
}; };
@@ -160,12 +172,12 @@ ScaleLine.prototype.handleUnitsChanged_ = function() {
/** /**
* Set the units to use in the scale line. * Set the units to use in the scale line.
* @param {module:ol/control/ScaleLineUnits~ScaleLineUnits} units The units to use in the scale line. * @param {module:ol/control/ScaleLine~Units} units The units to use in the scale line.
* @observable * @observable
* @api * @api
*/ */
ScaleLine.prototype.setUnits = function(units) { ScaleLine.prototype.setUnits = function(units) {
this.set(UNITS, units); this.set(UNITS_PROP, units);
}; };
@@ -186,21 +198,21 @@ ScaleLine.prototype.updateElement_ = function() {
const center = viewState.center; const center = viewState.center;
const projection = viewState.projection; const projection = viewState.projection;
const units = this.getUnits(); const units = this.getUnits();
const pointResolutionUnits = units == ScaleLineUnits.DEGREES ? const pointResolutionUnits = units == Units.DEGREES ?
Units.DEGREES : ProjUnits.DEGREES :
Units.METERS; ProjUnits.METERS;
let pointResolution = let pointResolution =
getPointResolution(projection, viewState.resolution, center, pointResolutionUnits); getPointResolution(projection, viewState.resolution, center, pointResolutionUnits);
if (projection.getUnits() != Units.DEGREES && projection.getMetersPerUnit() if (projection.getUnits() != ProjUnits.DEGREES && projection.getMetersPerUnit()
&& pointResolutionUnits == Units.METERS) { && pointResolutionUnits == ProjUnits.METERS) {
pointResolution *= projection.getMetersPerUnit(); pointResolution *= projection.getMetersPerUnit();
} }
let nominalCount = this.minWidth_ * pointResolution; let nominalCount = this.minWidth_ * pointResolution;
let suffix = ''; let suffix = '';
if (units == ScaleLineUnits.DEGREES) { if (units == Units.DEGREES) {
const metersPerDegree = METERS_PER_UNIT[Units.DEGREES]; const metersPerDegree = METERS_PER_UNIT[ProjUnits.DEGREES];
if (projection.getUnits() == Units.DEGREES) { if (projection.getUnits() == ProjUnits.DEGREES) {
nominalCount *= metersPerDegree; nominalCount *= metersPerDegree;
} else { } else {
pointResolution /= metersPerDegree; pointResolution /= metersPerDegree;
@@ -214,7 +226,7 @@ ScaleLine.prototype.updateElement_ = function() {
} else { } else {
suffix = '\u00b0'; // degrees suffix = '\u00b0'; // degrees
} }
} else if (units == ScaleLineUnits.IMPERIAL) { } else if (units == Units.IMPERIAL) {
if (nominalCount < 0.9144) { if (nominalCount < 0.9144) {
suffix = 'in'; suffix = 'in';
pointResolution /= 0.0254; pointResolution /= 0.0254;
@@ -225,10 +237,10 @@ ScaleLine.prototype.updateElement_ = function() {
suffix = 'mi'; suffix = 'mi';
pointResolution /= 1609.344; pointResolution /= 1609.344;
} }
} else if (units == ScaleLineUnits.NAUTICAL) { } else if (units == Units.NAUTICAL) {
pointResolution /= 1852; pointResolution /= 1852;
suffix = 'nm'; suffix = 'nm';
} else if (units == ScaleLineUnits.METRIC) { } else if (units == Units.METRIC) {
if (nominalCount < 0.001) { if (nominalCount < 0.001) {
suffix = 'μm'; suffix = 'μm';
pointResolution *= 1000000; pointResolution *= 1000000;
@@ -241,7 +253,7 @@ ScaleLine.prototype.updateElement_ = function() {
suffix = 'km'; suffix = 'km';
pointResolution /= 1000; pointResolution /= 1000;
} }
} else if (units == ScaleLineUnits.US) { } else if (units == Units.US) {
if (nominalCount < 0.9144) { if (nominalCount < 0.9144) {
suffix = 'in'; suffix = 'in';
pointResolution *= 39.37; pointResolution *= 39.37;
-16
View File
@@ -1,16 +0,0 @@
/**
* @module ol/control/ScaleLineUnits
*/
/**
* Units for the scale line. Supported values are `'degrees'`, `'imperial'`,
* `'nautical'`, `'metric'`, `'us'`.
* @enum {string}
*/
export default {
DEGREES: 'degrees',
IMPERIAL: 'imperial',
NAUTICAL: 'nautical',
METRIC: 'metric',
US: 'us'
};